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 import static org.mockito.Mockito.spy; 22 23 import android.app.Activity; 24 import android.content.ComponentName; 25 import android.content.Intent; 26 import android.widget.Toolbar; 27 28 import com.android.settings.testutils.FakeFeatureFactory; 29 import com.android.settings.testutils.SettingsRobolectricTestRunner; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.robolectric.Robolectric; 35 import org.robolectric.Shadows; 36 37 @RunWith(SettingsRobolectricTestRunner.class) 38 public class SearchFeatureProviderImplTest { 39 40 private SearchFeatureProviderImpl mProvider; 41 private Activity mActivity; 42 43 @Before setUp()44 public void setUp() { 45 FakeFeatureFactory.setupForTest(); 46 mActivity = Robolectric.buildActivity(Activity.class).create().visible().get(); 47 mProvider = spy(new SearchFeatureProviderImpl()); 48 } 49 50 @Test initSearchToolbar_shouldInitWithOnClickListener()51 public void initSearchToolbar_shouldInitWithOnClickListener() { 52 mProvider.initSearchToolbar(mActivity, null); 53 // Should not crash. 54 55 final Toolbar toolbar = new Toolbar(mActivity); 56 mProvider.initSearchToolbar(mActivity, toolbar); 57 58 toolbar.performClick(); 59 60 final Intent launchIntent = Shadows.shadowOf(mActivity).getNextStartedActivity(); 61 62 assertThat(launchIntent.getAction()) 63 .isEqualTo("com.android.settings.action.SETTINGS_SEARCH"); 64 } 65 66 @Test(expected = IllegalArgumentException.class) verifyLaunchSearchResultPageCaller_nullCaller_shouldCrash()67 public void verifyLaunchSearchResultPageCaller_nullCaller_shouldCrash() { 68 mProvider.verifyLaunchSearchResultPageCaller(mActivity, null /* caller */); 69 } 70 71 @Test(expected = SecurityException.class) verifyLaunchSearchResultPageCaller_badCaller_shouldCrash()72 public void verifyLaunchSearchResultPageCaller_badCaller_shouldCrash() { 73 final ComponentName cn = new ComponentName("pkg", "class"); 74 mProvider.verifyLaunchSearchResultPageCaller(mActivity, cn); 75 } 76 77 @Test verifyLaunchSearchResultPageCaller_settingsCaller_shouldNotCrash()78 public void verifyLaunchSearchResultPageCaller_settingsCaller_shouldNotCrash() { 79 final ComponentName cn = new ComponentName(mActivity.getPackageName(), "class"); 80 mProvider.verifyLaunchSearchResultPageCaller(mActivity, cn); 81 } 82 83 @Test verifyLaunchSearchResultPageCaller_settingsIntelligenceCaller_shouldNotCrash()84 public void verifyLaunchSearchResultPageCaller_settingsIntelligenceCaller_shouldNotCrash() { 85 final String packageName = mProvider.getSettingsIntelligencePkgName(); 86 final ComponentName cn = new ComponentName(packageName, "class"); 87 mProvider.verifyLaunchSearchResultPageCaller(mActivity, cn); 88 } 89 90 @Test cleanQuery_trimsWhitespace()91 public void cleanQuery_trimsWhitespace() { 92 final String query = " space "; 93 final String cleanQuery = "space"; 94 95 assertThat(mProvider.cleanQuery(query)).isEqualTo(cleanQuery); 96 } 97 } 98