1 /*
2  * Copyright (C) 2019 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.metrics
18 
19 import androidx.test.filters.SmallTest
20 import androidx.test.runner.AndroidJUnit4
21 import com.android.testutils.assertParcelingIsLossless
22 import java.lang.reflect.Modifier
23 import org.junit.Assert.assertEquals
24 import org.junit.Assert.assertTrue
25 import org.junit.Test
26 import org.junit.runner.RunWith
27 
28 private const val FIRST_VALIDATION: Int = 1 shl 8
29 private const val REVALIDATION: Int = 2 shl 8
30 
31 @RunWith(AndroidJUnit4::class)
32 @SmallTest
33 class ValidationProbeEventTest {
hasTypenull34     private infix fun Int.hasType(type: Int) = (type and this) == type
35 
36     @Test
37     fun testBuilderAndParcel() {
38         var validationProbeEvent = ValidationProbeEvent.Builder()
39                 .setProbeType(ValidationProbeEvent.PROBE_DNS, false).build()
40 
41         assertTrue(validationProbeEvent.probeType hasType REVALIDATION)
42 
43         validationProbeEvent = ValidationProbeEvent.Builder()
44                 .setDurationMs(Long.MAX_VALUE)
45                 .setProbeType(ValidationProbeEvent.PROBE_DNS, true)
46                 .setReturnCode(ValidationProbeEvent.DNS_SUCCESS)
47                 .build()
48 
49         assertEquals(Long.MAX_VALUE, validationProbeEvent.durationMs)
50         assertTrue(validationProbeEvent.probeType hasType ValidationProbeEvent.PROBE_DNS)
51         assertTrue(validationProbeEvent.probeType hasType FIRST_VALIDATION)
52         assertEquals(ValidationProbeEvent.DNS_SUCCESS, validationProbeEvent.returnCode)
53 
54         assertParcelingIsLossless(validationProbeEvent)
55     }
56 
57     @Test
testGetProbeNamenull58     fun testGetProbeName() {
59         val probeFields = ValidationProbeEvent::class.java.declaredFields.filter {
60             it.type == Int::class.javaPrimitiveType
61               && Modifier.isPublic(it.modifiers) && Modifier.isStatic(it.modifiers)
62               && it.name.contains("PROBE")
63         }
64 
65         probeFields.forEach {
66             val intValue = it.getInt(null)
67             val stringValue = ValidationProbeEvent.getProbeName(intValue)
68             assertEquals(it.name, stringValue)
69         }
70 
71     }
72 }
73