WifiNativeTest.java revision 754267d3fd20438d771570e923f6a219783dc50c
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.junit.Assert.assertTrue; 21import static org.mockito.Mockito.anyInt; 22import static org.mockito.Mockito.anyString; 23import static org.mockito.Mockito.spy; 24import static org.mockito.Mockito.verify; 25import static org.mockito.Mockito.when; 26 27import android.test.suitebuilder.annotation.SmallTest; 28 29import org.junit.Before; 30import org.junit.Test; 31 32import java.lang.reflect.Constructor; 33import java.util.HashMap; 34import java.util.Map; 35 36/** 37 * Unit tests for {@link com.android.server.wifi.WifiNative}. 38 */ 39@SmallTest 40public class WifiNativeTest { 41 private static final int NETWORK_ID = 0; 42 private static final String NETWORK_EXTRAS_VARIABLE = "test"; 43 private static final Map<String, String> NETWORK_EXTRAS_VALUES = new HashMap<>(); 44 static { 45 NETWORK_EXTRAS_VALUES.put("key1", "value1"); 46 NETWORK_EXTRAS_VALUES.put("key2", "value2"); 47 } 48 private static final String NETWORK_EXTRAS_SERIALIZED = 49 "\"%7B%22key2%22%3A%22value2%22%2C%22key1%22%3A%22value1%22%7D\""; 50 51 private WifiNative mWifiNative; 52 53 @Before 54 public void setUp() throws Exception { 55 final Constructor<WifiNative> wifiNativeConstructor = 56 WifiNative.class.getDeclaredConstructor(String.class, Boolean.TYPE); 57 wifiNativeConstructor.setAccessible(true); 58 mWifiNative = spy(wifiNativeConstructor.newInstance("test", true)); 59 } 60 61 /** 62 * Verifies that setNetworkExtra() correctly writes a serialized and URL-encoded JSON object. 63 */ 64 @Test 65 public void testSetNetworkExtra() { 66 when(mWifiNative.setNetworkVariable(anyInt(), anyString(), anyString())).thenReturn(true); 67 assertTrue(mWifiNative.setNetworkExtra(NETWORK_ID, NETWORK_EXTRAS_VARIABLE, 68 NETWORK_EXTRAS_VALUES)); 69 verify(mWifiNative).setNetworkVariable(NETWORK_ID, NETWORK_EXTRAS_VARIABLE, 70 NETWORK_EXTRAS_SERIALIZED); 71 } 72 73 /** 74 * Verifies that getNetworkExtra() correctly reads a serialized and URL-encoded JSON object. 75 */ 76 @Test 77 public void testGetNetworkExtra() { 78 when(mWifiNative.getNetworkVariable(NETWORK_ID, NETWORK_EXTRAS_VARIABLE)) 79 .thenReturn(NETWORK_EXTRAS_SERIALIZED); 80 final Map<String, String> actualValues = 81 mWifiNative.getNetworkExtra(NETWORK_ID, NETWORK_EXTRAS_VARIABLE); 82 assertEquals(NETWORK_EXTRAS_VALUES, actualValues); 83 } 84} 85