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 static org.mockito.Mockito.any;
20import static org.mockito.Mockito.anyInt;
21import static org.mockito.Mockito.mock;
22import static org.mockito.Mockito.never;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
25
26import android.app.Notification;
27import android.app.NotificationManager;
28import android.content.BroadcastReceiver;
29import android.content.ContentResolver;
30import android.content.Context;
31import android.content.IntentFilter;
32import android.content.res.Resources;
33import android.net.NetworkInfo;
34import android.net.wifi.ScanResult;
35import android.net.wifi.WifiManager;
36import android.os.UserHandle;
37import android.provider.Settings;
38import android.test.suitebuilder.annotation.SmallTest;
39
40import org.junit.Before;
41import org.junit.Test;
42import org.mockito.ArgumentCaptor;
43import org.mockito.Mock;
44import org.mockito.MockitoAnnotations;
45
46import java.util.ArrayList;
47import java.util.List;
48
49/**
50 * Unit tests for {@link com.android.server.wifi.WifiScanningServiceImpl}.
51 */
52@SmallTest
53public class WifiNotificationControllerTest {
54    public static final String TAG = "WifiScanningServiceTest";
55
56    @Mock private Context mContext;
57    @Mock private WifiStateMachine mWifiStateMachine;
58    @Mock private FrameworkFacade mFrameworkFacade;
59    @Mock private NotificationManager mNotificationManager;
60    WifiNotificationController mWifiNotificationController;
61
62    /**
63     * Internal BroadcastReceiver that WifiNotificationController uses to listen for broadcasts
64     * this is initialized by calling startServiceAndLoadDriver
65     */
66    BroadcastReceiver mBroadcastReceiver;
67
68    /** Initialize objects before each test run. */
69    @Before
70    public void setUp() throws Exception {
71        MockitoAnnotations.initMocks(this);
72
73        // Needed for the NotificationEnabledSettingObserver.
74        when(mContext.getContentResolver()).thenReturn(mock(ContentResolver.class));
75
76        when(mContext.getSystemService(Context.NOTIFICATION_SERVICE))
77                .thenReturn(mNotificationManager);
78
79        when(mFrameworkFacade.getIntegerSetting(mContext,
80                Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1)).thenReturn(1);
81
82        MockLooper mock_looper = new MockLooper();
83        mWifiNotificationController = new WifiNotificationController(
84                mContext, mock_looper.getLooper(), mWifiStateMachine, mFrameworkFacade,
85                mock(Notification.Builder.class));
86        ArgumentCaptor<BroadcastReceiver> broadcastReceiverCaptor =
87                ArgumentCaptor.forClass(BroadcastReceiver.class);
88
89        verify(mContext)
90                .registerReceiver(broadcastReceiverCaptor.capture(), any(IntentFilter.class));
91        mBroadcastReceiver = broadcastReceiverCaptor.getValue();
92    }
93
94    private void setOpenAccessPoint() {
95        List<ScanResult> scanResults = new ArrayList<>();
96        ScanResult scanResult = new ScanResult();
97        scanResult.capabilities = "[ESS]";
98        scanResults.add(scanResult);
99        when(mWifiStateMachine.syncGetScanResultsList()).thenReturn(scanResults);
100    }
101
102    /** Verifies that a notification is displayed (and retracted) given system events. */
103    @Test
104    public void verifyNotificationDisplayed() throws Exception {
105        TestUtil.sendWifiStateChanged(mBroadcastReceiver, mContext, WifiManager.WIFI_STATE_ENABLED);
106        TestUtil.sendNetworkStateChanged(mBroadcastReceiver, mContext,
107                NetworkInfo.DetailedState.DISCONNECTED);
108        setOpenAccessPoint();
109
110        // The notification should not be displayed after only two scan results.
111        TestUtil.sendScanResultsAvailable(mBroadcastReceiver, mContext);
112        TestUtil.sendScanResultsAvailable(mBroadcastReceiver, mContext);
113        verify(mNotificationManager, never())
114                .notifyAsUser(any(String.class), anyInt(), any(Notification.class),
115                        any(UserHandle.class));
116
117        // Changing to and from "SCANNING" state should not affect the counter.
118        TestUtil.sendNetworkStateChanged(mBroadcastReceiver, mContext,
119                NetworkInfo.DetailedState.SCANNING);
120        TestUtil.sendNetworkStateChanged(mBroadcastReceiver, mContext,
121                NetworkInfo.DetailedState.DISCONNECTED);
122
123        // Needed while WifiNotificationController creates its notification.
124        when(mContext.getResources()).thenReturn(mock(Resources.class));
125
126        // The third scan result notification will trigger the notification.
127        TestUtil.sendScanResultsAvailable(mBroadcastReceiver, mContext);
128        verify(mNotificationManager)
129                .notifyAsUser(any(String.class), anyInt(), any(Notification.class),
130                        any(UserHandle.class));
131        verify(mNotificationManager, never())
132                .cancelAsUser(any(String.class), anyInt(), any(UserHandle.class));
133
134        // Changing network state should cause the notification to go away.
135        TestUtil.sendNetworkStateChanged(mBroadcastReceiver, mContext,
136                NetworkInfo.DetailedState.CONNECTED);
137        verify(mNotificationManager)
138                .cancelAsUser(any(String.class), anyInt(), any(UserHandle.class));
139    }
140}
141