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.Intent; 24 import android.content.pm.ActivityInfo; 25 import android.content.pm.ResolveInfo; 26 import android.net.Uri; 27 import android.provider.Settings; 28 import android.widget.Toolbar; 29 30 import androidx.fragment.app.FragmentActivity; 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.Ignore; 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 @Ignore 65 @Config(shadows = ShadowUtils.class) initSearchToolbar_hasResolvedInfo_shouldStartCorrectIntent()66 public void initSearchToolbar_hasResolvedInfo_shouldStartCorrectIntent() { 67 final Intent searchIntent = new Intent(Settings.ACTION_APP_SEARCH_SETTINGS) 68 .setPackage(mActivity.getString(R.string.config_settingsintelligence_package_name)); 69 final ResolveInfo info = new ResolveInfo(); 70 final ActivityInfo activityInfo = new ActivityInfo(); 71 activityInfo.packageName = "com.android.example"; 72 info.activityInfo = activityInfo; 73 mPackageManager.addResolveInfoForIntent(searchIntent, info); 74 75 // Should not crash. 76 mProvider.initSearchToolbar(mActivity, null, SettingsEnums.TESTING); 77 78 final Toolbar toolbar = new Toolbar(mActivity); 79 // This ensures navigationView is created. 80 toolbar.setNavigationContentDescription("test"); 81 mProvider.initSearchToolbar(mActivity, toolbar, SettingsEnums.TESTING); 82 83 toolbar.performClick(); 84 85 final Intent launchIntent = Shadows.shadowOf(mActivity).getNextStartedActivity(); 86 87 assertThat(launchIntent.getAction()).isEqualTo(Settings.ACTION_APP_SEARCH_SETTINGS); 88 } 89 90 @Test 91 @Config(shadows = ShadowUtils.class) initSearchToolbar_noResolvedInfo_shouldNotStartActivity()92 public void initSearchToolbar_noResolvedInfo_shouldNotStartActivity() { 93 final Toolbar toolbar = new Toolbar(mActivity); 94 // This ensures navigationView is created. 95 toolbar.setNavigationContentDescription("test"); 96 mProvider.initSearchToolbar(mActivity, toolbar, SettingsEnums.TESTING); 97 98 toolbar.performClick(); 99 100 assertThat(Shadows.shadowOf(mActivity).getNextStartedActivity()).isNull(); 101 } 102 103 @Test initSearchToolbar_deviceNotProvisioned_shouldNotCreateSearchBar()104 public void initSearchToolbar_deviceNotProvisioned_shouldNotCreateSearchBar() { 105 final Toolbar toolbar = new Toolbar(mActivity); 106 // This ensures navigationView is created. 107 toolbar.setNavigationContentDescription("test"); 108 109 Settings.Global.putInt(mActivity.getContentResolver(), 110 Settings.Global.DEVICE_PROVISIONED, 0); 111 112 toolbar.performClick(); 113 114 assertThat(Shadows.shadowOf(mActivity).getNextStartedActivity()).isNull(); 115 } 116 117 @Test buildSearchIntent_shouldIncludeReferrer()118 public void buildSearchIntent_shouldIncludeReferrer() { 119 final Intent searchIntent = mProvider.buildSearchIntent(mActivity, SettingsEnums.TESTING); 120 final Uri referrer = searchIntent.getParcelableExtra(Intent.EXTRA_REFERRER); 121 122 assertThat(referrer.toString()).isEqualTo( 123 "android-app://" + mActivity.getPackageName() + "/" + SettingsEnums.TESTING); 124 } 125 126 @Test(expected = IllegalArgumentException.class) verifyLaunchSearchResultPageCaller_nullCaller_shouldCrash()127 public void verifyLaunchSearchResultPageCaller_nullCaller_shouldCrash() { 128 mProvider.verifyLaunchSearchResultPageCaller(mActivity, null /* caller */); 129 } 130 131 @Test(expected = SecurityException.class) verifyLaunchSearchResultPageCaller_badCaller_shouldCrash()132 public void verifyLaunchSearchResultPageCaller_badCaller_shouldCrash() { 133 final String packageName = "pkg"; 134 135 mProvider.verifyLaunchSearchResultPageCaller(mActivity, packageName); 136 } 137 138 @Test verifyLaunchSearchResultPageCaller_settingsCaller_shouldNotCrash()139 public void verifyLaunchSearchResultPageCaller_settingsCaller_shouldNotCrash() { 140 final String packageName = mActivity.getPackageName(); 141 142 mProvider.verifyLaunchSearchResultPageCaller(mActivity, packageName); 143 } 144 145 @Test verifyLaunchSearchResultPageCaller_settingsIntelligenceCaller_shouldNotCrash()146 public void verifyLaunchSearchResultPageCaller_settingsIntelligenceCaller_shouldNotCrash() { 147 final String packageName = mProvider.getSettingsIntelligencePkgName(mActivity); 148 149 mProvider.verifyLaunchSearchResultPageCaller(mActivity, packageName); 150 } 151 } 152