1e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu/* 2e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * Copyright (C) 2017 The Android Open Source Project 3e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * 4e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * Licensed under the Apache License, Version 2.0 (the "License"); 5e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * you may not use this file except in compliance with the License. 6e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * You may obtain a copy of the License at 7e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * 8e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * http://www.apache.org/licenses/LICENSE-2.0 9e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * 10e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * Unless required by applicable law or agreed to in writing, software 11e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * distributed under the License is distributed on an "AS IS" BASIS, 12e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * See the License for the specific language governing permissions and 14e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * limitations under the License. 15e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu */ 16e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu 17e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiupackage com.android.server.wifi; 18e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu 19e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiuimport static org.junit.Assert.*; 20e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiuimport static org.mockito.Mockito.*; 21e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu 22e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiuimport android.app.Notification; 23e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiuimport android.app.NotificationManager; 24e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiuimport android.content.Context; 2529da253de531bde521dc6fb1cd727c099f1e74caPeter Qiuimport android.content.Intent; 26e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiuimport android.content.res.Resources; 2729da253de531bde521dc6fb1cd727c099f1e74caPeter Qiuimport android.provider.Settings; 28e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu 29e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiuimport com.android.internal.notification.SystemNotificationChannels; 30e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu 31e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiuimport org.junit.Before; 32e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiuimport org.junit.Test; 33e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiuimport org.mockito.Answers; 3429da253de531bde521dc6fb1cd727c099f1e74caPeter Qiuimport org.mockito.ArgumentCaptor; 35e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiuimport org.mockito.Mock; 36e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiuimport org.mockito.MockitoAnnotations; 37e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu 38e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu/** 39e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * Unit tests for {@link com.android.server.wifi.WrongPasswordNotifier}. 40e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu */ 41e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiupublic class WrongPasswordNotifierTest { 42e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu private static final String TEST_SSID = "Test SSID"; 43e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu 44e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu @Mock Context mContext; 45e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu @Mock Resources mResources; 46e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu @Mock NotificationManager mNotificationManager; 47e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu @Mock FrameworkFacade mFrameworkFacade; 48e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Notification.Builder mNotificationBuilder; 49e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu WrongPasswordNotifier mWrongPassNotifier; 50e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu 51e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu /** 52e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * Sets up for unit test 53e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu */ 54e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu @Before 55e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu public void setUp() throws Exception { 56e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu MockitoAnnotations.initMocks(this); 57e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu when(mContext.getSystemService(Context.NOTIFICATION_SERVICE)) 58e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu .thenReturn(mNotificationManager); 59e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu when(mContext.getResources()).thenReturn(mResources); 60e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu mWrongPassNotifier = 61e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu new WrongPasswordNotifier(mContext, mFrameworkFacade); 62e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu } 63e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu 64e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu /** 65e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * Verify that a wrong password notification will be generated/pushed when a wrong password 66e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * error is detected for the current connection. 67e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * 68e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * @throws Exception 69e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu */ 70e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu @Test 71e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu public void onWrongPasswordError() throws Exception { 72e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu when(mFrameworkFacade.makeNotificationBuilder(any(), 73e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu eq(SystemNotificationChannels.NETWORK_ALERTS))).thenReturn(mNotificationBuilder); 74e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu mWrongPassNotifier.onWrongPasswordError(TEST_SSID); 75e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu verify(mNotificationManager).notify(eq(WrongPasswordNotifier.NOTIFICATION_ID), any()); 7629da253de531bde521dc6fb1cd727c099f1e74caPeter Qiu ArgumentCaptor<Intent> intent = ArgumentCaptor.forClass(Intent.class); 7729da253de531bde521dc6fb1cd727c099f1e74caPeter Qiu verify(mFrameworkFacade).getActivity( 7829da253de531bde521dc6fb1cd727c099f1e74caPeter Qiu any(Context.class), anyInt(), intent.capture(), anyInt()); 7929da253de531bde521dc6fb1cd727c099f1e74caPeter Qiu assertEquals(Settings.ACTION_WIFI_SETTINGS, intent.getValue().getAction()); 8029da253de531bde521dc6fb1cd727c099f1e74caPeter Qiu assertEquals(TEST_SSID, intent.getValue().getStringExtra("wifi_start_connect_ssid")); 81e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu } 82e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu 83e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu /** 84e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * Verify that we will attempt to dismiss the wrong password notification when starting a new 85e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * connection attempt with the previous connection resulting in a wrong password error. 86e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * 87e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * @throws Exception 88e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu */ 89e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu @Test 90e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu public void onNewConnectionAttemptWithPreviousWrongPasswordError() throws Exception { 91e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu onWrongPasswordError(); 92e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu reset(mNotificationManager); 93e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu 94e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu mWrongPassNotifier.onNewConnectionAttempt(); 95e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu verify(mNotificationManager).cancel(any(), eq(WrongPasswordNotifier.NOTIFICATION_ID)); 96e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu } 97e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu 98e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu /** 99e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * Verify that we don't attempt to dismiss the wrong password notification when starting a new 100e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * connection attempt with the previous connection not resulting in a wrong password error. 101e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * 102e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu * @throws Exception 103e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu */ 104e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu @Test 105e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu public void onNewConnectionAttemptWithoutPreviousWrongPasswordError() throws Exception { 106e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu mWrongPassNotifier.onNewConnectionAttempt(); 107e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu verify(mNotificationManager, never()).cancel(any(), anyInt()); 108e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu } 109e81e74137b3da0254cc7d77a5773ea9bff67d5f8Peter Qiu} 110