1 /*
2  * Copyright (C) 2017 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.Mockito.when;
22 
23 import android.app.Activity;
24 import android.content.Context;
25 import android.net.wifi.WifiManager;
26 import android.view.inputmethod.InputMethodManager;
27 
28 import com.android.settings.SettingsRobolectricTestRunner;
29 import com.android.settings.TestConfig;
30 import com.android.settings.testutils.shadow.ShadowNfcAdapter;
31 
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.RuntimeEnvironment;
39 import org.robolectric.annotation.Config;
40 import org.robolectric.util.ReflectionHelpers;
41 
42 @RunWith(SettingsRobolectricTestRunner.class)
43 @Config(
44         manifest = TestConfig.MANIFEST_PATH,
45         sdk = TestConfig.SDK_VERSION,
46         shadows = ShadowNfcAdapter.class
47 )
48 public class WriteWifiConfigToNfcDialogTest {
49     @Mock Activity mActivity;
50     @Mock WifiManagerWrapper mWifiManager;
51 
52     private WriteWifiConfigToNfcDialog mWriteWifiConfigToNfcDialog;
53 
54     @Before
setUp()55     public void setUp() {
56         MockitoAnnotations.initMocks(this);
57         when(mActivity.getApplicationContext()).thenReturn(mActivity);
58         when(mActivity.getSystemService(Context.INPUT_METHOD_SERVICE))
59                 .thenReturn(ReflectionHelpers.newInstance(InputMethodManager.class));
60 
61         mWriteWifiConfigToNfcDialog = new WriteWifiConfigToNfcDialog(RuntimeEnvironment.application,
62                 0 /* security */, mWifiManager);
63         mWriteWifiConfigToNfcDialog.setOwnerActivity(mActivity);
64         mWriteWifiConfigToNfcDialog.onCreate(null /* savedInstanceState */);
65     }
66 
67     @After
tearDown()68     public void tearDown() {
69         ShadowNfcAdapter.reset();
70     }
71 
72     @Test
testOnClick_nfcConfigurationTokenDoesNotContainPasswordHex()73     public void testOnClick_nfcConfigurationTokenDoesNotContainPasswordHex() {
74         when(mWifiManager.getCurrentNetworkWpsNfcConfigurationToken()).thenReturn("blah");
75 
76         mWriteWifiConfigToNfcDialog.onClick(null);
77 
78         assertThat(ShadowNfcAdapter.isReaderModeEnabled()).isFalse();
79     }
80 
81     @Test
testOnClick_nfcConfigurationTokenIsNull()82     public void testOnClick_nfcConfigurationTokenIsNull() {
83         when(mWifiManager.getCurrentNetworkWpsNfcConfigurationToken()).thenReturn(null);
84 
85         mWriteWifiConfigToNfcDialog.onClick(null);
86 
87         assertThat(ShadowNfcAdapter.isReaderModeEnabled()).isFalse();
88     }
89 
90     @Test
testOnClick_nfcConfigurationTokenContainsPasswordHex()91     public void testOnClick_nfcConfigurationTokenContainsPasswordHex() {
92         // This is the corresponding passwordHex for an empty string password.
93         when(mWifiManager.getCurrentNetworkWpsNfcConfigurationToken()).thenReturn("10270000");
94 
95         mWriteWifiConfigToNfcDialog.onClick(null);
96 
97         assertThat(ShadowNfcAdapter.isReaderModeEnabled()).isTrue();
98     }
99 }
100