1/*
2 * Copyright (C) 2018 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
17package com.android.settings.display;
18
19import static org.mockito.Mockito.any;
20import static org.mockito.Mockito.doReturn;
21import static org.mockito.Mockito.mock;
22import static org.mockito.Mockito.spy;
23import static org.mockito.Mockito.verify;
24
25import android.content.Context;
26
27import com.android.settings.gestures.DoubleTapScreenPreferenceController;
28import com.android.settings.gestures.PickupGesturePreferenceController;
29import com.android.settings.testutils.SettingsRobolectricTestRunner;
30import com.android.settingslib.core.AbstractPreferenceController;
31
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.mockito.MockitoAnnotations;
36import org.robolectric.RuntimeEnvironment;
37
38@RunWith(SettingsRobolectricTestRunner.class)
39public class AmbientDisplaySettingsTest {
40
41    private TestFragment mTestFragment;
42
43    private Context mContext;
44
45    @Before
46    public void setUp() throws Exception {
47        MockitoAnnotations.initMocks(this);
48        mContext = RuntimeEnvironment.application;
49        mTestFragment = spy(new TestFragment());
50    }
51
52    @Test
53    public void onAttach_alwaysOn_shouldInvokeSetters() {
54        final AmbientDisplayAlwaysOnPreferenceController controller = spy(
55                new AmbientDisplayAlwaysOnPreferenceController(mContext, "key"));
56        doReturn(controller).when(mTestFragment).use(
57                AmbientDisplayAlwaysOnPreferenceController.class);
58
59        mTestFragment.onAttach(mContext);
60        verify(controller).setConfig(any());
61        verify(controller).setCallback(any());
62    }
63
64    @Test
65    public void onAttach_notifications_shouldInvokeSetters() {
66        final AmbientDisplayNotificationsPreferenceController controller = spy(
67                new AmbientDisplayNotificationsPreferenceController(mContext, "key"));
68        doReturn(controller).when(mTestFragment).use(
69                AmbientDisplayNotificationsPreferenceController.class);
70
71        mTestFragment.onAttach(mContext);
72        verify(controller).setConfig(any());
73    }
74
75    @Test
76    public void onAttach_doubleTap_shouldInvokeSetters() {
77        final DoubleTapScreenPreferenceController controller = spy(
78                new DoubleTapScreenPreferenceController(mContext, "key"));
79        doReturn(controller).when(mTestFragment).use(DoubleTapScreenPreferenceController.class);
80
81        mTestFragment.onAttach(mContext);
82        verify(controller).setConfig(any());
83    }
84
85    @Test
86    public void onAttach_pickUp_shouldInvokeSetters() {
87        final PickupGesturePreferenceController controller = spy(
88                new PickupGesturePreferenceController(mContext, "key"));
89        doReturn(controller).when(mTestFragment).use(PickupGesturePreferenceController.class);
90
91        mTestFragment.onAttach(mContext);
92        verify(controller).setConfig(any());
93    }
94
95    public static class TestFragment extends AmbientDisplaySettings {
96        @Override
97        protected <T extends AbstractPreferenceController> T use(Class<T> clazz) {
98            return super.use(clazz);
99        }
100    }
101}