1/*
2 * Copyright (C) 2016 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.server.wifi;
18
19import static org.junit.Assert.assertEquals;
20import static org.mockito.Mockito.anyString;
21import static org.mockito.Mockito.times;
22import static org.mockito.Mockito.verify;
23import static org.mockito.Mockito.when;
24
25import android.test.suitebuilder.annotation.SmallTest;
26
27import org.junit.Before;
28import org.junit.Test;
29import org.mockito.Mock;
30import org.mockito.MockitoAnnotations;
31
32/**
33 * Unit tests for {@link com.android.server.wifi.WifiCountryCode}.
34 */
35@SmallTest
36public class WifiCountryCodeTest {
37
38    private static final String TAG = "WifiCountryCodeTest";
39    private String mDefaultCountryCode = "US";
40    private String mTelephonyCountryCode = "JP";
41    private boolean mRevertCountryCodeOnCellularLoss = true;
42    @Mock WifiNative mWifiNative;
43    private WifiCountryCode mWifiCountryCode;
44
45    /**
46     * Setup test.
47     */
48    @Before
49    public void setUp() throws Exception {
50        MockitoAnnotations.initMocks(this);
51
52        when(mWifiNative.setCountryCode(anyString())).thenReturn(true);
53
54        mWifiCountryCode = new WifiCountryCode(
55                mWifiNative,
56                mDefaultCountryCode,
57                mRevertCountryCodeOnCellularLoss);
58    }
59
60    /**
61     * Test if we do not receive country code from Telephony.
62     * @throws Exception
63     */
64    @Test
65    public void useDefaultCountryCode() throws Exception {
66        // Supplicant started.
67        mWifiCountryCode.setReadyForChange(true);
68        // Wifi get L2 connected.
69        mWifiCountryCode.setReadyForChange(false);
70        verify(mWifiNative).setCountryCode(anyString());
71        assertEquals(mDefaultCountryCode, mWifiCountryCode.getCountryCodeSentToDriver());
72    }
73
74    /**
75     * Test if we receive country code from Telephony before supplicant starts.
76     * @throws Exception
77     */
78    @Test
79    public void useTelephonyCountryCode() throws Exception {
80        mWifiCountryCode.setCountryCode(mTelephonyCountryCode);
81        assertEquals(null, mWifiCountryCode.getCountryCodeSentToDriver());
82        // Supplicant started.
83        mWifiCountryCode.setReadyForChange(true);
84        // Wifi get L2 connected.
85        mWifiCountryCode.setReadyForChange(false);
86        verify(mWifiNative).setCountryCode(anyString());
87        assertEquals(mTelephonyCountryCode, mWifiCountryCode.getCountryCodeSentToDriver());
88    }
89
90    /**
91     * Test if we receive country code from Telephony after supplicant starts.
92     * @throws Exception
93     */
94    @Test
95    public void setTelephonyCountryCodeAfterSupplicantStarts() throws Exception {
96        // Supplicant starts.
97        mWifiCountryCode.setReadyForChange(true);
98        assertEquals(mDefaultCountryCode, mWifiCountryCode.getCountryCodeSentToDriver());
99        // Telephony country code arrives.
100        mWifiCountryCode.setCountryCode(mTelephonyCountryCode);
101        // Wifi get L2 connected.
102        mWifiCountryCode.setReadyForChange(false);
103        verify(mWifiNative, times(2)).setCountryCode(anyString());
104        assertEquals(mTelephonyCountryCode, mWifiCountryCode.getCountryCodeSentToDriver());
105    }
106
107    /**
108     * Test if we receive country code from Telephony after we get L2 connected.
109     * @throws Exception
110     */
111    @Test
112    public void setTelephonyCountryCodeAfterL2Connected() throws Exception {
113        // Supplicant starts.
114        mWifiCountryCode.setReadyForChange(true);
115        // Wifi get L2 connected.
116        mWifiCountryCode.setReadyForChange(false);
117        // Telephony country code arrives.
118        mWifiCountryCode.setCountryCode(mTelephonyCountryCode);
119        // Telephony coutry code won't be applied at this time.
120        assertEquals(mDefaultCountryCode, mWifiCountryCode.getCountryCodeSentToDriver());
121        mWifiCountryCode.setReadyForChange(true);
122        // Telephony coutry is applied after supplicant is ready.
123        verify(mWifiNative, times(2)).setCountryCode(anyString());
124        assertEquals(mTelephonyCountryCode, mWifiCountryCode.getCountryCodeSentToDriver());
125    }
126
127    /**
128     * Test if we can reset the country code upon sim card is removed.
129     * @throws Exception
130     */
131    @Test
132    public void resetCountryCodeWhenSIMCardRemoved() throws Exception {
133        mWifiCountryCode.setCountryCode(mTelephonyCountryCode);
134        // Supplicant started.
135        mWifiCountryCode.setReadyForChange(true);
136        // Wifi get L2 connected.
137        mWifiCountryCode.setReadyForChange(false);
138        assertEquals(mTelephonyCountryCode, mWifiCountryCode.getCountryCodeSentToDriver());
139        // SIM card is removed.
140        mWifiCountryCode.simCardRemoved();
141        // Country code restting is not applied yet.
142        assertEquals(mTelephonyCountryCode, mWifiCountryCode.getCountryCodeSentToDriver());
143        mWifiCountryCode.setReadyForChange(true);
144        // Country code restting is applied when supplicant is ready.
145        verify(mWifiNative, times(2)).setCountryCode(anyString());
146        assertEquals(mDefaultCountryCode, mWifiCountryCode.getCountryCodeSentToDriver());
147    }
148
149    /**
150     * Test if we can reset the country code upon airplane mode is enabled.
151     * @throws Exception
152     */
153    @Test
154    public void resetCountryCodeWhenAirplaneModeEnabled() throws Exception {
155        mWifiCountryCode.setCountryCode(mTelephonyCountryCode);
156        // Supplicant started.
157        mWifiCountryCode.setReadyForChange(true);
158        // Wifi get L2 connected.
159        mWifiCountryCode.setReadyForChange(false);
160        assertEquals(mTelephonyCountryCode, mWifiCountryCode.getCountryCodeSentToDriver());
161        // Airplane mode is enabled.
162        mWifiCountryCode.simCardRemoved();
163        // Country code restting is not applied yet.
164        assertEquals(mTelephonyCountryCode, mWifiCountryCode.getCountryCodeSentToDriver());
165        mWifiCountryCode.setReadyForChange(true);
166        // Country code restting is applied when supplicant is ready.
167        verify(mWifiNative, times(2)).setCountryCode(anyString());
168        assertEquals(mDefaultCountryCode, mWifiCountryCode.getCountryCodeSentToDriver());
169    }
170
171    /**
172     * Test if we can reset to the default country code when phone is out of service.
173     * Telephony service calls |setCountryCode| with an empty string when phone is out of service.
174     * In this case we should fall back to the default country code.
175     * @throws Exception
176     */
177    @Test
178    public void resetCountryCodeWhenOutOfService() throws Exception {
179        assertEquals(mDefaultCountryCode, mWifiCountryCode.getCountryCode());
180        mWifiCountryCode.setCountryCode(mTelephonyCountryCode);
181        assertEquals(mTelephonyCountryCode, mWifiCountryCode.getCountryCode());
182        // Out of service.
183        mWifiCountryCode.setCountryCode("");
184        assertEquals(mDefaultCountryCode, mWifiCountryCode.getCountryCode());
185    }
186
187}
188