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