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 import static org.mockito.ArgumentMatchers.nullable; 21 import static org.mockito.Matchers.anyInt; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.app.ActivityManager; 29 import android.app.FragmentManager; 30 import android.app.FragmentTransaction; 31 import android.content.Context; 32 import android.content.Intent; 33 import android.graphics.Bitmap; 34 import android.os.Bundle; 35 import android.provider.Settings.Global; 36 import android.view.View; 37 38 import com.android.settings.testutils.SettingsRobolectricTestRunner; 39 import com.android.settings.testutils.shadow.SettingsShadowResources; 40 import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Mock; 46 import org.mockito.MockitoAnnotations; 47 import org.robolectric.Robolectric; 48 import org.robolectric.RuntimeEnvironment; 49 import org.robolectric.annotation.Config; 50 51 @RunWith(SettingsRobolectricTestRunner.class) 52 public class SettingsActivityTest { 53 54 @Mock 55 private FragmentManager mFragmentManager; 56 @Mock 57 private ActivityManager.TaskDescription mTaskDescription; 58 @Mock 59 private Bitmap mBitmap; 60 private SettingsActivity mActivity; 61 private Context mContext; 62 63 @Before setUp()64 public void setUp() { 65 MockitoAnnotations.initMocks(this); 66 67 mContext = RuntimeEnvironment.application; 68 mActivity = spy(new SettingsActivity()); 69 doReturn(mBitmap).when(mActivity).getBitmapFromXmlResource(anyInt()); 70 } 71 72 @Test 73 @Config(shadows = { 74 SettingsShadowResourcesImpl.class, 75 SettingsShadowResources.SettingsShadowTheme.class, 76 }) onCreate_deviceNotProvisioned_shouldDisableSearch()77 public void onCreate_deviceNotProvisioned_shouldDisableSearch() { 78 Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 0); 79 final Intent intent = new Intent(mContext, Settings.class); 80 final SettingsActivity activity = 81 Robolectric.buildActivity(SettingsActivity.class, intent).create(Bundle.EMPTY).get(); 82 83 assertThat(activity.findViewById(R.id.search_bar).getVisibility()) 84 .isEqualTo(View.INVISIBLE); 85 } 86 87 @Test 88 @Config(shadows = { 89 SettingsShadowResourcesImpl.class, 90 SettingsShadowResources.SettingsShadowTheme.class, 91 }) onCreate_deviceProvisioned_shouldEnableSearch()92 public void onCreate_deviceProvisioned_shouldEnableSearch() { 93 Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 1); 94 final Intent intent = new Intent(mContext, Settings.class); 95 final SettingsActivity activity = 96 Robolectric.buildActivity(SettingsActivity.class, intent).create(Bundle.EMPTY).get(); 97 98 assertThat(activity.findViewById(R.id.search_bar).getVisibility()).isEqualTo(View.VISIBLE); 99 } 100 101 @Test launchSettingFragment_nullExtraShowFragment_shouldNotCrash()102 public void launchSettingFragment_nullExtraShowFragment_shouldNotCrash() { 103 when(mActivity.getFragmentManager()).thenReturn(mFragmentManager); 104 when(mFragmentManager.beginTransaction()).thenReturn(mock(FragmentTransaction.class)); 105 106 doReturn(RuntimeEnvironment.application.getClassLoader()).when(mActivity).getClassLoader(); 107 108 mActivity.launchSettingFragment(null, true, mock(Intent.class)); 109 } 110 111 @Test testSetTaskDescription_IconChanged()112 public void testSetTaskDescription_IconChanged() { 113 mActivity.setTaskDescription(mTaskDescription); 114 115 verify(mTaskDescription).setIcon(nullable(Bitmap.class)); 116 } 117 } 118