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