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 junit.framework.Assert.assertTrue; 20 21import android.net.wifi.RttManager; 22import android.net.wifi.RttManager.ParcelableRttParams; 23import android.net.wifi.RttManager.RttParams; 24import android.os.Parcel; 25 26import org.junit.Test; 27 28/** 29 * Unit test for {@link RttManager} 30 */ 31public class RttManagerTest { 32 33 // Verify ParcelableRttParams are the same after writing and reading from parcel. 34 private void verifyReadWriteParcelForRttParams(ParcelableRttParams params) { 35 Parcel parcel = Parcel.obtain(); 36 params.writeToParcel(parcel, 0); 37 parcel.setDataPosition(0); 38 ParcelableRttParams paramsFromParcel = ParcelableRttParams.CREATOR.createFromParcel(parcel); 39 assertTrue(verifyEquals(params, paramsFromParcel)); 40 } 41 42 // Check if two ParcelableRttParams equals. 43 private boolean verifyEquals(ParcelableRttParams params, ParcelableRttParams params2) { 44 if (params.mParams == params2.mParams) { 45 return true; 46 } 47 if (params == null || params2.mParams == null) { 48 return false; 49 } 50 RttParams[] paramsArray = params.mParams; 51 RttParams[] paramsArray2 = params2.mParams; 52 if (paramsArray.length != paramsArray2.length) { 53 return false; 54 } 55 for (int i = 0; i < paramsArray.length; i++) { 56 if (!rttParamsEquals(paramsArray[i], paramsArray2[i])) { 57 return false; 58 } 59 } 60 return true; 61 } 62 63 // Check if two RttParams equals. Note only a subset of fields are checked. 64 private boolean rttParamsEquals(RttParams params1, RttParams params2) { 65 return params1.bssid.equals(params2.bssid) 66 && params1.secure == params2.secure 67 && params1.frequency == params2.frequency; 68 } 69 70 /** 71 * Test writing and reading {@link RttParams} from Parcel. 72 */ 73 @Test 74 public void testRttParamsReadWriteParcel() throws Exception { 75 RttParams params = new RttParams(); 76 params.bssid = "12-34-56-78-9A-BC"; 77 params.secure = true; 78 params.frequency = 5240; 79 80 RttParams params2 = new RttParams(); 81 params2.bssid = "12-34-56-78-9B-CD"; 82 params2.secure = false; 83 params2.frequency = 5220; 84 85 ParcelableRttParams parcelableParams = new ParcelableRttParams(new RttParams[] { 86 params, params2 87 }); 88 verifyReadWriteParcelForRttParams(parcelableParams); 89 // Make sure writing/reading parcel doesn't change value for empty RttParams. 90 verifyReadWriteParcelForRttParams(new ParcelableRttParams(new RttParams[0])); 91 } 92 93} 94