1 /*
2  * Copyright (c) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net.wifi;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.Mockito.validateMockitoUsage;
21 
22 import android.os.Parcel;
23 
24 import androidx.test.filters.SmallTest;
25 
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.MockitoAnnotations;
30 
31 
32 /**
33  * Unit tests for {@link android.net.wifi.WifiConnectedSessionInfo}.
34  */
35 @SmallTest
36 public class WifiConnectedSessionInfoTest {
37     /**
38      * Setup before tests.
39      */
40     @Before
setUp()41     public void setUp() throws Exception {
42         MockitoAnnotations.initMocks(this);
43     }
44 
45     /**
46      * Clean up after tests.
47      */
48     @After
cleanup()49     public void cleanup() {
50         validateMockitoUsage();
51     }
52 
53     /**
54      * Verify parcel read/write for Wifi connected session information.
55      */
56     @Test
verifySessionInfoWriteAndThenRead()57     public void verifySessionInfoWriteAndThenRead() throws Exception {
58         WifiConnectedSessionInfo writeResult = createResult();
59         WifiConnectedSessionInfo readResult = parcelWriteRead(writeResult);
60         assertWifiConnectedSessionInfoEquals(writeResult, readResult);
61     }
62 
63     /**
64      * Write the provided {@link WifiConnectedSessionInfo} to a parcel and deserialize it.
65      */
parcelWriteRead( WifiConnectedSessionInfo writeResult)66     private static WifiConnectedSessionInfo parcelWriteRead(
67             WifiConnectedSessionInfo writeResult) throws Exception {
68         Parcel parcel = Parcel.obtain();
69         writeResult.writeToParcel(parcel, 0);
70         parcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
71         return WifiConnectedSessionInfo.CREATOR.createFromParcel(parcel);
72     }
73 
createResult()74     private static WifiConnectedSessionInfo createResult() {
75         return new WifiConnectedSessionInfo.Builder(101)
76                 .setUserSelected(true)
77                 .build();
78     }
79 
assertWifiConnectedSessionInfoEquals( WifiConnectedSessionInfo expected, WifiConnectedSessionInfo actual)80     private static void assertWifiConnectedSessionInfoEquals(
81             WifiConnectedSessionInfo expected,
82             WifiConnectedSessionInfo actual) {
83         assertEquals(expected.getSessionId(), actual.getSessionId());
84         assertEquals(expected.isUserSelected(), actual.isUserSelected());
85     }
86 }
87