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