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