1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settingslib;
18 
19 import static com.android.settingslib.HelpUtils.MENU_HELP;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.anyInt;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.never;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 
31 import android.R;
32 import android.app.Activity;
33 import android.content.Context;
34 import android.content.Intent;
35 import android.content.pm.ActivityInfo;
36 import android.content.pm.ApplicationInfo;
37 import android.content.pm.PackageManager;
38 import android.content.pm.ResolveInfo;
39 import android.content.res.Resources;
40 import android.content.res.TypedArray;
41 import android.provider.Settings;
42 import android.view.Menu;
43 import android.view.MenuItem;
44 
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.Answers;
49 import org.mockito.Mock;
50 import org.mockito.MockitoAnnotations;
51 import org.robolectric.RobolectricTestRunner;
52 import org.robolectric.RuntimeEnvironment;
53 
54 /**
55  * Tests for {@link HelpUtils}.
56  */
57 @RunWith(RobolectricTestRunner.class)
58 public class HelpUtilsTest {
59     private static final String TEST_HELP_URL = "intent:#Intent;action=com.android.test;end";
60     private static final String PACKAGE_NAME_KEY = "package-name-key";
61     private static final String PACKAGE_NAME_VALUE = "package-name-value";
62     private static final String HELP_INTENT_EXTRA_KEY = "help-intent-extra";
63     private static final String HELP_INTENT_NAME_KEY = "help-intent-name";
64     private static final String FEEDBACK_INTENT_EXTRA_KEY = "feedback-intent-extra";
65     private static final String FEEDBACK_INTENT_NAME_KEY = "feedback-intent-name";
66 
67     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
68     private Context mContext;
69     @Mock
70     private Activity mActivity;
71     @Mock
72     private PackageManager mPackageManager;
73 
74 
75     @Before
setUp()76     public void setUp() {
77         MockitoAnnotations.initMocks(this);
78         when(mContext.getResources().getString(R.string.config_helpPackageNameKey))
79                 .thenReturn(PACKAGE_NAME_KEY);
80         when(mContext.getResources().getString(R.string.config_helpPackageNameValue))
81                 .thenReturn(PACKAGE_NAME_VALUE);
82         when(mContext.getResources().getString(R.string.config_helpIntentExtraKey))
83                 .thenReturn(HELP_INTENT_EXTRA_KEY);
84         when(mContext.getResources().getString(R.string.config_helpIntentNameKey))
85                 .thenReturn(HELP_INTENT_NAME_KEY);
86         when(mContext.getResources().getString(R.string.config_feedbackIntentExtraKey))
87                 .thenReturn(FEEDBACK_INTENT_EXTRA_KEY);
88         when(mContext.getResources().getString(R.string.config_feedbackIntentNameKey))
89                 .thenReturn(FEEDBACK_INTENT_NAME_KEY);
90         when(mActivity.getPackageManager()).thenReturn(mPackageManager);
91     }
92 
93     @Test
addIntentParameters_configTrue_argumentTrue()94     public void addIntentParameters_configTrue_argumentTrue() {
95         when(mContext.getResources().getBoolean(R.bool.config_sendPackageName)).thenReturn(true);
96         Intent intent = new Intent();
97 
98         HelpUtils.addIntentParameters(
99                 mContext, intent, null /* backupContext */, true /* sendPackageName */);
100 
101         assertThat(intent.getStringArrayExtra(HELP_INTENT_EXTRA_KEY)).asList()
102                 .containsExactly(PACKAGE_NAME_KEY);
103         assertThat(intent.getStringArrayExtra(HELP_INTENT_NAME_KEY)).asList()
104                 .containsExactly(PACKAGE_NAME_VALUE);
105         assertThat(intent.getStringArrayExtra(FEEDBACK_INTENT_EXTRA_KEY)).asList()
106                 .containsExactly(PACKAGE_NAME_KEY);
107         assertThat(intent.getStringArrayExtra(FEEDBACK_INTENT_NAME_KEY)).asList()
108                 .containsExactly(PACKAGE_NAME_VALUE);
109     }
110 
111     @Test
addIntentParameters_configTrue_argumentFalse()112     public void addIntentParameters_configTrue_argumentFalse() {
113         when(mContext.getResources().getBoolean(R.bool.config_sendPackageName)).thenReturn(true);
114         Intent intent = new Intent();
115 
116         HelpUtils.addIntentParameters(
117                 mContext, intent, null /* backupContext */, false /* sendPackageName */);
118 
119         assertThat(intent.hasExtra(HELP_INTENT_EXTRA_KEY)).isFalse();
120         assertThat(intent.hasExtra(HELP_INTENT_NAME_KEY)).isFalse();
121         assertThat(intent.hasExtra(FEEDBACK_INTENT_EXTRA_KEY)).isFalse();
122         assertThat(intent.hasExtra(FEEDBACK_INTENT_NAME_KEY)).isFalse();
123     }
124 
125     @Test
addIntentParameters_configFalse_argumentTrue()126     public void addIntentParameters_configFalse_argumentTrue() {
127         when(mContext.getResources().getBoolean(R.bool.config_sendPackageName)).thenReturn(false);
128         Intent intent = new Intent();
129 
130         HelpUtils.addIntentParameters(
131                 mContext, intent, null /* backupContext */, true /* sendPackageName */);
132 
133         assertThat(intent.hasExtra(HELP_INTENT_EXTRA_KEY)).isFalse();
134         assertThat(intent.hasExtra(HELP_INTENT_NAME_KEY)).isFalse();
135         assertThat(intent.hasExtra(FEEDBACK_INTENT_EXTRA_KEY)).isFalse();
136         assertThat(intent.hasExtra(FEEDBACK_INTENT_NAME_KEY)).isFalse();
137     }
138 
139     @Test
addIntentParameters_configFalse_argumentFalse()140     public void addIntentParameters_configFalse_argumentFalse() {
141         when(mContext.getResources().getBoolean(R.bool.config_sendPackageName)).thenReturn(false);
142         Intent intent = new Intent();
143 
144         HelpUtils.addIntentParameters(
145                 mContext, intent, null /* backupContext */, false /* sendPackageName */);
146 
147         assertThat(intent.hasExtra(HELP_INTENT_EXTRA_KEY)).isFalse();
148         assertThat(intent.hasExtra(HELP_INTENT_NAME_KEY)).isFalse();
149         assertThat(intent.hasExtra(FEEDBACK_INTENT_EXTRA_KEY)).isFalse();
150         assertThat(intent.hasExtra(FEEDBACK_INTENT_NAME_KEY)).isFalse();
151     }
152 
153     @Test
prepareHelpMenuItem_shouldShowIcon()154     public void prepareHelpMenuItem_shouldShowIcon() {
155         Settings.Global.putInt(RuntimeEnvironment.application.getContentResolver(),
156                 Settings.Global.DEVICE_PROVISIONED, 1);
157         final Resources res = mock(Resources.class);
158         final ResolveInfo resolveInfo = new ResolveInfo();
159         resolveInfo.activityInfo = new ActivityInfo();
160         resolveInfo.activityInfo.applicationInfo = new ApplicationInfo();
161         resolveInfo.activityInfo.applicationInfo.packageName = "pkg";
162         resolveInfo.activityInfo.name = "name";
163         final MenuItem item = mock(MenuItem.class);
164 
165 
166         when(mActivity.getContentResolver())
167                 .thenReturn(RuntimeEnvironment.application.getContentResolver());
168         when(mActivity.getResources()).thenReturn(res);
169         when(mActivity.obtainStyledAttributes(any(int[].class)))
170                 .thenReturn(mock(TypedArray.class));
171         when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
172                 .thenReturn(resolveInfo);
173 
174         HelpUtils.prepareHelpMenuItem(mActivity, item, TEST_HELP_URL, "backup_url");
175 
176         verify(item).setVisible(true);
177         verify(item).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
178     }
179 
180     @Test
prepareHelpMenuItem_noItem_addItem()181     public void prepareHelpMenuItem_noItem_addItem() {
182         final Menu item = mock(Menu.class);
183         when(item.findItem(MENU_HELP)).thenReturn(null);
184         when(item.add(0, MENU_HELP, 0,
185                 com.android.settingslib.widget.R.string.help_feedback_label)).thenReturn(
186                 mock(MenuItem.class));
187 
188         HelpUtils.prepareHelpMenuItem(mActivity, item, TEST_HELP_URL, "backup_url");
189         HelpUtils.prepareHelpMenuItem(mActivity, item, 0, "backup_url");
190 
191         verify(item, times(2)).add(0, MENU_HELP, 0,
192                 com.android.settingslib.widget.R.string.help_feedback_label);
193     }
194 
195     @Test
prepareHelpMenuItem_hasItem_notAddItem()196     public void prepareHelpMenuItem_hasItem_notAddItem() {
197         final Menu item = mock(Menu.class);
198         when(item.findItem(MENU_HELP)).thenReturn(mock(MenuItem.class));
199         when(item.add(0, MENU_HELP, 0,
200                 com.android.settingslib.widget.R.string.help_feedback_label)).thenReturn(
201                 mock(MenuItem.class));
202 
203         HelpUtils.prepareHelpMenuItem(mActivity, item, TEST_HELP_URL, "backup_url");
204         HelpUtils.prepareHelpMenuItem(mActivity, item, 0, "backup_url");
205 
206         verify(item, never()).add(0, MENU_HELP, 0,
207                 com.android.settingslib.widget.R.string.help_feedback_label);
208     }
209 }
210