From 21669c13043b9b80f97f9b1536267a8da4436865 Mon Sep 17 00:00:00 2001 From: timvolodine Date: Wed, 8 Apr 2015 07:34:01 -0700 Subject: [PATCH] [Android] Fix BatteryStatusManagerTest instrumentation tests for devices running L. Currently some instrumentation tests relating to Battery Status API appear to fail on Android L devices. This patch contains the fix. BUG=474637 Review URL: https://codereview.chromium.org/1061303003 Cr-Commit-Position: refs/heads/master@{#324225} --- .../device/battery/BatteryStatusManager.java | 2 +- .../device/battery/BatteryStatusManagerTest.java | 40 ++++++++++------------ 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/device/battery/android/java/src/org/chromium/device/battery/BatteryStatusManager.java b/device/battery/android/java/src/org/chromium/device/battery/BatteryStatusManager.java index 8d62538b90ef..d833b2c93b95 100644 --- a/device/battery/android/java/src/org/chromium/device/battery/BatteryStatusManager.java +++ b/device/battery/android/java/src/org/chromium/device/battery/BatteryStatusManager.java @@ -142,7 +142,7 @@ class BatteryStatusManager { batteryStatus.dischargingTime = dischargingTimeSeconds; batteryStatus.level = level; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + if (mLollipopBatteryManager != null) { updateBatteryStatusForLollipop(batteryStatus); } diff --git a/device/battery/android/javatests/src/org/chromium/device/battery/BatteryStatusManagerTest.java b/device/battery/android/javatests/src/org/chromium/device/battery/BatteryStatusManagerTest.java index 536e31b207ad..2390d407c4f2 100644 --- a/device/battery/android/javatests/src/org/chromium/device/battery/BatteryStatusManagerTest.java +++ b/device/battery/android/javatests/src/org/chromium/device/battery/BatteryStatusManagerTest.java @@ -35,6 +35,9 @@ public class BatteryStatusManagerTest extends AndroidTestCase { private BatteryStatusManager mManager; + // Can be non-null only for versions L and higher. + private BatteryManagerForTesting mLollipopManager; + private void verifyValues( boolean charging, double chargingTime, double dischargingTime, double level) { assertEquals(charging, mCharging); @@ -85,6 +88,7 @@ public class BatteryStatusManagerTest extends AndroidTestCase { } public void initializeManager(BatteryManagerForTesting managerForTesting) { + mLollipopManager = managerForTesting; mManager = BatteryStatusManager.createBatteryStatusManagerForTesting( getContext(), mCallback, managerForTesting); } @@ -168,11 +172,9 @@ public class BatteryStatusManagerTest extends AndroidTestCase { @SmallTest public void testLollipopChargingTimeEstimate() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) - return; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return; - BatteryManagerForTesting testManager = new BatteryManagerForTesting(); - initializeManager(testManager); + initializeManager(new BatteryManagerForTesting()); Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); intent.putExtra(BatteryManager.EXTRA_PRESENT, true); @@ -180,9 +182,9 @@ public class BatteryStatusManagerTest extends AndroidTestCase { intent.putExtra(BatteryManager.EXTRA_LEVEL, 50); intent.putExtra(BatteryManager.EXTRA_SCALE, 100); - testManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER, 1000); - testManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY, 50); - testManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE, 100); + mLollipopManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER, 1000); + mLollipopManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY, 50); + mLollipopManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE, 100); mManager.onReceive(intent); verifyValues(true, 0.5 * 10 * 3600, Double.POSITIVE_INFINITY, 0.5); @@ -190,11 +192,9 @@ public class BatteryStatusManagerTest extends AndroidTestCase { @SmallTest public void testLollipopDischargingTimeEstimate() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) - return; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return; - BatteryManagerForTesting testManager = new BatteryManagerForTesting(); - initializeManager(testManager); + initializeManager(new BatteryManagerForTesting()); Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); intent.putExtra(BatteryManager.EXTRA_PRESENT, true); @@ -203,9 +203,9 @@ public class BatteryStatusManagerTest extends AndroidTestCase { intent.putExtra(BatteryManager.EXTRA_SCALE, 100); intent.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_NOT_CHARGING); - testManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER, 1000); - testManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY, 60); - testManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE, -100); + mLollipopManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER, 1000); + mLollipopManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY, 60); + mLollipopManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE, -100); mManager.onReceive(intent); verifyValues(false, Double.POSITIVE_INFINITY, 0.6 * 10 * 3600, 0.6); @@ -213,11 +213,9 @@ public class BatteryStatusManagerTest extends AndroidTestCase { @SmallTest public void testLollipopDischargingTimeEstimateRounding() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) - return; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return; - BatteryManagerForTesting testManager = new BatteryManagerForTesting(); - initializeManager(testManager); + initializeManager(new BatteryManagerForTesting()); Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); intent.putExtra(BatteryManager.EXTRA_PRESENT, true); @@ -226,9 +224,9 @@ public class BatteryStatusManagerTest extends AndroidTestCase { intent.putExtra(BatteryManager.EXTRA_SCALE, 100); intent.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_NOT_CHARGING); - testManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER, 1999); - testManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY, 90); - testManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE, -1000); + mLollipopManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER, 1999); + mLollipopManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY, 90); + mLollipopManager.setIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE, -1000); mManager.onReceive(intent); verifyValues(false, Double.POSITIVE_INFINITY, Math.floor(0.9 * 1.999 * 3600), 0.9); -- 2.11.4.GIT