Merge "Snap for 4794321 from 6dc384b585f4867d4f5c7a3c16a009aada48dd8c to nougat-cts-release" into nougat-cts-release
diff --git a/apps/CtsVerifier/AndroidManifest.xml b/apps/CtsVerifier/AndroidManifest.xml
index 0c2ed50..e2a121b 100644
--- a/apps/CtsVerifier/AndroidManifest.xml
+++ b/apps/CtsVerifier/AndroidManifest.xml
@@ -503,8 +503,8 @@
                 </intent-filter>
         </activity>
 
-        <activity android:name=".net.ConnectivityScreenOffTestActivity"
-                android:label="@string/network_screen_off_test">
+        <activity android:name=".net.ConnectivityBackgroundTestActivity"
+                android:label="@string/network_background_test">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.cts.intent.category.MANUAL_TEST" />
diff --git a/apps/CtsVerifier/res/layout/network_screen_off.xml b/apps/CtsVerifier/res/layout/network_background.xml
similarity index 96%
rename from apps/CtsVerifier/res/layout/network_screen_off.xml
rename to apps/CtsVerifier/res/layout/network_background.xml
index 5a2446d..50b64e3 100644
--- a/apps/CtsVerifier/res/layout/network_screen_off.xml
+++ b/apps/CtsVerifier/res/layout/network_background.xml
@@ -18,7 +18,7 @@
         android:layout_height="match_parent">
 
     <Button android:id="@+id/start_btn"
-            android:text="@string/network_screen_off_test_start"
+            android:text="@string/network_background_test_start"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerHorizontal="true"
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index 0497639..0a882f1 100755
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -388,20 +388,20 @@
         try again, outside, also with the device stationary, with as much view of the sky as
         possible.) </string>
 
-    <!-- Strings for net.ConnectivityScreenOffTestActivity -->
-    <string name="network_screen_off_test">Network Connectivity Screen Off Test</string>
-    <string name="network_screen_off_test_instructions">
+    <!-- Strings for ConnectivityBackgroundTestActivity -->
+    <string name="network_background_test">Network Background Connectivity Test</string>
+    <string name="network_background_test_instructions">
         This test verifies that IPv6 network connectivity continues to work
-        when the screen is off.\n\n
+        when the screen is off or is idle for some time.\n\n
 
         1. Join a Wi-Fi network with IPv6 Internet access.\n
         2. If the device has battery power, disconnect all power connectors.\n
-        3. Turn the screen off.\n
-        4. Wait until the screen turns on (it will take at least two minutes).\n
+        3. Turn the screen off if possible.\n
+        4. Wait until the screen turns on or the test result is displayed (it will take at least two minutes).\n
         5. If necessary, unlock the device.\n
         6. Please mark the test according to the result status indicated.\n
     </string>
-    <string name="network_screen_off_test_start">Start</string>
+    <string name="network_background_test_start">Start</string>
 
     <!-- Strings for NfcTestActivity -->
     <string name="nfc_test">NFC Test</string>
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/net/ConnectivityScreenOffTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/net/ConnectivityBackgroundTestActivity.java
similarity index 92%
rename from apps/CtsVerifier/src/com/android/cts/verifier/net/ConnectivityScreenOffTestActivity.java
rename to apps/CtsVerifier/src/com/android/cts/verifier/net/ConnectivityBackgroundTestActivity.java
index 6109893..496bbe4 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/net/ConnectivityScreenOffTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/net/ConnectivityBackgroundTestActivity.java
@@ -30,7 +30,6 @@
 import android.net.LinkAddress;
 import android.net.LinkProperties;
 import android.net.Network;
-import android.net.NetworkCapabilities;
 import android.net.NetworkRequest;
 import android.os.BatteryManager;
 import android.os.Bundle;
@@ -59,7 +58,7 @@
 import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
 
 /**
- * A CTS Verifier test case for testing IPv6 network connectivity while the screen is off.
+ * A CTS Verifier test case for testing IPv6 network background connectivity.
  *
  * This tests that Wi-Fi implementations are compliant with section 7.4.5
  * ("Minimum Network Capability") of the CDD. Specifically, it requires that: "unicast IPv6
@@ -72,12 +71,12 @@
  *     [2] The device must join an IPv6-capable network (basic IPv6 connectivity to an
  *         Internet resource is tested).
  *     [3] If the device has a battery, the device must be disconnected from any power source.
- *     [4] The screen is put to sleep.
+ *     [4] The screen is put to sleep if this feature supported.
  *     [5] After two minutes, another IPv6 connectivity test is performed.
  */
-public class ConnectivityScreenOffTestActivity extends PassFailButtons.Activity {
+public class ConnectivityBackgroundTestActivity extends PassFailButtons.Activity {
 
-    private static final String TAG = ConnectivityScreenOffTestActivity.class.getSimpleName();
+    private static final String TAG = ConnectivityBackgroundTestActivity.class.getSimpleName();
     private static final String V6CONN_URL = "https://ipv6.google.com/generate_204";
     private static final String V6ADDR_URL = "https://google-ipv6test.appspot.com/ip.js?fmt=text";
 
@@ -102,7 +101,7 @@
     private long mUserActivityTimeout = -1;
 
 
-    public ConnectivityScreenOffTestActivity() {
+    public ConnectivityBackgroundTestActivity() {
         mLock = new Object();
         mState = new AppState();
 
@@ -132,12 +131,12 @@
     }
 
     private void setupUserInterface() {
-        setContentView(R.layout.network_screen_off);
+        setContentView(R.layout.network_background);
         setPassFailButtonClickListeners();
         getPassButton().setEnabled(false);
         setInfoResources(
-                R.string.network_screen_off_test,
-                R.string.network_screen_off_test_instructions,
+                R.string.network_background_test,
+                R.string.network_background_test_instructions,
                 -1);
 
         mScrollView = (ScrollView) findViewById(R.id.scroll);
@@ -161,7 +160,8 @@
 
         // Whether or not this device (currently) has a battery.
         mWaitForPowerDisconnected =
-                batteryInfo.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false) && !isLeanback();
+                batteryInfo.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false)
+                        && !hasPersistentPower();
 
         // Check if the device is already on battery power.
         if (mWaitForPowerDisconnected) {
@@ -310,7 +310,8 @@
 
         void setScreenOn() { mScreenOffTime = 0; }
         void setScreenOff() { mScreenOffTime = SystemClock.elapsedRealtime(); }
-        boolean validScreenStateForTesting() { return (mScreenOffTime > 0); }
+        boolean validScreenStateForTesting() {
+            return ((mScreenOffTime > 0) || !requiresScreenOffSupport()); }
 
         void setPowerConnected() { mPowerDisconnectTime = 0; }
         void setPowerDisconnected() { mPowerDisconnectTime = SystemClock.elapsedRealtime(); }
@@ -458,6 +459,13 @@
                     continue;
                 }
 
+                if ((localState.mScreenOffTime == 0) && !requiresScreenOffSupport()) {
+                    // mScreenOffTime may never be initialized on some devices
+                    // so do it now regardless of screen state to let the test start
+                    // on devices where screen-off function support is not required
+                    mState.setScreenOff();
+                }
+
                 if (mWaitForPowerDisconnected) {
                     final long delta = SystemClock.elapsedRealtime() - localState.mPowerDisconnectTime;
                     if (delta < MIN_POWER_DISCONNECT_MS) {
@@ -635,8 +643,19 @@
         return new HttpResult(rcode, msg);
     }
 
-    private boolean isLeanback() {
-        final PackageManager pm = this.getPackageManager();
-        return (pm != null && pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK));
+    private boolean hasPersistentPower() {
+        // Cars and TVsets are always connected to the persistent power source
+        final PackageManager pm = getPackageManager();
+        return (pm != null
+                && (pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK)
+                    || pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)));
     }
+
+    private boolean requiresScreenOffSupport() {
+        // Cars may not support screen-off function
+        final PackageManager pm = getPackageManager();
+        return (pm != null
+                && !pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE));
+    }
+
 }