Merge "Record in progress count when parceling Timer" into nyc-dev

am: 752434a

* commit '752434a0ff54ac30b7701292b66361e64b2ae5a0':
  Record in progress count when parceling Timer

Change-Id: Ia54421b870e7b1519d607ac841fd1470964aab2a
This commit is contained in:
Adam Lesinski
2016-04-20 01:31:08 +00:00
committed by android-build-merger
6 changed files with 109 additions and 24 deletions

View File

@@ -60,6 +60,7 @@ import android.util.TimeUtils;
import android.util.Xml;
import android.view.Display;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.net.NetworkStatsFactory;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.FastPrintWriter;
@@ -1092,7 +1093,7 @@ public class BatteryStatsImpl extends BatteryStats {
public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
+ computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
out.writeInt(mCount);
out.writeInt(computeCurrentCountLocked());
out.writeInt(mLoadedCount);
out.writeInt(mUnpluggedCount);
out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
@@ -1109,7 +1110,7 @@ public class BatteryStatsImpl extends BatteryStats {
+ " old mUnpluggedCount=" + mUnpluggedCount);
}
mUnpluggedTime = computeRunTimeLocked(baseRealtime);
mUnpluggedCount = mCount;
mUnpluggedCount = computeCurrentCountLocked();
if (DEBUG && mType < 0) {
Log.v(TAG, "unplug #" + mType
+ ": new mUnpluggedTime=" + mUnpluggedTime
@@ -1192,7 +1193,7 @@ public class BatteryStatsImpl extends BatteryStats {
public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
out.writeLong(runTime);
out.writeInt(mCount);
out.writeInt(computeCurrentCountLocked());
}
public void readSummaryFromParcelLocked(Parcel in) {
@@ -1249,7 +1250,8 @@ public class BatteryStatsImpl extends BatteryStats {
*/
int mUpdateVersion;
SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
@VisibleForTesting
public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
super(clocks, 0, timeBase, in);
mCurrentReportedCount = in.readInt();
mUnpluggedReportedCount = in.readInt();
@@ -1259,7 +1261,8 @@ public class BatteryStatsImpl extends BatteryStats {
mTimeBaseRunning = timeBase.isRunning();
}
SamplingTimer(Clocks clocks, TimeBase timeBase, boolean trackReportedValues) {
@VisibleForTesting
public SamplingTimer(Clocks clocks, TimeBase timeBase, boolean trackReportedValues) {
super(clocks, 0, timeBase);
mTrackingReportedValues = trackReportedValues;
mTimeBaseRunning = timeBase.isRunning();

View File

@@ -0,0 +1,88 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.android.internal.os;
import android.os.BatteryStats;
import android.os.Parcel;
import android.support.test.filters.SmallTest;
import junit.framework.TestCase;
public class BatteryStatsSamplingTimerTest extends TestCase {
@SmallTest
public void testSampleTimerSummaryParceling() throws Exception {
final MockClocks clocks = new MockClocks();
clocks.realtime = 0;
clocks.uptime = 0;
final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
BatteryStatsImpl.SamplingTimer timer = new BatteryStatsImpl.SamplingTimer(clocks, timeBase,
true);
// Start running on battery.
timeBase.setRunning(true, clocks.uptimeMillis(), clocks.elapsedRealtime());
// The first update on battery consumes the values as a way of starting cleanly.
timer.addCurrentReportedTotalTime(10);
timer.addCurrentReportedCount(1);
timer.addCurrentReportedTotalTime(10);
timer.addCurrentReportedCount(1);
clocks.realtime = 20;
clocks.uptime = 20;
assertEquals(10, timer.getTotalTimeLocked(clocks.elapsedRealtime(),
BatteryStats.STATS_SINCE_CHARGED));
assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
// Grab a summary parcel while on battery.
final Parcel onBatterySummaryParcel = Parcel.obtain();
timer.writeSummaryFromParcelLocked(onBatterySummaryParcel, clocks.elapsedRealtime() * 1000);
onBatterySummaryParcel.setDataPosition(0);
// Stop running on battery.
timeBase.setRunning(false, clocks.uptimeMillis(), clocks.elapsedRealtime());
assertEquals(10, timer.getTotalTimeLocked(clocks.elapsedRealtime(),
BatteryStats.STATS_SINCE_CHARGED));
assertEquals(1, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
// Grab a summary parcel while not on battery.
final Parcel offBatterySummaryParcel = Parcel.obtain();
timer.writeSummaryFromParcelLocked(offBatterySummaryParcel,
clocks.elapsedRealtime() * 1000);
offBatterySummaryParcel.setDataPosition(0);
// Read the on battery summary from the parcel.
BatteryStatsImpl.SamplingTimer unparceledTimer = new BatteryStatsImpl.SamplingTimer(
clocks, timeBase, true);
unparceledTimer.readSummaryFromParcelLocked(onBatterySummaryParcel);
assertEquals(10, unparceledTimer.getTotalTimeLocked(0, BatteryStats.STATS_SINCE_CHARGED));
assertEquals(1, unparceledTimer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
// Read the off battery summary from the parcel.
unparceledTimer = new BatteryStatsImpl.SamplingTimer(clocks, timeBase, true);
unparceledTimer.readSummaryFromParcelLocked(offBatterySummaryParcel);
assertEquals(10, unparceledTimer.getTotalTimeLocked(0, BatteryStats.STATS_SINCE_CHARGED));
assertEquals(1, unparceledTimer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
}
}

View File

@@ -16,20 +16,13 @@
package com.android.internal.os;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import android.os.BatteryStats;
import android.os.Parcel;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
import android.support.test.filters.SmallTest;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.android.internal.os.BatteryStatsImpl;
/**
* Provides test cases for android.os.BatteryStats.
*/

View File

@@ -5,6 +5,7 @@ import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
BatteryStatsSamplingTimerTest.class,
BatteryStatsServTest.class,
BatteryStatsTimeBaseTest.class,
BatteryStatsTimerTest.class,

View File

@@ -18,11 +18,10 @@ package com.android.internal.os;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import android.os.BatteryStats;
import android.os.Parcel;
import android.test.suitebuilder.annotation.SmallTest;
import android.support.test.filters.SmallTest;
import android.util.Log;
import junit.framework.Assert;

View File

@@ -18,8 +18,7 @@ package com.android.internal.os;
import android.os.BatteryStats;
import android.os.Parcel;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
import android.support.test.filters.SmallTest;
import android.util.StringBuilderPrinter;
import junit.framework.Assert;
@@ -148,7 +147,7 @@ public class BatteryStatsTimerTest extends TestCase {
timer.onTimeStarted(10, 20, 50);
Assert.assertEquals(50, timer.lastComputeRunTimeRealtime);
Assert.assertEquals(4, timer.getUnpluggedTime());
Assert.assertEquals(0, timer.getUnpluggedCount());
Assert.assertEquals(3000, timer.getUnpluggedCount());
// Test that stopping the timer updates mTotalTime and mCount
timer.nextComputeRunTime = 17;
@@ -168,15 +167,16 @@ public class BatteryStatsTimerTest extends TestCase {
// Test write then read
TestTimer timer1 = new TestTimer(clocks, 0, timeBase);
timer1.setCount(1);
timer1.setLoadedCount(2);
timer1.setLastCount(3);
timer1.setUnpluggedCount(4);
timer1.setLoadedCount(3);
timer1.setLastCount(4);
timer1.setUnpluggedCount(5);
timer1.setTotalTime(9223372036854775807L);
timer1.setLoadedTime(9223372036854775806L);
timer1.setLastTime(9223372036854775805L);
timer1.setUnpluggedTime(9223372036854775804L);
timer1.setTimeBeforeMark(9223372036854775803L);
timer1.nextComputeRunTime = 201;
timer1.nextComputeCurrentCount = 2;
Parcel parcel = Parcel.obtain();
Timer.writeTimerToParcel(parcel, timer1, 77);
@@ -185,10 +185,10 @@ public class BatteryStatsTimerTest extends TestCase {
Assert.assertTrue("parcel null object", parcel.readInt() != 0);
TestTimer timer2 = new TestTimer(clocks, 0, timeBase, parcel);
Assert.assertEquals(1, timer2.getCount());
Assert.assertEquals(2, timer2.getLoadedCount());
Assert.assertEquals(2, timer2.getCount()); // from computeTotalCountLocked()
Assert.assertEquals(3, timer2.getLoadedCount());
Assert.assertEquals(0, timer2.getLastCount()); // NOT saved
Assert.assertEquals(4, timer2.getUnpluggedCount());
Assert.assertEquals(5, timer2.getUnpluggedCount());
Assert.assertEquals(201, timer2.getTotalTime()); // from computeRunTimeLocked()
Assert.assertEquals(9223372036854775806L, timer2.getLoadedTime());
Assert.assertEquals(0, timer2.getLastTime()); // NOT saved
@@ -309,6 +309,7 @@ public class BatteryStatsTimerTest extends TestCase {
Parcel parcel = Parcel.obtain();
timer1.nextComputeRunTime = 9223372036854775800L;
timer1.nextComputeCurrentCount = 1;
timer1.writeSummaryFromParcelLocked(parcel, 201);
Assert.assertEquals(40, timer1.lastComputeRunTimeRealtime);