1956f54b391677d78379729dd14518edddf3c7660Etan Cohen/*
2956f54b391677d78379729dd14518edddf3c7660Etan Cohen * Copyright (C) 2016 The Android Open Source Project
3956f54b391677d78379729dd14518edddf3c7660Etan Cohen *
4956f54b391677d78379729dd14518edddf3c7660Etan Cohen * Licensed under the Apache License, Version 2.0 (the "License");
5956f54b391677d78379729dd14518edddf3c7660Etan Cohen * you may not use this file except in compliance with the License.
6956f54b391677d78379729dd14518edddf3c7660Etan Cohen * You may obtain a copy of the License at
7956f54b391677d78379729dd14518edddf3c7660Etan Cohen *
8956f54b391677d78379729dd14518edddf3c7660Etan Cohen *      http://www.apache.org/licenses/LICENSE-2.0
9956f54b391677d78379729dd14518edddf3c7660Etan Cohen *
10956f54b391677d78379729dd14518edddf3c7660Etan Cohen * Unless required by applicable law or agreed to in writing, software
11956f54b391677d78379729dd14518edddf3c7660Etan Cohen * distributed under the License is distributed on an "AS IS" BASIS,
12956f54b391677d78379729dd14518edddf3c7660Etan Cohen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13956f54b391677d78379729dd14518edddf3c7660Etan Cohen * See the License for the specific language governing permissions and
14956f54b391677d78379729dd14518edddf3c7660Etan Cohen * limitations under the License.
15956f54b391677d78379729dd14518edddf3c7660Etan Cohen */
16956f54b391677d78379729dd14518edddf3c7660Etan Cohen
17956f54b391677d78379729dd14518edddf3c7660Etan Cohenpackage com.android.server.wifi.nan;
18956f54b391677d78379729dd14518edddf3c7660Etan Cohen
19956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport static org.hamcrest.core.IsEqual.equalTo;
2022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohenimport static org.hamcrest.core.IsNull.nullValue;
2122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohenimport static org.junit.Assert.assertTrue;
22956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport static org.mockito.Matchers.anyShort;
23956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport static org.mockito.Matchers.eq;
24956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport static org.mockito.Mockito.inOrder;
25956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport static org.mockito.Mockito.mock;
26956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport static org.mockito.Mockito.verifyNoMoreInteractions;
27956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport static org.mockito.Mockito.verifyZeroInteractions;
28956f54b391677d78379729dd14518edddf3c7660Etan Cohen
29956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport android.net.wifi.nan.ConfigRequest;
30956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport android.net.wifi.nan.IWifiNanEventListener;
31956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport android.net.wifi.nan.IWifiNanSessionListener;
32956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport android.net.wifi.nan.PublishData;
33956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport android.net.wifi.nan.PublishSettings;
34956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport android.net.wifi.nan.SubscribeData;
35956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport android.net.wifi.nan.SubscribeSettings;
36956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport android.net.wifi.nan.WifiNanEventListener;
37956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport android.net.wifi.nan.WifiNanSessionListener;
38956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport android.test.suitebuilder.annotation.SmallTest;
3922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohenimport android.util.SparseArray;
40956f54b391677d78379729dd14518edddf3c7660Etan Cohen
41956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport com.android.server.wifi.MockLooper;
42956f54b391677d78379729dd14518edddf3c7660Etan Cohen
43956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport libcore.util.HexEncoding;
44956f54b391677d78379729dd14518edddf3c7660Etan Cohen
45956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport org.junit.Before;
46956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport org.junit.Rule;
47956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport org.junit.Test;
48956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport org.junit.rules.ErrorCollector;
49956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport org.mockito.ArgumentCaptor;
50956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport org.mockito.InOrder;
51956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport org.mockito.Mock;
52956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport org.mockito.MockitoAnnotations;
53956f54b391677d78379729dd14518edddf3c7660Etan Cohen
54956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport java.lang.reflect.Constructor;
55956f54b391677d78379729dd14518edddf3c7660Etan Cohenimport java.lang.reflect.Field;
56956f54b391677d78379729dd14518edddf3c7660Etan Cohen
57956f54b391677d78379729dd14518edddf3c7660Etan Cohen/**
58956f54b391677d78379729dd14518edddf3c7660Etan Cohen * Unit test harness for WifiNanStateManager.
59956f54b391677d78379729dd14518edddf3c7660Etan Cohen */
60956f54b391677d78379729dd14518edddf3c7660Etan Cohen@SmallTest
61956f54b391677d78379729dd14518edddf3c7660Etan Cohenpublic class WifiNanStateManagerTest {
62956f54b391677d78379729dd14518edddf3c7660Etan Cohen    private MockLooper mMockLooper;
63956f54b391677d78379729dd14518edddf3c7660Etan Cohen    private WifiNanStateManager mDut;
64956f54b391677d78379729dd14518edddf3c7660Etan Cohen    @Mock private WifiNanNative mMockNative;
65956f54b391677d78379729dd14518edddf3c7660Etan Cohen
66956f54b391677d78379729dd14518edddf3c7660Etan Cohen    @Rule
67956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public ErrorCollector collector = new ErrorCollector();
68956f54b391677d78379729dd14518edddf3c7660Etan Cohen
69956f54b391677d78379729dd14518edddf3c7660Etan Cohen    @Before
70956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public void setUp() throws Exception {
71956f54b391677d78379729dd14518edddf3c7660Etan Cohen        MockitoAnnotations.initMocks(this);
72956f54b391677d78379729dd14518edddf3c7660Etan Cohen
73956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper = new MockLooper();
74956f54b391677d78379729dd14518edddf3c7660Etan Cohen
7522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut = installNewNanStateManagerAndResetState();
76956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.start(mMockLooper.getLooper());
77956f54b391677d78379729dd14518edddf3c7660Etan Cohen
78956f54b391677d78379729dd14518edddf3c7660Etan Cohen        installMockWifiNanNative(mMockNative);
79956f54b391677d78379729dd14518edddf3c7660Etan Cohen    }
80956f54b391677d78379729dd14518edddf3c7660Etan Cohen
81956f54b391677d78379729dd14518edddf3c7660Etan Cohen    @Test
82956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public void testNanEventsDelivered() throws Exception {
83956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int uid = 1005;
8422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterLow1 = 5;
8522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterHigh1 = 100;
8622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int masterPref1 = 111;
8722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterLow2 = 7;
8822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterHigh2 = 155;
8922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int masterPref2 = 0;
90956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int reason = WifiNanSessionListener.FAIL_REASON_NO_RESOURCES;
91956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final byte[] someMac = HexEncoding.decode("000102030405".toCharArray(), false);
92956f54b391677d78379729dd14518edddf3c7660Etan Cohen
9322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ConfigRequest configRequest1 = new ConfigRequest.Builder().setClusterLow(clusterLow1)
9422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setClusterHigh(clusterHigh1).setMasterPreference(masterPref1).build();
9522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
9622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ConfigRequest configRequest2 = new ConfigRequest.Builder().setClusterLow(clusterLow2)
9722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setClusterHigh(clusterHigh2).setMasterPreference(masterPref2).build();
9822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
99956f54b391677d78379729dd14518edddf3c7660Etan Cohen        IWifiNanEventListener mockListener = mock(IWifiNanEventListener.class);
10022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ArgumentCaptor<Short> transactionId = ArgumentCaptor.forClass(Short.class);
10122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        InOrder inOrder = inOrder(mockListener, mMockNative);
102956f54b391677d78379729dd14518edddf3c7660Etan Cohen
103956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.connect(uid, mockListener,
104956f54b391677d78379729dd14518edddf3c7660Etan Cohen                WifiNanEventListener.LISTEN_CONFIG_COMPLETED
105956f54b391677d78379729dd14518edddf3c7660Etan Cohen                        | WifiNanEventListener.LISTEN_CONFIG_FAILED
106956f54b391677d78379729dd14518edddf3c7660Etan Cohen                        | WifiNanEventListener.LISTEN_IDENTITY_CHANGED
107956f54b391677d78379729dd14518edddf3c7660Etan Cohen                        | WifiNanEventListener.LISTEN_NAN_DOWN);
10822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.requestConfig(uid, configRequest1);
10922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
11022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
11122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest1));
11222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionId1 = transactionId.getValue();
11322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
11422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.requestConfig(uid, configRequest2);
11522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
11622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
11722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest2));
11822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionId2 = transactionId.getValue();
11922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
120956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onClusterChange(WifiNanClientState.CLUSTER_CHANGE_EVENT_STARTED, someMac);
12122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onConfigCompleted(transactionId1);
12222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onConfigFailed(transactionId2, reason);
123956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onInterfaceAddressChange(someMac);
124956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onNanDown(reason);
125956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
126956f54b391677d78379729dd14518edddf3c7660Etan Cohen
127956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mockListener).onIdentityChanged();
12822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mockListener).onConfigCompleted(configRequest1);
12922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mockListener).onConfigFailed(configRequest2, reason);
130956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mockListener).onIdentityChanged();
131956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mockListener).onNanDown(reason);
132956f54b391677d78379729dd14518edddf3c7660Etan Cohen        verifyNoMoreInteractions(mockListener);
13322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
13422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId1);
13522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId2);
136956f54b391677d78379729dd14518edddf3c7660Etan Cohen    }
137956f54b391677d78379729dd14518edddf3c7660Etan Cohen
138956f54b391677d78379729dd14518edddf3c7660Etan Cohen    @Test
139956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public void testNanEventsNotDelivered() throws Exception {
140956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int uid = 1005;
14122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterLow1 = 5;
14222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterHigh1 = 100;
14322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int masterPref1 = 111;
14422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterLow2 = 7;
14522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterHigh2 = 155;
14622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int masterPref2 = 0;
147956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int reason = WifiNanSessionListener.FAIL_REASON_NO_RESOURCES;
148956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final byte[] someMac = HexEncoding.decode("000102030405".toCharArray(), false);
149956f54b391677d78379729dd14518edddf3c7660Etan Cohen
15022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ConfigRequest configRequest1 = new ConfigRequest.Builder().setClusterLow(clusterLow1)
15122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setClusterHigh(clusterHigh1).setMasterPreference(masterPref1).build();
15222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
15322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ConfigRequest configRequest2 = new ConfigRequest.Builder().setClusterLow(clusterLow2)
15422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setClusterHigh(clusterHigh2).setMasterPreference(masterPref2).build();
15522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
156956f54b391677d78379729dd14518edddf3c7660Etan Cohen        IWifiNanEventListener mockListener = mock(IWifiNanEventListener.class);
15722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ArgumentCaptor<Short> transactionId = ArgumentCaptor.forClass(Short.class);
15822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        InOrder inOrder = inOrder(mockListener, mMockNative);
159956f54b391677d78379729dd14518edddf3c7660Etan Cohen
160956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.connect(uid, mockListener, 0);
16122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.requestConfig(uid, configRequest1);
16222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
16322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
16422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest1));
16522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionId1 = transactionId.getValue();
16622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
16722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.requestConfig(uid, configRequest2);
16822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
16922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
17022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest2));
17122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionId2 = transactionId.getValue();
17222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
173956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onClusterChange(WifiNanClientState.CLUSTER_CHANGE_EVENT_JOINED, someMac);
17422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onConfigCompleted(transactionId1);
17522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onConfigFailed(transactionId2, reason);
176956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onInterfaceAddressChange(someMac);
177956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onNanDown(reason);
178956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
179956f54b391677d78379729dd14518edddf3c7660Etan Cohen
18022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        verifyZeroInteractions(mockListener);
18122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
18222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId1);
18322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId2);
184956f54b391677d78379729dd14518edddf3c7660Etan Cohen    }
185956f54b391677d78379729dd14518edddf3c7660Etan Cohen
186956f54b391677d78379729dd14518edddf3c7660Etan Cohen    @Test
187956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public void testPublish() throws Exception {
188956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int uid = 1005;
189956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int sessionId = 20;
190956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final String serviceName = "some-service-name";
191956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final String ssi = "some much longer and more arbitrary data";
192956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int publishCount = 7;
193956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int reasonFail = WifiNanSessionListener.FAIL_REASON_NO_RESOURCES;
194956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int reasonTerminate = WifiNanSessionListener.TERMINATE_REASON_DONE;
19522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int publishId1 = 15;
19622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int publishId2 = 22;
197956f54b391677d78379729dd14518edddf3c7660Etan Cohen
19822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        PublishData publishData = new PublishData.Builder().setServiceName(serviceName)
19922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setServiceSpecificInfo(ssi).build();
200956f54b391677d78379729dd14518edddf3c7660Etan Cohen
20122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        PublishSettings publishSettings = new PublishSettings.Builder()
20222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setPublishType(PublishSettings.PUBLISH_TYPE_UNSOLICITED)
20322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setPublishCount(publishCount).build();
204956f54b391677d78379729dd14518edddf3c7660Etan Cohen
205956f54b391677d78379729dd14518edddf3c7660Etan Cohen        IWifiNanSessionListener mockListener = mock(IWifiNanSessionListener.class);
206956f54b391677d78379729dd14518edddf3c7660Etan Cohen        ArgumentCaptor<Short> transactionId = ArgumentCaptor.forClass(Short.class);
207956f54b391677d78379729dd14518edddf3c7660Etan Cohen        InOrder inOrder = inOrder(mockListener, mMockNative);
208956f54b391677d78379729dd14518edddf3c7660Etan Cohen
209956f54b391677d78379729dd14518edddf3c7660Etan Cohen        int allEvents = WifiNanSessionListener.LISTEN_PUBLISH_FAIL
210956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_PUBLISH_TERMINATED
211956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_FAIL
212956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_TERMINATED
213956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MATCH
214956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_SUCCESS
215956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_FAIL
216956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_RECEIVED;
217956f54b391677d78379729dd14518edddf3c7660Etan Cohen
218956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.connect(uid, null, 0);
219956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.createSession(uid, sessionId, mockListener, allEvents);
220956f54b391677d78379729dd14518edddf3c7660Etan Cohen
221956f54b391677d78379729dd14518edddf3c7660Etan Cohen        // publish - fail
222956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.publish(uid, sessionId, publishData, publishSettings);
223956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
224956f54b391677d78379729dd14518edddf3c7660Etan Cohen
225956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).publish(transactionId.capture(), eq(0), eq(publishData),
226956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(publishSettings));
227956f54b391677d78379729dd14518edddf3c7660Etan Cohen
228956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onPublishFail(transactionId.getValue(), reasonFail);
229956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
230956f54b391677d78379729dd14518edddf3c7660Etan Cohen
231956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mockListener).onPublishFail(reasonFail);
23222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
233956f54b391677d78379729dd14518edddf3c7660Etan Cohen
234956f54b391677d78379729dd14518edddf3c7660Etan Cohen        // publish - success/terminate
235956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.publish(uid, sessionId, publishData, publishSettings);
236956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
237956f54b391677d78379729dd14518edddf3c7660Etan Cohen
238956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).publish(transactionId.capture(), eq(0), eq(publishData),
239956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(publishSettings));
240956f54b391677d78379729dd14518edddf3c7660Etan Cohen
24122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onPublishSuccess(transactionId.getValue(), publishId1);
24222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
24322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
24422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onPublishTerminated(publishId1, reasonTerminate);
245956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
246956f54b391677d78379729dd14518edddf3c7660Etan Cohen
247956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mockListener).onPublishTerminated(reasonTerminate);
24822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
249956f54b391677d78379729dd14518edddf3c7660Etan Cohen
250956f54b391677d78379729dd14518edddf3c7660Etan Cohen        // re-publish
251956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.publish(uid, sessionId, publishData, publishSettings);
252956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
253956f54b391677d78379729dd14518edddf3c7660Etan Cohen
254956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).publish(transactionId.capture(), eq(0), eq(publishData),
255956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(publishSettings));
256956f54b391677d78379729dd14518edddf3c7660Etan Cohen
25722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onPublishSuccess(transactionId.getValue(), publishId2);
258956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.publish(uid, sessionId, publishData, publishSettings);
259956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
260956f54b391677d78379729dd14518edddf3c7660Etan Cohen
26122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
26222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).publish(transactionId.capture(), eq(publishId2),
263956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(publishData), eq(publishSettings));
264956f54b391677d78379729dd14518edddf3c7660Etan Cohen        verifyNoMoreInteractions(mockListener, mMockNative);
265956f54b391677d78379729dd14518edddf3c7660Etan Cohen    }
266956f54b391677d78379729dd14518edddf3c7660Etan Cohen
267956f54b391677d78379729dd14518edddf3c7660Etan Cohen    @Test
268956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public void testPublishNoCallbacks() throws Exception {
269956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int uid = 1005;
270956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int sessionId = 20;
271956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final String serviceName = "some-service-name";
272956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final String ssi = "some much longer and more arbitrary data";
273956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int publishCount = 7;
274956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int reasonFail = WifiNanSessionListener.FAIL_REASON_NO_RESOURCES;
275956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int reasonTerminate = WifiNanSessionListener.TERMINATE_REASON_DONE;
276956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int publishId = 15;
277956f54b391677d78379729dd14518edddf3c7660Etan Cohen
27822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        PublishData publishData = new PublishData.Builder().setServiceName(serviceName)
27922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setServiceSpecificInfo(ssi).build();
280956f54b391677d78379729dd14518edddf3c7660Etan Cohen
28122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        PublishSettings publishSettings = new PublishSettings.Builder()
28222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setPublishType(PublishSettings.PUBLISH_TYPE_UNSOLICITED)
28322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setPublishCount(publishCount).build();
284956f54b391677d78379729dd14518edddf3c7660Etan Cohen
285956f54b391677d78379729dd14518edddf3c7660Etan Cohen        IWifiNanSessionListener mockListener = mock(IWifiNanSessionListener.class);
286956f54b391677d78379729dd14518edddf3c7660Etan Cohen        ArgumentCaptor<Short> transactionId = ArgumentCaptor.forClass(Short.class);
287956f54b391677d78379729dd14518edddf3c7660Etan Cohen        InOrder inOrder = inOrder(mockListener, mMockNative);
288956f54b391677d78379729dd14518edddf3c7660Etan Cohen
289956f54b391677d78379729dd14518edddf3c7660Etan Cohen        int allEvents = WifiNanSessionListener.LISTEN_PUBLISH_FAIL
290956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_PUBLISH_TERMINATED
291956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_FAIL
292956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_TERMINATED
293956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MATCH
294956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_SUCCESS
295956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_FAIL
296956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_RECEIVED;
297956f54b391677d78379729dd14518edddf3c7660Etan Cohen
298956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.connect(uid, null, 0);
299956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.createSession(uid, sessionId, mockListener,
300956f54b391677d78379729dd14518edddf3c7660Etan Cohen                allEvents & ~WifiNanSessionListener.LISTEN_PUBLISH_FAIL
301956f54b391677d78379729dd14518edddf3c7660Etan Cohen                        & ~WifiNanSessionListener.LISTEN_PUBLISH_TERMINATED);
302956f54b391677d78379729dd14518edddf3c7660Etan Cohen
303956f54b391677d78379729dd14518edddf3c7660Etan Cohen        // publish - fail
304956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.publish(uid, sessionId, publishData, publishSettings);
305956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
306956f54b391677d78379729dd14518edddf3c7660Etan Cohen
307956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).publish(transactionId.capture(), eq(0), eq(publishData),
308956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(publishSettings));
309956f54b391677d78379729dd14518edddf3c7660Etan Cohen
310956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onPublishFail(transactionId.getValue(), reasonFail);
311956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
312956f54b391677d78379729dd14518edddf3c7660Etan Cohen
31322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
31422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
315956f54b391677d78379729dd14518edddf3c7660Etan Cohen        // publish - success/terminate
316956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.publish(uid, sessionId, publishData, publishSettings);
317956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
318956f54b391677d78379729dd14518edddf3c7660Etan Cohen
319956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).publish(transactionId.capture(), eq(0), eq(publishData),
320956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(publishSettings));
321956f54b391677d78379729dd14518edddf3c7660Etan Cohen
322956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onPublishSuccess(transactionId.getValue(), publishId);
32322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
32422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
325956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onPublishTerminated(publishId, reasonTerminate);
326956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
327956f54b391677d78379729dd14518edddf3c7660Etan Cohen
32822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
329956f54b391677d78379729dd14518edddf3c7660Etan Cohen        verifyNoMoreInteractions(mockListener, mMockNative);
330956f54b391677d78379729dd14518edddf3c7660Etan Cohen    }
331956f54b391677d78379729dd14518edddf3c7660Etan Cohen
332956f54b391677d78379729dd14518edddf3c7660Etan Cohen    @Test
333956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public void testSubscribe() throws Exception {
334956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int uid = 1005;
335956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int sessionId = 20;
336956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final String serviceName = "some-service-name";
337956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final String ssi = "some much longer and more arbitrary data";
338956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int subscribeCount = 7;
339956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int reasonFail = WifiNanSessionListener.FAIL_REASON_NO_RESOURCES;
340956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int reasonTerminate = WifiNanSessionListener.TERMINATE_REASON_DONE;
34122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int subscribeId1 = 15;
34222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int subscribeId2 = 10;
343956f54b391677d78379729dd14518edddf3c7660Etan Cohen
34422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        SubscribeData subscribeData = new SubscribeData.Builder().setServiceName(serviceName)
34522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setServiceSpecificInfo(ssi).build();
346956f54b391677d78379729dd14518edddf3c7660Etan Cohen
34722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        SubscribeSettings subscribeSettings = new SubscribeSettings.Builder()
34822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setSubscribeType(SubscribeSettings.SUBSCRIBE_TYPE_PASSIVE)
34922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setSubscribeCount(subscribeCount).build();
350956f54b391677d78379729dd14518edddf3c7660Etan Cohen
351956f54b391677d78379729dd14518edddf3c7660Etan Cohen        IWifiNanSessionListener mockListener = mock(IWifiNanSessionListener.class);
352956f54b391677d78379729dd14518edddf3c7660Etan Cohen        ArgumentCaptor<Short> transactionId = ArgumentCaptor.forClass(Short.class);
353956f54b391677d78379729dd14518edddf3c7660Etan Cohen        InOrder inOrder = inOrder(mockListener, mMockNative);
354956f54b391677d78379729dd14518edddf3c7660Etan Cohen
355956f54b391677d78379729dd14518edddf3c7660Etan Cohen        int allEvents = WifiNanSessionListener.LISTEN_PUBLISH_FAIL
356956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_PUBLISH_TERMINATED
357956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_FAIL
358956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_TERMINATED
359956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MATCH
360956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_SUCCESS
361956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_FAIL
362956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_RECEIVED;
363956f54b391677d78379729dd14518edddf3c7660Etan Cohen
364956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.connect(uid, null, 0);
365956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.createSession(uid, sessionId, mockListener, allEvents);
366956f54b391677d78379729dd14518edddf3c7660Etan Cohen
367956f54b391677d78379729dd14518edddf3c7660Etan Cohen        // subscribe - fail
368956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.subscribe(uid, sessionId, subscribeData, subscribeSettings);
369956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
370956f54b391677d78379729dd14518edddf3c7660Etan Cohen
371956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).subscribe(transactionId.capture(), eq(0), eq(subscribeData),
372956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(subscribeSettings));
373956f54b391677d78379729dd14518edddf3c7660Etan Cohen
374956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onSubscribeFail(transactionId.getValue(), reasonFail);
375956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
376956f54b391677d78379729dd14518edddf3c7660Etan Cohen
37722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
378956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mockListener).onSubscribeFail(reasonFail);
379956f54b391677d78379729dd14518edddf3c7660Etan Cohen
380956f54b391677d78379729dd14518edddf3c7660Etan Cohen        // subscribe - success/terminate
381956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.subscribe(uid, sessionId, subscribeData, subscribeSettings);
382956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
383956f54b391677d78379729dd14518edddf3c7660Etan Cohen
384956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).subscribe(transactionId.capture(), eq(0), eq(subscribeData),
385956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(subscribeSettings));
386956f54b391677d78379729dd14518edddf3c7660Etan Cohen
38722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onSubscribeSuccess(transactionId.getValue(), subscribeId1);
388956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
389956f54b391677d78379729dd14518edddf3c7660Etan Cohen
39022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onSubscribeTerminated(subscribeId1, reasonTerminate);
39122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
39222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
39322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
394956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mockListener).onSubscribeTerminated(reasonTerminate);
395956f54b391677d78379729dd14518edddf3c7660Etan Cohen
396956f54b391677d78379729dd14518edddf3c7660Etan Cohen        // re-subscribe
397956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.subscribe(uid, sessionId, subscribeData, subscribeSettings);
398956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
399956f54b391677d78379729dd14518edddf3c7660Etan Cohen
400956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).subscribe(transactionId.capture(), eq(0), eq(subscribeData),
401956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(subscribeSettings));
402956f54b391677d78379729dd14518edddf3c7660Etan Cohen
40322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onSubscribeSuccess(transactionId.getValue(), subscribeId2);
404956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.subscribe(uid, sessionId, subscribeData, subscribeSettings);
405956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
406956f54b391677d78379729dd14518edddf3c7660Etan Cohen
40722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
40822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).subscribe(transactionId.capture(), eq(subscribeId2),
409956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(subscribeData), eq(subscribeSettings));
410956f54b391677d78379729dd14518edddf3c7660Etan Cohen        verifyNoMoreInteractions(mockListener, mMockNative);
411956f54b391677d78379729dd14518edddf3c7660Etan Cohen    }
412956f54b391677d78379729dd14518edddf3c7660Etan Cohen
413956f54b391677d78379729dd14518edddf3c7660Etan Cohen    @Test
414956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public void testSubscribeNoCallbacks() throws Exception {
415956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int uid = 1005;
416956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int sessionId = 20;
417956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final String serviceName = "some-service-name";
418956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final String ssi = "some much longer and more arbitrary data";
419956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int subscribeCount = 7;
420956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int reasonFail = WifiNanSessionListener.FAIL_REASON_NO_RESOURCES;
421956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int reasonTerminate = WifiNanSessionListener.TERMINATE_REASON_DONE;
422956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int subscribeId = 15;
423956f54b391677d78379729dd14518edddf3c7660Etan Cohen
42422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        SubscribeData subscribeData = new SubscribeData.Builder().setServiceName(serviceName)
42522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setServiceSpecificInfo(ssi).build();
426956f54b391677d78379729dd14518edddf3c7660Etan Cohen
42722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        SubscribeSettings subscribeSettings = new SubscribeSettings.Builder()
42822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setSubscribeType(SubscribeSettings.SUBSCRIBE_TYPE_PASSIVE)
42922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setSubscribeCount(subscribeCount).build();
430956f54b391677d78379729dd14518edddf3c7660Etan Cohen
431956f54b391677d78379729dd14518edddf3c7660Etan Cohen        IWifiNanSessionListener mockListener = mock(IWifiNanSessionListener.class);
432956f54b391677d78379729dd14518edddf3c7660Etan Cohen        ArgumentCaptor<Short> transactionId = ArgumentCaptor.forClass(Short.class);
433956f54b391677d78379729dd14518edddf3c7660Etan Cohen        InOrder inOrder = inOrder(mockListener, mMockNative);
434956f54b391677d78379729dd14518edddf3c7660Etan Cohen
435956f54b391677d78379729dd14518edddf3c7660Etan Cohen        int allEvents = WifiNanSessionListener.LISTEN_PUBLISH_FAIL
436956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_PUBLISH_TERMINATED
437956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_FAIL
438956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_TERMINATED
439956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MATCH
440956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_SUCCESS
441956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_FAIL
442956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_RECEIVED;
443956f54b391677d78379729dd14518edddf3c7660Etan Cohen
444956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.connect(uid, null, 0);
445956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.createSession(uid, sessionId, mockListener,
446956f54b391677d78379729dd14518edddf3c7660Etan Cohen                allEvents & ~WifiNanSessionListener.LISTEN_SUBSCRIBE_FAIL
447956f54b391677d78379729dd14518edddf3c7660Etan Cohen                        & ~WifiNanSessionListener.LISTEN_SUBSCRIBE_TERMINATED);
448956f54b391677d78379729dd14518edddf3c7660Etan Cohen
449956f54b391677d78379729dd14518edddf3c7660Etan Cohen        // subscribe - fail
450956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.subscribe(uid, sessionId, subscribeData, subscribeSettings);
451956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
452956f54b391677d78379729dd14518edddf3c7660Etan Cohen
453956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).subscribe(transactionId.capture(), eq(0), eq(subscribeData),
454956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(subscribeSettings));
455956f54b391677d78379729dd14518edddf3c7660Etan Cohen
456956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onSubscribeFail(transactionId.getValue(), reasonFail);
457956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
458956f54b391677d78379729dd14518edddf3c7660Etan Cohen
45922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
46022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
461956f54b391677d78379729dd14518edddf3c7660Etan Cohen        // subscribe - success/terminate
462956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.subscribe(uid, sessionId, subscribeData, subscribeSettings);
463956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
464956f54b391677d78379729dd14518edddf3c7660Etan Cohen
465956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).subscribe(transactionId.capture(), eq(0), eq(subscribeData),
466956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(subscribeSettings));
467956f54b391677d78379729dd14518edddf3c7660Etan Cohen
468956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onSubscribeSuccess(transactionId.getValue(), subscribeId);
46922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
47022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
471956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onSubscribeTerminated(subscribeId, reasonTerminate);
472956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
473956f54b391677d78379729dd14518edddf3c7660Etan Cohen
47422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
475956f54b391677d78379729dd14518edddf3c7660Etan Cohen        verifyNoMoreInteractions(mockListener, mMockNative);
476956f54b391677d78379729dd14518edddf3c7660Etan Cohen    }
477956f54b391677d78379729dd14518edddf3c7660Etan Cohen
478956f54b391677d78379729dd14518edddf3c7660Etan Cohen    @Test
479956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public void testMatchAndMessages() throws Exception {
480956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int uid = 1005;
481956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int sessionId = 20;
482956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final String serviceName = "some-service-name";
483956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final String ssi = "some much longer and more arbitrary data";
484956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int subscribeCount = 7;
485956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int reasonFail = WifiNanSessionListener.FAIL_REASON_NO_RESOURCES;
486956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int subscribeId = 15;
487956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int requestorId = 22;
488956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final byte[] peerMac = HexEncoding.decode("060708090A0B".toCharArray(), false);
489956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final String peerSsi = "some peer ssi data";
490956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final String peerMatchFilter = "filter binary array represented as string";
491956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final String peerMsg = "some message from peer";
4922e09c384f5ce86061b115f20fe3ca75a175d87f0Etan Cohen        final int messageId = 6948;
493956f54b391677d78379729dd14518edddf3c7660Etan Cohen
49422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        SubscribeData subscribeData = new SubscribeData.Builder().setServiceName(serviceName)
49522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setServiceSpecificInfo(ssi).build();
496956f54b391677d78379729dd14518edddf3c7660Etan Cohen
49722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        SubscribeSettings subscribeSettings = new SubscribeSettings.Builder()
49822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setSubscribeType(SubscribeSettings.SUBSCRIBE_TYPE_PASSIVE)
49922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setSubscribeCount(subscribeCount).build();
500956f54b391677d78379729dd14518edddf3c7660Etan Cohen
501956f54b391677d78379729dd14518edddf3c7660Etan Cohen        IWifiNanSessionListener mockListener = mock(IWifiNanSessionListener.class);
502956f54b391677d78379729dd14518edddf3c7660Etan Cohen        ArgumentCaptor<Short> transactionId = ArgumentCaptor.forClass(Short.class);
503956f54b391677d78379729dd14518edddf3c7660Etan Cohen        InOrder inOrder = inOrder(mockListener, mMockNative);
504956f54b391677d78379729dd14518edddf3c7660Etan Cohen
505956f54b391677d78379729dd14518edddf3c7660Etan Cohen        int allEvents = WifiNanSessionListener.LISTEN_PUBLISH_FAIL
506956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_PUBLISH_TERMINATED
507956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_FAIL
508956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_TERMINATED
509956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MATCH
510956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_SUCCESS
511956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_FAIL
512956f54b391677d78379729dd14518edddf3c7660Etan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_RECEIVED;
513956f54b391677d78379729dd14518edddf3c7660Etan Cohen
514956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.connect(uid, null, 0);
515956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.createSession(uid, sessionId, mockListener, allEvents);
516956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.subscribe(uid, sessionId, subscribeData, subscribeSettings);
517956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
518956f54b391677d78379729dd14518edddf3c7660Etan Cohen
519956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).subscribe(transactionId.capture(), eq(0), eq(subscribeData),
520956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(subscribeSettings));
521956f54b391677d78379729dd14518edddf3c7660Etan Cohen
522956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onSubscribeSuccess(transactionId.getValue(), subscribeId);
523956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onMatch(subscribeId, requestorId, peerMac, peerSsi.getBytes(), peerSsi.length(),
524956f54b391677d78379729dd14518edddf3c7660Etan Cohen                peerMatchFilter.getBytes(), peerMatchFilter.length());
525956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onMessageReceived(subscribeId, requestorId, peerMac, peerMsg.getBytes(),
526956f54b391677d78379729dd14518edddf3c7660Etan Cohen                peerMsg.length());
527956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
528956f54b391677d78379729dd14518edddf3c7660Etan Cohen
52922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
530956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mockListener).onMatch(requestorId, peerSsi.getBytes(), peerSsi.length(),
531956f54b391677d78379729dd14518edddf3c7660Etan Cohen                peerMatchFilter.getBytes(), peerMatchFilter.length());
532956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mockListener).onMessageReceived(requestorId, peerMsg.getBytes(),
533956f54b391677d78379729dd14518edddf3c7660Etan Cohen                peerMsg.length());
534956f54b391677d78379729dd14518edddf3c7660Etan Cohen
5352e09c384f5ce86061b115f20fe3ca75a175d87f0Etan Cohen        mDut.sendMessage(uid, sessionId, requestorId, ssi.getBytes(), ssi.length(), messageId);
536956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
537956f54b391677d78379729dd14518edddf3c7660Etan Cohen
538956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).sendMessage(transactionId.capture(), eq(subscribeId),
539956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(requestorId), eq(peerMac), eq(ssi.getBytes()), eq(ssi.length()));
540956f54b391677d78379729dd14518edddf3c7660Etan Cohen
541956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onMessageSendFail(transactionId.getValue(), reasonFail);
542956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
543956f54b391677d78379729dd14518edddf3c7660Etan Cohen
54422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
5452e09c384f5ce86061b115f20fe3ca75a175d87f0Etan Cohen        inOrder.verify(mockListener).onMessageSendFail(messageId, reasonFail);
546956f54b391677d78379729dd14518edddf3c7660Etan Cohen
5472e09c384f5ce86061b115f20fe3ca75a175d87f0Etan Cohen        mDut.sendMessage(uid, sessionId, requestorId, ssi.getBytes(), ssi.length(), messageId);
548956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
549956f54b391677d78379729dd14518edddf3c7660Etan Cohen
550956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).sendMessage(transactionId.capture(), eq(subscribeId),
551956f54b391677d78379729dd14518edddf3c7660Etan Cohen                eq(requestorId), eq(peerMac), eq(ssi.getBytes()), eq(ssi.length()));
552956f54b391677d78379729dd14518edddf3c7660Etan Cohen
553956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onMessageSendSuccess(transactionId.getValue());
554956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
555956f54b391677d78379729dd14518edddf3c7660Etan Cohen
55622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
5572e09c384f5ce86061b115f20fe3ca75a175d87f0Etan Cohen        inOrder.verify(mockListener).onMessageSendSuccess(messageId);
558956f54b391677d78379729dd14518edddf3c7660Etan Cohen
559956f54b391677d78379729dd14518edddf3c7660Etan Cohen        verifyNoMoreInteractions(mockListener, mMockNative);
560956f54b391677d78379729dd14518edddf3c7660Etan Cohen    }
561956f54b391677d78379729dd14518edddf3c7660Etan Cohen
56222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    /**
56322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * Summary: in a single publish session interact with multiple peers
56422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * (different MAC addresses).
56522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     */
56622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    @Test
56722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    public void testMultipleMessageSources() throws Exception {
56822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int uid = 300;
56922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterLow = 7;
57022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterHigh = 7;
57122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int masterPref = 0;
57222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int sessionId = 26;
57322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final String serviceName = "some-service-name";
57422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int publishId = 88;
57522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int peerId1 = 568;
57622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int peerId2 = 873;
57722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final byte[] peerMac1 = HexEncoding.decode("000102030405".toCharArray(), false);
57822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final byte[] peerMac2 = HexEncoding.decode("060708090A0B".toCharArray(), false);
57922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final String msgFromPeer1 = "hey from 000102...";
58022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final String msgFromPeer2 = "hey from 0607...";
58122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final String msgToPeer1 = "hey there 000102...";
58222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final String msgToPeer2 = "hey there 0506...";
58322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int msgToPeerId1 = 546;
58422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int msgToPeerId2 = 9654;
58522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int reason = WifiNanSessionListener.FAIL_REASON_OTHER;
58622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
58722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ConfigRequest configRequest = new ConfigRequest.Builder().setClusterLow(clusterLow)
58822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setClusterHigh(clusterHigh).setMasterPreference(masterPref).build();
58922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
59022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        PublishData publishData = new PublishData.Builder().setServiceName(serviceName).build();
59122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
59222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        PublishSettings publishSettings = new PublishSettings.Builder()
59322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setPublishType(PublishSettings.PUBLISH_TYPE_UNSOLICITED).build();
59422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
59522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ArgumentCaptor<Short> transactionId = ArgumentCaptor.forClass(Short.class);
59622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        IWifiNanEventListener mockListener = mock(IWifiNanEventListener.class);
59722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        IWifiNanSessionListener mockSessionListener = mock(IWifiNanSessionListener.class);
59822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        InOrder inOrder = inOrder(mMockNative, mockListener, mockSessionListener);
59922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
60022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        int allEvents = WifiNanEventListener.LISTEN_CONFIG_COMPLETED
60122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanEventListener.LISTEN_CONFIG_FAILED
60222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanEventListener.LISTEN_IDENTITY_CHANGED
60322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanEventListener.LISTEN_NAN_DOWN;
60422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
60522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        int allSessionEvents = WifiNanSessionListener.LISTEN_PUBLISH_FAIL
60622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_PUBLISH_TERMINATED
60722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_FAIL
60822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_TERMINATED
60922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MATCH
61022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_SUCCESS
61122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_FAIL
61222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_RECEIVED;
61322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
61422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.connect(uid, mockListener, allEvents);
61522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.requestConfig(uid, configRequest);
61622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.createSession(uid, sessionId, mockSessionListener, allSessionEvents);
61722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.publish(uid, sessionId, publishData, publishSettings);
61822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
61922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
62022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest));
62122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionIdConfig = transactionId.getValue();
62222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
62322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).publish(transactionId.capture(), eq(0), eq(publishData),
62422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                eq(publishSettings));
62522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionIdPublish = transactionId.getValue();
62622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
62722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onConfigCompleted(transactionIdConfig);
62822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onPublishSuccess(transactionIdPublish, publishId);
62922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onMessageReceived(publishId, peerId1, peerMac1, msgFromPeer1.getBytes(),
63022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                msgFromPeer1.length());
63122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onMessageReceived(publishId, peerId2, peerMac2, msgFromPeer2.getBytes(),
63222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                msgFromPeer2.length());
63322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.sendMessage(uid, sessionId, peerId2, msgToPeer2.getBytes(), msgToPeer2.length(),
63422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                msgToPeerId2);
63522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.sendMessage(uid, sessionId, peerId1, msgToPeer1.getBytes(), msgToPeer1.length(),
63622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                msgToPeerId1);
63722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
63822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
63922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionIdConfig);
64022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionIdPublish);
64122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mockListener).onConfigCompleted(configRequest);
64222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mockSessionListener).onMessageReceived(peerId1, msgFromPeer1.getBytes(),
64322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                msgFromPeer1.length());
64422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mockSessionListener).onMessageReceived(peerId2, msgFromPeer2.getBytes(),
64522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                msgFromPeer2.length());
64622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).sendMessage(transactionId.capture(), eq(publishId), eq(peerId2),
64722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                eq(peerMac2), eq(msgToPeer2.getBytes()), eq(msgToPeer2.length()));
64822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionIdMsg2 = transactionId.getValue();
64922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).sendMessage(transactionId.capture(), eq(publishId), eq(peerId1),
65022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                eq(peerMac1), eq(msgToPeer1.getBytes()), eq(msgToPeer1.length()));
65122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionIdMsg1 = transactionId.getValue();
65222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
65322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onMessageSendFail(transactionIdMsg1, reason);
65422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onMessageSendSuccess(transactionIdMsg2);
65522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
65622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
65722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionIdMsg1);
65822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionIdMsg2);
65922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mockSessionListener).onMessageSendFail(msgToPeerId1, reason);
66022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mockSessionListener).onMessageSendSuccess(msgToPeerId2);
66122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        verifyNoMoreInteractions(mMockNative, mockListener, mockSessionListener);
66222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    }
66322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
66422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    /**
66522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * Summary: interact with a peer which changed its identity (MAC address)
66622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * but which keeps its requestor instance ID. Should be transparent.
66722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     */
66822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    @Test
66922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    public void testMessageWhilePeerChangesIdentity() throws Exception {
67022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int uid = 300;
67122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterLow = 7;
67222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterHigh = 7;
67322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int masterPref = 0;
67422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int sessionId = 26;
67522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final String serviceName = "some-service-name";
67622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int publishId = 88;
67722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int peerId = 568;
67822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final byte[] peerMacOrig = HexEncoding.decode("000102030405".toCharArray(), false);
67922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final byte[] peerMacLater = HexEncoding.decode("060708090A0B".toCharArray(), false);
68022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final String msgFromPeer1 = "hey from 000102...";
68122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final String msgFromPeer2 = "hey from 0607...";
68222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final String msgToPeer1 = "hey there 000102...";
68322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final String msgToPeer2 = "hey there 0506...";
68422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int msgToPeerId1 = 546;
68522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int msgToPeerId2 = 9654;
68622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ConfigRequest configRequest = new ConfigRequest.Builder().setClusterLow(clusterLow)
68722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setClusterHigh(clusterHigh).setMasterPreference(masterPref).build();
68822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
68922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        PublishData publishData = new PublishData.Builder().setServiceName(serviceName).build();
69022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
69122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        PublishSettings publishSettings = new PublishSettings.Builder()
69222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setPublishType(PublishSettings.PUBLISH_TYPE_UNSOLICITED).build();
69322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
69422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ArgumentCaptor<Short> transactionId = ArgumentCaptor.forClass(Short.class);
69522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        IWifiNanEventListener mockListener = mock(IWifiNanEventListener.class);
69622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        IWifiNanSessionListener mockSessionListener = mock(IWifiNanSessionListener.class);
69722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        InOrder inOrder = inOrder(mMockNative, mockListener, mockSessionListener);
69822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
69922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        int allEvents = WifiNanEventListener.LISTEN_CONFIG_COMPLETED
70022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanEventListener.LISTEN_CONFIG_FAILED
70122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanEventListener.LISTEN_IDENTITY_CHANGED
70222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanEventListener.LISTEN_NAN_DOWN;
70322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
70422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        int allSessionEvents = WifiNanSessionListener.LISTEN_PUBLISH_FAIL
70522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_PUBLISH_TERMINATED
70622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_FAIL
70722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_TERMINATED
70822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MATCH
70922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_SUCCESS
71022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_FAIL
71122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_RECEIVED;
71222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
71322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.connect(uid, mockListener, allEvents);
71422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.requestConfig(uid, configRequest);
71522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.createSession(uid, sessionId, mockSessionListener, allSessionEvents);
71622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.publish(uid, sessionId, publishData, publishSettings);
71722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
71822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
71922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest));
72022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionIdConfig = transactionId.getValue();
72122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
72222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).publish(transactionId.capture(), eq(0), eq(publishData),
72322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                eq(publishSettings));
72422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionIdPublish = transactionId.getValue();
72522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
72622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onConfigCompleted(transactionIdConfig);
72722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onPublishSuccess(transactionIdPublish, publishId);
72822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onMessageReceived(publishId, peerId, peerMacOrig, msgFromPeer1.getBytes(),
72922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                msgFromPeer1.length());
73022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.sendMessage(uid, sessionId, peerId, msgToPeer1.getBytes(), msgToPeer1.length(),
73122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                msgToPeerId1);
73222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
73322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
73422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionIdConfig);
73522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionIdPublish);
73622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mockListener).onConfigCompleted(configRequest);
73722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mockSessionListener).onMessageReceived(peerId, msgFromPeer1.getBytes(),
73822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                msgFromPeer1.length());
73922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).sendMessage(transactionId.capture(), eq(publishId), eq(peerId),
74022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                eq(peerMacOrig), eq(msgToPeer1.getBytes()), eq(msgToPeer1.length()));
74122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionIdMsg = transactionId.getValue();
74222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
74322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onMessageSendSuccess(transactionIdMsg);
74422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onMessageReceived(publishId, peerId, peerMacLater, msgFromPeer2.getBytes(),
74522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                msgFromPeer2.length());
74622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.sendMessage(uid, sessionId, peerId, msgToPeer2.getBytes(), msgToPeer2.length(),
74722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                msgToPeerId2);
74822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
74922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
75022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionIdMsg);
75122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mockSessionListener).onMessageSendSuccess(msgToPeerId1);
75222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mockSessionListener).onMessageReceived(peerId, msgFromPeer2.getBytes(),
75322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                msgFromPeer2.length());
75422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).sendMessage(transactionId.capture(), eq(publishId), eq(peerId),
75522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                eq(peerMacLater), eq(msgToPeer2.getBytes()), eq(msgToPeer2.length()));
75622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        transactionIdMsg = transactionId.getValue();
75722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
75822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onMessageSendSuccess(transactionIdMsg);
75922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
76022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
76122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionIdMsg);
76222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mockSessionListener).onMessageSendSuccess(msgToPeerId2);
76322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
76422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        verifyNoMoreInteractions(mMockNative, mockListener, mockSessionListener);
76522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    }
76622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
767956f54b391677d78379729dd14518edddf3c7660Etan Cohen    @Test
768956f54b391677d78379729dd14518edddf3c7660Etan Cohen    public void testConfigs() throws Exception {
769956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int uid1 = 9999;
770956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int clusterLow1 = 5;
771956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int clusterHigh1 = 100;
772956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int masterPref1 = 111;
773956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int uid2 = 1001;
774956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final boolean support5g2 = true;
775956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int clusterLow2 = 7;
776956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int clusterHigh2 = 155;
777956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int masterPref2 = 0;
778956f54b391677d78379729dd14518edddf3c7660Etan Cohen        final int uid3 = 55;
779956f54b391677d78379729dd14518edddf3c7660Etan Cohen
780956f54b391677d78379729dd14518edddf3c7660Etan Cohen        ArgumentCaptor<Short> transactionId = ArgumentCaptor.forClass(Short.class);
781956f54b391677d78379729dd14518edddf3c7660Etan Cohen        ArgumentCaptor<ConfigRequest> crCapture = ArgumentCaptor.forClass(ConfigRequest.class);
782956f54b391677d78379729dd14518edddf3c7660Etan Cohen
78322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ConfigRequest configRequest1 = new ConfigRequest.Builder().setClusterLow(clusterLow1)
78422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setClusterHigh(clusterHigh1).setMasterPreference(masterPref1).build();
785956f54b391677d78379729dd14518edddf3c7660Etan Cohen
78622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ConfigRequest configRequest2 = new ConfigRequest.Builder().setSupport5gBand(support5g2)
78722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setClusterLow(clusterLow2).setClusterHigh(clusterHigh2)
78822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setMasterPreference(masterPref2).build();
789956f54b391677d78379729dd14518edddf3c7660Etan Cohen
79022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ConfigRequest configRequest3 = new ConfigRequest.Builder().build();
791956f54b391677d78379729dd14518edddf3c7660Etan Cohen
792956f54b391677d78379729dd14518edddf3c7660Etan Cohen        IWifiNanEventListener mockListener1 = mock(IWifiNanEventListener.class);
793956f54b391677d78379729dd14518edddf3c7660Etan Cohen        IWifiNanEventListener mockListener2 = mock(IWifiNanEventListener.class);
794956f54b391677d78379729dd14518edddf3c7660Etan Cohen        IWifiNanEventListener mockListener3 = mock(IWifiNanEventListener.class);
795956f54b391677d78379729dd14518edddf3c7660Etan Cohen
796956f54b391677d78379729dd14518edddf3c7660Etan Cohen        InOrder inOrder = inOrder(mMockNative, mockListener1, mockListener2, mockListener3);
797956f54b391677d78379729dd14518edddf3c7660Etan Cohen
798956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.connect(uid1, mockListener1, WifiNanEventListener.LISTEN_CONFIG_COMPLETED);
799956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.requestConfig(uid1, configRequest1);
800956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
801956f54b391677d78379729dd14518edddf3c7660Etan Cohen
802956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(),
803956f54b391677d78379729dd14518edddf3c7660Etan Cohen                crCapture.capture());
804956f54b391677d78379729dd14518edddf3c7660Etan Cohen        collector.checkThat("merge: stage 0", configRequest1, equalTo(crCapture.getValue()));
805956f54b391677d78379729dd14518edddf3c7660Etan Cohen
806956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onConfigCompleted(transactionId.getValue());
807956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
808956f54b391677d78379729dd14518edddf3c7660Etan Cohen
80922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
810956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mockListener1).onConfigCompleted(configRequest1);
811956f54b391677d78379729dd14518edddf3c7660Etan Cohen
812956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.connect(uid2, mockListener2, WifiNanEventListener.LISTEN_CONFIG_COMPLETED);
813956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.requestConfig(uid2, configRequest2);
814956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
815956f54b391677d78379729dd14518edddf3c7660Etan Cohen
816956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(),
817956f54b391677d78379729dd14518edddf3c7660Etan Cohen                crCapture.capture());
818956f54b391677d78379729dd14518edddf3c7660Etan Cohen        collector.checkThat("merge: stage 1: support 5g", crCapture.getValue().mSupport5gBand,
819956f54b391677d78379729dd14518edddf3c7660Etan Cohen                equalTo(true));
820956f54b391677d78379729dd14518edddf3c7660Etan Cohen        collector.checkThat("merge: stage 1: master pref", crCapture.getValue().mMasterPreference,
821956f54b391677d78379729dd14518edddf3c7660Etan Cohen                equalTo(Math.max(masterPref1, masterPref2)));
822956f54b391677d78379729dd14518edddf3c7660Etan Cohen        collector.checkThat("merge: stage 1: cluster low", crCapture.getValue().mClusterLow,
823956f54b391677d78379729dd14518edddf3c7660Etan Cohen                equalTo(Math.min(clusterLow1, clusterLow2)));
824956f54b391677d78379729dd14518edddf3c7660Etan Cohen        collector.checkThat("merge: stage 1: cluster high", crCapture.getValue().mClusterHigh,
825956f54b391677d78379729dd14518edddf3c7660Etan Cohen                equalTo(Math.max(clusterHigh1, clusterHigh2)));
826956f54b391677d78379729dd14518edddf3c7660Etan Cohen
827956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onConfigCompleted(transactionId.getValue());
828956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
829956f54b391677d78379729dd14518edddf3c7660Etan Cohen
83022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
831956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mockListener1).onConfigCompleted(crCapture.getValue());
832956f54b391677d78379729dd14518edddf3c7660Etan Cohen
833956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.connect(uid3, mockListener3, WifiNanEventListener.LISTEN_CONFIG_COMPLETED);
834956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.requestConfig(uid3, configRequest3);
835956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
836956f54b391677d78379729dd14518edddf3c7660Etan Cohen
837956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(),
838956f54b391677d78379729dd14518edddf3c7660Etan Cohen                crCapture.capture());
839956f54b391677d78379729dd14518edddf3c7660Etan Cohen        collector.checkThat("merge: stage 2: support 5g", crCapture.getValue().mSupport5gBand,
840956f54b391677d78379729dd14518edddf3c7660Etan Cohen                equalTo(true));
841956f54b391677d78379729dd14518edddf3c7660Etan Cohen        collector.checkThat("merge: stage 2: master pref", crCapture.getValue().mMasterPreference,
842956f54b391677d78379729dd14518edddf3c7660Etan Cohen                equalTo(Math.max(masterPref1, masterPref2)));
843956f54b391677d78379729dd14518edddf3c7660Etan Cohen        collector.checkThat("merge: stage 2: cluster low", crCapture.getValue().mClusterLow,
844956f54b391677d78379729dd14518edddf3c7660Etan Cohen                equalTo(Math.min(clusterLow1, clusterLow2)));
845956f54b391677d78379729dd14518edddf3c7660Etan Cohen        collector.checkThat("merge: stage 2: cluster high", crCapture.getValue().mClusterHigh,
846956f54b391677d78379729dd14518edddf3c7660Etan Cohen                equalTo(Math.max(clusterHigh1, clusterHigh2)));
847956f54b391677d78379729dd14518edddf3c7660Etan Cohen
848956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onConfigCompleted(transactionId.getValue());
849956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
850956f54b391677d78379729dd14518edddf3c7660Etan Cohen
85122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
852956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mockListener1).onConfigCompleted(crCapture.getValue());
853956f54b391677d78379729dd14518edddf3c7660Etan Cohen
854956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.disconnect(uid2);
855956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
856956f54b391677d78379729dd14518edddf3c7660Etan Cohen
85722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalClientInfoCleanedUp(uid2);
858956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(),
859956f54b391677d78379729dd14518edddf3c7660Etan Cohen                crCapture.capture());
860956f54b391677d78379729dd14518edddf3c7660Etan Cohen        collector.checkThat("merge: stage 3", configRequest1, equalTo(crCapture.getValue()));
861956f54b391677d78379729dd14518edddf3c7660Etan Cohen
862956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onConfigCompleted(transactionId.getValue());
863956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
864956f54b391677d78379729dd14518edddf3c7660Etan Cohen
86522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
866956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mockListener1).onConfigCompleted(crCapture.getValue());
867956f54b391677d78379729dd14518edddf3c7660Etan Cohen
868956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.disconnect(uid1);
869956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
870956f54b391677d78379729dd14518edddf3c7660Etan Cohen
87122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalClientInfoCleanedUp(uid2);
872956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(),
873956f54b391677d78379729dd14518edddf3c7660Etan Cohen                crCapture.capture());
874956f54b391677d78379729dd14518edddf3c7660Etan Cohen        collector.checkThat("merge: stage 4", configRequest3, equalTo(crCapture.getValue()));
875956f54b391677d78379729dd14518edddf3c7660Etan Cohen
876956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.onConfigCompleted(transactionId.getValue());
877956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
878956f54b391677d78379729dd14518edddf3c7660Etan Cohen
87922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionId.getValue());
880956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mockListener3).onConfigCompleted(crCapture.getValue());
881956f54b391677d78379729dd14518edddf3c7660Etan Cohen
882956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mDut.disconnect(uid3);
883956f54b391677d78379729dd14518edddf3c7660Etan Cohen        mMockLooper.dispatchAll();
884956f54b391677d78379729dd14518edddf3c7660Etan Cohen
88522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalClientInfoCleanedUp(uid2);
886956f54b391677d78379729dd14518edddf3c7660Etan Cohen        inOrder.verify(mMockNative).disable(anyShort());
887956f54b391677d78379729dd14518edddf3c7660Etan Cohen
888956f54b391677d78379729dd14518edddf3c7660Etan Cohen        verifyNoMoreInteractions(mMockNative);
889956f54b391677d78379729dd14518edddf3c7660Etan Cohen    }
890956f54b391677d78379729dd14518edddf3c7660Etan Cohen
89122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    /**
89222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * Summary: disconnect a client while there are pending transactions.
89322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * Validate that no callbacks are called and that internal state is
89422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * cleaned-up.
89522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     */
89622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    @Test
89722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    public void testDisconnectWithPendingTransactions() throws Exception {
89822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int uid = 125;
89922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterLow = 5;
90022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterHigh = 100;
90122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int masterPref = 111;
90222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int sessionId = 20;
90322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final String serviceName = "some-service-name";
90422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final String ssi = "some much longer and more arbitrary data";
90522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int publishCount = 7;
90622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int reason = WifiNanSessionListener.TERMINATE_REASON_DONE;
90722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int publishId = 22;
90822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
90922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ConfigRequest configRequest = new ConfigRequest.Builder().setClusterLow(clusterLow)
91022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setClusterHigh(clusterHigh).setMasterPreference(masterPref).build();
91122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
91222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        PublishData publishData = new PublishData.Builder().setServiceName(serviceName)
91322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setServiceSpecificInfo(ssi).build();
91422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
91522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        PublishSettings publishSettings = new PublishSettings.Builder()
91622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setPublishType(PublishSettings.PUBLISH_TYPE_UNSOLICITED)
91722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setPublishCount(publishCount).build();
91822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
91922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ArgumentCaptor<Short> transactionId = ArgumentCaptor.forClass(Short.class);
92022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        IWifiNanEventListener mockListener = mock(IWifiNanEventListener.class);
92122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        IWifiNanSessionListener mockSessionListener = mock(IWifiNanSessionListener.class);
92222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        InOrder inOrder = inOrder(mMockNative, mockListener, mockSessionListener);
92322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
92422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        int allEvents = WifiNanEventListener.LISTEN_CONFIG_COMPLETED
92522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanEventListener.LISTEN_CONFIG_FAILED
92622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanEventListener.LISTEN_IDENTITY_CHANGED
92722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanEventListener.LISTEN_NAN_DOWN;
92822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
92922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        int allSessionEvents = WifiNanSessionListener.LISTEN_PUBLISH_FAIL
93022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_PUBLISH_TERMINATED
93122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_FAIL
93222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_TERMINATED
93322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MATCH
93422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_SUCCESS
93522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_FAIL
93622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_RECEIVED;
93722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
93822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.connect(uid, mockListener, allEvents);
93922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.createSession(uid, sessionId, mockSessionListener, allSessionEvents);
94022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.requestConfig(uid, configRequest);
94122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.publish(uid, sessionId, publishData, publishSettings);
94222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.disconnect(uid);
94322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
94422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
94522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest));
94622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionIdConfig = transactionId.getValue();
94722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
94822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).publish(transactionId.capture(), eq(0), eq(publishData),
94922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                eq(publishSettings));
95022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionIdPublish = transactionId.getValue();
95122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
95222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalClientInfoCleanedUp(uid);
95322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionIdPublish);
95422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
95522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onConfigCompleted(transactionIdConfig);
95622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onPublishSuccess(transactionIdPublish, publishId);
95722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
95822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
95922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onPublishTerminated(publishId, reason);
96022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
96122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
96222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        verifyZeroInteractions(mockListener, mockSessionListener);
96322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    }
96422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
96522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    /**
96622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * Summary: destroy a session while there are pending transactions. Validate
96722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * that no callbacks are called and that internal state is cleaned-up.
96822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     */
96922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    @Test
97022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    public void testDestroySessionWithPendingTransactions() throws Exception {
97122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int uid = 128;
97222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterLow = 15;
97322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int clusterHigh = 192;
97422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int masterPref = 234;
97522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int publishSessionId = 19;
97622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int subscribeSessionId = 24;
97722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final String serviceName = "some-service-name";
97822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final String ssi = "some much longer and more arbitrary data";
97922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int publishCount = 15;
98022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int subscribeCount = 22;
98122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int reason = WifiNanSessionListener.TERMINATE_REASON_DONE;
98222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int publishId = 23;
98322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        final int subscribeId = 55;
98422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
98522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ConfigRequest configRequest = new ConfigRequest.Builder().setClusterLow(clusterLow)
98622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setClusterHigh(clusterHigh).setMasterPreference(masterPref).build();
98722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
98822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        PublishData publishData = new PublishData.Builder().setServiceName(serviceName)
98922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setServiceSpecificInfo(ssi).build();
99022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
99122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        PublishSettings publishSettings = new PublishSettings.Builder()
99222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setPublishType(PublishSettings.PUBLISH_TYPE_UNSOLICITED)
99322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setPublishCount(publishCount).build();
99422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
99522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        SubscribeData subscribeData = new SubscribeData.Builder().setServiceName(serviceName)
99622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setServiceSpecificInfo(ssi).build();
99722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
99822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        SubscribeSettings subscribeSettings = new SubscribeSettings.Builder()
99922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setSubscribeType(SubscribeSettings.SUBSCRIBE_TYPE_PASSIVE)
100022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                .setSubscribeCount(subscribeCount).build();
100122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
100222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        ArgumentCaptor<Short> transactionId = ArgumentCaptor.forClass(Short.class);
100322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        IWifiNanEventListener mockListener = mock(IWifiNanEventListener.class);
100422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        IWifiNanSessionListener mockPublishSessionListener = mock(IWifiNanSessionListener.class);
100522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        IWifiNanSessionListener mockSubscribeSessionListener = mock(IWifiNanSessionListener.class);
100622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        InOrder inOrder = inOrder(mMockNative, mockListener, mockPublishSessionListener,
100722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                mockSubscribeSessionListener);
100822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
100922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        int allEvents = WifiNanEventListener.LISTEN_CONFIG_COMPLETED
101022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanEventListener.LISTEN_CONFIG_FAILED
101122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanEventListener.LISTEN_IDENTITY_CHANGED
101222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanEventListener.LISTEN_NAN_DOWN;
101322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
101422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        int allSessionEvents = WifiNanSessionListener.LISTEN_PUBLISH_FAIL
101522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_PUBLISH_TERMINATED
101622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_FAIL
101722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_SUBSCRIBE_TERMINATED
101822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MATCH
101922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_SUCCESS
102022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_SEND_FAIL
102122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                | WifiNanSessionListener.LISTEN_MESSAGE_RECEIVED;
102222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
102322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.connect(uid, mockListener, allEvents);
102422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.requestConfig(uid, configRequest);
102522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.createSession(uid, publishSessionId, mockPublishSessionListener, allSessionEvents);
102622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.publish(uid, publishSessionId, publishData, publishSettings);
102722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.createSession(uid, subscribeSessionId, mockSubscribeSessionListener, allSessionEvents);
102822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.subscribe(uid, subscribeSessionId, subscribeData, subscribeSettings);
102922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.destroySession(uid, publishSessionId);
103022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
103122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
103222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest));
103322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionIdConfig = transactionId.getValue();
103422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
103522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).publish(transactionId.capture(), eq(0), eq(publishData),
103622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                eq(publishSettings));
103722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionIdPublish = transactionId.getValue();
103822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
103922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mMockNative).subscribe(transactionId.capture(), eq(0), eq(subscribeData),
104022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                eq(subscribeSettings));
104122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short transactionIdSubscribe = transactionId.getValue();
104222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
104322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        validateInternalTransactionInfoCleanedUp(transactionIdPublish);
104422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
104522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onConfigCompleted(transactionIdConfig);
104622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onPublishSuccess(transactionIdPublish, publishId);
104722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onSubscribeSuccess(transactionIdSubscribe, subscribeId);
104822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
104922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
105022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.onPublishTerminated(publishId, reason);
105122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mDut.destroySession(uid, subscribeSessionId);
105222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        mMockLooper.dispatchAll();
105322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
105422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        inOrder.verify(mockListener).onConfigCompleted(configRequest);
105522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        verifyZeroInteractions(mockPublishSessionListener);
105622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        verifyNoMoreInteractions(mockSubscribeSessionListener);
105722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    }
105822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
105922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    @Test
106022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    public void testTransactionIdIncrement() {
106122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        int loopCount = 100;
106222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
106322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        short prevId = 0;
106422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        for (int i = 0; i < loopCount; ++i) {
106522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen            short id = mDut.createNextTransactionId();
106622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen            if (i != 0) {
106722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                assertTrue("Transaction ID incrementing", id > prevId);
106822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen            }
106922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen            prevId = id;
107022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        }
107122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    }
107222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
107322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    /*
107422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * Tests of internal state of WifiNanStateManager: very limited (not usually
107522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * a good idea). However, these test that the internal state is cleaned-up
107622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * appropriately. Alternatively would cause issues with memory leaks or
107722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * information leak between sessions.
107822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     */
107922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
108022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    /**
108122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * Utility routine used to validate that the internal state is cleaned-up
108222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * after the specific transaction ID. To be used in every test which
108322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * involves a transaction.
108422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     *
108522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * @param transactionId The transaction ID whose state should be erased.
108622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     */
108722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    public void validateInternalTransactionInfoCleanedUp(short transactionId) throws Exception {
108822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        Object info = getInternalPendingTransactionInfo(mDut, transactionId);
108922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        collector.checkThat("Transaction record not cleared up for transactionId=" + transactionId,
109022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen                info, nullValue());
109122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    }
109222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
109322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    /**
109422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * Utility routine used to validate that the internal state is cleaned-up
109522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * after a client is disconnected. To be used in every test which terminates
109622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * a client.
109722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     *
109822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     * @param uid The ID of the client which should be deleted.
109922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen     */
110022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    public void validateInternalClientInfoCleanedUp(int uid) throws Exception {
110122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        WifiNanClientState client = getInternalClientState(mDut, uid);
110222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        collector.checkThat("Client record not cleared up for uid=" + uid, client, nullValue());
110322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    }
110422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
1105956f54b391677d78379729dd14518edddf3c7660Etan Cohen    /*
1106956f54b391677d78379729dd14518edddf3c7660Etan Cohen     * Utilities
1107956f54b391677d78379729dd14518edddf3c7660Etan Cohen     */
1108956f54b391677d78379729dd14518edddf3c7660Etan Cohen
110922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    private static WifiNanStateManager installNewNanStateManagerAndResetState() throws Exception {
1110956f54b391677d78379729dd14518edddf3c7660Etan Cohen        Constructor<WifiNanStateManager> ctr = WifiNanStateManager.class.getDeclaredConstructor();
1111956f54b391677d78379729dd14518edddf3c7660Etan Cohen        ctr.setAccessible(true);
1112956f54b391677d78379729dd14518edddf3c7660Etan Cohen        WifiNanStateManager nanStateManager = ctr.newInstance();
1113956f54b391677d78379729dd14518edddf3c7660Etan Cohen
111422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        Field field = WifiNanStateManager.class.getDeclaredField("sNanStateManagerSingleton");
1115956f54b391677d78379729dd14518edddf3c7660Etan Cohen        field.setAccessible(true);
1116956f54b391677d78379729dd14518edddf3c7660Etan Cohen        field.set(null, nanStateManager);
1117956f54b391677d78379729dd14518edddf3c7660Etan Cohen
1118956f54b391677d78379729dd14518edddf3c7660Etan Cohen        return WifiNanStateManager.getInstance();
1119956f54b391677d78379729dd14518edddf3c7660Etan Cohen    }
1120956f54b391677d78379729dd14518edddf3c7660Etan Cohen
1121956f54b391677d78379729dd14518edddf3c7660Etan Cohen    private static void installMockWifiNanNative(WifiNanNative obj) throws Exception {
1122956f54b391677d78379729dd14518edddf3c7660Etan Cohen        Field field = WifiNanNative.class.getDeclaredField("sWifiNanNativeSingleton");
1123956f54b391677d78379729dd14518edddf3c7660Etan Cohen        field.setAccessible(true);
1124956f54b391677d78379729dd14518edddf3c7660Etan Cohen        field.set(null, obj);
1125956f54b391677d78379729dd14518edddf3c7660Etan Cohen    }
112622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
112722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    private static Object getInternalPendingTransactionInfo(WifiNanStateManager dut,
112822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen            short transactionId) throws Exception {
112922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        Field field = WifiNanStateManager.class.getDeclaredField("mPendingResponses");
113022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        field.setAccessible(true);
113122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        @SuppressWarnings("unchecked")
113222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        SparseArray<Object> pendingResponses = (SparseArray<Object>) field.get(dut);
113322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
113422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        return pendingResponses.get(transactionId);
113522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    }
113622b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
113722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    private static WifiNanClientState getInternalClientState(WifiNanStateManager dut,
113822b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen            int uid) throws Exception {
113922b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        Field field = WifiNanStateManager.class.getDeclaredField("mClients");
114022b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        field.setAccessible(true);
114122b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        @SuppressWarnings("unchecked")
114222b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        SparseArray<WifiNanClientState> clients = (SparseArray<WifiNanClientState>) field.get(dut);
114322b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
114422b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen        return clients.get(uid);
114522b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen    }
1146956f54b391677d78379729dd14518edddf3c7660Etan Cohen}
114722b4156d2948e3108ae8439dc72f76fb97526aceEtan Cohen
1148