Home | History | Annotate | Download | only in apitest
      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 com.android.support.car.apitest;
     18 
     19 import android.content.ComponentName;
     20 import android.os.Looper;
     21 import android.support.car.Car;
     22 import android.support.car.ServiceConnectionListener;
     23 import android.support.car.hardware.CarSensorManager;
     24 import android.test.AndroidTestCase;
     25 import android.test.suitebuilder.annotation.MediumTest;
     26 
     27 import java.util.concurrent.Semaphore;
     28 import java.util.concurrent.TimeUnit;
     29 
     30 @MediumTest
     31 public class CarTest extends AndroidTestCase {
     32     private static final long DEFAULT_WAIT_TIMEOUT_MS = 3000;
     33 
     34     private final Semaphore mConnectionWait = new Semaphore(0);
     35 
     36     private final ServiceConnectionListener mConnectionListener = new ServiceConnectionListener() {
     37 
     38         @Override
     39         public void onServiceSuspended(int cause) {
     40             assertMainThread();
     41         }
     42 
     43         @Override
     44         public void onServiceDisconnected(ComponentName name) {
     45             assertMainThread();
     46         }
     47 
     48         @Override
     49         public void onServiceConnectionFailed(int cause) {
     50             assertMainThread();
     51         }
     52 
     53         @Override
     54         public void onServiceConnected(ComponentName name) {
     55             assertMainThread();
     56             mConnectionWait.release();
     57         }
     58     };
     59 
     60     private void assertMainThread() {
     61         assertTrue(Looper.getMainLooper().isCurrentThread());
     62     }
     63 
     64     private void waitForConnection(long timeoutMs) throws InterruptedException {
     65         mConnectionWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS);
     66     }
     67 
     68     public void testCarConnection() throws Exception {
     69         Car car = Car.createCar(getContext(), mConnectionListener);
     70         assertFalse(car.isConnected());
     71         assertFalse(car.isConnecting());
     72         car.connect();
     73         //TODO fix race here
     74         assertTrue(car.isConnecting());
     75         waitForConnection(DEFAULT_WAIT_TIMEOUT_MS);
     76         assertTrue(car.isConnected());
     77         assertFalse(car.isConnecting());
     78         CarSensorManager carSensorManager =
     79                 (CarSensorManager) car.getCarManager(Car.SENSOR_SERVICE);
     80         assertNotNull(carSensorManager);
     81         CarSensorManager carSensorManager2 =
     82                 (CarSensorManager) car.getCarManager(Car.SENSOR_SERVICE);
     83         assertEquals(carSensorManager, carSensorManager2);
     84         Object noSuchService = car.getCarManager("No such service");
     85         assertNull(noSuchService);
     86         // double disconnect should be safe.
     87         car.disconnect();
     88         car.disconnect();
     89         assertFalse(car.isConnected());
     90         assertFalse(car.isConnecting());
     91     }
     92 
     93     public void testDoubleConnect() throws Exception {
     94         Car car = Car.createCar(getContext(), mConnectionListener);
     95         assertFalse(car.isConnected());
     96         assertFalse(car.isConnecting());
     97         car.connect();
     98         try {
     99             car.connect();
    100             fail("dobule connect should throw");
    101         } catch (IllegalStateException e) {
    102             // expected
    103         }
    104         car.disconnect();
    105     }
    106 }
    107