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 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.Robolectric;
47 import org.robolectric.RobolectricTestRunner;
48 import org.robolectric.RuntimeEnvironment;
49 
50 import java.util.ArrayList;
51 import java.util.List;
52 
53 @RunWith(RobolectricTestRunner.class)
54 public class SettingsActivityTest {
55 
56     @Mock
57     private FragmentManager mFragmentManager;
58     @Mock
59     private ActivityManager.TaskDescription mTaskDescription;
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(Robolectric.buildActivity(SettingsActivity.class).create().get());
69     }
70 
71     @Test
launchSettingFragment_nullExtraShowFragment_shouldNotCrash()72     public void launchSettingFragment_nullExtraShowFragment_shouldNotCrash() {
73         when(mActivity.getSupportFragmentManager()).thenReturn(mFragmentManager);
74         doReturn(mContext.getContentResolver()).when(mActivity).getContentResolver();
75         when(mFragmentManager.beginTransaction()).thenReturn(mock(FragmentTransaction.class));
76         doReturn(RuntimeEnvironment.application.getClassLoader()).when(mActivity).getClassLoader();
77 
78         mActivity.launchSettingFragment(null, mock(Intent.class));
79     }
80 
81     @Test
setTaskDescription_shouldUpdateIcon()82     public void setTaskDescription_shouldUpdateIcon() {
83         mActivity.setTaskDescription(mTaskDescription);
84 
85         verify(mTaskDescription).setIcon(any());
86     }
87 
88     @Test
getSharedPreferences_intentExtraIsNull_shouldNotCrash()89     public void getSharedPreferences_intentExtraIsNull_shouldNotCrash() {
90         final Intent intent = new Intent();
91         intent.putExtra(EXTRA_SHOW_FRAGMENT, (String)null);
92         doReturn(intent).when(mActivity).getIntent();
93         doReturn(mContext.getPackageName()).when(mActivity).getPackageName();
94         FakeFeatureFactory.setupForTest();
95 
96         mActivity.getSharedPreferences(mContext.getPackageName() + "_preferences", 0);
97     }
98 
99     @Test
onActivityResult_shouldDelegateToListener()100     public void onActivityResult_shouldDelegateToListener() {
101         final List<Fragment> fragments = new ArrayList<>();
102         fragments.add(new Fragment());
103         fragments.add(new ListenerFragment());
104 
105         final FragmentManager manager = mock(FragmentManager.class);
106         when(mActivity.getSupportFragmentManager()).thenReturn(manager);
107         when(manager.getFragments()).thenReturn(fragments);
108 
109         mActivity.onActivityResult(0, 0, new Intent());
110 
111         assertThat(((ListenerFragment) fragments.get(1)).mOnActivityResultCalled).isTrue();
112     }
113 
114     public static class ListenerFragment extends Fragment implements OnActivityResultListener {
115 
116         private boolean mOnActivityResultCalled;
117 
118         @Override
onActivityResult(int requestCode, int resultCode, Intent data)119         public void onActivityResult(int requestCode, int resultCode, Intent data) {
120             mOnActivityResultCalled = true;
121         }
122     }
123 }
124