Home | History | Annotate | Download | only in usage
      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 
     17 package android.app.usage;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import android.support.test.filters.SmallTest;
     22 import android.support.test.runner.AndroidJUnit4;
     23 
     24 import org.junit.Before;
     25 import org.junit.Test;
     26 import org.junit.runner.RunWith;
     27 
     28 @RunWith(AndroidJUnit4.class)
     29 @SmallTest
     30 public class UsageStatsTest {
     31     private UsageStats left;
     32     private UsageStats right;
     33 
     34     @Before
     35     public void setUp() throws Exception {
     36         left = new UsageStats();
     37         right = new UsageStats();
     38     }
     39 
     40     @Test
     41     public void testEarlierBeginTimeTakesPriorityOnAdd() {
     42         left.mPackageName = "com.test";
     43         left.mBeginTimeStamp = 100000;
     44         right.mPackageName = "com.test";
     45         right.mBeginTimeStamp = 99999;
     46 
     47         left.add(right);
     48 
     49         assertThat(left.getFirstTimeStamp()).isEqualTo(99999);
     50     }
     51 
     52     @Test
     53     public void testLaterEndTimeTakesPriorityOnAdd() {
     54         left.mPackageName = "com.test";
     55         left.mEndTimeStamp = 100000;
     56         right.mPackageName = "com.test";
     57         right.mEndTimeStamp = 100001;
     58 
     59         left.add(right);
     60 
     61         assertThat(left.getLastTimeStamp()).isEqualTo(100001);
     62     }
     63 
     64     @Test
     65     public void testLastUsedTimeIsOverriddenByLaterStats() {
     66         left.mPackageName = "com.test";
     67         left.mBeginTimeStamp = 100000;
     68         left.mLastTimeUsed = 200000;
     69         right.mPackageName = "com.test";
     70         right.mBeginTimeStamp = 100001;
     71         right.mLastTimeUsed = 200001;
     72 
     73         left.add(right);
     74 
     75         assertThat(left.getLastTimeUsed()).isEqualTo(200001);
     76     }
     77 
     78     @Test
     79     public void testLastUsedTimeIsNotOverriddenByLaterStatsIfUseIsEarlier() {
     80         left.mPackageName = "com.test";
     81         left.mBeginTimeStamp = 100000;
     82         left.mLastTimeUsed = 200000;
     83         right.mPackageName = "com.test";
     84         right.mBeginTimeStamp = 100001;
     85         right.mLastTimeUsed = 150000;
     86 
     87         left.add(right);
     88 
     89         assertThat(left.getLastTimeUsed()).isEqualTo(200000);
     90     }
     91 
     92     @Test
     93     public void testForegroundTimeIsSummed() {
     94         left.mPackageName = "com.test";
     95         left.mBeginTimeStamp = 100000;
     96         left.mTotalTimeInForeground = 10;
     97         right.mPackageName = "com.test";
     98         right.mBeginTimeStamp = 100001;
     99         right.mTotalTimeInForeground = 1;
    100 
    101         left.add(right);
    102 
    103         assertThat(left.getTotalTimeInForeground()).isEqualTo(11);
    104     }
    105 }
    106