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 package com.android.settings; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.nullable; 22 import static org.mockito.Matchers.anyInt; 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.app.Activity; 30 import android.app.ActivityManager; 31 import android.app.FragmentManager; 32 import android.app.FragmentTransaction; 33 import android.content.ComponentName; 34 import android.content.Context; 35 import android.content.Intent; 36 import android.graphics.Bitmap; 37 import android.os.Bundle; 38 import android.provider.Settings.Global; 39 import android.view.View; 40 41 import com.android.settings.search.SearchActivity; 42 import com.android.settings.testutils.SettingsRobolectricTestRunner; 43 import org.junit.Before; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.mockito.Answers; 47 import org.mockito.Mock; 48 import org.mockito.MockitoAnnotations; 49 import org.robolectric.RuntimeEnvironment; 50 import org.robolectric.annotation.Config; 51 import org.robolectric.shadows.ShadowApplication; 52 import org.robolectric.util.ReflectionHelpers; 53 54 @RunWith(SettingsRobolectricTestRunner.class) 55 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 56 public class SettingsActivityTest { 57 58 @Mock 59 private FragmentManager mFragmentManager; 60 @Mock 61 private ActivityManager.TaskDescription mTaskDescription; 62 @Mock 63 private Bitmap mBitmap; 64 @Mock 65 private View mSearchBar; 66 private SettingsActivity mActivity; 67 private Context mContext; 68 69 @Before setUp()70 public void setUp() { 71 MockitoAnnotations.initMocks(this); 72 73 mContext = RuntimeEnvironment.application; 74 mActivity = spy(new SettingsActivity()); 75 doReturn(mBitmap).when(mActivity).getBitmapFromXmlResource(anyInt()); 76 doReturn(mContext.getContentResolver()).when(mActivity).getContentResolver(); 77 doReturn(mSearchBar).when(mActivity).findViewById(R.id.search_bar); 78 } 79 80 @Test setSearchBarVisibility_deviceNotProvisioned_shouldDisableSearch()81 public void setSearchBarVisibility_deviceNotProvisioned_shouldDisableSearch() { 82 Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 0); 83 84 mActivity.setSearchBarVisibility(); 85 86 verify(mSearchBar).setVisibility(View.INVISIBLE); 87 } 88 89 @Test setSearchBarVisibility_deviceProvisioned_shouldEnableSearch()90 public void setSearchBarVisibility_deviceProvisioned_shouldEnableSearch() { 91 Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 1); 92 93 mActivity.setSearchBarVisibility(); 94 95 verify(mSearchBar).setVisibility(View.VISIBLE); 96 } 97 98 @Test launchSettingFragment_nullExtraShowFragment_shouldNotCrash()99 public void launchSettingFragment_nullExtraShowFragment_shouldNotCrash() 100 throws ClassNotFoundException { 101 when(mActivity.getFragmentManager()).thenReturn(mFragmentManager); 102 when(mFragmentManager.beginTransaction()).thenReturn(mock(FragmentTransaction.class)); 103 104 doReturn(RuntimeEnvironment.application.getClassLoader()).when(mActivity).getClassLoader(); 105 106 mActivity.launchSettingFragment(null, true, mock(Intent.class)); 107 } 108 109 @Test testSetTaskDescription_IconChanged()110 public void testSetTaskDescription_IconChanged() { 111 mActivity.setTaskDescription(mTaskDescription); 112 113 verify(mTaskDescription).setIcon(nullable(Bitmap.class)); 114 } 115 116 @Test testSaveState_EnabledHomeSaved()117 public void testSaveState_EnabledHomeSaved() { 118 mActivity.mDisplayHomeAsUpEnabled = true; 119 Bundle bundle = new Bundle(); 120 mActivity.saveState(bundle); 121 122 assertThat((boolean) bundle.get(SettingsActivity.SAVE_KEY_SHOW_HOME_AS_UP)).isTrue(); 123 } 124 125 @Test testOnClick()126 public void testOnClick() { 127 doReturn("com.android.settings").when(mActivity).getPackageName(); 128 129 mActivity.onClick(null); 130 131 Intent intent = ShadowApplication.getInstance().getNextStartedActivity(); 132 assertThat(intent.getComponent()).isEqualTo( 133 new ComponentName("com.android.settings", SearchActivity.class.getName())); 134 } 135 } 136