• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.google.android.utils.chre;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.hardware.location.ContextHubInfo;
23 import android.hardware.location.ContextHubIntentEvent;
24 import android.util.Log;
25 
26 import org.junit.Assert;
27 
28 import java.util.concurrent.ArrayBlockingQueue;
29 import java.util.concurrent.TimeUnit;
30 
31 /** A BroadcastReceiver that waits for Intent events from the Context Hub. */
32 public class ContextHubBroadcastReceiver extends BroadcastReceiver {
33     private static final String TAG = "ContextHubBroadcastReceiver";
34 
35     /*
36      * The queue to store received ContextHubIntentEvents.
37      */
38     private static final ArrayBlockingQueue<ContextHubIntentEvent> sQueue =
39             new ArrayBlockingQueue<>(1);
40 
41     /*
42      * Note: This method runs on the main UI thread of this app.
43      */
44     @Override
onReceive(Context context, Intent intent)45     public void onReceive(Context context, Intent intent) {
46         ContextHubIntentEvent event = null;
47         try {
48             event = ContextHubIntentEvent.fromIntent(intent);
49         } catch (IllegalArgumentException e) {
50             Assert.fail("Exception while parsing intent event: " + e.getMessage());
51         }
52         Log.d(TAG, "Received intent event: " + event);
53 
54         Assert.assertEquals("Received too many Intent events", /* expected= */ 0, sQueue.size());
55         sQueue.add(event);
56     }
57 
58     /**
59      * Waits for an Intent event to be delivered to an instance of this BroadcastReceiver.
60      *
61      * @param timeout the timeout to wait
62      * @param unit the unit of the timeout
63      * @param eventType the type of the event type of the expected Intent event
64      * @param contextHubInfo the ContextHubInfo of the expected Intent event
65      * @param nanoAppId the ID of the nanoapp of the expected Intent event
66      * @return the ContextHubIntentEvent generated from the Intent event
67      */
pollIntentEvent( long timeout, TimeUnit unit, int eventType, ContextHubInfo contextHubInfo, long nanoAppId)68     public static ContextHubIntentEvent pollIntentEvent(
69             long timeout,
70             TimeUnit unit,
71             int eventType,
72             ContextHubInfo contextHubInfo,
73             long nanoAppId) {
74         ContextHubIntentEvent event = null;
75         try {
76             event = sQueue.poll(timeout, unit);
77         } catch (InterruptedException e) {
78             Assert.fail("InterruptedException while waiting for Intent event");
79         }
80         if (event == null) {
81             Assert.fail("Timed out while waiting for Intent event");
82         }
83 
84         if (event.getEventType() != eventType) {
85             Assert.fail(
86                     "Received unexpected event type, expected "
87                             + eventType
88                             + " received "
89                             + event.getEventType());
90         }
91         if (!event.getContextHubInfo().equals(contextHubInfo)) {
92             Assert.fail(
93                     "Received unexpected ContextHubInfo, expected "
94                             + contextHubInfo
95                             + " received "
96                             + event.getContextHubInfo());
97         }
98         if (event.getNanoAppId() != nanoAppId) {
99             Assert.fail(
100                     "Received unexpected nanoapp ID, expected "
101                             + nanoAppId
102                             + " received "
103                             + event.getNanoAppId());
104         }
105 
106         return event;
107     }
108 
109     /**
110      * Asserts that no Intent events are delivered to an instance of this BroadcastReceiver.
111      *
112      * @param timeout the timeout to wait
113      * @param unit the unit of the timeout
114      */
assertNoIntentEventReceived(long timeout, TimeUnit unit)115     public static void assertNoIntentEventReceived(long timeout, TimeUnit unit) {
116         ContextHubIntentEvent event = null;
117         try {
118             event = sQueue.poll(timeout, unit);
119         } catch (InterruptedException e) {
120             Assert.fail("InterruptedException while waiting for Intent event");
121         }
122 
123         Assert.assertNull("Received unexpected intent event " + event, event);
124     }
125 }
126