1 /*
2  * Copyright 2017, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.managedprovisioning.model;
18 
19 import static org.hamcrest.CoreMatchers.equalTo;
20 import static org.hamcrest.CoreMatchers.not;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertThat;
23 
24 import android.os.Parcel;
25 
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.robolectric.RobolectricTestRunner;
29 
30 /**
31  * Robolectric tests for {@link WifiInfo}.
32  */
33 @RunWith(RobolectricTestRunner.class)
34 public class WifiInfoRoboTest {
35     private static final String TEST_SSID = "TestWifi";
36     private static final boolean TEST_HIDDEN = true;
37     private static final String TEST_SECURITY_TYPE = "WPA2";
38     private static final String TEST_PASSWORD = "TestPassword";
39     private static final String TEST_PROXY_HOST = "proxy.example.com";
40     private static final int TEST_PROXY_PORT = 7689;
41     private static final String TEST_PROXY_BYPASS_HOSTS = "http://host1.com;https://host2.com";
42     private static final String TEST_PAC_URL = "pac.example.com";
43 
44     @Test
testBuilderWriteAndReadBack()45     public void testBuilderWriteAndReadBack() {
46         // WHEN a WifiInfo object is constructed by a set of parameters.
47         WifiInfo wifiInfo = WifiInfo.Builder.builder()
48                 .setSsid(TEST_SSID)
49                 .setHidden(TEST_HIDDEN)
50                 .setSecurityType(TEST_SECURITY_TYPE)
51                 .setPassword(TEST_PASSWORD)
52                 .setProxyHost(TEST_PROXY_HOST)
53                 .setProxyPort(TEST_PROXY_PORT)
54                 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
55                 .setPacUrl(TEST_PAC_URL)
56                 .build();
57         // THEN the same set of values are set to the object.
58         assertEquals(TEST_SSID, wifiInfo.ssid);
59         assertEquals(TEST_HIDDEN, wifiInfo.hidden);
60         assertEquals(TEST_SECURITY_TYPE, wifiInfo.securityType);
61         assertEquals(TEST_PASSWORD, wifiInfo.password);
62         assertEquals(TEST_PROXY_HOST, wifiInfo.proxyHost);
63         assertEquals(TEST_PROXY_PORT, wifiInfo.proxyPort);
64         assertEquals(TEST_PROXY_BYPASS_HOSTS, wifiInfo.proxyBypassHosts);
65         assertEquals(TEST_PAC_URL, wifiInfo.pacUrl);
66     }
67 
68     @Test(expected = IllegalArgumentException.class)
testFailToConstructWifiInfoWithoutSsid()69     public void testFailToConstructWifiInfoWithoutSsid() {
70         WifiInfo.Builder.builder()
71                 .setHidden(TEST_HIDDEN)
72                 .setSecurityType(TEST_SECURITY_TYPE)
73                 .setPassword(TEST_PASSWORD)
74                 .setProxyHost(TEST_PROXY_HOST)
75                 .setProxyPort(TEST_PROXY_PORT)
76                 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
77                 .setPacUrl(TEST_PAC_URL)
78                 .build();
79     }
80 
81     @Test
testEquals()82     public void testEquals() {
83         // GIVEN 2 WifiInfo objects are constructed with the same set of parameters.
84         WifiInfo wifiInfo1 = WifiInfo.Builder.builder()
85                 .setSsid(TEST_SSID)
86                 .setHidden(TEST_HIDDEN)
87                 .setSecurityType(TEST_SECURITY_TYPE)
88                 .setPassword(TEST_PASSWORD)
89                 .setProxyHost(TEST_PROXY_HOST)
90                 .setProxyPort(TEST_PROXY_PORT)
91                 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
92                 .setPacUrl(TEST_PAC_URL)
93                 .build();
94         WifiInfo wifiInfo2 = WifiInfo.Builder.builder()
95                 .setSsid(TEST_SSID)
96                 .setHidden(TEST_HIDDEN)
97                 .setSecurityType(TEST_SECURITY_TYPE)
98                 .setPassword(TEST_PASSWORD)
99                 .setProxyHost(TEST_PROXY_HOST)
100                 .setProxyPort(TEST_PROXY_PORT)
101                 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
102                 .setPacUrl(TEST_PAC_URL)
103                 .build();
104         // WHEN comparing these two objects.
105         // THEN they are equal.
106         assertEquals(wifiInfo1, wifiInfo2);
107     }
108 
109     @Test
testNotEquals()110     public void testNotEquals() {
111         // GIVEN 2 WifiInfo objects are constructed with the same set of parameters.
112         WifiInfo wifiInfo1 = WifiInfo.Builder.builder()
113                 .setSsid(TEST_SSID)
114                 .setHidden(TEST_HIDDEN)
115                 .setSecurityType(TEST_SECURITY_TYPE)
116                 .setPassword(TEST_PASSWORD)
117                 .setProxyHost(TEST_PROXY_HOST)
118                 .setProxyPort(TEST_PROXY_PORT)
119                 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
120                 .setPacUrl(TEST_PAC_URL)
121                 .build();
122         WifiInfo wifiInfo2 = WifiInfo.Builder.builder()
123                 .setSsid("TestWifi2")
124                 .setHidden(TEST_HIDDEN)
125                 .setSecurityType(TEST_SECURITY_TYPE)
126                 .setPassword(TEST_PASSWORD)
127                 .setProxyHost(TEST_PROXY_HOST)
128                 .setProxyPort(TEST_PROXY_PORT)
129                 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
130                 .setPacUrl(TEST_PAC_URL)
131                 .build();
132         // WHEN comparing these two objects.
133         // THEN they are not equal.
134         assertThat(wifiInfo2, not(equalTo(wifiInfo1)));
135     }
136 
137     @Test
testParceable()138     public void testParceable() {
139         // GIVEN a WifiInfo object.
140         WifiInfo expectedWifiInfo = WifiInfo.Builder.builder()
141                 .setSsid(TEST_SSID)
142                 .setHidden(TEST_HIDDEN)
143                 .setSecurityType(TEST_SECURITY_TYPE)
144                 .setPassword(TEST_PASSWORD)
145                 .setProxyHost(TEST_PROXY_HOST)
146                 .setProxyPort(TEST_PROXY_PORT)
147                 .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
148                 .setPacUrl(TEST_PAC_URL)
149                 .build();
150 
151         // WHEN the WifiInfo is written to parcel and then read back.
152         Parcel parcel = Parcel.obtain();
153         expectedWifiInfo.writeToParcel(parcel, 0);
154         parcel.setDataPosition(0);
155         WifiInfo actualWifiInfo = WifiInfo.CREATOR.createFromParcel(parcel);
156 
157         // THEN the same WifiInfo is obtained.
158         assertEquals(expectedWifiInfo, actualWifiInfo);
159     }
160 }
161