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.mockito.Mockito.*;
20 
21 import android.content.Context;
22 import android.content.pm.ApplicationInfo;
23 import android.os.Handler;
24 import android.os.IBinder;
25 import android.os.test.TestLooper;
26 import android.test.suitebuilder.annotation.SmallTest;
27 
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 
36 import java.util.Map;
37 
38 /** Unit tests for android.net.lowpan.LowpanInterface. */
39 @RunWith(AndroidJUnit4.class)
40 @SmallTest
41 public class LowpanInterfaceTest {
42     private static final String TEST_PACKAGE_NAME = "TestPackage";
43 
44     @Mock Context mContext;
45     @Mock ILowpanInterface mLowpanInterfaceService;
46     @Mock IBinder mLowpanInterfaceBinder;
47     @Mock ApplicationInfo mApplicationInfo;
48     @Mock IBinder mAppBinder;
49     @Mock LowpanInterface.Callback mLowpanInterfaceCallback;
50 
51     private Handler mHandler;
52     private final TestLooper mTestLooper = new TestLooper();
53     private ILowpanInterfaceListener mInterfaceListener;
54     private LowpanInterface mLowpanInterface;
55     private Map<String, Object> mPropertyMap;
56 
57     @Before
setUp()58     public void setUp() throws Exception {
59         MockitoAnnotations.initMocks(this);
60         when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo);
61         when(mContext.getOpPackageName()).thenReturn(TEST_PACKAGE_NAME);
62         when(mLowpanInterfaceService.getName()).thenReturn("wpan0");
63         when(mLowpanInterfaceService.asBinder()).thenReturn(mLowpanInterfaceBinder);
64 
65         mLowpanInterface =
66                 new LowpanInterface(mContext, mLowpanInterfaceService, mTestLooper.getLooper());
67     }
68 
69     @Test
testStateChangedCallback()70     public void testStateChangedCallback() throws Exception {
71         // Register our callback
72         mLowpanInterface.registerCallback(mLowpanInterfaceCallback);
73 
74         // Verify a listener was added
75         verify(mLowpanInterfaceService)
76                 .addListener(
77                         argThat(
78                                 listener -> {
79                                     mInterfaceListener = listener;
80                                     return listener instanceof ILowpanInterfaceListener;
81                                 }));
82 
83         // Change some properties
84         mInterfaceListener.onStateChanged(LowpanInterface.STATE_OFFLINE);
85         mTestLooper.dispatchAll();
86 
87         // Verify that the property was changed
88         verify(mLowpanInterfaceCallback)
89                 .onStateChanged(
90                         argThat(stateString -> stateString.equals(LowpanInterface.STATE_OFFLINE)));
91     }
92 }
93