Skip Doze Mode-related tests when device does not support it.

Fixes: 29072117
Change-Id: I7ca37eae58258c021ed6297a9f1ee3b2749da7d7
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractAppIdleTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractAppIdleTestCase.java
index 13ce6ce..ba56665 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractAppIdleTestCase.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractAppIdleTestCase.java
@@ -16,6 +16,8 @@
 
 package com.android.cts.net.hostside;
 
+import android.util.Log;
+
 /**
  * Base class for metered and non-metered tests on idle apps.
  */
@@ -25,6 +27,8 @@
     protected final void setUp() throws Exception {
         super.setUp();
 
+        if (!isSupported()) return;
+
         // Set initial state.
         setUpMeteredNetwork();
         removePowerSaveModeWhitelist(TEST_APP2_PKG);
@@ -38,6 +42,8 @@
     protected final void tearDown() throws Exception {
         super.tearDown();
 
+        if (!isSupported()) return;
+
         try {
             tearDownMeteredNetwork();
         } finally {
@@ -46,6 +52,16 @@
         }
     }
 
+    @Override
+    protected boolean isSupported() throws Exception {
+        boolean supported = isDozeModeEnabled();
+        if (!supported) {
+            Log.i(TAG, "Skipping " + getClass() + "." + getName()
+                    + "() because device does not support Doze Mode");
+        }
+        return supported;
+    }
+
     /**
      * Sets the initial (non) metered network state.
      *
@@ -63,6 +79,8 @@
     }
 
     public void testBackgroundNetworkAccess_enabled() throws Exception {
+        if (!isSupported()) return;
+
         setAppIdle(true);
         assertBackgroundNetworkAccess(false);
 
@@ -92,6 +110,8 @@
     }
 
     public void testBackgroundNetworkAccess_whitelisted() throws Exception {
+        if (!isSupported()) return;
+
         setAppIdle(true);
         assertBackgroundNetworkAccess(false);
 
@@ -111,6 +131,8 @@
     }
 
     public void testBackgroundNetworkAccess_disabled() throws Exception {
+        if (!isSupported()) return;
+
         assertBackgroundNetworkAccess(true);
 
         assertsForegroundAlwaysHasNetworkAccess();
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractBatterySaverModeTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractBatterySaverModeTestCase.java
index 2acc670..c1c91da 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractBatterySaverModeTestCase.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractBatterySaverModeTestCase.java
@@ -16,6 +16,8 @@
 
 package com.android.cts.net.hostside;
 
+import android.util.Log;
+
 /**
  * Base class for metered and non-metered Battery Saver Mode tests.
  */
@@ -25,6 +27,8 @@
     protected final void setUp() throws Exception {
         super.setUp();
 
+        if (!isSupported()) return;
+
         // Set initial state.
         setUpMeteredNetwork();
         removePowerSaveModeWhitelist(TEST_APP2_PKG);
@@ -37,6 +41,8 @@
     protected final void tearDown() throws Exception {
         super.tearDown();
 
+        if (!isSupported()) return;
+
         try {
             tearDownMeteredNetwork();
         } finally {
@@ -44,6 +50,16 @@
         }
     }
 
+    @Override
+    protected boolean isSupported() throws Exception {
+        boolean supported = isDozeModeEnabled();
+        if (!supported) {
+            Log.i(TAG, "Skipping " + getClass() + "." + getName()
+                    + "() because device does not support Doze Mode");
+        }
+        return supported;
+    }
+
     /**
      * Sets the initial (non) metered network state.
      *
@@ -61,6 +77,8 @@
     }
 
     public void testBackgroundNetworkAccess_enabled() throws Exception {
+        if (!isSupported()) return;
+
         setBatterySaverMode(true);
         assertBackgroundNetworkAccess(false);
 
@@ -87,6 +105,8 @@
     }
 
     public void testBackgroundNetworkAccess_whitelisted() throws Exception {
+        if (!isSupported()) return;
+
         setBatterySaverMode(true);
         assertBackgroundNetworkAccess(false);
 
@@ -101,6 +121,8 @@
     }
 
     public void testBackgroundNetworkAccess_disabled() throws Exception {
+        if (!isSupported()) return;
+
         assertBackgroundNetworkAccess(true);
 
         assertsForegroundAlwaysHasNetworkAccess();
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractDozeModeTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractDozeModeTestCase.java
index e0ba76b..b89cf93 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractDozeModeTestCase.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractDozeModeTestCase.java
@@ -17,6 +17,7 @@
 package com.android.cts.net.hostside;
 
 import android.os.SystemClock;
+import android.util.Log;
 
 /**
  * Base class for metered and non-metered Doze Mode tests.
@@ -27,6 +28,8 @@
     protected final void setUp() throws Exception {
         super.setUp();
 
+        if (!isSupported()) return;
+
         // Set initial state.
         setUpMeteredNetwork();
         removePowerSaveModeWhitelist(TEST_APP2_PKG);
@@ -39,6 +42,8 @@
     protected final void tearDown() throws Exception {
         super.tearDown();
 
+        if (!isSupported()) return;
+
         try {
             tearDownMeteredNetwork();
         } finally {
@@ -46,6 +51,16 @@
         }
     }
 
+    @Override
+    protected boolean isSupported() throws Exception {
+        boolean supported = isDozeModeEnabled();
+        if (!supported) {
+            Log.i(TAG, "Skipping " + getClass() + "." + getName()
+                    + "() because device does not support Doze Mode");
+        }
+        return supported;
+    }
+
     /**
      * Sets the initial (non) metered network state.
      *
@@ -63,6 +78,8 @@
     }
 
     public void testBackgroundNetworkAccess_enabled() throws Exception {
+        if (!isSupported()) return;
+
         setDozeMode(true);
         assertBackgroundNetworkAccess(false);
 
@@ -81,6 +98,8 @@
     }
 
     public void testBackgroundNetworkAccess_whitelisted() throws Exception {
+        if (!isSupported()) return;
+
         setDozeMode(true);
         assertBackgroundNetworkAccess(false);
 
@@ -95,6 +114,8 @@
     }
 
     public void testBackgroundNetworkAccess_disabled() throws Exception {
+        if (!isSupported()) return;
+
         assertBackgroundNetworkAccess(true);
 
         assertsForegroundAlwaysHasNetworkAccess();
@@ -103,6 +124,8 @@
 
     public void testBackgroundNetworkAccess_enabledButWhitelistedOnNotificationAction()
             throws Exception {
+        if (!isSupported()) return;
+
         setPendingIntentWhitelistDuration(NETWORK_TIMEOUT_MS);
         try {
             registerNotificationListenerService();
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java
index 3125dfa..439fbbe 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java
@@ -188,6 +188,22 @@
     }
 
     /**
+     * Whether this device suport this type of test.
+     *
+     * <p>Should be overridden when necessary, and explicitly used before each test. Example:
+     *
+     * <pre><code>
+     * public void testSomething() {
+     *    if (!isSupported()) return;
+     * </code></pre>
+     *
+     * @return {@code true} by default.
+     */
+    protected boolean isSupported() throws Exception {
+        return true;
+    }
+
+    /**
      * Asserts that an app always have access while on foreground or running a foreground service.
      *
      * <p>This method will launch an activity and a foreground service to make the assertion, but
@@ -598,6 +614,9 @@
     }
 
     protected void setDozeMode(boolean enabled) throws Exception {
+        // Sanity check, since tests should check beforehand....
+        assertTrue("Device does not support Doze Mode", isDozeModeEnabled());
+
         Log.i(TAG, "Setting Doze Mode to " + enabled);
         if (enabled) {
             turnBatteryOff();
@@ -616,6 +635,11 @@
         assertDelayedShellCommand("dumpsys deviceidle get deep", enabled ? "IDLE" : "ACTIVE");
     }
 
+    protected boolean isDozeModeEnabled() throws Exception {
+        final String result = executeShellCommand("cmd deviceidle enabled deep").trim();
+        return result.equals("1");
+    }
+
     protected void setAppIdle(boolean enabled) throws Exception {
         Log.i(TAG, "Setting app idle to " + enabled);
         executeSilentShellCommand("am set-inactive " + TEST_APP2_PKG + " " + enabled );
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java
index 1895156..3e6bd33 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java
@@ -30,6 +30,8 @@
     public void setUp() throws Exception {
         super.setUp();
 
+        if (!isSupported()) return;
+
         // Set initial state.
         setMeteredNetwork();
         setRestrictBackground(false);
@@ -44,6 +46,8 @@
     protected void tearDown() throws Exception {
         super.tearDown();
 
+        if (!isSupported()) return;
+
         try {
             resetMeteredNetwork();
         } finally {
@@ -52,6 +56,8 @@
     }
 
     public void testGetRestrictBackgroundStatus_disabled() throws Exception {
+        if (!isSupported()) return;
+
         assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_DISABLED);
 
         // Sanity check: make sure status is always disabled, never whitelisted
@@ -64,6 +70,8 @@
     }
 
     public void testGetRestrictBackgroundStatus_whitelisted() throws Exception {
+        if (!isSupported()) return;
+
         setRestrictBackground(true);
         assertRestrictBackgroundChangedReceived(1);
         assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_ENABLED);
@@ -81,6 +89,8 @@
     }
 
     public void testGetRestrictBackgroundStatus_enabled() throws Exception {
+        if (!isSupported()) return;
+
         setRestrictBackground(true);
         assertRestrictBackgroundChangedReceived(1);
         assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_ENABLED);
@@ -108,6 +118,8 @@
     }
 
     public void testGetRestrictBackgroundStatus_blacklisted() throws Exception {
+        if (!isSupported()) return;
+
         addRestrictBackgroundBlacklist(mUid);
         assertRestrictBackgroundChangedReceived(1);
         assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_ENABLED);
@@ -136,6 +148,8 @@
     }
 
     public void testGetRestrictBackgroundStatus_requiredWhitelistedPackages() throws Exception {
+        if (!isSupported()) return;
+
         final StringBuilder error = new StringBuilder();
         for (String packageName : REQUIRED_WHITELISTED_PACKAGES) {
             int uid = -1;
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/MixedModesTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/MixedModesTest.java
index c97a0f9..af52eee 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/MixedModesTest.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/MixedModesTest.java
@@ -32,6 +32,8 @@
     public void setUp() throws Exception {
         super.setUp();
 
+        if (!isSupported()) return;
+
         // Set initial state.
         removeRestrictBackgroundWhitelist(mUid);
         removeRestrictBackgroundBlacklist(mUid);
@@ -44,6 +46,8 @@
     protected void tearDown() throws Exception {
         super.tearDown();
 
+        if (!isSupported()) return;
+
         try {
             setRestrictBackground(false);
         } finally {
@@ -55,6 +59,14 @@
      * Tests all DS ON and BS ON scenarios from network-policy-restrictions.md on metered networks.
      */
     public void testDataAndBatterySaverModes_meteredNetwork() throws Exception {
+        if (!isSupported()) return;
+
+        if (!isDozeModeEnabled()) {
+            Log.w(TAG, "testDataAndBatterySaverModes_meteredNetwork() skipped because "
+                    + "device does not support Doze Mode");
+            return;
+        }
+
         Log.i(TAG, "testDataAndBatterySaverModes_meteredNetwork() tests");
         setMeteredNetwork();
 
@@ -119,6 +131,14 @@
      * networks.
      */
     public void testDataAndBatterySaverModes_nonMeteredNetwork() throws Exception {
+        if (!isSupported()) return;
+
+        if (!isDozeModeEnabled()) {
+            Log.w(TAG, "testDataAndBatterySaverModes_nonMeteredNetwork() skipped because "
+                    + "device does not support Doze Mode");
+            return;
+        }
+
         if (mCm.isActiveNetworkMetered()) {
             Log.w(TAG, "testDataAndBatterySaverModes_nonMeteredNetwork() skipped because network"
                     + " is metered");
diff --git a/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java b/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java
index c741b12..7d5f817 100644
--- a/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java
+++ b/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java
@@ -101,6 +101,12 @@
     }
 
     public void testBatterySaverMode_reinstall() throws Exception {
+        if (!isDozeModeEnabled()) {
+            Log.w(TAG, "testBatterySaverMode_reinstall() skipped because device does not support "
+                    + "Doze Mode");
+            return;
+        }
+
         addPowerSaveModeWhitelist(TEST_APP2_PKG);
 
         uninstallPackage(TEST_APP2_PKG, true);
@@ -287,4 +293,9 @@
         runCommand("dumpsys deviceidle whitelist +" + packageName);
         assertPowerSaveModeWhitelist(packageName, true); // Sanity check
     }
+
+    protected boolean isDozeModeEnabled() throws Exception {
+        final String result = runCommand("cmd deviceidle enabled deep").trim();
+        return result.equals("1");
+    }
 }