1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16package com.android.internal.os;
17
18import android.os.BatteryStats;
19import android.support.test.filters.SmallTest;
20
21import junit.framework.TestCase;
22
23/**
24 * Test BatteryStatsImpl.DualTimer.
25 */
26public class BatteryStatsDualTimerTest extends TestCase {
27
28    @SmallTest
29    public void testResetDetach() throws Exception {
30        final MockClocks clocks = new MockClocks();
31        clocks.realtime = clocks.uptime = 100;
32
33        final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
34        timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
35        final BatteryStatsImpl.TimeBase subTimeBase = new BatteryStatsImpl.TimeBase();
36        subTimeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
37
38        final BatteryStatsImpl.DualTimer timer = new BatteryStatsImpl.DualTimer(clocks,
39                null, BatteryStats.WAKE_TYPE_PARTIAL, null, timeBase, subTimeBase);
40
41        assertTrue(timeBase.hasObserver(timer));
42        assertFalse(subTimeBase.hasObserver(timer));
43        assertFalse(timeBase.hasObserver(timer.getSubTimer()));
44        assertTrue(subTimeBase.hasObserver(timer.getSubTimer()));
45
46        // Timer is running so resetting it should not remove it from timerbases.
47        clocks.realtime = clocks.uptime = 200;
48        timer.startRunningLocked(clocks.realtime);
49        timer.reset(true);
50        assertTrue(timeBase.hasObserver(timer));
51        assertTrue(subTimeBase.hasObserver(timer.getSubTimer()));
52
53        // Stop timer and ensure that resetting removes it from timebases.
54        clocks.realtime = clocks.uptime = 300;
55        timer.stopRunningLocked(clocks.realtime);
56        timer.reset(true);
57        assertFalse(timeBase.hasObserver(timer));
58        assertFalse(timeBase.hasObserver(timer.getSubTimer()));
59    }
60}
61