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 org.junit.Test 30 import org.junit.runner.RunWith 31 import kotlin.test.assertFalse 32 import kotlin.time.Duration.Companion.milliseconds 33 import kotlin.time.Duration.Companion.seconds 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 withShellPermissionIdentity(REQUEST_COMPANION_SELF_MANAGED) { 55 val associationId = createSelfManagedAssociation(DEVICE_DISPLAY_NAME_A) 56 57 // This should neither throw an Exception nor cause system to crash, even when the 58 // companion application does not define any valid CompanionDeviceServices. 59 // (If the system crashes this instrumentation test won't complete). 60 cdm.notifyDeviceAppeared(associationId) 61 62 // Every 100ms check if any of the services is bound or received a callback. 63 assertFalse("None of the services should be bound or receive a callback") { 64 waitFor(timeout = 1.seconds, interval = 100.milliseconds) { 65 val isBound = PrimaryCompanionService.isBound || 66 SecondaryCompanionService.isBound || 67 MissingPermissionCompanionService.isBound || 68 MissingIntentFilterActionCompanionService.isBound 69 val receivedCallback = 70 PrimaryCompanionService.connectedDevices.isNotEmpty() || 71 SecondaryCompanionService.connectedDevices.isNotEmpty() || 72 MissingPermissionCompanionService.connectedDevices.isNotEmpty() || 73 MissingIntentFilterActionCompanionService.connectedDevices 74 .isNotEmpty() 75 return@waitFor isBound || receivedCallback 76 } 77 } 78 79 // This should neither throw an Exception nor cause system to crash, even when the 80 // companion application does not define any valid CompanionDeviceServices. 81 // (If the system crashes this instrumentation test won't complete). 82 cdm.notifyDeviceDisappeared(associationId) 83 } 84 }