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.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.content.Context; 30 import android.content.Intent; 31 32 import androidx.fragment.app.Fragment; 33 import androidx.fragment.app.FragmentManager; 34 import androidx.fragment.app.FragmentTransaction; 35 36 import com.android.settings.core.OnActivityResultListener; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.RobolectricTestRunner; 44 import org.robolectric.RuntimeEnvironment; 45 46 import java.util.ArrayList; 47 import java.util.List; 48 49 @RunWith(RobolectricTestRunner.class) 50 public class SettingsActivityTest { 51 52 @Mock 53 private FragmentManager mFragmentManager; 54 @Mock 55 private ActivityManager.TaskDescription mTaskDescription; 56 private SettingsActivity mActivity; 57 private Context mContext; 58 59 @Before setUp()60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 63 mContext = RuntimeEnvironment.application; 64 mActivity = spy(new SettingsActivity()); 65 } 66 67 @Test launchSettingFragment_nullExtraShowFragment_shouldNotCrash()68 public void launchSettingFragment_nullExtraShowFragment_shouldNotCrash() { 69 when(mActivity.getSupportFragmentManager()).thenReturn(mFragmentManager); 70 doReturn(mContext.getContentResolver()).when(mActivity).getContentResolver(); 71 when(mFragmentManager.beginTransaction()).thenReturn(mock(FragmentTransaction.class)); 72 73 doReturn(RuntimeEnvironment.application.getClassLoader()).when(mActivity).getClassLoader(); 74 75 mActivity.launchSettingFragment(null, mock(Intent.class)); 76 } 77 78 @Test setTaskDescription_shouldUpdateIcon()79 public void setTaskDescription_shouldUpdateIcon() { 80 mActivity.setTaskDescription(mTaskDescription); 81 82 verify(mTaskDescription).setIcon(anyInt()); 83 } 84 85 @Test onActivityResult_shouldDelegateToListener()86 public void onActivityResult_shouldDelegateToListener() { 87 final List<Fragment> fragments = new ArrayList<>(); 88 fragments.add(new Fragment()); 89 fragments.add(new ListenerFragment()); 90 91 final FragmentManager manager = mock(FragmentManager.class); 92 when(mActivity.getSupportFragmentManager()).thenReturn(manager); 93 when(manager.getFragments()).thenReturn(fragments); 94 95 mActivity.onActivityResult(0, 0, new Intent()); 96 97 assertThat(((ListenerFragment) fragments.get(1)).mOnActivityResultCalled).isTrue(); 98 } 99 100 public static class ListenerFragment extends Fragment implements OnActivityResultListener { 101 102 private boolean mOnActivityResultCalled; 103 104 @Override onActivityResult(int requestCode, int resultCode, Intent data)105 public void onActivityResult(int requestCode, int resultCode, Intent data) { 106 mOnActivityResultCalled = true; 107 } 108 } 109 } 110