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 18 19 import android.net.wifi.aware.DiscoverySession 20 import android.net.wifi.aware.PeerHandle 21 import android.net.wifi.aware.WifiAwareNetworkSpecifier 22 import android.os.Build 23 import androidx.test.filters.SmallTest 24 import androidx.test.runner.AndroidJUnit4 25 import com.android.testutils.ConnectivityModuleTest 26 import com.android.testutils.DevSdkIgnoreRule 27 import com.android.testutils.DevSdkIgnoreRule.IgnoreAfter 28 import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo 29 import com.android.testutils.assertParcelingIsLossless 30 import org.junit.Assert.assertFalse 31 import org.junit.Rule 32 import org.junit.Test 33 import org.junit.runner.RunWith 34 import org.mockito.Mockito 35 36 @RunWith(AndroidJUnit4::class) 37 @SmallTest 38 @ConnectivityModuleTest 39 class MatchAllNetworkSpecifierTest { 40 @Rule @JvmField 41 val ignoreRule: DevSdkIgnoreRule = DevSdkIgnoreRule() 42 43 private val specifier = MatchAllNetworkSpecifier() 44 private val discoverySession = Mockito.mock(DiscoverySession::class.java) 45 private val peerHandle = Mockito.mock(PeerHandle::class.java) 46 private val wifiAwareNetworkSpecifier = WifiAwareNetworkSpecifier.Builder(discoverySession, 47 peerHandle).build() 48 49 @Test testParcelnull50 fun testParcel() { 51 assertParcelingIsLossless(MatchAllNetworkSpecifier()) 52 } 53 54 @Test 55 @IgnoreAfter(Build.VERSION_CODES.R) 56 // Only run this test on Android R. 57 // The method - satisfiedBy() has changed to canBeSatisfiedBy() starting from Android R, so the 58 // method - canBeSatisfiedBy() cannot be found when running this test on Android Q. testCanBeSatisfiedBy_OnlyForRnull59 fun testCanBeSatisfiedBy_OnlyForR() { 60 // MatchAllNetworkSpecifier didn't follow its parent class to change the satisfiedBy() to 61 // canBeSatisfiedBy(), so if a caller calls MatchAllNetworkSpecifier#canBeSatisfiedBy(), the 62 // NetworkSpecifier#canBeSatisfiedBy() will be called actually, and false will be returned. 63 // Although it's not meeting the expectation, the behavior still needs to be verified. 64 assertFalse(specifier.canBeSatisfiedBy(wifiAwareNetworkSpecifier)) 65 } 66 67 @Test(expected = IllegalStateException::class) 68 @IgnoreUpTo(Build.VERSION_CODES.R) testCanBeSatisfiedBynull69 fun testCanBeSatisfiedBy() { 70 specifier.canBeSatisfiedBy(wifiAwareNetworkSpecifier) 71 } 72 } 73