• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 package com.android.settings.search;
19 
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import android.app.Activity;
23 import android.app.settings.SettingsEnums;
24 import android.content.ComponentName;
25 import android.content.Intent;
26 import android.content.pm.ActivityInfo;
27 import android.content.pm.ResolveInfo;
28 import android.net.Uri;
29 import android.provider.Settings;
30 import android.widget.Toolbar;
31 
32 import com.android.settings.R;
33 import com.android.settings.testutils.FakeFeatureFactory;
34 import com.android.settings.testutils.shadow.ShadowUtils;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.robolectric.Robolectric;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.Shadows;
42 import org.robolectric.annotation.Config;
43 import org.robolectric.shadows.ShadowPackageManager;
44 
45 @RunWith(RobolectricTestRunner.class)
46 public class SearchFeatureProviderImplTest {
47 
48     private SearchFeatureProviderImpl mProvider;
49     private Activity mActivity;
50     private ShadowPackageManager mPackageManager;
51 
52     @Before
setUp()53     public void setUp() {
54         FakeFeatureFactory.setupForTest();
55         mActivity = Robolectric.setupActivity(Activity.class);
56         mProvider = new SearchFeatureProviderImpl();
57         mPackageManager = Shadows.shadowOf(mActivity.getPackageManager());
58         Settings.Global.putInt(mActivity.getContentResolver(),
59                 Settings.Global.DEVICE_PROVISIONED, 1);
60     }
61 
62     @Test
63     @Config(shadows = ShadowUtils.class)
initSearchToolbar_hasResolvedInfo_shouldStartCorrectIntent()64     public void initSearchToolbar_hasResolvedInfo_shouldStartCorrectIntent() {
65         final Intent searchIntent = new Intent(Settings.ACTION_APP_SEARCH_SETTINGS)
66                 .setPackage(mActivity.getString(R.string.config_settingsintelligence_package_name));
67         final ResolveInfo info = new ResolveInfo();
68         info.activityInfo = new ActivityInfo();
69         mPackageManager.addResolveInfoForIntent(searchIntent, info);
70 
71         // Should not crash.
72         mProvider.initSearchToolbar(mActivity, null, SettingsEnums.TESTING);
73 
74         final Toolbar toolbar = new Toolbar(mActivity);
75         // This ensures navigationView is created.
76         toolbar.setNavigationContentDescription("test");
77         mProvider.initSearchToolbar(mActivity, toolbar, SettingsEnums.TESTING);
78 
79         toolbar.performClick();
80 
81         final Intent launchIntent = Shadows.shadowOf(mActivity).getNextStartedActivity();
82 
83         assertThat(launchIntent.getAction()).isEqualTo(Settings.ACTION_APP_SEARCH_SETTINGS);
84     }
85 
86     @Test
87     @Config(shadows = ShadowUtils.class)
initSearchToolbar_noResolvedInfo_shouldNotStartActivity()88     public void initSearchToolbar_noResolvedInfo_shouldNotStartActivity() {
89         final Toolbar toolbar = new Toolbar(mActivity);
90         // This ensures navigationView is created.
91         toolbar.setNavigationContentDescription("test");
92         mProvider.initSearchToolbar(mActivity, toolbar, SettingsEnums.TESTING);
93 
94         toolbar.performClick();
95 
96         assertThat(Shadows.shadowOf(mActivity).getNextStartedActivity()).isNull();
97     }
98 
99     @Test
initSearchToolbar_deviceNotProvisioned_shouldNotCreateSearchBar()100     public void initSearchToolbar_deviceNotProvisioned_shouldNotCreateSearchBar() {
101         final Toolbar toolbar = new Toolbar(mActivity);
102         // This ensures navigationView is created.
103         toolbar.setNavigationContentDescription("test");
104 
105         Settings.Global.putInt(mActivity.getContentResolver(),
106                 Settings.Global.DEVICE_PROVISIONED, 0);
107 
108         toolbar.performClick();
109 
110         assertThat(Shadows.shadowOf(mActivity).getNextStartedActivity()).isNull();
111     }
112 
113     @Test
buildSearchIntent_shouldIncludeReferrer()114     public void buildSearchIntent_shouldIncludeReferrer() {
115         final Intent searchIntent = mProvider.buildSearchIntent(mActivity, SettingsEnums.TESTING);
116         final Uri referrer = searchIntent.getParcelableExtra(Intent.EXTRA_REFERRER);
117 
118         assertThat(referrer.toSafeString()).isEqualTo(
119                 "android-app://" + mActivity.getPackageName() + "/" + SettingsEnums.TESTING);
120     }
121 
122     @Test(expected = IllegalArgumentException.class)
verifyLaunchSearchResultPageCaller_nullCaller_shouldCrash()123     public void verifyLaunchSearchResultPageCaller_nullCaller_shouldCrash() {
124         mProvider.verifyLaunchSearchResultPageCaller(mActivity, null /* caller */);
125     }
126 
127     @Test(expected = SecurityException.class)
verifyLaunchSearchResultPageCaller_badCaller_shouldCrash()128     public void verifyLaunchSearchResultPageCaller_badCaller_shouldCrash() {
129         final ComponentName cn = new ComponentName("pkg", "class");
130         mProvider.verifyLaunchSearchResultPageCaller(mActivity, cn);
131     }
132 
133     @Test
verifyLaunchSearchResultPageCaller_settingsCaller_shouldNotCrash()134     public void verifyLaunchSearchResultPageCaller_settingsCaller_shouldNotCrash() {
135         final ComponentName cn = new ComponentName(mActivity.getPackageName(), "class");
136         mProvider.verifyLaunchSearchResultPageCaller(mActivity, cn);
137     }
138 
139     @Test
verifyLaunchSearchResultPageCaller_settingsIntelligenceCaller_shouldNotCrash()140     public void verifyLaunchSearchResultPageCaller_settingsIntelligenceCaller_shouldNotCrash() {
141         final String packageName = mProvider.getSettingsIntelligencePkgName(mActivity);
142         final ComponentName cn = new ComponentName(packageName, "class");
143         mProvider.verifyLaunchSearchResultPageCaller(mActivity, cn);
144     }
145 }
146