diff options
author | Joe LaPenna <jlapenna@google.com> | 2017-03-13 23:03:45 -0700 |
---|---|---|
committer | Joe LaPenna <jlapenna@google.com> | 2017-03-13 23:16:16 -0700 |
commit | afd6abfd70716de9bae17bfbfa754bdaccc86c63 (patch) | |
tree | cd5aa3c79e27eee75b6888efe6bc47966562fa56 | |
parent | 3f3872359dd2e6286c8afcb97488d625b97ffe90 (diff) | |
download | NetworkRecommendation-afd6abfd70716de9bae17bfbfa754bdaccc86c63.tar.gz |
Update notify and wakeup code.
Test: mma NetworkRecommendation RunNetworkRecommendationRoboTests
Bug: 34944625
Change-Id: I337ad8b7e7c82478537525571e114bd374f8b8d6
7 files changed, 94 insertions, 17 deletions
diff --git a/robotests/src/com/android/networkrecommendation/notify/WifiNotificationControllerTest.java b/robotests/src/com/android/networkrecommendation/notify/WifiNotificationControllerTest.java index 6f918d9..943ffc7 100644 --- a/robotests/src/com/android/networkrecommendation/notify/WifiNotificationControllerTest.java +++ b/robotests/src/com/android/networkrecommendation/notify/WifiNotificationControllerTest.java @@ -357,6 +357,7 @@ public class WifiNotificationControllerTest { // Send click settings intent Intent intent = new Intent(WifiNotificationController.ACTION_PICK_WIFI_NETWORK); ShadowApplication.getInstance().sendBroadcast(intent); + verify(mNotificationManager).cancel(anyString(), anyInt()); } /** Verifies the flow when notification is reset on captive portal check. */ diff --git a/robotests/src/com/android/networkrecommendation/util/SsidUtilTest.java b/robotests/src/com/android/networkrecommendation/util/SsidUtilTest.java index 7634524..e3cdff8 100644 --- a/robotests/src/com/android/networkrecommendation/util/SsidUtilTest.java +++ b/robotests/src/com/android/networkrecommendation/util/SsidUtilTest.java @@ -15,11 +15,17 @@ */ package com.android.networkrecommendation.util; +import static com.android.networkrecommendation.TestData.NETWORK_KEY1; +import static com.android.networkrecommendation.TestData.SSID_1; +import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import com.android.networkrecommendation.config.Flag; +import com.android.networkrecommendation.config.G; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -37,6 +43,11 @@ public class SsidUtilTest { private static final String QUOTED_SSID = "\"foo\""; private static final String UNQUOTED_SSID = "foo"; + @Before + public void setUp() { + Flag.initForTest(); + } + @Test public void testQuote() { assertEquals(QUOTED_SSID, SsidUtil.quoteSsid(UNQUOTED_SSID)); @@ -56,4 +67,16 @@ public class SsidUtilTest { thrownException.expect(IllegalArgumentException.class); SsidUtil.checkIsValidQuotedSsid(UNQUOTED_SSID); } + + @Test + public void testRedactedId() { + G.Netrec.enableSensitiveLogging.override(false); + assertThat(SsidUtil.getRedactedId(NETWORK_KEY1)).doesNotContain(SSID_1); + } + + @Test + public void testRedactedId_sensitiveEnabled() { + G.Netrec.enableSensitiveLogging.override(true); + assertEquals("\"ssid1\"/01:01:01:01:01:01", SsidUtil.getRedactedId(NETWORK_KEY1)); + } } diff --git a/robotests/src/com/android/networkrecommendation/wakeup/WifiWakeupHelperTest.java b/robotests/src/com/android/networkrecommendation/wakeup/WifiWakeupHelperTest.java index 9c6cb80..a74e5fc 100644 --- a/robotests/src/com/android/networkrecommendation/wakeup/WifiWakeupHelperTest.java +++ b/robotests/src/com/android/networkrecommendation/wakeup/WifiWakeupHelperTest.java @@ -15,6 +15,7 @@ */ package com.android.networkrecommendation.wakeup; +import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; @@ -45,6 +46,7 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; +import org.robolectric.Shadows; import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowLooper; @@ -52,6 +54,7 @@ import org.robolectric.shadows.ShadowLooper; @RunWith(RobolectricTestRunner.class) @Config(manifest = "packages/services/NetworkRecommendation/AndroidManifest.xml", sdk = 23) public class WifiWakeupHelperTest { + private static final String SSID = "ssid"; private Context mContext; @@ -103,7 +106,6 @@ public class WifiWakeupHelperTest { @Test public void notificationCanceledWhenNeverConnected() { mWifiWakeupHelper.startWifiSession(mWifiConfiguration); - ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); verify(mNotificationManager).cancel(anyString(), anyInt()); @@ -116,9 +118,11 @@ public class WifiWakeupHelperTest { when(mWifiInfo.getSSID()).thenReturn(SSID); when(mWifiManager.isWifiEnabled()).thenReturn(true, false); - mContext.sendBroadcast(new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION)); - mContext.sendBroadcast(new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION)); + mContext.sendBroadcast(new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION)); + mContext.sendBroadcast(new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION)); + verify(mNotificationManager, times(1)) + .notify(anyString(), anyInt(), any(Notification.class)); verify(mNotificationManager).cancel(anyString(), anyInt()); } @@ -129,11 +133,11 @@ public class WifiWakeupHelperTest { when(mWifiInfo.getSSID()).thenReturn(SSID, "blah"); when(mWifiManager.isWifiEnabled()).thenReturn(true); - mContext.sendBroadcast(new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION)); + mContext.sendBroadcast(new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION)); verify(mNotificationManager, never()).cancel(anyString(), anyInt()); - mContext.sendBroadcast(new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION)); + mContext.sendBroadcast(new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION)); verify(mNotificationManager).cancel(anyString(), anyInt()); } @@ -149,11 +153,34 @@ public class WifiWakeupHelperTest { when(mWifiManager.isWifiEnabled()).thenReturn(true); mWifiWakeupHelper.startWifiSession(mWifiConfiguration); - mContext.sendBroadcast(new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION)); - mContext.sendBroadcast(new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION)); + mContext.sendBroadcast(new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION)); + mContext.sendBroadcast(new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION)); verify(mNotificationManager, never()) .notify(anyString(), anyInt(), any(Notification.class)); verify(mNotificationManager, never()).cancel(anyString(), anyInt()); } + + @Test + public void tappingOnSettingsFromNotificationOpensSettingsActivity() { + mWifiWakeupHelper.startWifiSession(mWifiConfiguration); + + mContext.sendBroadcast(new Intent(WifiWakeupHelper.ACTION_WIFI_SETTINGS)); + + Intent intent = Shadows.shadowOf(RuntimeEnvironment.application).getNextStartedActivity(); + + assertThat(intent.getAction()).isEqualTo("android.settings.CONFIGURE_WIFI_SETTINGS"); + } + + @Test + public void dismissingNotificationCancelsNotification() { + mWifiWakeupHelper.startWifiSession(mWifiConfiguration); + + mContext.sendBroadcast( + new Intent(WifiWakeupHelper.ACTION_DISMISS_WIFI_ENABLED_NOTIFICATION)); + + verify(mNotificationManager, times(1)) + .notify(anyString(), anyInt(), any(Notification.class)); + verify(mNotificationManager).cancel(anyString(), anyInt()); + } } diff --git a/src/com/android/networkrecommendation/config/Flag.java b/src/com/android/networkrecommendation/config/Flag.java index 4170cb1..953fb1e 100644 --- a/src/com/android/networkrecommendation/config/Flag.java +++ b/src/com/android/networkrecommendation/config/Flag.java @@ -21,17 +21,23 @@ package com.android.networkrecommendation.config; */ public class Flag<T> { - private T mValue; + private final T mDefaultValue; + private T mOverride; - public Flag(T value) { - mValue = value; + public Flag(T defaultValue) { + mDefaultValue = defaultValue; + mOverride = null; } /** Get the currently set flag value. */ public T get() { - return mValue; + return mOverride != null ? mOverride : mDefaultValue; } + /** Force a value for testing. */ + public void override(T value) { + mOverride = value; + } /** Ensure flag state is initialized for tests. */ public static void initForTest() { diff --git a/src/com/android/networkrecommendation/notify/WifiNotificationController.java b/src/com/android/networkrecommendation/notify/WifiNotificationController.java index 3a0e31a..aaba9c9 100644 --- a/src/com/android/networkrecommendation/notify/WifiNotificationController.java +++ b/src/com/android/networkrecommendation/notify/WifiNotificationController.java @@ -401,6 +401,7 @@ public class WifiNotificationController { mContext.startActivity( new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); + removeNotification(); } /** diff --git a/src/com/android/networkrecommendation/util/SsidUtil.java b/src/com/android/networkrecommendation/util/SsidUtil.java index e1f9503..5f7e5e8 100644 --- a/src/com/android/networkrecommendation/util/SsidUtil.java +++ b/src/com/android/networkrecommendation/util/SsidUtil.java @@ -17,6 +17,7 @@ package com.android.networkrecommendation.util; import static com.android.networkrecommendation.Constants.TAG; +import android.net.NetworkKey; import android.net.WifiKey; import android.support.annotation.Nullable; import com.android.networkrecommendation.config.G; @@ -76,6 +77,18 @@ public final class SsidUtil { } } + /** + * Returns a string version of the NetworkKey SSID/BSSID pair for logging which is typically + * redacted. + * + * <p>The IDs will only be returned verbatim if the enableSentitiveLogging flag is set. + */ + public static String getRedactedId(NetworkKey networkKey) { + return Blog.pii( + String.format("%s/%s", networkKey.wifiKey.ssid, networkKey.wifiKey.bssid), + G.Netrec.enableSensitiveLogging.get()); + } + // Can't instantiate. private SsidUtil() {} } diff --git a/src/com/android/networkrecommendation/wakeup/WifiWakeupHelper.java b/src/com/android/networkrecommendation/wakeup/WifiWakeupHelper.java index c62c24e..17b001e 100644 --- a/src/com/android/networkrecommendation/wakeup/WifiWakeupHelper.java +++ b/src/com/android/networkrecommendation/wakeup/WifiWakeupHelper.java @@ -50,16 +50,22 @@ public class WifiWakeupHelper { private static final int NOTIFICATION_ID = R.string.wifi_wakeup_enabled_notification_title; @VisibleForTesting static final String KEY_SHOWN_SSIDS = "key_shown_ssids"; - private static final String ACTION_WIFI_SETTINGS = + + @VisibleForTesting + static final String ACTION_WIFI_SETTINGS = "com.android.networkrecommendation.wakeup.ACTION_WIFI_SETTINGS"; - private static final String ACTION_DISMISS_WIFI_ENABLED_NOTIFICATION = + + @VisibleForTesting + static final String ACTION_DISMISS_WIFI_ENABLED_NOTIFICATION = "com.android.networkrecommendation.wakeup.ACTION_DISMISS_WIFI_ENABLED_NOTIFICATION"; - private static final IntentFilter INTENT_FILTER = new IntentFilter(); + private static final long NETWORK_CONNECTED_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(30); + private static final IntentFilter INTENT_FILTER = new IntentFilter(); static { INTENT_FILTER.addAction(ACTION_DISMISS_WIFI_ENABLED_NOTIFICATION); - INTENT_FILTER.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); + INTENT_FILTER.addAction(ACTION_WIFI_SETTINGS); + INTENT_FILTER.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); } private final Context mContext; @@ -83,13 +89,13 @@ public class WifiWakeupHelper { try { if (ACTION_WIFI_SETTINGS.equals(intent.getAction())) { // TODO(netrec): Change to @SystemApi Settings.CONFIGURE_WIFI_SETTINGS - context.startActivity( + mContext.startActivity( new Intent("android.settings.CONFIGURE_WIFI_SETTINGS") .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); } else if (ACTION_DISMISS_WIFI_ENABLED_NOTIFICATION.equals( intent.getAction())) { cancelNotificationIfNeeded(); - } else if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals( + } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals( intent.getAction())) { networkStateChanged(); } |