1 /*
2  * Copyright (C) 2016 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 com.android.managedprovisioning.analytics;
18 
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.verifyNoMoreInteractions;
21 import static org.mockito.Mockito.verifyZeroInteractions;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.test.AndroidTestCase;
26 import android.test.suitebuilder.annotation.SmallTest;
27 
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 
31 /**
32  * Unit-tests for {@link TimeLogger}.
33  */
34 @SmallTest
35 public class TimeLoggerTest extends AndroidTestCase {
36 
37     private static final int CATEGORY = 1;
38     private static final long START_TIME_MS = 1500;
39     private static final long STOP_TIME_MS = 2500;
40 
41     private TimeLogger mTimeLogger;
42 
43     @Mock private Context mContext;
44     @Mock private MetricsLoggerWrapper mMetricsLoggerWrapper;
45     @Mock private AnalyticsUtils mAnalyticsUtils;
46 
47     @Override
setUp()48     public void setUp() {
49         // this is necessary for mockito to work
50         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
51 
52         MockitoAnnotations.initMocks(this);
53 
54         mTimeLogger = new TimeLogger(mContext, CATEGORY, mMetricsLoggerWrapper, mAnalyticsUtils);
55     }
56 
57     @SmallTest
testTimeLogger_withStartTime()58     public void testTimeLogger_withStartTime() {
59         // GIVEN that START_TIME_MS is the elapsed real time.
60         when(mAnalyticsUtils.elapsedRealTime()).thenReturn(START_TIME_MS);
61         // WHEN logging time starts.
62         mTimeLogger.start();
63 
64         // GIVEN that STOP_TIME_MS is the elapsed real time.
65         when(mAnalyticsUtils.elapsedRealTime()).thenReturn(STOP_TIME_MS);
66         // WHEN logging time stops.
67         mTimeLogger.stop();
68 
69         // THEN time taken should be logged and the value should be stop time - start time.
70         verify(mMetricsLoggerWrapper).logAction(mContext, CATEGORY,
71                 (int) (STOP_TIME_MS - START_TIME_MS));
72     }
73 
74     @SmallTest
testTimeLogger_withStartTime_stopsTwice()75     public void testTimeLogger_withStartTime_stopsTwice() {
76         // GIVEN that START_TIME_MS is the elapsed real time.
77         when(mAnalyticsUtils.elapsedRealTime()).thenReturn(START_TIME_MS);
78         // WHEN logging time starts.
79         mTimeLogger.start();
80 
81         // GIVEN that STOP_TIME_MS is the elapsed real time.
82         when(mAnalyticsUtils.elapsedRealTime()).thenReturn(STOP_TIME_MS);
83         // WHEN logging time stops.
84         mTimeLogger.stop();
85 
86         // THEN time taken should be logged and the value should be stop time - start time.
87         verify(mMetricsLoggerWrapper).logAction(mContext, CATEGORY,
88                 (int) (STOP_TIME_MS - START_TIME_MS));
89 
90         // WHEN logging time stops.
91         mTimeLogger.stop();
92         // THEN nothing should be logged.
93         verifyNoMoreInteractions(mMetricsLoggerWrapper);
94     }
95 
96     @SmallTest
testTimeLogger_withoutStartTime()97     public void testTimeLogger_withoutStartTime() {
98         // GIVEN there is no start time.
99         // WHEN logging time stops.
100         mTimeLogger.stop();
101         // THEN nothing should be logged.
102         verifyZeroInteractions(mMetricsLoggerWrapper);
103     }
104 }
105