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