WakeupControllerTest.java revision 04263765dc4bb2a74722d69db56c5b42e7fb1bc9
1/*
2 * Copyright 2017 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.server.wifi;
18
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21import static org.mockito.ArgumentMatchers.any;
22import static org.mockito.Mockito.verify;
23import static org.mockito.Mockito.when;
24
25import android.content.Context;
26import android.os.test.TestLooper;
27import android.provider.Settings;
28
29import org.junit.Before;
30import org.junit.Test;
31import org.mockito.Mock;
32import org.mockito.MockitoAnnotations;
33
34import java.io.ByteArrayOutputStream;
35import java.io.PrintWriter;
36
37/**
38 * Unit tests for {@link WakeupController}.
39 */
40public class WakeupControllerTest {
41
42    @Mock private Context mContext;
43    @Mock private WakeupLock mWakeupLock;
44    @Mock private WifiConfigManager mWifiConfigManager;
45    @Mock private WifiConfigStore mWifiConfigStore;
46    @Mock private FrameworkFacade mFrameworkFacade;
47
48    private TestLooper mLooper;
49    private WakeupController mWakeupController;
50
51    /** Initialize objects before each test run. */
52    @Before
53    public void setUp() {
54        MockitoAnnotations.initMocks(this);
55        mLooper = new TestLooper();
56    }
57
58    private WakeupController newWakeupController() {
59        return new WakeupController(mContext, mLooper.getLooper(), mWakeupLock, mWifiConfigManager,
60                mWifiConfigStore, mFrameworkFacade);
61    }
62
63    /**
64     * Verify WakeupController is enabled when the settings toggle is true.
65     */
66    @Test
67    public void verifyEnabledWhenToggledOn() {
68        when(mFrameworkFacade.getIntegerSetting(mContext,
69                Settings.Global.WIFI_WAKEUP_ENABLED, 0)).thenReturn(1);
70        mWakeupController = newWakeupController();
71
72        assertTrue(mWakeupController.isEnabled());
73    }
74
75    /**
76     * Verify WakeupController is disabled when the settings toggle is false.
77     */
78    @Test
79    public void verifyDisabledWhenToggledOff() {
80        when(mFrameworkFacade.getIntegerSetting(mContext,
81                Settings.Global.WIFI_WAKEUP_ENABLED, 0)).thenReturn(0);
82        mWakeupController = newWakeupController();
83
84        assertFalse(mWakeupController.isEnabled());
85    }
86
87    /**
88     * Verify WakeupController registers its store data with the WifiConfigStore on construction.
89     */
90    @Test
91    public void registersWakeupConfigStoreData() {
92        mWakeupController = newWakeupController();
93        verify(mWifiConfigStore).registerStoreData(any(WakeupConfigStoreData.class));
94    }
95
96    /**
97     * Verify that dump calls also dump the state of the WakeupLock.
98     */
99    @Test
100    public void dumpIncludesWakeupLock() {
101        mWakeupController = newWakeupController();
102        ByteArrayOutputStream stream = new ByteArrayOutputStream();
103        PrintWriter writer = new PrintWriter(stream);
104        mWakeupController.dump(null, writer, null);
105
106        verify(mWakeupLock).dump(null, writer, null);
107    }
108}
109