14a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz/*
24a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz * Copyright (C) 2017 The Android Open Source Project
34a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz *
44a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz * Licensed under the Apache License, Version 2.0 (the "License"); you may not
54a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz * use this file except in compliance with the License. You may obtain a copy of
64a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz * the License at
74a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz *
84a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz * http://www.apache.org/licenses/LICENSE-2.0
94a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz *
104a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz * Unless required by applicable law or agreed to in writing, software
114a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
124a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
134a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz * License for the specific language governing permissions and limitations under
144a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz * the License.
154a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz */
164a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatzpackage com.android.internal.os;
174a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz
184a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatzimport android.os.BatteryStats;
194a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatzimport android.support.test.filters.SmallTest;
204a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz
214a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatzimport junit.framework.TestCase;
224a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz
234a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz/**
244a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz * Test BatteryStatsImpl.DualTimer.
254a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz */
264a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatzpublic class BatteryStatsDualTimerTest extends TestCase {
274a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz
284a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz    @SmallTest
294a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz    public void testResetDetach() throws Exception {
304a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        final MockClocks clocks = new MockClocks();
314a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        clocks.realtime = clocks.uptime = 100;
324a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz
334a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
344a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
354a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        final BatteryStatsImpl.TimeBase subTimeBase = new BatteryStatsImpl.TimeBase();
364a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        subTimeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
374a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz
384a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        final BatteryStatsImpl.DualTimer timer = new BatteryStatsImpl.DualTimer(clocks,
394a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz                null, BatteryStats.WAKE_TYPE_PARTIAL, null, timeBase, subTimeBase);
404a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz
414a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        assertTrue(timeBase.hasObserver(timer));
424a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        assertFalse(subTimeBase.hasObserver(timer));
434a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        assertFalse(timeBase.hasObserver(timer.getSubTimer()));
444a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        assertTrue(subTimeBase.hasObserver(timer.getSubTimer()));
454a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz
464a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        // Timer is running so resetting it should not remove it from timerbases.
474a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        clocks.realtime = clocks.uptime = 200;
484a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        timer.startRunningLocked(clocks.realtime);
494a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        timer.reset(true);
504a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        assertTrue(timeBase.hasObserver(timer));
514a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        assertTrue(subTimeBase.hasObserver(timer.getSubTimer()));
524a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz
534a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        // Stop timer and ensure that resetting removes it from timebases.
544a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        clocks.realtime = clocks.uptime = 300;
554a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        timer.stopRunningLocked(clocks.realtime);
564a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        timer.reset(true);
574a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        assertFalse(timeBase.hasObserver(timer));
584a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz        assertFalse(timeBase.hasObserver(timer.getSubTimer()));
594a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz    }
604a3eda992b70601e26a76d9492b2f9381cf0fe51Bookatz}
61