OSDN Git Service

c009d0c92c330ad87b86fade0120e50562ff7a60
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / core / instrumentation / VisibilityLoggerMixinTest.java
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 android.content.Context;
19
20 import com.android.settings.TestConfig;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.robolectric.RobolectricTestRunner;
28 import org.robolectric.annotation.Config;
29
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.anyInt;
32 import static org.mockito.Matchers.eq;
33 import static org.mockito.Mockito.never;
34 import static org.mockito.Mockito.times;
35 import static org.mockito.Mockito.verify;
36
37
38 @RunWith(RobolectricTestRunner.class)
39 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
40 public class VisibilityLoggerMixinTest {
41
42     @Mock
43     private MetricsFeatureProvider mMetricsFeature;
44
45     private VisibilityLoggerMixin mMixin;
46
47     @Before
48     public void init() {
49         MockitoAnnotations.initMocks(this);
50         mMixin = new VisibilityLoggerMixin(TestInstrumentable.TEST_METRIC, mMetricsFeature);
51     }
52
53     @Test
54     public void shouldLogVisibleOnResume() {
55         mMixin.onResume();
56
57         verify(mMetricsFeature, times(1))
58                 .visible(any(Context.class), eq(TestInstrumentable.TEST_METRIC));
59     }
60
61     @Test
62     public void shouldLogHideOnPause() {
63         mMixin.onPause();
64
65         verify(mMetricsFeature, times(1))
66                 .hidden(any(Context.class), eq(TestInstrumentable.TEST_METRIC));
67     }
68
69     @Test
70     public void shouldNotLogIfMetricsFeatureIsNull() {
71         mMixin = new VisibilityLoggerMixin(TestInstrumentable.TEST_METRIC);
72         mMixin.onResume();
73         mMixin.onPause();
74
75         verify(mMetricsFeature, never())
76                 .hidden(any(Context.class), anyInt());
77     }
78
79     private final class TestInstrumentable implements Instrumentable {
80
81         public static final int TEST_METRIC = 12345;
82
83         @Override
84         public int getMetricsCategory() {
85             return TEST_METRIC;
86         }
87     }
88 }