• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.car.extendedapitest;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.app.Service;
22 import android.car.Car;
23 import android.car.CarProjectionManager;
24 import android.car.extendedapitest.testbase.CarApiTestBase;
25 import android.car.test.PermissionsCheckerRule;
26 import android.car.test.PermissionsCheckerRule.EnsureHasPermission;
27 import android.content.Intent;
28 import android.os.Binder;
29 import android.os.IBinder;
30 
31 import androidx.test.platform.app.InstrumentationRegistry;
32 
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 
37 
38 public final class CarProjectionManagerTest extends CarApiTestBase {
39     private static final String TAG = CarProjectionManagerTest.class.getSimpleName();
40     @Rule
41     public final PermissionsCheckerRule mPermissionsCheckerRule = new PermissionsCheckerRule();
42 
43     private CarProjectionManager mManager;
44 
45     public static final class TestService extends Service {
46         public static Object mLock = new Object();
47         private static boolean sBound;
48         private final Binder mBinder = new Binder() {};
49 
setBound(boolean bound)50         private static synchronized void setBound(boolean bound) {
51             sBound = bound;
52         }
53 
isBound()54         public static synchronized boolean isBound() {
55             return sBound;
56         }
57 
58         @Override
onBind(Intent intent)59         public IBinder onBind(Intent intent) {
60             setBound(true);
61             synchronized (mLock) {
62                 mLock.notifyAll();
63             }
64             return mBinder;
65         }
66     }
67 
68     @Before
setUp()69     public void setUp() throws Exception {
70         mManager = (CarProjectionManager) getCar().getCarManager(Car.PROJECTION_SERVICE);
71         assertThat(mManager).isNotNull();
72     }
73 
74     @Test
75     @EnsureHasPermission(Car.PERMISSION_CAR_PROJECTION)
testRegisterProjectionRunner()76     public void testRegisterProjectionRunner() throws Exception {
77         Intent intent = new Intent(
78                 InstrumentationRegistry.getInstrumentation().getContext(), TestService.class);
79         assertThat(TestService.isBound()).isFalse();
80         mManager.registerProjectionRunner(intent);
81         synchronized (TestService.mLock) {
82             try {
83                 TestService.mLock.wait(5000);
84             } catch (InterruptedException e) {
85                 // Do nothing
86             }
87         }
88         assertThat(TestService.isBound()).isTrue();
89         mManager.unregisterProjectionRunner(intent);
90     }
91 }
92