Home | History | Annotate | Download | only in cts
      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.bluetooth.cts;
     18 
     19 import android.bluetooth.le.AdvertiseCallback;
     20 import android.bluetooth.le.AdvertiseSettings;
     21 import android.test.AndroidTestCase;
     22 import android.test.suitebuilder.annotation.SmallTest;
     23 
     24 import java.util.HashSet;
     25 import java.util.Set;
     26 
     27 /**
     28  * Test of {@link AdvertiseCallback}.
     29  */
     30 public class AdvertiseCallbackTest extends AndroidTestCase {
     31 
     32     private final static int ADVERTISE_TYPE_SUCCESS = 0;
     33     private final static int ADVERTISE_TYPE_FAIL = 1;
     34 
     35     private final MockAdvertiser mMockAdvertiser = new MockAdvertiser();
     36     private final BleAdvertiseCallback mAdvertiseCallback = new BleAdvertiseCallback();
     37 
     38     @SmallTest
     39     public void testAdvertiseSuccess() {
     40         mAdvertiseCallback.mAdvertiseType = ADVERTISE_TYPE_SUCCESS;
     41         mMockAdvertiser.startAdvertise(mAdvertiseCallback);
     42     }
     43 
     44     @SmallTest
     45     public void testAdvertiseFailure() {
     46         mAdvertiseCallback.mAdvertiseType = ADVERTISE_TYPE_SUCCESS;
     47         mMockAdvertiser.startAdvertise(mAdvertiseCallback);
     48 
     49         // Second advertise with the same callback should fail.
     50         mAdvertiseCallback.mAdvertiseType = ADVERTISE_TYPE_FAIL;
     51         mMockAdvertiser.startAdvertise(mAdvertiseCallback);
     52     }
     53 
     54     // A mock advertiser which emulate BluetoothLeAdvertiser behavior.
     55     private static class MockAdvertiser {
     56         private Set<AdvertiseCallback> mCallbacks = new HashSet<>();
     57 
     58         void startAdvertise(AdvertiseCallback callback) {
     59             synchronized (mCallbacks) {
     60                 if (mCallbacks.contains(callback)) {
     61                     callback.onStartFailure(AdvertiseCallback.ADVERTISE_FAILED_ALREADY_STARTED);
     62                 } else {
     63                     callback.onStartSuccess(null);
     64                     mCallbacks.add(callback);
     65                 }
     66             }
     67         }
     68     }
     69 
     70     private static class BleAdvertiseCallback extends AdvertiseCallback {
     71         int mAdvertiseType = ADVERTISE_TYPE_SUCCESS;
     72 
     73         @Override
     74         public void onStartSuccess(AdvertiseSettings settings) {
     75             if (mAdvertiseType == ADVERTISE_TYPE_FAIL) {
     76                 fail("advertise should fail");
     77             }
     78         }
     79 
     80         @Override
     81         public void onStartFailure(int error) {
     82             if (mAdvertiseType == ADVERTISE_TYPE_SUCCESS) {
     83                 assertEquals(AdvertiseCallback.ADVERTISE_FAILED_ALREADY_STARTED, error);
     84             }
     85         }
     86     }
     87 }
     88