1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package android.net.wifi.hotspot2;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertTrue;
21
22import android.graphics.drawable.Icon;
23import android.net.Uri;
24import android.net.wifi.WifiSsid;
25import android.os.Parcel;
26import android.support.test.filters.SmallTest;
27
28import org.junit.Test;
29
30import java.nio.charset.StandardCharsets;
31import java.util.ArrayList;
32import java.util.Arrays;
33import java.util.List;
34
35/**
36 * Unit tests for {@link android.net.wifi.hotspot2.OsuProvider}.
37 */
38@SmallTest
39public class OsuProviderTest {
40    private static final WifiSsid TEST_SSID =
41            WifiSsid.createFromByteArray("TEST SSID".getBytes(StandardCharsets.UTF_8));
42    private static final String TEST_FRIENDLY_NAME = "Friendly Name";
43    private static final String TEST_SERVICE_DESCRIPTION = "Dummy Service";
44    private static final Uri TEST_SERVER_URI = Uri.parse("https://test.com");
45    private static final String TEST_NAI = "test.access.com";
46    private static final List<Integer> TEST_METHOD_LIST =
47            Arrays.asList(OsuProvider.METHOD_SOAP_XML_SPP);
48    private static final Icon TEST_ICON = Icon.createWithData(new byte[10], 0, 10);
49
50    /**
51     * Verify parcel write and read consistency for the given {@link OsuProvider}.
52     *
53     * @param writeInfo The {@link OsuProvider} to verify
54     * @throws Exception
55     */
56    private static void verifyParcel(OsuProvider writeInfo) throws Exception {
57        Parcel parcel = Parcel.obtain();
58        writeInfo.writeToParcel(parcel, 0);
59
60        parcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
61        OsuProvider readInfo = OsuProvider.CREATOR.createFromParcel(parcel);
62        assertEquals(writeInfo, readInfo);
63    }
64
65    /**
66     * Verify parcel read/write for an OSU provider containing no information.
67     *
68     * @throws Exception
69     */
70    @Test
71    public void verifyParcelWithEmptyProviderInfo() throws Exception {
72        verifyParcel(new OsuProvider(null, null, null, null, null, null, null));
73    }
74
75    /**
76     * Verify parcel read/write for an OSU provider containing full information.
77     *
78     * @throws Exception
79     */
80    @Test
81    public void verifyParcelWithFullProviderInfo() throws Exception {
82        verifyParcel(new OsuProvider(TEST_SSID, TEST_FRIENDLY_NAME, TEST_SERVICE_DESCRIPTION,
83                TEST_SERVER_URI, TEST_NAI, TEST_METHOD_LIST, TEST_ICON));
84    }
85
86    /**
87     * Verify copy constructor with a null source.
88     * @throws Exception
89     */
90    @Test
91    public void verifyCopyConstructorWithNullSource() throws Exception {
92        OsuProvider expected = new OsuProvider(null, null, null, null, null, null, null);
93        assertEquals(expected, new OsuProvider(null));
94    }
95
96    /**
97     * Verify copy constructor with a valid source.
98     *
99     * @throws Exception
100     */
101    @Test
102    public void verifyCopyConstructorWithValidSource() throws Exception {
103        OsuProvider source = new OsuProvider(TEST_SSID, TEST_FRIENDLY_NAME, TEST_SERVICE_DESCRIPTION,
104                TEST_SERVER_URI, TEST_NAI, TEST_METHOD_LIST, TEST_ICON);
105        assertEquals(source, new OsuProvider(source));
106    }
107
108    /**
109     * Verify getter methods.
110     *
111     * @throws Exception
112     */
113    @Test
114    public void verifyGetters() throws Exception {
115        OsuProvider provider = new OsuProvider(TEST_SSID, TEST_FRIENDLY_NAME,
116                TEST_SERVICE_DESCRIPTION, TEST_SERVER_URI, TEST_NAI, TEST_METHOD_LIST, TEST_ICON);
117        assertTrue(TEST_SSID.equals(provider.getOsuSsid()));
118        assertTrue(TEST_FRIENDLY_NAME.equals(provider.getFriendlyName()));
119        assertTrue(TEST_SERVICE_DESCRIPTION.equals(provider.getServiceDescription()));
120        assertTrue(TEST_SERVER_URI.equals(provider.getServerUri()));
121        assertTrue(TEST_NAI.equals(provider.getNetworkAccessIdentifier()));
122        assertTrue(TEST_METHOD_LIST.equals(provider.getMethodList()));
123        assertTrue(TEST_ICON.sameAs(provider.getIcon()));
124    }
125}
126