diff --git a/src/com/android/settings/fuelgauge/BatteryInfoLoader.java b/src/com/android/settings/fuelgauge/BatteryInfoLoader.java index 227f4a5555d..c60f42314f7 100644 --- a/src/com/android/settings/fuelgauge/BatteryInfoLoader.java +++ b/src/com/android/settings/fuelgauge/BatteryInfoLoader.java @@ -20,6 +20,8 @@ import android.content.Context; 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 @@ public class BatteryInfoLoader extends AsyncLoader{ 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 @@ public class BatteryInfoLoader extends AsyncLoader{ @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 448d8f17ba5..627c62e55f9 100644 --- a/src/com/android/settings/slices/SlicesDatabaseHelper.java +++ b/src/com/android/settings/slices/SlicesDatabaseHelper.java @@ -136,7 +136,7 @@ public class SlicesDatabaseHelper extends SQLiteOpenHelper { 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 727188488a1..e73c3231afb 100644 --- a/tests/robotests/Android.mk +++ b/tests/robotests/Android.mk @@ -14,7 +14,7 @@ LOCAL_STATIC_JAVA_LIBRARIES := \ 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_INSTRUMENT_SOURCE_DIRS := $(dir $(LOCAL_PATH))../src 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 4459f6197e4..1deba785299 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 @@ public class BluetoothPairingPreferenceControllerTest { 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 305bd581fd9..4e1b26ca624 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 @@ public class BatteryInfoLoaderTest { @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 2bec503afe5..af7a4628886 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 @@ public class XmlParserUtilTest { 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 68c95558d99..ed4f3ff363a 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 @@ public class SlicesIndexerTest { 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 7c374e9e92d..f071f173633 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 @@ public class SettingsRobolectricTestRunner extends RobolectricTestRunner { */ @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 getIncludedResourcePaths() { - List 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 getIncludedResourcePaths() { + final List paths = super.getIncludedResourcePaths(); + addIncludedResourcePaths(paths); + return paths; + } + }; + } catch (MalformedURLException e) { + throw new RuntimeException("SettingsRobolectricTestRunner failure", e); + } } - public static void getIncludedResourcePaths(String packageName, List 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 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 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); + } } }