Refactored tests to use 'list wifi-networks'.

BUG: 27808364
Change-Id: Ife3f35e9c3c6a4285b671eeb7d7d0223be908a1b
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 41fd638..360257a 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
@@ -244,19 +244,36 @@
     /**
      * Asserts the result of a command, wait and re-running it a couple times if necessary.
      */
-    protected void assertDelayedShellCommand(String command, String expectedResult)
+    protected void assertDelayedShellCommand(String command, final String expectedResult)
+            throws Exception {
+        assertDelayedShellCommand(command, new ExpectResultChecker() {
+
+            @Override
+            public boolean isExpected(String result) {
+                return expectedResult.equals(result);
+            }
+
+            @Override
+            public String getExpected() {
+                return expectedResult;
+            }
+        });
+    }
+
+    protected void assertDelayedShellCommand(String command, ExpectResultChecker checker)
             throws Exception {
         final int maxTries = 5;
         String result = "";
         for (int i = 1; i <= maxTries; i++) {
             result = executeShellCommand(command).trim();
-            if (result.equals(expectedResult))
-                return;
+            if (checker.isExpected(result)) return;
             Log.v(TAG, "Command '" + command + "' returned '" + result + " instead of '"
-                    + expectedResult + "' on attempt #" + i + "; sleeping 1s before trying again");
+                    + checker.getExpected() + "' on attempt #" + i
+                    + "; sleeping 1s before trying again");
             Thread.sleep(SECOND_IN_MS);
         }
-        fail("Command '" + command + "' did not return '" + expectedResult + "' after " + maxTries
+        fail("Command '" + command + "' did not return '" + checker.getExpected() + "' after "
+                + maxTries
                 + " attempts. Last result: '" + result + "'");
     }
 
@@ -274,9 +291,11 @@
                 + mCm.getActiveNetworkInfo() +")", mCm.isActiveNetworkMetered());
         // Set flag so status is reverted on teardown.
         mResetMeteredWifi = true;
+        // Sanity check.
+        assertMeteredNetwork(netId, true);
     }
 
-    protected String setWifiMeteredStatus(boolean metered) throws Exception {
+    private String setWifiMeteredStatus(boolean metered) throws Exception {
         // We could call setWifiEnabled() here, but it might take sometime to be in a consistent
         // state (for example, if one of the saved network is not properly authenticated), so it's
         // better to let the hostside test take care of that.
@@ -292,13 +311,26 @@
         final String setCommand = "cmd netpolicy set metered-network " + netId + " " + metered;
         assertDelayedShellCommand(setCommand, "");
 
-        // Sanity check.
-        final  String getCommand = "cmd netpolicy get metered-network " + netId;
-        assertDelayedShellCommand(getCommand, Boolean.toString(metered));
-
         return netId;
     }
 
+    private void assertMeteredNetwork(String netId, boolean status) throws Exception {
+        final String command = "cmd netpolicy list wifi-networks";
+        final String expectedLine = netId + ";" + status;
+        assertDelayedShellCommand(command, new ExpectResultChecker() {
+
+            @Override
+            public boolean isExpected(String result) {
+                return result.contains(expectedLine);
+            }
+
+            @Override
+            public String getExpected() {
+                return "line containing " + expectedLine;
+            }
+        });
+    }
+
     protected void setRestrictBackground(boolean enabled) throws Exception {
         executeShellCommand("cmd netpolicy set restrict-background " + enabled);
         final String output = executeShellCommand("cmd netpolicy get restrict-background ");
@@ -451,4 +483,9 @@
             return fullState;
         }
     }
+
+    protected static interface ExpectResultChecker {
+        boolean isExpected(String result);
+        String getExpected();
+    }
 }