1 /*
2  * Copyright (C) 2021 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.wifi;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.when;
23 
24 import android.app.IActivityManager;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.net.wifi.WifiManager;
28 import android.os.RemoteException;
29 
30 import androidx.annotation.Nullable;
31 import androidx.test.core.app.ActivityScenario;
32 import androidx.test.core.app.ApplicationProvider;
33 import androidx.test.ext.junit.runners.AndroidJUnit4;
34 
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Ignore;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.Spy;
43 import org.mockito.junit.MockitoJUnit;
44 import org.mockito.junit.MockitoRule;
45 
46 @Ignore
47 @RunWith(AndroidJUnit4.class)
48 public class RequestToggleWiFiActivityTest {
49 
50     @Rule
51     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
52     @Spy
53     private final Context mContext = ApplicationProvider.getApplicationContext();
54     @Mock
55     private WifiManager mWifiManager;
56     @Mock
57     private IActivityManager mIActivityManager;
58 
59     private ActivityScenario<RequestToggleWiFiActivity> mActivityScenario;
60     private RequestToggleWiFiActivity mActivity;
61 
62     @Before
setUp()63     public void setUp() {
64         when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager);
65         when(mWifiManager.getWifiState()).thenReturn(WifiManager.WIFI_STATE_DISABLED);
66 
67         mActivityScenario = ActivityScenario.launch(new Intent(WifiManager.ACTION_REQUEST_ENABLE));
68         mActivityScenario.onActivity(activity -> mActivity = activity);
69 
70     }
71 
72     @After
cleanUp()73     public void cleanUp() {
74         mActivity = null;
75         if (mActivityScenario != null) {
76             mActivityScenario.close();
77         }
78     }
79 
80     @Test
getAppLabel_nullPackageName_returnNull()81     public void getAppLabel_nullPackageName_returnNull() {
82         fakeCallingPackage(null);
83 
84         assertThat(mActivity.getAppLabel()).isNull();
85     }
86 
87     @Test
getAppLabel_settingsPackageName_returnNotNull()88     public void getAppLabel_settingsPackageName_returnNotNull() {
89         fakeCallingPackage("com.android.settings");
90 
91         assertThat(mActivity.getAppLabel()).isNotNull();
92     }
93 
fakeCallingPackage(@ullable String packageName)94     private void fakeCallingPackage(@Nullable String packageName) {
95         assertThat(mActivity).isNotNull();
96         mActivity.mActivityManager = mIActivityManager;
97         try {
98             when(mIActivityManager.getLaunchedFromPackage(any())).thenReturn(packageName);
99         } catch (RemoteException e) {
100             // Do nothing.
101         }
102     }
103 }
104