• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.provider.Settings.EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_INTENT_URI;
20 
21 import static com.android.settings.SettingsActivity.EXTRA_SHOW_FRAGMENT;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 
32 import android.app.ActivityManager;
33 import android.content.Context;
34 import android.content.Intent;
35 import android.net.Uri;
36 
37 import androidx.fragment.app.Fragment;
38 import androidx.fragment.app.FragmentManager;
39 import androidx.fragment.app.FragmentTransaction;
40 
41 import com.android.settings.core.OnActivityResultListener;
42 import com.android.settings.testutils.FakeFeatureFactory;
43 import com.android.settings.testutils.shadow.ShadowUserManager;
44 
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.Mock;
49 import org.mockito.MockitoAnnotations;
50 import org.robolectric.Robolectric;
51 import org.robolectric.RobolectricTestRunner;
52 import org.robolectric.RuntimeEnvironment;
53 import org.robolectric.annotation.Config;
54 
55 import java.net.URISyntaxException;
56 import java.util.ArrayList;
57 import java.util.List;
58 
59 @RunWith(RobolectricTestRunner.class)
60 @Config(shadows = ShadowUserManager.class)
61 public class SettingsActivityTest {
62 
63     @Mock
64     private FragmentManager mFragmentManager;
65     @Mock
66     private ActivityManager.TaskDescription mTaskDescription;
67     private SettingsActivity mActivity;
68     private Context mContext;
69 
70     @Before
setUp()71     public void setUp() {
72         MockitoAnnotations.initMocks(this);
73 
74         mContext = RuntimeEnvironment.application;
75         mActivity = spy(Robolectric.buildActivity(SettingsActivity.class).create().get());
76     }
77 
78     @Test
launchSettingFragment_nullExtraShowFragment_shouldNotCrash()79     public void launchSettingFragment_nullExtraShowFragment_shouldNotCrash() {
80         when(mActivity.getSupportFragmentManager()).thenReturn(mFragmentManager);
81         doReturn(mContext.getContentResolver()).when(mActivity).getContentResolver();
82         when(mFragmentManager.beginTransaction()).thenReturn(mock(FragmentTransaction.class));
83         doReturn(RuntimeEnvironment.application.getClassLoader()).when(mActivity).getClassLoader();
84 
85         mActivity.launchSettingFragment(null, mock(Intent.class));
86     }
87 
88     @Test
setTaskDescription_shouldUpdateIcon()89     public void setTaskDescription_shouldUpdateIcon() {
90         mActivity.setTaskDescription(mTaskDescription);
91 
92         verify(mTaskDescription).setIcon(any());
93     }
94 
95     @Test
getSharedPreferences_intentExtraIsNull_shouldNotCrash()96     public void getSharedPreferences_intentExtraIsNull_shouldNotCrash() {
97         final Intent intent = new Intent();
98         intent.putExtra(EXTRA_SHOW_FRAGMENT, (String)null);
99         doReturn(intent).when(mActivity).getIntent();
100         doReturn(mContext.getPackageName()).when(mActivity).getPackageName();
101         FakeFeatureFactory.setupForTest();
102 
103         mActivity.getSharedPreferences(mContext.getPackageName() + "_preferences", 0);
104     }
105 
106     @Test
onActivityResult_shouldDelegateToListener()107     public void onActivityResult_shouldDelegateToListener() {
108         final List<Fragment> fragments = new ArrayList<>();
109         fragments.add(new Fragment());
110         fragments.add(new ListenerFragment());
111 
112         final FragmentManager manager = mock(FragmentManager.class);
113         when(mActivity.getSupportFragmentManager()).thenReturn(manager);
114         when(manager.getFragments()).thenReturn(fragments);
115 
116         mActivity.onActivityResult(0, 0, new Intent());
117 
118         assertThat(((ListenerFragment) fragments.get(1)).mOnActivityResultCalled).isTrue();
119     }
120 
121     @Test
getTrampolineIntent_intentSelector_shouldNotChangeIntentAction()122     public void getTrampolineIntent_intentSelector_shouldNotChangeIntentAction() {
123         Intent targetIntent = new Intent().setClassName("android",
124                 "com.android.internal.app.PlatLogoActivity");
125         Intent intent = new Intent(android.provider.Settings.ACTION_DISPLAY_SETTINGS);
126         intent.setComponent(intent.resolveActivity(mContext.getPackageManager()));
127         intent.setSelector(new
128         Intent().setData(Uri.fromParts(targetIntent.toUri(Intent.URI_INTENT_SCHEME), /* ssp= */ "",
129                 /* fragment= */ null)));
130 
131         Intent resultIntent = SettingsActivity.getTrampolineIntent(intent, "menu_key");
132 
133         String intentUriString =
134                 resultIntent.getStringExtra(EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_INTENT_URI);
135         Intent parsedIntent = null;
136         try {
137             parsedIntent = Intent.parseUri(intentUriString, Intent.URI_INTENT_SCHEME);
138         } catch (URISyntaxException e) {
139             // Do nothng.
140         }
141         assertThat(parsedIntent.getAction()).isEqualTo(intent.getAction());
142     }
143 
144     public static class ListenerFragment extends Fragment implements OnActivityResultListener {
145 
146         private boolean mOnActivityResultCalled;
147 
148         @Override
onActivityResult(int requestCode, int resultCode, Intent data)149         public void onActivityResult(int requestCode, int resultCode, Intent data) {
150             mOnActivityResultCalled = true;
151         }
152     }
153 }
154