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