1/*
2 * Copyright (C) 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.*;
20import static org.mockito.Mockito.*;
21
22import android.app.Notification;
23import android.app.NotificationManager;
24import android.content.Context;
25import android.content.Intent;
26import android.content.res.Resources;
27import android.provider.Settings;
28
29import com.android.internal.notification.SystemNotificationChannels;
30
31import org.junit.Before;
32import org.junit.Test;
33import org.mockito.Answers;
34import org.mockito.ArgumentCaptor;
35import org.mockito.Mock;
36import org.mockito.MockitoAnnotations;
37
38/**
39 * Unit tests for {@link com.android.server.wifi.WrongPasswordNotifier}.
40 */
41public class WrongPasswordNotifierTest {
42    private static final String TEST_SSID = "Test SSID";
43
44    @Mock Context mContext;
45    @Mock Resources mResources;
46    @Mock NotificationManager mNotificationManager;
47    @Mock FrameworkFacade mFrameworkFacade;
48    @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Notification.Builder mNotificationBuilder;
49    WrongPasswordNotifier mWrongPassNotifier;
50
51    /**
52     * Sets up for unit test
53     */
54    @Before
55    public void setUp() throws Exception {
56        MockitoAnnotations.initMocks(this);
57        when(mContext.getSystemService(Context.NOTIFICATION_SERVICE))
58                .thenReturn(mNotificationManager);
59        when(mContext.getResources()).thenReturn(mResources);
60        mWrongPassNotifier =
61                new WrongPasswordNotifier(mContext, mFrameworkFacade);
62    }
63
64    /**
65     * Verify that a wrong password notification will be generated/pushed when a wrong password
66     * error is detected for the current connection.
67     *
68     * @throws Exception
69     */
70    @Test
71    public void onWrongPasswordError() throws Exception {
72        when(mFrameworkFacade.makeNotificationBuilder(any(),
73                eq(SystemNotificationChannels.NETWORK_ALERTS))).thenReturn(mNotificationBuilder);
74        mWrongPassNotifier.onWrongPasswordError(TEST_SSID);
75        verify(mNotificationManager).notify(eq(WrongPasswordNotifier.NOTIFICATION_ID), any());
76        ArgumentCaptor<Intent> intent = ArgumentCaptor.forClass(Intent.class);
77        verify(mFrameworkFacade).getActivity(
78                any(Context.class), anyInt(), intent.capture(), anyInt());
79        assertEquals(Settings.ACTION_WIFI_SETTINGS, intent.getValue().getAction());
80        assertEquals(TEST_SSID, intent.getValue().getStringExtra("wifi_start_connect_ssid"));
81    }
82
83    /**
84     * Verify that we will attempt to dismiss the wrong password notification when starting a new
85     * connection attempt with the previous connection resulting in a wrong password error.
86     *
87     * @throws Exception
88     */
89    @Test
90    public void onNewConnectionAttemptWithPreviousWrongPasswordError() throws Exception {
91        onWrongPasswordError();
92        reset(mNotificationManager);
93
94        mWrongPassNotifier.onNewConnectionAttempt();
95        verify(mNotificationManager).cancel(any(), eq(WrongPasswordNotifier.NOTIFICATION_ID));
96    }
97
98    /**
99     * Verify that we don't attempt to dismiss the wrong password notification when starting a new
100     * connection attempt with the previous connection not resulting in a wrong password error.
101     *
102     * @throws Exception
103     */
104    @Test
105    public void onNewConnectionAttemptWithoutPreviousWrongPasswordError() throws Exception {
106        mWrongPassNotifier.onNewConnectionAttempt();
107        verify(mNotificationManager, never()).cancel(any(), anyInt());
108    }
109}
110