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
17package com.android.server.wifi;
18
19import com.android.internal.app.IBatteryStats;
20
21import static org.mockito.Mockito.any;
22import static org.mockito.Mockito.anyInt;
23import static org.mockito.Mockito.times;
24import static org.mockito.Mockito.verify;
25
26import android.test.suitebuilder.annotation.SmallTest;
27
28import org.junit.Before;
29import org.junit.Test;
30import org.mockito.Mock;
31import org.mockito.MockitoAnnotations;
32
33/*
34 * Unit tests for {@link com.android.server.wifi.WifiStateTracker}.
35 */
36@SmallTest
37public class WifiStateTrackerTest {
38
39    private static final String TAG = "WifiStateTrackerTest";
40    @Mock IBatteryStats mBatteryStats;
41    private WifiStateTracker mWifiStateTracker;
42
43    /**
44     * Setup test.
45     */
46    @Before
47    public void setUp() {
48        MockitoAnnotations.initMocks(this);
49        mWifiStateTracker = new WifiStateTracker(mBatteryStats);
50    }
51
52    /**
53     * Ensure BatteryStats's noteWifiState() is called when the method
54     * updateState() is invoked on WifiStateTracker for relevant states.
55     */
56    @Test
57    public void testBatteryStatsUpdated() throws Exception {
58        int[] relevantStates = new int[] { WifiStateTracker.SCAN_MODE,
59                WifiStateTracker.CONNECTED, WifiStateTracker.DISCONNECTED,
60                WifiStateTracker.SOFT_AP};
61        for (int i = 0; i < relevantStates.length; i++) {
62            mWifiStateTracker.updateState(relevantStates[i]);
63        }
64        verify(mBatteryStats, times(relevantStates.length)).noteWifiState(anyInt(), any());
65    }
66
67    /**
68     * Ensure BatteryStats's noteWifiState() is not called when the method
69     * updateState() is invoked on WifiStateTracker for irrelevant states.
70     */
71    @Test
72    public void testBatteryStatsNotUpdated() throws Exception {
73        int[] irrelevantStates = new int[] { WifiStateTracker.SCAN_MODE - 1,
74                WifiStateTracker.SOFT_AP + 1};
75        for (int i = 0; i < irrelevantStates.length; i++) {
76            mWifiStateTracker.updateState(irrelevantStates[i]);
77        }
78        verify(mBatteryStats, times(0)).noteWifiState(anyInt(), any());
79    }
80}
81