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 */
16package com.android.systemui.statusbar.policy;
17
18import android.os.HandlerThread;
19import android.telephony.SubscriptionInfo;
20import android.test.AndroidTestCase;
21import android.test.suitebuilder.annotation.SmallTest;
22import com.android.systemui.R;
23import com.android.systemui.statusbar.policy.NetworkController.EmergencyListener;
24import com.android.systemui.statusbar.policy.NetworkController.IconState;
25import com.android.systemui.statusbar.policy.NetworkController.SignalCallback;
26import org.mockito.ArgumentCaptor;
27import org.mockito.Mock;
28import org.mockito.Mockito;
29import org.mockito.MockitoAnnotations;
30
31import java.util.ArrayList;
32import java.util.List;
33
34@SmallTest
35public class CallbackHandlerTest extends AndroidTestCase {
36
37    private CallbackHandler mHandler;
38    private HandlerThread mHandlerThread;
39
40    @Mock
41    private EmergencyListener mEmengencyListener;
42    @Mock
43    private SignalCallback mSignalCallback;
44
45    @Override
46    protected void setUp() throws Exception {
47        super.setUp();
48
49        mHandlerThread = new HandlerThread("TestThread");
50        mHandlerThread.start();
51        mHandler = new CallbackHandler(mHandlerThread.getLooper());
52
53        MockitoAnnotations.initMocks(this);
54        mHandler.setListening(mEmengencyListener, true);
55        mHandler.setListening(mSignalCallback, true);
56    }
57
58    public void testEmergencyListener() {
59        mHandler.setEmergencyCallsOnly(true);
60        waitForCallbacks();
61
62        ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class);
63        Mockito.verify(mEmengencyListener).setEmergencyCallsOnly(captor.capture());
64        assertTrue(captor.getValue());
65    }
66
67    public void testSignalCallback_setWifiIndicators() {
68        boolean enabled = true;
69        IconState status = new IconState(true, 0, "");
70        IconState qs = new IconState(true, 1, "");
71        boolean in = true;
72        boolean out = true;
73        String description = "Test";
74        mHandler.setWifiIndicators(enabled, status, qs, in, out, description);
75        waitForCallbacks();
76
77        ArgumentCaptor<Boolean> enableArg = ArgumentCaptor.forClass(Boolean.class);
78        ArgumentCaptor<IconState> statusArg = ArgumentCaptor.forClass(IconState.class);
79        ArgumentCaptor<IconState> qsArg = ArgumentCaptor.forClass(IconState.class);
80        ArgumentCaptor<Boolean> inArg = ArgumentCaptor.forClass(Boolean.class);
81        ArgumentCaptor<Boolean> outArg = ArgumentCaptor.forClass(Boolean.class);
82        ArgumentCaptor<String> descArg = ArgumentCaptor.forClass(String.class);
83        Mockito.verify(mSignalCallback).setWifiIndicators(enableArg.capture(),
84                statusArg.capture(), qsArg.capture(), inArg.capture(), outArg.capture(),
85                descArg.capture());
86        assertEquals(enabled, (boolean) enableArg.getValue());
87        assertEquals(status, statusArg.getValue());
88        assertEquals(qs, qsArg.getValue());
89        assertEquals(in, (boolean) inArg.getValue());
90        assertEquals(out, (boolean) outArg.getValue());
91        assertEquals(description, descArg.getValue());
92    }
93
94    public void testSignalCallback_setMobileDataIndicators() {
95        IconState status = new IconState(true, 0, "");
96        IconState qs = new IconState(true, 1, "");
97        boolean in = true;
98        boolean out = true;
99        String typeDescription = "Test 1";
100        String description = "Test 2";
101        int type = R.drawable.stat_sys_data_fully_connected_1x;
102        int qsType = R.drawable.ic_qs_signal_1x;
103        boolean wide = true;
104        int subId = 5;
105        mHandler.setMobileDataIndicators(status, qs, type, qsType, in, out, typeDescription,
106                description, wide, subId);
107        waitForCallbacks();
108
109        ArgumentCaptor<IconState> statusArg = ArgumentCaptor.forClass(IconState.class);
110        ArgumentCaptor<IconState> qsArg = ArgumentCaptor.forClass(IconState.class);
111        ArgumentCaptor<Integer> typeIconArg = ArgumentCaptor.forClass(Integer.class);
112        ArgumentCaptor<Integer> qsTypeIconArg = ArgumentCaptor.forClass(Integer.class);
113        ArgumentCaptor<Boolean> inArg = ArgumentCaptor.forClass(Boolean.class);
114        ArgumentCaptor<Boolean> outArg = ArgumentCaptor.forClass(Boolean.class);
115        ArgumentCaptor<String> typeContentArg = ArgumentCaptor.forClass(String.class);
116        ArgumentCaptor<String> descArg = ArgumentCaptor.forClass(String.class);
117        ArgumentCaptor<Boolean> wideArg = ArgumentCaptor.forClass(Boolean.class);
118        ArgumentCaptor<Integer> subIdArg = ArgumentCaptor.forClass(Integer.class);
119        Mockito.verify(mSignalCallback).setMobileDataIndicators(statusArg.capture(),
120                qsArg.capture(), typeIconArg.capture(), qsTypeIconArg.capture(), inArg.capture(),
121                outArg.capture(), typeContentArg.capture(), descArg.capture(), wideArg.capture(),
122                subIdArg.capture());
123        assertEquals(status, statusArg.getValue());
124        assertEquals(qs, qsArg.getValue());
125        assertEquals(type, (int) typeIconArg.getValue());
126        assertEquals(qsType, (int) qsTypeIconArg.getValue());
127        assertEquals(in, (boolean) inArg.getValue());
128        assertEquals(out, (boolean) outArg.getValue());
129        assertEquals(typeDescription, typeContentArg.getValue());
130        assertEquals(description, descArg.getValue());
131        assertEquals(wide, (boolean) wideArg.getValue());
132        assertEquals(subId, (int) subIdArg.getValue());
133    }
134
135    @SuppressWarnings("unchecked")
136    public void testSignalCallback_setSubs() {
137        List<SubscriptionInfo> subs = new ArrayList<>();
138        mHandler.setSubs(subs);
139        waitForCallbacks();
140
141        ArgumentCaptor<ArrayList> subsArg = ArgumentCaptor.forClass(ArrayList.class);
142        Mockito.verify(mSignalCallback).setSubs(subsArg.capture());
143        assertTrue(subs == subsArg.getValue());
144    }
145
146    public void testSignalCallback_setNoSims() {
147        boolean noSims = true;
148        mHandler.setNoSims(noSims);
149        waitForCallbacks();
150
151        ArgumentCaptor<Boolean> noSimsArg = ArgumentCaptor.forClass(Boolean.class);
152        Mockito.verify(mSignalCallback).setNoSims(noSimsArg.capture());
153        assertEquals(noSims, (boolean) noSimsArg.getValue());
154    }
155
156    public void testSignalCallback_setEthernetIndicators() {
157        IconState state = new IconState(true, R.drawable.stat_sys_ethernet, "Test Description");
158        mHandler.setEthernetIndicators(state);
159        waitForCallbacks();
160
161        ArgumentCaptor<IconState> iconArg = ArgumentCaptor.forClass(IconState.class);
162        Mockito.verify(mSignalCallback).setEthernetIndicators(iconArg.capture());
163        assertEquals(state, iconArg.getValue());
164    }
165
166    public void testSignalCallback_setIsAirplaneMode() {
167        IconState state = new IconState(true, R.drawable.stat_sys_airplane_mode, "Test Description");
168        mHandler.setIsAirplaneMode(state);
169        waitForCallbacks();
170
171        ArgumentCaptor<IconState> iconArg = ArgumentCaptor.forClass(IconState.class);
172        Mockito.verify(mSignalCallback).setIsAirplaneMode(iconArg.capture());
173        assertEquals(state, iconArg.getValue());
174    }
175
176    private void waitForCallbacks() {
177        mHandlerThread.quitSafely();
178        try {
179            mHandlerThread.join();
180        } catch (InterruptedException e) {
181        }
182    }
183
184}
185