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 
17 package android.net.lowpan;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.Mockito.*;
23 
24 import android.content.Context;
25 import android.content.pm.ApplicationInfo;
26 import android.os.Handler;
27 import android.os.IBinder;
28 import android.os.test.TestLooper;
29 import android.test.suitebuilder.annotation.SmallTest;
30 
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 
39 /** Unit tests for android.net.lowpan.LowpanManager. */
40 @RunWith(AndroidJUnit4.class)
41 @SmallTest
42 public class LowpanManagerTest {
43     private static final String TEST_PACKAGE_NAME = "TestPackage";
44 
45     @Mock Context mContext;
46     @Mock ILowpanManager mLowpanService;
47     @Mock ILowpanInterface mLowpanInterfaceService;
48     @Mock IBinder mLowpanInterfaceBinder;
49     @Mock ApplicationInfo mApplicationInfo;
50     @Mock IBinder mAppBinder;
51     @Mock LowpanManager.Callback mLowpanManagerCallback;
52 
53     private Handler mHandler;
54     private final TestLooper mTestLooper = new TestLooper();
55     private LowpanManager mLowpanManager;
56 
57     private ILowpanManagerListener mManagerListener;
58     private LowpanInterface mLowpanInterface;
59 
60     @Before
setUp()61     public void setUp() throws Exception {
62         MockitoAnnotations.initMocks(this);
63         when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo);
64         when(mContext.getOpPackageName()).thenReturn(TEST_PACKAGE_NAME);
65 
66         mLowpanManager = new LowpanManager(mContext, mLowpanService, mTestLooper.getLooper());
67     }
68 
69     @Test
testGetEmptyInterfaceList()70     public void testGetEmptyInterfaceList() throws Exception {
71         when(mLowpanService.getInterfaceList()).thenReturn(new String[0]);
72         assertTrue(mLowpanManager.getInterfaceList().length == 0);
73         assertTrue(mLowpanManager.getInterface() == null);
74     }
75 
76     @Test
testGetInterfaceList()77     public void testGetInterfaceList() throws Exception {
78         when(mLowpanService.getInterfaceList()).thenReturn(new String[] {"wpan0"});
79         when(mLowpanService.getInterface("wpan0")).thenReturn(mLowpanInterfaceService);
80         when(mLowpanInterfaceService.getName()).thenReturn("wpan0");
81         when(mLowpanInterfaceService.asBinder()).thenReturn(mLowpanInterfaceBinder);
82         assertEquals(mLowpanManager.getInterfaceList().length, 1);
83 
84         LowpanInterface iface = mLowpanManager.getInterface();
85         assertNotNull(iface);
86         assertEquals(iface.getName(), "wpan0");
87     }
88 
89     @Test
testRegisterCallback()90     public void testRegisterCallback() throws Exception {
91         when(mLowpanInterfaceService.getName()).thenReturn("wpan0");
92         when(mLowpanInterfaceService.asBinder()).thenReturn(mLowpanInterfaceBinder);
93 
94         // Register our callback
95         mLowpanManager.registerCallback(mLowpanManagerCallback);
96 
97         // Verify a listener was added
98         verify(mLowpanService)
99                 .addListener(
100                         argThat(
101                                 listener -> {
102                                     mManagerListener = listener;
103                                     return listener instanceof ILowpanManagerListener;
104                                 }));
105 
106         // Add an interface
107         mManagerListener.onInterfaceAdded(mLowpanInterfaceService);
108         mTestLooper.dispatchAll();
109 
110         // Verify that the interface was added
111         verify(mLowpanManagerCallback)
112                 .onInterfaceAdded(
113                         argThat(
114                                 iface -> {
115                                     mLowpanInterface = iface;
116                                     return iface instanceof LowpanInterface;
117                                 }));
118         verifyNoMoreInteractions(mLowpanManagerCallback);
119 
120         // This check causes the test to fail with a weird error, but I'm not sure why.
121         assertEquals(mLowpanInterface.getService(), mLowpanInterfaceService);
122 
123         // Verify that calling getInterface on the LowpanManager object will yield the same
124         // LowpanInterface object.
125         when(mLowpanService.getInterfaceList()).thenReturn(new String[] {"wpan0"});
126         when(mLowpanService.getInterface("wpan0")).thenReturn(mLowpanInterfaceService);
127         assertEquals(mLowpanManager.getInterface(), mLowpanInterface);
128 
129         // Remove the service
130         mManagerListener.onInterfaceRemoved(mLowpanInterfaceService);
131         mTestLooper.dispatchAll();
132 
133         // Verify that the interface was removed
134         verify(mLowpanManagerCallback).onInterfaceRemoved(mLowpanInterface);
135     }
136 
137     @Test
testUnregisterCallback()138     public void testUnregisterCallback() throws Exception {
139         when(mLowpanInterfaceService.getName()).thenReturn("wpan0");
140         when(mLowpanInterfaceService.asBinder()).thenReturn(mLowpanInterfaceBinder);
141 
142         // Register our callback
143         mLowpanManager.registerCallback(mLowpanManagerCallback);
144 
145         // Verify a listener was added
146         verify(mLowpanService)
147                 .addListener(
148                         argThat(
149                                 listener -> {
150                                     mManagerListener = listener;
151                                     return listener instanceof ILowpanManagerListener;
152                                 }));
153 
154         // Add an interface
155         mManagerListener.onInterfaceAdded(mLowpanInterfaceService);
156         mTestLooper.dispatchAll();
157 
158         // Verify that the interface was added
159         verify(mLowpanManagerCallback)
160                 .onInterfaceAdded(
161                         argThat(
162                                 iface -> {
163                                     mLowpanInterface = iface;
164                                     return iface instanceof LowpanInterface;
165                                 }));
166         verifyNoMoreInteractions(mLowpanManagerCallback);
167 
168         // Unregister our callback
169         mLowpanManager.unregisterCallback(mLowpanManagerCallback);
170 
171         // Verify the listener was removed
172         verify(mLowpanService).removeListener(mManagerListener);
173 
174         // Verify that the callback wasn't invoked.
175         verifyNoMoreInteractions(mLowpanManagerCallback);
176     }
177 }
178