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.android.settings.SettingsActivity.EXTRA_SHOW_FRAGMENT; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.ArgumentMatchers.any; 24 import static org.mockito.Mockito.doReturn; 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.spy; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.when; 29 30 import android.app.ActivityManager; 31 import android.content.Context; 32 import android.content.Intent; 33 34 import androidx.fragment.app.Fragment; 35 import androidx.fragment.app.FragmentManager; 36 import androidx.fragment.app.FragmentTransaction; 37 38 import com.android.settings.core.OnActivityResultListener; 39 import com.android.settings.testutils.FakeFeatureFactory; 40 import com.android.settings.testutils.shadow.ShadowUserManager; 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.RobolectricTestRunner; 49 import org.robolectric.RuntimeEnvironment; 50 import org.robolectric.annotation.Config; 51 52 import java.util.ArrayList; 53 import java.util.List; 54 55 @RunWith(RobolectricTestRunner.class) 56 @Config(shadows = ShadowUserManager.class) 57 public class SettingsActivityTest { 58 59 @Mock 60 private FragmentManager mFragmentManager; 61 @Mock 62 private ActivityManager.TaskDescription mTaskDescription; 63 private SettingsActivity mActivity; 64 private Context mContext; 65 66 @Before setUp()67 public void setUp() { 68 MockitoAnnotations.initMocks(this); 69 70 mContext = RuntimeEnvironment.application; 71 mActivity = spy(Robolectric.buildActivity(SettingsActivity.class).create().get()); 72 } 73 74 @Test launchSettingFragment_nullExtraShowFragment_shouldNotCrash()75 public void launchSettingFragment_nullExtraShowFragment_shouldNotCrash() { 76 when(mActivity.getSupportFragmentManager()).thenReturn(mFragmentManager); 77 doReturn(mContext.getContentResolver()).when(mActivity).getContentResolver(); 78 when(mFragmentManager.beginTransaction()).thenReturn(mock(FragmentTransaction.class)); 79 doReturn(RuntimeEnvironment.application.getClassLoader()).when(mActivity).getClassLoader(); 80 81 mActivity.launchSettingFragment(null, mock(Intent.class)); 82 } 83 84 @Test setTaskDescription_shouldUpdateIcon()85 public void setTaskDescription_shouldUpdateIcon() { 86 mActivity.setTaskDescription(mTaskDescription); 87 88 verify(mTaskDescription).setIcon(any()); 89 } 90 91 @Test getSharedPreferences_intentExtraIsNull_shouldNotCrash()92 public void getSharedPreferences_intentExtraIsNull_shouldNotCrash() { 93 final Intent intent = new Intent(); 94 intent.putExtra(EXTRA_SHOW_FRAGMENT, (String)null); 95 doReturn(intent).when(mActivity).getIntent(); 96 doReturn(mContext.getPackageName()).when(mActivity).getPackageName(); 97 FakeFeatureFactory.setupForTest(); 98 99 mActivity.getSharedPreferences(mContext.getPackageName() + "_preferences", 0); 100 } 101 102 @Test onActivityResult_shouldDelegateToListener()103 public void onActivityResult_shouldDelegateToListener() { 104 final List<Fragment> fragments = new ArrayList<>(); 105 fragments.add(new Fragment()); 106 fragments.add(new ListenerFragment()); 107 108 final FragmentManager manager = mock(FragmentManager.class); 109 when(mActivity.getSupportFragmentManager()).thenReturn(manager); 110 when(manager.getFragments()).thenReturn(fragments); 111 112 mActivity.onActivityResult(0, 0, new Intent()); 113 114 assertThat(((ListenerFragment) fragments.get(1)).mOnActivityResultCalled).isTrue(); 115 } 116 117 public static class ListenerFragment extends Fragment implements OnActivityResultListener { 118 119 private boolean mOnActivityResultCalled; 120 121 @Override onActivityResult(int requestCode, int resultCode, Intent data)122 public void onActivityResult(int requestCode, int resultCode, Intent data) { 123 mOnActivityResultCalled = true; 124 } 125 } 126 } 127