summaryrefslogtreecommitdiff
path: root/lowpan/tests/java/android/net/lowpan/LowpanInterfaceTest.java
blob: a495d3d7a7848bf4f85588aba17be306cbfecf60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net.lowpan;

import static org.mockito.Mockito.*;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Handler;
import android.os.IBinder;
import android.os.test.TestLooper;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/** Unit tests for android.net.lowpan.LowpanInterface. */
@RunWith(AndroidJUnit4.class)
@SmallTest
public class LowpanInterfaceTest {
    private static final String TEST_PACKAGE_NAME = "TestPackage";

    @Mock Context mContext;
    @Mock ILowpanInterface mLowpanInterfaceService;
    @Mock IBinder mLowpanInterfaceBinder;
    @Mock ApplicationInfo mApplicationInfo;
    @Mock IBinder mAppBinder;
    @Mock LowpanInterface.Callback mLowpanInterfaceCallback;

    private Handler mHandler;
    private final TestLooper mTestLooper = new TestLooper();
    private ILowpanInterfaceListener mInterfaceListener;
    private LowpanInterface mLowpanInterface;
    private Map<String, Object> mPropertyMap;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo);
        when(mContext.getOpPackageName()).thenReturn(TEST_PACKAGE_NAME);
        when(mLowpanInterfaceService.getName()).thenReturn("wpan0");
        when(mLowpanInterfaceService.asBinder()).thenReturn(mLowpanInterfaceBinder);

        mLowpanInterface =
                new LowpanInterface(mContext, mLowpanInterfaceService, mTestLooper.getLooper());
    }

    @Test
    public void testStateChangedCallback() throws Exception {
        // Register our callback
        mLowpanInterface.registerCallback(mLowpanInterfaceCallback);

        // Verify a listener was added
        verify(mLowpanInterfaceService)
                .addListener(
                        argThat(
                                listener -> {
                                    mInterfaceListener = listener;
                                    return listener instanceof ILowpanInterfaceListener;
                                }));

        // Change some properties
        mInterfaceListener.onStateChanged(LowpanInterface.STATE_OFFLINE);
        mTestLooper.dispatchAll();

        // Verify that the property was changed
        verify(mLowpanInterfaceCallback)
                .onStateChanged(
                        argThat(stateString -> stateString.equals(LowpanInterface.STATE_OFFLINE)));
    }
}