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