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 package com.android.settings.core.instrumentation; 17 18 import static com.android.settings.core.instrumentation.Instrumentable.METRICS_CATEGORY_UNKNOWN; 19 20 import static org.mockito.ArgumentMatchers.nullable; 21 import static org.mockito.Matchers.anyInt; 22 import static org.mockito.Matchers.eq; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.never; 25 import static org.mockito.Mockito.times; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.app.Activity; 30 import android.content.Context; 31 import android.content.Intent; 32 33 import com.android.internal.logging.nano.MetricsProto; 34 import com.android.settings.SettingsActivity; 35 import com.android.settings.testutils.SettingsRobolectricTestRunner; 36 import com.android.settings.TestConfig; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.annotation.Config; 44 45 46 @RunWith(SettingsRobolectricTestRunner.class) 47 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 48 public class VisibilityLoggerMixinTest { 49 50 @Mock 51 private MetricsFeatureProvider mMetricsFeature; 52 53 private VisibilityLoggerMixin mMixin; 54 55 @Before 56 public void init() { 57 MockitoAnnotations.initMocks(this); 58 mMixin = new VisibilityLoggerMixin(TestInstrumentable.TEST_METRIC, mMetricsFeature); 59 } 60 61 @Test 62 public void shouldLogVisibleOnResume() { 63 mMixin.onResume(); 64 65 verify(mMetricsFeature, times(1)) 66 .visible(nullable(Context.class), eq(MetricsProto.MetricsEvent.VIEW_UNKNOWN), 67 eq(TestInstrumentable.TEST_METRIC)); 68 } 69 70 @Test 71 public void shouldLogVisibleWithSource() { 72 final Intent sourceIntent = new Intent() 73 .putExtra(SettingsActivity.EXTRA_SOURCE_METRICS_CATEGORY, 74 MetricsProto.MetricsEvent.SETTINGS_GESTURES); 75 final Activity activity = mock(Activity.class); 76 when(activity.getIntent()).thenReturn(sourceIntent); 77 mMixin.setSourceMetricsCategory(activity); 78 mMixin.onResume(); 79 80 verify(mMetricsFeature, times(1)) 81 .visible(nullable(Context.class), eq(MetricsProto.MetricsEvent.SETTINGS_GESTURES), 82 eq(TestInstrumentable.TEST_METRIC)); 83 } 84 85 @Test 86 public void shouldLogHideOnPause() { 87 mMixin.onPause(); 88 89 verify(mMetricsFeature, times(1)) 90 .hidden(nullable(Context.class), eq(TestInstrumentable.TEST_METRIC)); 91 } 92 93 @Test 94 public void shouldNotLogIfMetricsFeatureIsNull() { 95 mMixin = new VisibilityLoggerMixin(TestInstrumentable.TEST_METRIC); 96 mMixin.onResume(); 97 mMixin.onPause(); 98 99 verify(mMetricsFeature, never()) 100 .hidden(nullable(Context.class), anyInt()); 101 } 102 103 @Test 104 public void shouldNotLogIfMetricsCategoryIsUnknown() { 105 mMixin = new VisibilityLoggerMixin(METRICS_CATEGORY_UNKNOWN, mMetricsFeature); 106 107 mMixin.onResume(); 108 mMixin.onPause(); 109 110 verify(mMetricsFeature, never()) 111 .hidden(nullable(Context.class), anyInt()); 112 } 113 114 private final class TestInstrumentable implements Instrumentable { 115 116 public static final int TEST_METRIC = 12345; 117 118 @Override 119 public int getMetricsCategory() { 120 return TEST_METRIC; 121 } 122 } 123 } 124