From 5bac36a15a3e9b163784db53283e9806b6f18e3e Mon Sep 17 00:00:00 2001 From: Amin Shaikh Date: Wed, 15 Feb 2017 17:47:04 -0800 Subject: [PATCH] Fix NPE in WriteWifiConfigToNfcDialog. Bug: 34691792 Test: m RunSettingsRoboTests Change-Id: I0e396f4f590c8de6636c7494e20d74c562b7c9cd --- .../wifi/WriteWifiConfigToNfcDialog.java | 2 +- .../testutils/shadow/ShadowNfcAdapter.java | 57 ++++++++++ .../wifi/WriteWifiConfigToNfcDialogTest.java | 101 ++++++++++++++++++ 3 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 tests/robotests/src/com/android/settings/testutils/shadow/ShadowNfcAdapter.java create mode 100644 tests/robotests/src/com/android/settings/wifi/WriteWifiConfigToNfcDialogTest.java diff --git a/src/com/android/settings/wifi/WriteWifiConfigToNfcDialog.java b/src/com/android/settings/wifi/WriteWifiConfigToNfcDialog.java index 00b24462933..0d3f3b6c1d8 100644 --- a/src/com/android/settings/wifi/WriteWifiConfigToNfcDialog.java +++ b/src/com/android/settings/wifi/WriteWifiConfigToNfcDialog.java @@ -145,7 +145,7 @@ class WriteWifiConfigToNfcDialog extends AlertDialog passwordHex = String.format(PASSWORD_FORMAT, passwordLength, passwordHex).toUpperCase(); - if (wpsNfcConfigurationToken.contains(passwordHex)) { + if (wpsNfcConfigurationToken != null && wpsNfcConfigurationToken.contains(passwordHex)) { mWpsNfcConfigurationToken = wpsNfcConfigurationToken; Activity activity = getOwnerActivity(); diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowNfcAdapter.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowNfcAdapter.java new file mode 100644 index 00000000000..e4421eee813 --- /dev/null +++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowNfcAdapter.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.settings.testutils.shadow; + +import android.app.Activity; +import android.content.Context; +import android.nfc.NfcAdapter; +import android.os.Bundle; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; +import org.robolectric.annotation.Resetter; +import org.robolectric.util.ReflectionHelpers; +import org.robolectric.util.ReflectionHelpers.ClassParameter; + +/** + * Shadow of {@link NfcAdapter}. + */ +@Implements(NfcAdapter.class) +public class ShadowNfcAdapter { + private static boolean sReaderModeEnabled; + + @Implementation + public void enableReaderMode(Activity activity, NfcAdapter.ReaderCallback callback, int flags, + Bundle extras) { + sReaderModeEnabled = true; + } + + @Implementation + public static NfcAdapter getDefaultAdapter(Context context) { + return ReflectionHelpers.callConstructor( + NfcAdapter.class, ClassParameter.from(Context.class, context)); + } + + public static boolean isReaderModeEnabled() { + return sReaderModeEnabled; + } + + @Resetter + public static void reset() { + sReaderModeEnabled = false; + } +} \ No newline at end of file diff --git a/tests/robotests/src/com/android/settings/wifi/WriteWifiConfigToNfcDialogTest.java b/tests/robotests/src/com/android/settings/wifi/WriteWifiConfigToNfcDialogTest.java new file mode 100644 index 00000000000..6e45cdedf43 --- /dev/null +++ b/tests/robotests/src/com/android/settings/wifi/WriteWifiConfigToNfcDialogTest.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.settings.wifi; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.when; + +import android.app.Activity; +import android.content.Context; +import android.net.wifi.WifiManager; +import android.view.inputmethod.InputMethodManager; + +import com.android.settings.SettingsRobolectricTestRunner; +import com.android.settings.TestConfig; +import com.android.settings.testutils.shadow.ShadowNfcAdapter; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; +import org.robolectric.util.ReflectionHelpers; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config( + manifest = TestConfig.MANIFEST_PATH, + sdk = TestConfig.SDK_VERSION, + shadows = ShadowNfcAdapter.class +) +public class WriteWifiConfigToNfcDialogTest { + private static final int NETWORK_ID = 17; + + @Mock Activity mActivity; + @Mock WifiManager mWifiManager; + + private WriteWifiConfigToNfcDialog mWriteWifiConfigToNfcDialog; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(mActivity.getApplicationContext()).thenReturn(mActivity); + when(mActivity.getSystemService(Context.INPUT_METHOD_SERVICE)) + .thenReturn(ReflectionHelpers.newInstance(InputMethodManager.class)); + + mWriteWifiConfigToNfcDialog = new WriteWifiConfigToNfcDialog(RuntimeEnvironment.application, + NETWORK_ID, 0 /* security */, mWifiManager); + mWriteWifiConfigToNfcDialog.setOwnerActivity(mActivity); + mWriteWifiConfigToNfcDialog.onCreate(null /* savedInstanceState */); + } + + @After + public void tearDown() { + ShadowNfcAdapter.reset(); + } + + @Test + public void testOnClick_nfcConfigurationTokenDoesNotContainPasswordHex() { + when(mWifiManager.getWpsNfcConfigurationToken(NETWORK_ID)).thenReturn("blah"); + + mWriteWifiConfigToNfcDialog.onClick(null); + + assertThat(ShadowNfcAdapter.isReaderModeEnabled()).isFalse(); + } + + @Test + public void testOnClick_nfcConfigurationTokenIsNull() { + when(mWifiManager.getWpsNfcConfigurationToken(NETWORK_ID)).thenReturn(null); + + mWriteWifiConfigToNfcDialog.onClick(null); + + assertThat(ShadowNfcAdapter.isReaderModeEnabled()).isFalse(); + } + + @Test + public void testOnClick_nfcConfigurationTokenContainsPasswordHex() { + // This is the corresponding passwordHex for an empty string password. + when(mWifiManager.getWpsNfcConfigurationToken(NETWORK_ID)).thenReturn("10270000"); + + mWriteWifiConfigToNfcDialog.onClick(null); + + assertThat(ShadowNfcAdapter.isReaderModeEnabled()).isTrue(); + } +}