Fabian Kozynski | bdd3d3c | 2019-03-05 13:36:26 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
| 17 | package com.android.systemui.statusbar.policy; |
| 18 | |
| 19 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 20 | import static org.mockito.ArgumentMatchers.anyInt; |
| 21 | import static org.mockito.ArgumentMatchers.eq; |
| 22 | import static org.mockito.Mockito.any; |
| 23 | import static org.mockito.Mockito.doAnswer; |
| 24 | import static org.mockito.Mockito.never; |
| 25 | import static org.mockito.Mockito.times; |
| 26 | import static org.mockito.Mockito.verify; |
| 27 | |
| 28 | import android.net.ConnectivityManager; |
| 29 | import android.net.wifi.WifiManager; |
| 30 | import android.os.Handler; |
| 31 | import android.testing.AndroidTestingRunner; |
| 32 | import android.testing.TestableLooper; |
| 33 | |
| 34 | import androidx.test.filters.SmallTest; |
| 35 | |
| 36 | import com.android.systemui.SysuiTestCase; |
| 37 | |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | import org.mockito.Mock; |
| 42 | import org.mockito.MockitoAnnotations; |
| 43 | import org.mockito.invocation.InvocationOnMock; |
| 44 | |
| 45 | @SmallTest |
| 46 | @RunWith(AndroidTestingRunner.class) |
| 47 | @TestableLooper.RunWithLooper |
| 48 | public class HotspotControllerImplTest extends SysuiTestCase { |
| 49 | |
| 50 | @Mock |
| 51 | private ConnectivityManager mConnectivityManager; |
| 52 | @Mock |
| 53 | private WifiManager mWifiManager; |
| 54 | @Mock |
| 55 | private HotspotController.Callback mCallback1; |
| 56 | @Mock |
| 57 | private HotspotController.Callback mCallback2; |
| 58 | private HotspotControllerImpl mController; |
| 59 | private TestableLooper mLooper; |
| 60 | |
| 61 | @Before |
| 62 | public void setUp() { |
| 63 | MockitoAnnotations.initMocks(this); |
| 64 | mLooper = TestableLooper.get(this); |
| 65 | |
| 66 | mContext.addMockSystemService(ConnectivityManager.class, mConnectivityManager); |
| 67 | mContext.addMockSystemService(WifiManager.class, mWifiManager); |
| 68 | |
| 69 | doAnswer((InvocationOnMock invocation) -> { |
| 70 | ((WifiManager.SoftApCallback) invocation.getArgument(0)).onNumClientsChanged(1); |
| 71 | return null; |
| 72 | }).when(mWifiManager).registerSoftApCallback(any(WifiManager.SoftApCallback.class), |
| 73 | any(Handler.class)); |
| 74 | |
| 75 | mController = new HotspotControllerImpl(mContext, new Handler(mLooper.getLooper())); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testAddingTwoCallbacksRegistersToWifiManagerOnce() { |
| 80 | mController.addCallback(mCallback1); |
| 81 | mController.addCallback(mCallback2); |
| 82 | |
| 83 | verify(mWifiManager, times(1)).registerSoftApCallback(eq(mController), any()); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void testAddingCallbacksDoesntUpdateAll() { |
| 88 | mController.addCallback(mCallback1); |
| 89 | mController.addCallback(mCallback2); |
| 90 | |
| 91 | mLooper.processAllMessages(); |
| 92 | // Each callback should be updated only once |
| 93 | verify(mCallback1, times(1)).onHotspotChanged(anyBoolean(), anyInt()); |
| 94 | verify(mCallback2, times(1)).onHotspotChanged(anyBoolean(), anyInt()); |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void testRemovingTwoCallbacksUnegistersToWifiManagerOnce() { |
| 99 | mController.addCallback(mCallback1); |
| 100 | mController.addCallback(mCallback2); |
| 101 | |
| 102 | mController.removeCallback(mCallback1); |
| 103 | mController.removeCallback(mCallback2); |
| 104 | |
| 105 | verify(mWifiManager, times(1)).unregisterSoftApCallback(mController); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testDoNotUnregisterIfRemainingCallbacks() { |
| 110 | mController.addCallback(mCallback1); |
| 111 | mController.addCallback(mCallback2); |
| 112 | |
| 113 | verify(mWifiManager, never()).unregisterSoftApCallback(any()); |
| 114 | } |
| 115 | |
| 116 | } |