WifiAwareNativeManagerTest.java revision da7d73da44a47933cfc99660c2d247c24fd72544
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 com.android.server.wifi.aware;
18
19import static org.hamcrest.core.IsEqual.equalTo;
20import static org.hamcrest.core.IsNull.nullValue;
21import static org.mockito.Matchers.any;
22import static org.mockito.Matchers.eq;
23import static org.mockito.Mockito.inOrder;
24import static org.mockito.Mockito.verifyNoMoreInteractions;
25import static org.mockito.Mockito.when;
26
27import android.hardware.wifi.V1_0.IWifiNanIface;
28import android.hardware.wifi.V1_0.IWifiNanIfaceEventCallback;
29import android.hardware.wifi.V1_0.IfaceType;
30import android.hardware.wifi.V1_0.WifiStatus;
31import android.hardware.wifi.V1_0.WifiStatusCode;
32import android.os.Looper;
33
34import com.android.server.wifi.DisabledForUpdateToAnyMatcher;
35import com.android.server.wifi.HalDeviceManager;
36
37import org.junit.Before;
38import org.junit.Rule;
39import org.junit.Test;
40import org.junit.rules.ErrorCollector;
41import org.mockito.ArgumentCaptor;
42import org.mockito.InOrder;
43import org.mockito.Mock;
44import org.mockito.MockitoAnnotations;
45
46/**
47 * Unit test harness for WifiAwareNativeManager.
48 */
49public class WifiAwareNativeManagerTest {
50    private WifiAwareNativeManager mDut;
51    @Mock private WifiAwareStateManager mWifiAwareStateManagerMock;
52    @Mock private HalDeviceManager mHalDeviceManager;
53    @Mock private WifiAwareNativeCallback mWifiAwareNativeCallback;
54    @Mock private IWifiNanIface mWifiNanIfaceMock;
55    private ArgumentCaptor<HalDeviceManager.ManagerStatusListener> mManagerStatusListenerCaptor =
56            ArgumentCaptor.forClass(HalDeviceManager.ManagerStatusListener.class);
57    private ArgumentCaptor<HalDeviceManager.InterfaceDestroyedListener>
58            mDestroyedListenerCaptor = ArgumentCaptor.forClass(
59            HalDeviceManager.InterfaceDestroyedListener.class);
60    private ArgumentCaptor<HalDeviceManager.InterfaceAvailableForRequestListener>
61            mAvailListenerCaptor = ArgumentCaptor.forClass(
62            HalDeviceManager.InterfaceAvailableForRequestListener.class);
63    private InOrder mInOrder;
64    @Rule public ErrorCollector collector = new ErrorCollector();
65
66    private WifiStatus mStatusOk;
67
68    @Before
69    public void setUp() throws Exception {
70        MockitoAnnotations.initMocks(this);
71
72        mStatusOk = new WifiStatus();
73        mStatusOk.code = WifiStatusCode.SUCCESS;
74
75        when(mWifiNanIfaceMock.registerEventCallback(
76                any(IWifiNanIfaceEventCallback.class))).thenReturn(mStatusOk);
77
78        mDut = new WifiAwareNativeManager(mWifiAwareStateManagerMock,
79                mHalDeviceManager, mWifiAwareNativeCallback);
80
81        mInOrder = inOrder(mWifiAwareStateManagerMock, mHalDeviceManager);
82    }
83
84    /**
85     * Test the control flow of the manager:
86     * 1. onStatusChange (ready/started)
87     * 2. null NAN iface
88     * 3. onAvailableForRequest
89     * 4. non-null NAN iface -> enableUsage
90     * 5. onStatusChange (!started) -> disableUsage
91     * 6. onStatusChange (ready/started)
92     * 7. non-null NAN iface -> enableUsage
93     * 8. onDestroyed -> disableUsage
94     * 9. onStatusChange (!started)
95     */
96    @DisabledForUpdateToAnyMatcher
97    @Test
98    public void testControlFlow() {
99        // configure HalDeviceManager as ready/wifi started
100        when(mHalDeviceManager.isReady()).thenReturn(true);
101        when(mHalDeviceManager.isStarted()).thenReturn(true);
102
103        // validate (and capture) that register manage status callback
104        mInOrder.verify(mHalDeviceManager).registerStatusListener(
105                mManagerStatusListenerCaptor.capture(), any(Looper.class));
106
107        // 1 & 2 onStatusChange (ready/started): validate that trying to get a NAN interface
108        // (make sure gets a NULL)
109        when(mHalDeviceManager.createNanIface(
110                any(HalDeviceManager.InterfaceDestroyedListener.class),
111                any(Looper.class)))
112                .thenReturn(null);
113
114        mManagerStatusListenerCaptor.getValue().onStatusChanged();
115        mInOrder.verify(mHalDeviceManager).registerInterfaceAvailableForRequestListener(
116                eq(IfaceType.NAN), mAvailListenerCaptor.capture(), any(Looper.class));
117        mAvailListenerCaptor.getValue().onAvailableForRequest();
118
119        mInOrder.verify(mHalDeviceManager).createNanIface(
120                mDestroyedListenerCaptor.capture(), any(Looper.class));
121        collector.checkThat("null interface", mDut.getWifiNanIface(), nullValue());
122
123        // 3 & 4 onAvailableForRequest + non-null return value: validate that enables usage
124        when(mHalDeviceManager.createNanIface(
125                any(HalDeviceManager.InterfaceDestroyedListener.class),
126                any(Looper.class)))
127                .thenReturn(mWifiNanIfaceMock);
128
129        mAvailListenerCaptor.getValue().onAvailableForRequest();
130
131        mInOrder.verify(mHalDeviceManager).createNanIface(
132                mDestroyedListenerCaptor.capture(), any(Looper.class));
133        mInOrder.verify(mWifiAwareStateManagerMock).enableUsage();
134        collector.checkThat("non-null interface", mDut.getWifiNanIface(),
135                equalTo(mWifiNanIfaceMock));
136
137        // 5 onStatusChange (!started): disable usage
138        when(mHalDeviceManager.isStarted()).thenReturn(false);
139        mManagerStatusListenerCaptor.getValue().onStatusChanged();
140
141        mInOrder.verify(mWifiAwareStateManagerMock).disableUsage();
142        collector.checkThat("null interface", mDut.getWifiNanIface(), nullValue());
143
144        // 6 & 7 onStatusChange (ready/started) + non-null NAN interface: enable usage
145        when(mHalDeviceManager.isStarted()).thenReturn(true);
146        mManagerStatusListenerCaptor.getValue().onStatusChanged();
147
148        mManagerStatusListenerCaptor.getValue().onStatusChanged();
149        mInOrder.verify(mHalDeviceManager).registerInterfaceAvailableForRequestListener(
150                eq(IfaceType.NAN), mAvailListenerCaptor.capture(), any(Looper.class));
151        mAvailListenerCaptor.getValue().onAvailableForRequest();
152
153        mInOrder.verify(mHalDeviceManager).createNanIface(
154                mDestroyedListenerCaptor.capture(), any(Looper.class));
155        mInOrder.verify(mWifiAwareStateManagerMock).enableUsage();
156        collector.checkThat("non-null interface", mDut.getWifiNanIface(),
157                equalTo(mWifiNanIfaceMock));
158
159        // 8 onDestroyed: disable usage
160        mDestroyedListenerCaptor.getValue().onDestroyed();
161
162        mInOrder.verify(mWifiAwareStateManagerMock).disableUsage();
163        collector.checkThat("null interface", mDut.getWifiNanIface(), nullValue());
164
165        // 9 onStatusChange (!started): nothing more happens
166        when(mHalDeviceManager.isStarted()).thenReturn(false);
167        mManagerStatusListenerCaptor.getValue().onStatusChanged();
168
169        verifyNoMoreInteractions(mWifiAwareStateManagerMock);
170    }
171}
172