Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2018 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 package com.android.compatibility.common.util;
     17 
     18 import static junit.framework.Assert.fail;
     19 
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.database.ContentObserver;
     25 import android.net.Uri;
     26 import android.support.test.InstrumentationRegistry;
     27 import android.util.Log;
     28 
     29 import java.util.concurrent.CountDownLatch;
     30 import java.util.concurrent.TimeUnit;
     31 import java.util.function.Predicate;
     32 
     33 /**
     34  * CallbackAsserter helps wait until a callback is called.
     35  */
     36 public class CallbackAsserter {
     37     private static final String TAG = "CallbackAsserter";
     38 
     39     final CountDownLatch mLatch = new CountDownLatch(1);
     40 
     41     CallbackAsserter() {
     42     }
     43 
     44     /**
     45      * Call this to assert a callback be called within the given timeout.
     46      */
     47     public final void assertCalled(String message, int timeoutSeconds) throws Exception {
     48         try {
     49             if (mLatch.await(timeoutSeconds, TimeUnit.SECONDS)) {
     50                 return;
     51             }
     52             fail("Didn't receive callback: " + message);
     53         } finally {
     54             cleanUp();
     55         }
     56     }
     57 
     58     void cleanUp() {
     59     }
     60 
     61     /**
     62      * Create an instance for a broadcast.
     63      */
     64     public static CallbackAsserter forBroadcast(IntentFilter filter) {
     65         return forBroadcast(filter, null);
     66     }
     67 
     68     /**
     69      * Create an instance for a broadcast.
     70      */
     71     public static CallbackAsserter forBroadcast(IntentFilter filter, Predicate<Intent> checker) {
     72         return new BroadcastAsserter(filter, checker);
     73     }
     74 
     75     /**
     76      * Create an instance for a content changed notification.
     77      */
     78     public static CallbackAsserter forContentUri(Uri watchUri) {
     79         return forContentUri(watchUri, null);
     80     }
     81 
     82     /**
     83      * Create an instance for a content changed notification.
     84      */
     85     public static CallbackAsserter forContentUri(Uri watchUri, Predicate<Uri> checker) {
     86         return new ContentObserverAsserter(watchUri, checker);
     87     }
     88 
     89     private static class BroadcastAsserter extends CallbackAsserter {
     90         private final BroadcastReceiver mReceiver;
     91 
     92         BroadcastAsserter(IntentFilter filter, Predicate<Intent> checker) {
     93             mReceiver = new BroadcastReceiver() {
     94                 @Override
     95                 public void onReceive(Context context, Intent intent) {
     96                     if (checker != null && !checker.test(intent)) {
     97                         Log.v(TAG, "Ignoring intent: " + intent);
     98                         return;
     99                     }
    100                     mLatch.countDown();
    101                 }
    102             };
    103             InstrumentationRegistry.getContext().registerReceiver(mReceiver, filter);
    104         }
    105 
    106         @Override
    107         void cleanUp() {
    108             InstrumentationRegistry.getContext().unregisterReceiver(mReceiver);
    109         }
    110     }
    111 
    112     private static class ContentObserverAsserter extends CallbackAsserter {
    113         private final ContentObserver mObserver;
    114 
    115         ContentObserverAsserter(Uri watchUri, Predicate<Uri> checker) {
    116             mObserver = new ContentObserver(null) {
    117                 @Override
    118                 public void onChange(boolean selfChange, Uri uri) {
    119                     if (checker != null && !checker.test(uri)) {
    120                         Log.v(TAG, "Ignoring notification on URI: " + uri);
    121                         return;
    122                     }
    123                     mLatch.countDown();
    124                 }
    125             };
    126             InstrumentationRegistry.getContext().getContentResolver().registerContentObserver(
    127                     watchUri, true, mObserver);
    128         }
    129 
    130         @Override
    131         void cleanUp() {
    132             InstrumentationRegistry.getContext().getContentResolver().unregisterContentObserver(
    133                     mObserver);
    134         }
    135     }
    136 }
    137