Leif Wilden | 5eb7748 | 2018-01-23 23:54:05 +0000 | [diff] [blame] | 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.settingslib.core.instrumentation; |
| 17 | |
| 18 | import static com.android.settingslib.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.settingslib.SettingsLibRobolectricTestRunner; |
| 35 | import com.android.settingslib.TestConfig; |
| 36 | |
| 37 | import org.junit.Before; |
| 38 | import org.junit.Test; |
| 39 | import org.junit.runner.RunWith; |
| 40 | import org.mockito.Mock; |
| 41 | import org.mockito.MockitoAnnotations; |
| 42 | import org.robolectric.annotation.Config; |
| 43 | |
| 44 | |
| 45 | @RunWith(SettingsLibRobolectricTestRunner.class) |
| 46 | @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) |
| 47 | public class VisibilityLoggerMixinTest { |
| 48 | |
| 49 | @Mock |
| 50 | private MetricsFeatureProvider mMetricsFeature; |
| 51 | |
| 52 | private VisibilityLoggerMixin mMixin; |
| 53 | |
| 54 | @Before |
| 55 | public void init() { |
| 56 | MockitoAnnotations.initMocks(this); |
| 57 | mMixin = new VisibilityLoggerMixin(TestInstrumentable.TEST_METRIC, mMetricsFeature); |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void shouldLogVisibleOnResume() { |
| 62 | mMixin.onResume(); |
| 63 | |
| 64 | verify(mMetricsFeature, times(1)) |
| 65 | .visible(nullable(Context.class), eq(MetricsProto.MetricsEvent.VIEW_UNKNOWN), |
| 66 | eq(TestInstrumentable.TEST_METRIC)); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void shouldLogVisibleWithSource() { |
| 71 | final Intent sourceIntent = new Intent() |
| 72 | .putExtra(VisibilityLoggerMixin.EXTRA_SOURCE_METRICS_CATEGORY, |
| 73 | MetricsProto.MetricsEvent.SETTINGS_GESTURES); |
| 74 | final Activity activity = mock(Activity.class); |
| 75 | when(activity.getIntent()).thenReturn(sourceIntent); |
| 76 | mMixin.setSourceMetricsCategory(activity); |
| 77 | mMixin.onResume(); |
| 78 | |
| 79 | verify(mMetricsFeature, times(1)) |
| 80 | .visible(nullable(Context.class), eq(MetricsProto.MetricsEvent.SETTINGS_GESTURES), |
| 81 | eq(TestInstrumentable.TEST_METRIC)); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void shouldLogHideOnPause() { |
| 86 | mMixin.onPause(); |
| 87 | |
| 88 | verify(mMetricsFeature, times(1)) |
| 89 | .hidden(nullable(Context.class), eq(TestInstrumentable.TEST_METRIC)); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void shouldNotLogIfMetricsFeatureIsNull() { |
| 94 | mMixin = new VisibilityLoggerMixin(TestInstrumentable.TEST_METRIC, null); |
| 95 | mMixin.onResume(); |
| 96 | mMixin.onPause(); |
| 97 | |
| 98 | verify(mMetricsFeature, never()) |
| 99 | .hidden(nullable(Context.class), anyInt()); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void shouldNotLogIfMetricsCategoryIsUnknown() { |
| 104 | mMixin = new VisibilityLoggerMixin(METRICS_CATEGORY_UNKNOWN, mMetricsFeature); |
| 105 | |
| 106 | mMixin.onResume(); |
| 107 | mMixin.onPause(); |
| 108 | |
| 109 | verify(mMetricsFeature, never()) |
| 110 | .hidden(nullable(Context.class), anyInt()); |
| 111 | } |
| 112 | |
| 113 | private final class TestInstrumentable implements Instrumentable { |
| 114 | |
| 115 | public static final int TEST_METRIC = 12345; |
| 116 | |
| 117 | @Override |
| 118 | public int getMetricsCategory() { |
| 119 | return TEST_METRIC; |
| 120 | } |
| 121 | } |
| 122 | } |