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 17package com.android.car.apitest; 18 19import android.content.ComponentName; 20import android.content.ServiceConnection; 21import android.os.IBinder; 22import android.os.Looper; 23import android.car.Car; 24import android.car.content.pm.CarPackageManager; 25import android.test.AndroidTestCase; 26import android.test.suitebuilder.annotation.MediumTest; 27 28import java.util.concurrent.Semaphore; 29import java.util.concurrent.TimeUnit; 30 31@MediumTest 32public class CarPackageManagerTest extends AndroidTestCase { 33 private static final long DEFAULT_WAIT_TIMEOUT_MS = 3000; 34 35 private final Semaphore mConnectionWait = new Semaphore(0); 36 37 private Car mCar; 38 private CarPackageManager mCarPackageManager; 39 40 private final ServiceConnection mConnectionListener = new ServiceConnection() { 41 42 @Override 43 public void onServiceConnected(ComponentName name, IBinder service) { 44 assertMainThread(); 45 mConnectionWait.release(); 46 } 47 48 @Override 49 public void onServiceDisconnected(ComponentName name) { 50 assertMainThread(); 51 } 52 53 }; 54 55 private void assertMainThread() { 56 assertTrue(Looper.getMainLooper().isCurrentThread()); 57 } 58 private void waitForConnection(long timeoutMs) throws InterruptedException { 59 mConnectionWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS); 60 } 61 62 @Override 63 protected void setUp() throws Exception { 64 super.setUp(); 65 mCar = Car.createCar(getContext(), mConnectionListener); 66 mCar.connect(); 67 waitForConnection(DEFAULT_WAIT_TIMEOUT_MS); 68 mCarPackageManager = (CarPackageManager) mCar.getCarManager(Car.PACKAGE_SERVICE); 69 assertNotNull(mCarPackageManager); 70 } 71 72 @Override 73 protected void tearDown() throws Exception { 74 super.tearDown(); 75 mCar.disconnect(); 76 } 77 78 public void testCreate() throws Exception { 79 //nothing to do for now 80 } 81} 82