1 /*
2  * Copyright (C) 2022 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.companion.cts.noservices
18 
19 import android.Manifest.permission.REQUEST_COMPANION_SELF_MANAGED
20 import android.companion.cts.common.DEVICE_DISPLAY_NAME_A
21 import android.companion.cts.common.MissingIntentFilterActionCompanionService
22 import android.companion.cts.common.MissingPermissionCompanionService
23 import android.companion.cts.common.PrimaryCompanionService
24 import android.companion.cts.common.SecondaryCompanionService
25 import android.companion.cts.common.TestBase
26 import android.companion.cts.common.waitFor
27 import android.platform.test.annotations.AppModeFull
28 import androidx.test.ext.junit.runners.AndroidJUnit4
29 import kotlin.test.assertFalse
30 import kotlin.time.Duration.Companion.milliseconds
31 import kotlin.time.Duration.Companion.seconds
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 
35 /**
36  * Tests CDM handling the case when the companion application does not define a valid
37  * [CompanionDeviceService][android.companion.CompanionDeviceService].
38  *
39  * Build/Install/Run:
40  * atest CtsCompanionDeviceManagerNoCompanionServicesTestCases:NoCompanionDeviceServiceTest
41  */
42 @AppModeFull(reason = "CompanionDeviceManager APIs are not available to the instant apps.")
43 @RunWith(AndroidJUnit4::class)
44 class NoCompanionDeviceServiceTest : TestBase() {
45 
46     /**
47      * Ensures that CDM service DOES NOT try to bind
48      * [CompanionDeviceServices][android.companion.CompanionDeviceService] that do not meet all the
49      * requirements, as well as that the system's stability in case when the companion applications
50      * do not define any valid CompanionDeviceServices.
51      */
52     @Test
test_noServicenull53     fun test_noService() {
54         val associationId = createSelfManagedAssociation(DEVICE_DISPLAY_NAME_A)
55 
56         // This should neither throw an Exception nor cause system to crash, even when the
57         // companion application does not define any valid CompanionDeviceServices.
58         // (If the system crashes this instrumentation test won't complete).
59         withShellPermissionIdentity(REQUEST_COMPANION_SELF_MANAGED) {
60             cdm.notifyDeviceAppeared(associationId)
61         }
62 
63         // Every 100ms check if any of the services is bound or received a callback.
64         assertFalse("None of the services should be bound or receive a callback") {
65             waitFor(timeout = 1.seconds, interval = 100.milliseconds) {
66                 val isBound = PrimaryCompanionService.isBound ||
67                         SecondaryCompanionService.isBound ||
68                         MissingPermissionCompanionService.isBound ||
69                         MissingIntentFilterActionCompanionService.isBound
70                 val receivedCallback = PrimaryCompanionService.connectedDevices.isNotEmpty() ||
71                         SecondaryCompanionService.connectedDevices.isNotEmpty() ||
72                         MissingPermissionCompanionService.connectedDevices.isNotEmpty() ||
73                         MissingIntentFilterActionCompanionService.connectedDevices.isNotEmpty()
74                 return@waitFor isBound || receivedCallback
75             }
76         }
77 
78         // This should neither throw an Exception nor cause system to crash, even when the
79         // companion application does not define any valid CompanionDeviceServices.
80         // (If the system crashes this instrumentation test won't complete).
81         withShellPermissionIdentity(REQUEST_COMPANION_SELF_MANAGED) {
82             cdm.notifyDeviceDisappeared(associationId)
83         }
84     }
85 }
86