Bug 705084 - Part A - Add kDefaultRemainingTime and add guards to make sure backends are correct. r=sicking

This commit is contained in:
Mounir Lamouri 2011-12-01 11:49:42 +01:00
parent e4ed980ff3
commit 9012bb7763
3 changed files with 11 additions and 2 deletions

View File

@ -87,7 +87,7 @@ NS_IMPL_RELEASE_INHERITED(BatteryManager, nsDOMEventTargetWrapperCache)
BatteryManager::BatteryManager()
: mLevel(kDefaultLevel)
, mCharging(kDefaultCharging)
, mRemainingTime(kUnknownRemainingTime)
, mRemainingTime(kDefaultRemainingTime)
{
}
@ -191,6 +191,14 @@ BatteryManager::UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInf
mLevel = aBatteryInfo.level();
mCharging = aBatteryInfo.charging();
mRemainingTime = aBatteryInfo.remainingTime();
// Add some guards to make sure the values are coherent.
if (mLevel == 1.0 && mCharging == true &&
mRemainingTime != kDefaultRemainingTime) {
mRemainingTime = kDefaultRemainingTime;
NS_ERROR("Battery API: When charging and level at 1.0, remaining time "
"should be 0. Please fix your backend!");
}
}
void

View File

@ -48,6 +48,7 @@ namespace battery {
static const double kDefaultLevel = 1.0;
static const bool kDefaultCharging = true;
static const double kDefaultRemainingTime = 0;
static const double kUnknownRemainingTime = -1;
} // namespace battery

View File

@ -26,7 +26,7 @@ var battery = navigator.mozBattery;
is(battery.level, 1.0, "Default battery level should be 1.0");
is(battery.charging, true, "Default charging value should be true");
is(battery.dischargingTime, Infinity, "Default dischargingTime should be Inifinity");
is(battery.chargingTime, Infinity, "Default chargingTime should be Inifinity");
is(battery.chargingTime, 0, "Default chargingTime should be 0");
</script>
</pre>