Adopt robolectric 3.6.1

Bug: 71596155
Test: make RunSettingsRoboTests
Change-Id: I46362bae1e3ddd3ce19ade1c93250d272f3366e6
diff --git a/src/com/android/settings/fuelgauge/BatteryInfoLoader.java b/src/com/android/settings/fuelgauge/BatteryInfoLoader.java
index 227f4a5..c60f423 100644
--- a/src/com/android/settings/fuelgauge/BatteryInfoLoader.java
+++ b/src/com/android/settings/fuelgauge/BatteryInfoLoader.java
@@ -20,6 +20,8 @@
 import com.android.internal.os.BatteryStatsHelper;
 import com.android.settingslib.utils.AsyncLoader;
 
+import com.android.internal.annotations.VisibleForTesting;
+
 /**
  * Loader that can be used by classes to load BatteryInfo in a background thread. This loader will
  * automatically grab enhanced battery estimates if available or fall back to the system estimate
@@ -30,9 +32,13 @@
     BatteryStatsHelper mStatsHelper;
     private static final String LOG_TAG = "BatteryInfoLoader";
 
+    @VisibleForTesting
+    BatteryUtils batteryUtils;
+
     public BatteryInfoLoader(Context context, BatteryStatsHelper batteryStatsHelper) {
         super(context);
         mStatsHelper = batteryStatsHelper;
+        batteryUtils = BatteryUtils.getInstance(context);
     }
 
     @Override
@@ -42,7 +48,6 @@
 
     @Override
     public BatteryInfo loadInBackground() {
-        final BatteryUtils batteryUtils = BatteryUtils.getInstance(getContext());
         return batteryUtils.getBatteryInfo(mStatsHelper, LOG_TAG);
     }
 }
diff --git a/src/com/android/settings/slices/SlicesDatabaseHelper.java b/src/com/android/settings/slices/SlicesDatabaseHelper.java
index 448d8f1..627c62e 100644
--- a/src/com/android/settings/slices/SlicesDatabaseHelper.java
+++ b/src/com/android/settings/slices/SlicesDatabaseHelper.java
@@ -136,7 +136,7 @@
         mContext.getSharedPreferences(SHARED_PREFS_TAG, Context.MODE_PRIVATE)
                 .edit()
                 .clear()
-                .commit();
+                .apply();
         dropTables(db);
         createDatabases(db);
     }
diff --git a/tests/robotests/Android.mk b/tests/robotests/Android.mk
index 7271884..e73c323 100644
--- a/tests/robotests/Android.mk
+++ b/tests/robotests/Android.mk
@@ -14,7 +14,7 @@
 
 LOCAL_JAVA_LIBRARIES := \
     junit \
-    platform-robolectric-3.5.1-prebuilt \
+    platform-robolectric-3.6.1-prebuilt \
     telephony-common
 
 LOCAL_INSTRUMENTATION_FOR := Settings
@@ -42,4 +42,4 @@
 
 LOCAL_ROBOTEST_TIMEOUT := 36000
 
-include prebuilts/misc/common/robolectric/3.5.1/run_robotests.mk
+include prebuilts/misc/common/robolectric/3.6.1/run_robotests.mk
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingPreferenceControllerTest.java
index 4459f61..1deba78 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingPreferenceControllerTest.java
@@ -90,7 +90,7 @@
         Preference pref = mController.createBluetoothPairingPreference(ORDER);
 
         assertThat(pref.getKey()).isEqualTo(BluetoothPairingPreferenceController.KEY_PAIRING);
-        assertThat(pref.getIcon()).isEqualTo(mContext.getDrawable(R.drawable.ic_add));
+        assertThat(pref.getIcon()).isEqualTo(mContext.getDrawable(R.drawable.ic_menu_add));
         assertThat(pref.getOrder()).isEqualTo(ORDER);
         assertThat(pref.getTitle()).isEqualTo(
                 mContext.getString(R.string.bluetooth_pairing_pref_title));
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryInfoLoaderTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BatteryInfoLoaderTest.java
index 305bd58..4e1b26c 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/BatteryInfoLoaderTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryInfoLoaderTest.java
@@ -76,6 +76,7 @@
     @Test
     public void test_loadInBackground_dischargingOldEstimate_dischargingLabelNotNull() {
         BatteryInfoLoader loader = new BatteryInfoLoader(mContext, mHelper);
+        loader.batteryUtils = new BatteryUtils(mContext);
 
         BatteryInfo info = loader.loadInBackground();
 
diff --git a/tests/robotests/src/com/android/settings/search/XmlParserUtilTest.java b/tests/robotests/src/com/android/settings/search/XmlParserUtilTest.java
index 2bec503..af7a462 100644
--- a/tests/robotests/src/com/android/settings/search/XmlParserUtilTest.java
+++ b/tests/robotests/src/com/android/settings/search/XmlParserUtilTest.java
@@ -190,8 +190,7 @@
             while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
                     && type != XmlPullParser.START_TAG) {
             }
-            while (parser.getName() != xmlType) {
-                parser.next();
+            while (parser.getName() != xmlType && parser.next() != XmlPullParser.END_DOCUMENT) {
             }
         } catch (Exception e) {
 
diff --git a/tests/robotests/src/com/android/settings/slices/SlicesIndexerTest.java b/tests/robotests/src/com/android/settings/slices/SlicesIndexerTest.java
index 68c9555..ed4f3ff 100644
--- a/tests/robotests/src/com/android/settings/slices/SlicesIndexerTest.java
+++ b/tests/robotests/src/com/android/settings/slices/SlicesIndexerTest.java
@@ -79,7 +79,6 @@
         String newKey = "newKey";
         String newTitle = "newTitle";
         SlicesDatabaseHelper.getInstance(mContext).setIndexedState();
-        Locale.setDefault(new Locale("ca"));
         insertSpecialCase(newKey, newTitle);
 
         // Attempt indexing - should not do anything.
diff --git a/tests/robotests/src/com/android/settings/testutils/SettingsRobolectricTestRunner.java b/tests/robotests/src/com/android/settings/testutils/SettingsRobolectricTestRunner.java
index 7c374e9..f071f17 100644
--- a/tests/robotests/src/com/android/settings/testutils/SettingsRobolectricTestRunner.java
+++ b/tests/robotests/src/com/android/settings/testutils/SettingsRobolectricTestRunner.java
@@ -15,25 +15,17 @@
  */
 package com.android.settings.testutils;
 
-import android.app.Fragment;
-import android.content.Intent;
-
+import java.net.MalformedURLException;
+import java.net.URL;
 import org.junit.runners.model.InitializationError;
 import org.robolectric.RobolectricTestRunner;
-import org.robolectric.android.controller.ActivityController;
 import org.robolectric.annotation.Config;
 import org.robolectric.manifest.AndroidManifest;
 import org.robolectric.res.Fs;
 import org.robolectric.res.ResourcePath;
-import org.robolectric.util.ReflectionHelpers;
 
 import java.util.List;
 
-import static com.android.settings.SettingsActivity.EXTRA_SHOW_FRAGMENT;
-import static org.robolectric.Robolectric.getShadowsAdapter;
-
-import com.android.settings.SettingsActivity;
-
 /**
  * Custom test runner for the testing of BluetoothPairingDialogs. This is needed because the
  * default behavior for robolectric is just to grab the resource directory in the target package.
@@ -54,68 +46,49 @@
      */
     @Override
     protected AndroidManifest getAppManifest(Config config) {
-        // Using the manifest file's relative path, we can figure out the application directory.
-        final String appRoot = "packages/apps/Settings";
-        final String manifestPath = appRoot + "/AndroidManifest.xml";
-        final String resDir = appRoot + "/tests/robotests/res";
-        final String assetsDir = appRoot + config.assetDir();
+        try {
+            // Using the manifest file's relative path, we can figure out the application directory.
+            final URL appRoot = new URL("file:packages/apps/Settings/");
+            final URL manifestPath = new URL(appRoot, "AndroidManifest.xml");
+            final URL resDir = new URL(appRoot, "tests/robotests/res");
+            final URL assetsDir = new URL(appRoot, "tests/robotests/assets");
 
-        // By adding any resources from libraries we need the AndroidManifest, we can access
-        // them from within the parallel universe's resource loader.
-        return new AndroidManifest(Fs.fileFromPath(manifestPath), Fs.fileFromPath(resDir),
-            Fs.fileFromPath(assetsDir), "com.android.settings") {
-            @Override
-            public List<ResourcePath> getIncludedResourcePaths() {
-                List<ResourcePath> paths = super.getIncludedResourcePaths();
-                SettingsRobolectricTestRunner.getIncludedResourcePaths(getPackageName(), paths);
-                return paths;
-            }
-        };
+            // By adding any resources from libraries we need the AndroidManifest, we can access
+            // them from within the parallel universe's resource loader.
+            return new AndroidManifest(Fs.fromURL(manifestPath), Fs.fromURL(resDir),
+                Fs.fromURL(assetsDir), "com.android.settings") {
+                @Override
+                public List<ResourcePath> getIncludedResourcePaths() {
+                    final List<ResourcePath> paths = super.getIncludedResourcePaths();
+                    addIncludedResourcePaths(paths);
+                    return paths;
+                }
+            };
+        } catch (MalformedURLException e) {
+            throw new RuntimeException("SettingsRobolectricTestRunner failure", e);
+        }
     }
 
-    public static void getIncludedResourcePaths(String packageName, List<ResourcePath> paths) {
-        paths.add(new ResourcePath(
-                null,
-                Fs.fileFromPath("./packages/apps/Settings/res"),
-                null));
-        paths.add(new ResourcePath(
-                null,
-                Fs.fileFromPath("./frameworks/base/packages/SettingsLib/res"),
-                null));
-        paths.add(new ResourcePath(
-                null,
-                Fs.fileFromPath("./frameworks/base/core/res/res"),
-                null));
-        paths.add(new ResourcePath(
-                null,
-                Fs.fileFromPath("./frameworks/opt/setupwizard/library/main/res"),
-                null));
-        paths.add(new ResourcePath(
-                null,
-                Fs.fileFromPath("./frameworks/opt/setupwizard/library/gingerbread/res"),
-                null));
-        paths.add(new ResourcePath(
-                null,
-                Fs.fileFromPath("./frameworks/opt/setupwizard/library/recyclerview/res"),
-                null));
-        paths.add(new ResourcePath(
-                null,
-                Fs.fileFromPath("./frameworks/support/v7/appcompat/res"),
-                null));
-        paths.add(new ResourcePath(
-                null,
-                Fs.fileFromPath("./frameworks/support/v7/cardview/res"),
-                null));
-    }
-
-    // A simple utility class to start a Settings fragment with an intent. The code here is almost
-    // the same as FragmentTestUtil.startFragment except that it starts an activity with an intent.
-    public static void startSettingsFragment(
-            Fragment fragment, Class<? extends SettingsActivity> activityClass) {
-        Intent intent = new Intent().putExtra(EXTRA_SHOW_FRAGMENT, fragment.getClass().getName());
-        SettingsActivity activity = ActivityController.of(
-                getShadowsAdapter(), ReflectionHelpers.callConstructor(activityClass), intent)
-                .setup().get();
-        activity.getFragmentManager().beginTransaction().add(fragment, null).commit();
+    public static void addIncludedResourcePaths(List<ResourcePath> paths) {
+        try {
+            paths.add(new ResourcePath(null,
+                Fs.fromURL(new URL("file:packages/apps/Settings/res")), null));
+            paths.add(new ResourcePath(null,
+                Fs.fromURL(new URL("file:frameworks/base/packages/SettingsLib/res")), null));
+            paths.add(new ResourcePath(null,
+                Fs.fromURL(new URL("file:frameworks/base/core/res/res")), null));
+            paths.add(new ResourcePath(null,
+                Fs.fromURL(new URL("file:frameworks/opt/setupwizard/library/main/res")), null));
+            paths.add(new ResourcePath(null,
+                Fs.fromURL(new URL("file:frameworks/opt/setupwizard/library/gingerbread/res")), null));
+            paths.add(new ResourcePath(null,
+                Fs.fromURL(new URL("file:frameworks/opt/setupwizard/library/recyclerview/res")), null));
+            paths.add(new ResourcePath(null,
+                Fs.fromURL(new URL("file:frameworks/support/v7/appcompat/res")), null));
+            paths.add(new ResourcePath(null,
+                Fs.fromURL(new URL("file:frameworks/support/v7/cardview/res")), null));
+        } catch (MalformedURLException e) {
+            throw new RuntimeException("SettingsRobolectricTestRunner failure", e);
+        }
     }
 }