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
17package com.android.managedprovisioning.model;
18
19import static org.hamcrest.CoreMatchers.equalTo;
20import static org.hamcrest.CoreMatchers.not;
21import static org.junit.Assert.assertEquals;
22import static org.junit.Assert.assertThat;
23
24import android.os.Parcel;
25
26import org.junit.Test;
27import org.junit.runner.RunWith;
28import org.robolectric.RobolectricTestRunner;
29import org.robolectric.annotation.Config;
30
31/**
32 * Robolectric tests for {@link WifiInfo}.
33 */
34@RunWith(RobolectricTestRunner.class)
35@Config(manifest = "packages/apps/ManagedProvisioning/AndroidManifest.xml",
36        sdk = 23)
37public class WifiInfoRoboTest {
38    private static final String TEST_SSID = "TestWifi";
39    private static final boolean TEST_HIDDEN = true;
40    private static final String TEST_SECURITY_TYPE = "WPA2";
41    private static final String TEST_PASSWORD = "TestPassword";
42    private static final String TEST_PROXY_HOST = "proxy.example.com";
43    private static final int TEST_PROXY_PORT = 7689;
44    private static final String TEST_PROXY_BYPASS_HOSTS = "http://host1.com;https://host2.com";
45    private static final String TEST_PAC_URL = "pac.example.com";
46
47    @Test
48    public void testBuilderWriteAndReadBack() {
49        // WHEN a WifiInfo object is constructed by a set of parameters.
50        WifiInfo wifiInfo = WifiInfo.Builder.builder()
51                .setSsid(TEST_SSID)
52                .setHidden(TEST_HIDDEN)
53                .setSecurityType(TEST_SECURITY_TYPE)
54                .setPassword(TEST_PASSWORD)
55                .setProxyHost(TEST_PROXY_HOST)
56                .setProxyPort(TEST_PROXY_PORT)
57                .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
58                .setPacUrl(TEST_PAC_URL)
59                .build();
60        // THEN the same set of values are set to the object.
61        assertEquals(TEST_SSID, wifiInfo.ssid);
62        assertEquals(TEST_HIDDEN, wifiInfo.hidden);
63        assertEquals(TEST_SECURITY_TYPE, wifiInfo.securityType);
64        assertEquals(TEST_PASSWORD, wifiInfo.password);
65        assertEquals(TEST_PROXY_HOST, wifiInfo.proxyHost);
66        assertEquals(TEST_PROXY_PORT, wifiInfo.proxyPort);
67        assertEquals(TEST_PROXY_BYPASS_HOSTS, wifiInfo.proxyBypassHosts);
68        assertEquals(TEST_PAC_URL, wifiInfo.pacUrl);
69    }
70
71    @Test(expected = IllegalArgumentException.class)
72    public void testFailToConstructWifiInfoWithoutSsid() {
73        WifiInfo.Builder.builder()
74                .setHidden(TEST_HIDDEN)
75                .setSecurityType(TEST_SECURITY_TYPE)
76                .setPassword(TEST_PASSWORD)
77                .setProxyHost(TEST_PROXY_HOST)
78                .setProxyPort(TEST_PROXY_PORT)
79                .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
80                .setPacUrl(TEST_PAC_URL)
81                .build();
82    }
83
84    @Test
85    public void testEquals() {
86        // GIVEN 2 WifiInfo objects are constructed with the same set of parameters.
87        WifiInfo wifiInfo1 = WifiInfo.Builder.builder()
88                .setSsid(TEST_SSID)
89                .setHidden(TEST_HIDDEN)
90                .setSecurityType(TEST_SECURITY_TYPE)
91                .setPassword(TEST_PASSWORD)
92                .setProxyHost(TEST_PROXY_HOST)
93                .setProxyPort(TEST_PROXY_PORT)
94                .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
95                .setPacUrl(TEST_PAC_URL)
96                .build();
97        WifiInfo wifiInfo2 = WifiInfo.Builder.builder()
98                .setSsid(TEST_SSID)
99                .setHidden(TEST_HIDDEN)
100                .setSecurityType(TEST_SECURITY_TYPE)
101                .setPassword(TEST_PASSWORD)
102                .setProxyHost(TEST_PROXY_HOST)
103                .setProxyPort(TEST_PROXY_PORT)
104                .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
105                .setPacUrl(TEST_PAC_URL)
106                .build();
107        // WHEN comparing these two objects.
108        // THEN they are equal.
109        assertEquals(wifiInfo1, wifiInfo2);
110    }
111
112    @Test
113    public void testNotEquals() {
114        // GIVEN 2 WifiInfo objects are constructed with the same set of parameters.
115        WifiInfo wifiInfo1 = WifiInfo.Builder.builder()
116                .setSsid(TEST_SSID)
117                .setHidden(TEST_HIDDEN)
118                .setSecurityType(TEST_SECURITY_TYPE)
119                .setPassword(TEST_PASSWORD)
120                .setProxyHost(TEST_PROXY_HOST)
121                .setProxyPort(TEST_PROXY_PORT)
122                .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
123                .setPacUrl(TEST_PAC_URL)
124                .build();
125        WifiInfo wifiInfo2 = WifiInfo.Builder.builder()
126                .setSsid("TestWifi2")
127                .setHidden(TEST_HIDDEN)
128                .setSecurityType(TEST_SECURITY_TYPE)
129                .setPassword(TEST_PASSWORD)
130                .setProxyHost(TEST_PROXY_HOST)
131                .setProxyPort(TEST_PROXY_PORT)
132                .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
133                .setPacUrl(TEST_PAC_URL)
134                .build();
135        // WHEN comparing these two objects.
136        // THEN they are not equal.
137        assertThat(wifiInfo2, not(equalTo(wifiInfo1)));
138    }
139
140    @Test
141    public void testParceable() {
142        // GIVEN a WifiInfo object.
143        WifiInfo expectedWifiInfo = WifiInfo.Builder.builder()
144                .setSsid(TEST_SSID)
145                .setHidden(TEST_HIDDEN)
146                .setSecurityType(TEST_SECURITY_TYPE)
147                .setPassword(TEST_PASSWORD)
148                .setProxyHost(TEST_PROXY_HOST)
149                .setProxyPort(TEST_PROXY_PORT)
150                .setProxyBypassHosts(TEST_PROXY_BYPASS_HOSTS)
151                .setPacUrl(TEST_PAC_URL)
152                .build();
153
154        // WHEN the WifiInfo is written to parcel and then read back.
155        Parcel parcel = Parcel.obtain();
156        expectedWifiInfo.writeToParcel(parcel, 0);
157        parcel.setDataPosition(0);
158        WifiInfo actualWifiInfo = WifiInfo.CREATOR.createFromParcel(parcel);
159
160        // THEN the same WifiInfo is obtained.
161        assertEquals(expectedWifiInfo, actualWifiInfo);
162    }
163}
164