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 org.mockito.ArgumentMatchers.nullable; 20 import static org.mockito.Matchers.anyInt; 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.spy; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.app.ActivityManager; 28 import android.app.FragmentManager; 29 import android.app.FragmentTransaction; 30 import android.content.Intent; 31 import android.graphics.Bitmap; 32 33 import com.android.settings.testutils.SettingsRobolectricTestRunner; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RuntimeEnvironment; 41 42 @RunWith(SettingsRobolectricTestRunner.class) 43 public class SettingsActivityTest { 44 45 @Mock 46 private FragmentManager mFragmentManager; 47 @Mock 48 private ActivityManager.TaskDescription mTaskDescription; 49 @Mock 50 private Bitmap mBitmap; 51 private SettingsActivity mActivity; 52 53 @Before 54 public void setUp() { 55 MockitoAnnotations.initMocks(this); 56 57 mActivity = spy(new SettingsActivity()); 58 doReturn(mBitmap).when(mActivity).getBitmapFromXmlResource(anyInt()); 59 } 60 61 @Test 62 public void launchSettingFragment_nullExtraShowFragment_shouldNotCrash() { 63 when(mActivity.getFragmentManager()).thenReturn(mFragmentManager); 64 when(mFragmentManager.beginTransaction()).thenReturn(mock(FragmentTransaction.class)); 65 66 doReturn(RuntimeEnvironment.application.getClassLoader()).when(mActivity).getClassLoader(); 67 68 mActivity.launchSettingFragment(null, true, mock(Intent.class)); 69 } 70 71 @Test 72 public void testSetTaskDescription_IconChanged() { 73 mActivity.setTaskDescription(mTaskDescription); 74 75 verify(mTaskDescription).setIcon(nullable(Bitmap.class)); 76 } 77 } 78