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