Home | History | Annotate | Download | only in car
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3 *
      4 * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of 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,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.car;
     17 
     18 import android.test.suitebuilder.annotation.MediumTest;
     19 import com.android.car.systeminterface.TimeInterface;
     20 import com.android.car.test.utils.TemporaryFile;
     21 
     22 import junit.framework.TestCase;
     23 
     24 @MediumTest
     25 public class UptimeTrackerTest extends TestCase {
     26     static final String TAG = UptimeTrackerTest.class.getSimpleName();
     27 
     28     static final class MockTimeInterface implements TimeInterface {
     29         private long mCurrentTime = 0;
     30         private Runnable mRunnable = null;
     31 
     32         MockTimeInterface incrementTime(long by) {
     33             mCurrentTime += by;
     34             return this;
     35         }
     36 
     37         MockTimeInterface tick() {
     38             if (mRunnable != null) {
     39                 mRunnable.run();
     40             }
     41             return this;
     42         }
     43 
     44         @Override
     45         public long getUptime(boolean includeDeepSleepTime) {
     46             return mCurrentTime;
     47         }
     48 
     49         @Override
     50         public void scheduleAction(Runnable r, long delayMs) {
     51             if (mRunnable != null) {
     52                 throw new IllegalStateException("task already scheduled");
     53             }
     54             mRunnable = r;
     55         }
     56 
     57         @Override
     58         public void cancelAllActions() {
     59             mRunnable = null;
     60         }
     61     }
     62 
     63     private static final long SNAPSHOT_INTERVAL = 0; // actual time doesn't matter for this test
     64 
     65     public void testUptimeTrackerFromCleanSlate() throws Exception {
     66         MockTimeInterface timeInterface = new MockTimeInterface();
     67         try (TemporaryFile uptimeFile = new TemporaryFile(TAG)) {
     68             UptimeTracker uptimeTracker = new UptimeTracker(uptimeFile.getFile(),
     69                 SNAPSHOT_INTERVAL, timeInterface);
     70 
     71             assertEquals(0, uptimeTracker.getTotalUptime());
     72 
     73             timeInterface.incrementTime(5000).tick();
     74             assertEquals(5000, uptimeTracker.getTotalUptime());
     75 
     76             timeInterface.tick();
     77             assertEquals(5000, uptimeTracker.getTotalUptime());
     78 
     79             timeInterface.incrementTime(1000).tick();
     80             assertEquals(6000, uptimeTracker.getTotalUptime());
     81 
     82             timeInterface.incrementTime(400).tick();
     83             assertEquals(6400, uptimeTracker.getTotalUptime());
     84         }
     85     }
     86 
     87     public void testUptimeTrackerWithHistoricalState() throws Exception {
     88         MockTimeInterface timeInterface = new MockTimeInterface();
     89         try (TemporaryFile uptimeFile = new TemporaryFile(TAG)) {
     90             uptimeFile.write("{\"uptime\" : 5000}");
     91             UptimeTracker uptimeTracker = new UptimeTracker(uptimeFile.getFile(),
     92                 SNAPSHOT_INTERVAL, timeInterface);
     93 
     94             assertEquals(5000, uptimeTracker.getTotalUptime());
     95 
     96             timeInterface.incrementTime(5000).tick();
     97             assertEquals(10000, uptimeTracker.getTotalUptime());
     98 
     99             timeInterface.incrementTime(1000).tick();
    100             assertEquals(11000, uptimeTracker.getTotalUptime());
    101         }
    102     }
    103 
    104     public void testUptimeTrackerAcrossHistoricalState() throws Exception {
    105         MockTimeInterface timeInterface = new MockTimeInterface();
    106         try (TemporaryFile uptimeFile = new TemporaryFile(TAG)) {
    107             uptimeFile.write("{\"uptime\" : 5000}");
    108             UptimeTracker uptimeTracker = new UptimeTracker(uptimeFile.getFile(),
    109                 SNAPSHOT_INTERVAL, timeInterface);
    110 
    111             assertEquals(5000, uptimeTracker.getTotalUptime());
    112 
    113             timeInterface.incrementTime(5000).tick();
    114             assertEquals(10000, uptimeTracker.getTotalUptime());
    115 
    116             timeInterface.incrementTime(500).tick();
    117             uptimeTracker.onDestroy();
    118             timeInterface.cancelAllActions();
    119 
    120             uptimeTracker = new UptimeTracker(uptimeFile.getFile(),
    121                 SNAPSHOT_INTERVAL, timeInterface);
    122 
    123             timeInterface.incrementTime(3000).tick();
    124             assertEquals(13500, uptimeTracker.getTotalUptime());
    125         }
    126     }
    127 
    128     public void testUptimeTrackerShutdown() throws Exception {
    129         MockTimeInterface timeInterface = new MockTimeInterface();
    130         try (TemporaryFile uptimeFile = new TemporaryFile(TAG)) {
    131             UptimeTracker uptimeTracker = new UptimeTracker(uptimeFile.getFile(),
    132                 SNAPSHOT_INTERVAL, timeInterface);
    133 
    134             timeInterface.incrementTime(6000);
    135             uptimeTracker.onDestroy();
    136             timeInterface.cancelAllActions();
    137 
    138             uptimeTracker = new UptimeTracker(uptimeFile.getFile(),
    139                 SNAPSHOT_INTERVAL, timeInterface);
    140             assertEquals(6000, uptimeTracker.getTotalUptime());
    141         }
    142     }
    143 }
    144