• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.virtualdevice.cts.util;
18 
19 import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.junit.Assume.assumeTrue;
24 
25 import android.companion.AssociationInfo;
26 import android.companion.CompanionDeviceManager;
27 import android.content.pm.PackageManager;
28 import android.os.Process;
29 
30 import androidx.test.platform.app.InstrumentationRegistry;
31 
32 import com.android.compatibility.common.util.SystemUtil;
33 
34 import org.junit.rules.ExternalResource;
35 
36 import java.util.List;
37 
38 /**
39  * A test rule that creates a {@link CompanionDeviceManager} association with the instrumented
40  * package for the duration of the test.
41  */
42 public class FakeAssociationRule extends ExternalResource {
43 
44     private static final String FAKE_ASSOCIATION_ADDRESS = "00:00:00:00:00:10";
45 
46     private AssociationInfo mAssociationInfo;
47     private CompanionDeviceManager mCompanionDeviceManager;
48 
49     @Override
before()50     protected void before() throws Throwable {
51         super.before();
52         assumeTrue(
53                 getApplicationContext().getPackageManager()
54                         .hasSystemFeature(PackageManager.FEATURE_COMPANION_DEVICE_SETUP));
55         mCompanionDeviceManager =
56                 getApplicationContext().getSystemService(CompanionDeviceManager.class);
57         clearExistingAssociations();
58         SystemUtil.runShellCommand(String.format("cmd companiondevice associate %d %s %s",
59                 Process.myUserHandle().getIdentifier(),
60                 InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageName(),
61                 FAKE_ASSOCIATION_ADDRESS));
62         List<AssociationInfo> associations = mCompanionDeviceManager.getMyAssociations();
63         assertThat(associations).hasSize(1);
64         mAssociationInfo = associations.get(0);
65     }
66 
67     @Override
after()68     protected void after() {
69         super.after();
70         if (mAssociationInfo != null) {
71             mCompanionDeviceManager.disassociate(mAssociationInfo.getId());
72         }
73     }
74 
clearExistingAssociations()75     private void clearExistingAssociations() {
76         List<AssociationInfo> associations = mCompanionDeviceManager.getMyAssociations();
77         for (AssociationInfo association : associations) {
78             mCompanionDeviceManager.disassociate(association.getId());
79         }
80         assertThat(mCompanionDeviceManager.getMyAssociations()).isEmpty();
81     }
82 
getAssociationInfo()83     public AssociationInfo getAssociationInfo() {
84         return mAssociationInfo;
85     }
86 }
87