1 /*
2  * Copyright (C) 2015 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 package com.android.systemui.statusbar.connectivity;
17 
18 import static junit.framework.Assert.assertEquals;
19 import static junit.framework.Assert.assertTrue;
20 
21 import static org.mockito.Matchers.eq;
22 
23 import android.os.HandlerThread;
24 import android.telephony.SubscriptionInfo;
25 
26 import androidx.test.filters.SmallTest;
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import com.android.settingslib.mobile.TelephonyIcons;
30 import com.android.systemui.SysuiTestCase;
31 import com.android.systemui.res.R;
32 import com.android.systemui.statusbar.connectivity.NetworkController.EmergencyListener;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.ArgumentCaptor;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 
42 import java.util.ArrayList;
43 import java.util.List;
44 
45 @SmallTest
46 @RunWith(AndroidJUnit4.class)
47 public class CallbackHandlerTest extends SysuiTestCase {
48 
49     private CallbackHandler mHandler;
50     private HandlerThread mHandlerThread;
51 
52     @Mock
53     private EmergencyListener mEmengencyListener;
54     @Mock
55     private SignalCallback mSignalCallback;
56 
57     @Before
setUp()58     public void setUp() throws Exception {
59         mHandlerThread = new HandlerThread("TestThread");
60         mHandlerThread.start();
61         mHandler = new CallbackHandler(mHandlerThread.getLooper());
62 
63         MockitoAnnotations.initMocks(this);
64         mHandler.setListening(mEmengencyListener, true);
65         mHandler.setListening(mSignalCallback, true);
66     }
67 
68     @Test
testEmergencyListener()69     public void testEmergencyListener() {
70         mHandler.setEmergencyCallsOnly(true);
71         waitForCallbacks();
72 
73         ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class);
74         Mockito.verify(mEmengencyListener).setEmergencyCallsOnly(captor.capture());
75         assertTrue(captor.getValue());
76     }
77 
78     @Test
testSignalCallback_setWifiIndicators()79     public void testSignalCallback_setWifiIndicators() {
80         boolean enabled = true;
81         IconState status = new IconState(true, 0, "");
82         IconState qs = new IconState(true, 1, "");
83         boolean in = true;
84         boolean out = true;
85         String description = "Test";
86         String secondaryLabel = "Secondary label";
87         WifiIndicators indicators = new WifiIndicators(
88                 enabled, status, qs, in, out, description, true, secondaryLabel);
89         mHandler.setWifiIndicators(indicators);
90         waitForCallbacks();
91 
92         ArgumentCaptor<WifiIndicators> indicatorArg =
93                 ArgumentCaptor.forClass(WifiIndicators.class);
94         Mockito.verify(mSignalCallback).setWifiIndicators(indicatorArg.capture());
95         WifiIndicators expected = indicatorArg.getValue();
96 
97         assertEquals(enabled, expected.enabled);
98         assertEquals(status, expected.statusIcon);
99         assertEquals(qs, expected.qsIcon);
100         assertEquals(in, expected.activityIn);
101         assertEquals(out, expected.activityOut);
102         assertEquals(description, expected.description);
103         assertTrue(expected.isTransient);
104         assertEquals(secondaryLabel, expected.statusLabel);
105     }
106 
107     @Test
testSignalCallback_setMobileDataIndicators()108     public void testSignalCallback_setMobileDataIndicators() {
109         IconState status = new IconState(true, 0, "");
110         IconState qs = new IconState(true, 1, "");
111         boolean in = true;
112         boolean out = true;
113         CharSequence typeDescription = "Test 1";
114         CharSequence typeDescriptionHtml = "<b>Test 1</b>";
115         CharSequence description = "Test 2";
116         int type = TelephonyIcons.ICON_1X;
117         int qsType = TelephonyIcons.ICON_1X;
118         boolean wide = true;
119         int subId = 5;
120         boolean roaming = true;
121         MobileDataIndicators indicators = new MobileDataIndicators(
122                 status, qs, type, qsType, in, out, typeDescription,
123                 typeDescriptionHtml, description, subId, roaming, true);
124         mHandler.setMobileDataIndicators(indicators);
125         waitForCallbacks();
126 
127         ArgumentCaptor<MobileDataIndicators> indicatorArg =
128                 ArgumentCaptor.forClass(MobileDataIndicators.class);
129         Mockito.verify(mSignalCallback).setMobileDataIndicators(indicatorArg.capture());
130         MobileDataIndicators expected = indicatorArg.getValue();
131 
132         assertEquals(status, expected.statusIcon);
133         assertEquals(qs, expected.qsIcon);
134         assertEquals(type, expected.statusType);
135         assertEquals(qsType, expected.qsType);
136         assertEquals(in, expected.activityIn);
137         assertEquals(out, expected.activityOut);
138         assertEquals(typeDescription, expected.typeContentDescription);
139         assertEquals(typeDescriptionHtml, expected.typeContentDescriptionHtml);
140         assertEquals(description, expected.qsDescription);
141         assertEquals(subId, expected.subId);
142         assertTrue(expected.roaming);
143         assertTrue(expected.showTriangle);
144     }
145 
146     @SuppressWarnings("unchecked")
147     @Test
testSignalCallback_setSubs()148     public void testSignalCallback_setSubs() {
149         List<SubscriptionInfo> subs = new ArrayList<>();
150         mHandler.setSubs(subs);
151         waitForCallbacks();
152 
153         ArgumentCaptor<ArrayList> subsArg = ArgumentCaptor.forClass(ArrayList.class);
154         Mockito.verify(mSignalCallback).setSubs(subsArg.capture());
155         assertTrue(subs == subsArg.getValue());
156     }
157 
158     @Test
testSignalCallback_setNoSims()159     public void testSignalCallback_setNoSims() {
160         boolean noSims = true;
161         boolean simDetected = false;
162         mHandler.setNoSims(noSims, simDetected);
163         waitForCallbacks();
164 
165         Mockito.verify(mSignalCallback).setNoSims(eq(noSims), eq(simDetected));
166     }
167 
168     @Test
testSignalCallback_setEthernetIndicators()169     public void testSignalCallback_setEthernetIndicators() {
170         IconState state = new IconState(true, R.drawable.stat_sys_ethernet, "Test Description");
171         mHandler.setEthernetIndicators(state);
172         waitForCallbacks();
173 
174         ArgumentCaptor<IconState> iconArg = ArgumentCaptor.forClass(IconState.class);
175         Mockito.verify(mSignalCallback).setEthernetIndicators(iconArg.capture());
176         assertEquals(state, iconArg.getValue());
177     }
178 
179     @Test
testSignalCallback_setIsAirplaneMode()180     public void testSignalCallback_setIsAirplaneMode() {
181         IconState state =
182                 new IconState(true, com.android.settingslib.R.drawable.stat_sys_airplane_mode, "Test Description");
183         mHandler.setIsAirplaneMode(state);
184         waitForCallbacks();
185 
186         ArgumentCaptor<IconState> iconArg = ArgumentCaptor.forClass(IconState.class);
187         Mockito.verify(mSignalCallback).setIsAirplaneMode(iconArg.capture());
188         assertEquals(state, iconArg.getValue());
189     }
190 
waitForCallbacks()191     private void waitForCallbacks() {
192         mHandlerThread.quitSafely();
193         try {
194             mHandlerThread.join();
195         } catch (InterruptedException e) {
196         }
197     }
198 
199 }
200