1 /*
2  * Copyright (C) 2020 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 
17 package android.net.wifi;
18 
19 import static android.net.wifi.CoexUnsafeChannel.POWER_CAP_NONE;
20 import static android.net.wifi.WifiScanner.WIFI_BAND_24_GHZ;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.junit.Assume.assumeTrue;
25 
26 import android.os.Parcel;
27 
28 import androidx.test.filters.SmallTest;
29 
30 import com.android.modules.utils.build.SdkLevel;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 
35 /**
36  * Unit tests for {@link android.net.wifi.CoexUnsafeChannel}.
37  */
38 @SmallTest
39 public class CoexUnsafeChannelTest {
40     @Before
setUp()41     public void setUp() {
42         assumeTrue(SdkLevel.isAtLeastS());
43     }
44 
45     /**
46      * Verifies {@link CoexUnsafeChannel#getPowerCapDbm()} returns POWER_CAP_NONE if no cap is set.
47      */
48     @Test
testGetPowerCapDbm_noPowerCap_returnsPowerCapNone()49     public void testGetPowerCapDbm_noPowerCap_returnsPowerCapNone() {
50         CoexUnsafeChannel unsafeChannel = new CoexUnsafeChannel(WIFI_BAND_24_GHZ, 6);
51 
52         assertThat(unsafeChannel.getPowerCapDbm()).isEqualTo(POWER_CAP_NONE);
53     }
54 
55     /**
56      * Verifies {@link CoexUnsafeChannel#getPowerCapDbm()} returns a power cap if a cap is set.
57      */
58     @Test
testGetPowerCapDbm_powerCapSet_returnsSetPowerCap()59     public void testGetPowerCapDbm_powerCapSet_returnsSetPowerCap() {
60         CoexUnsafeChannel unsafeChannel = new CoexUnsafeChannel(WIFI_BAND_24_GHZ, 6, -50);
61 
62         assertThat(unsafeChannel.getPowerCapDbm()).isEqualTo(-50);
63     }
64 
65     /**
66      * Verify parcel read/write for CoexUnsafeChannel with or without power cap.
67      */
68     @Test
testParcelReadWrite_withOrWithoutCap_readEqualsWritten()69     public void testParcelReadWrite_withOrWithoutCap_readEqualsWritten() throws Exception {
70         CoexUnsafeChannel writeUnsafeChannelNoCap =
71                 new CoexUnsafeChannel(WIFI_BAND_24_GHZ, 6);
72         CoexUnsafeChannel writeUnsafeChannelCapped =
73                 new CoexUnsafeChannel(WIFI_BAND_24_GHZ, 6, -50);
74 
75         CoexUnsafeChannel readUnsafeChannelNoCap = parcelReadWrite(writeUnsafeChannelNoCap);
76         CoexUnsafeChannel readUnsafeChannelCapped = parcelReadWrite(writeUnsafeChannelCapped);
77 
78         assertThat(writeUnsafeChannelNoCap).isEqualTo(readUnsafeChannelNoCap);
79         assertThat(writeUnsafeChannelCapped).isEqualTo(readUnsafeChannelCapped);
80     }
81 
82     /**
83      * Write the provided {@link CoexUnsafeChannel} to a parcel and deserialize it.
84      */
parcelReadWrite(CoexUnsafeChannel writeResult)85     private static CoexUnsafeChannel parcelReadWrite(CoexUnsafeChannel writeResult)
86             throws Exception {
87         Parcel parcel = Parcel.obtain();
88         writeResult.writeToParcel(parcel, 0);
89         parcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
90         return CoexUnsafeChannel.CREATOR.createFromParcel(parcel);
91     }
92 }
93