1 /*
2  * Copyright (C) 2021 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.stats.cts_root;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.app.StatsManager;
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 
27 import androidx.test.platform.app.InstrumentationRegistry;
28 
29 import com.android.server.stats.StatsHelper;
30 
31 import org.junit.Test;
32 
33 public class StatsHelperTest {
34     private static final String TAG = StatsHelperTest.class.getSimpleName();
35 
36     private static class MockReceiver extends BroadcastReceiver  {
37         protected boolean mCalledOnReceive = false;
38 
39         @Override
onReceive(Context context, Intent intent)40         public synchronized void onReceive(Context context, Intent intent) {
41             mCalledOnReceive = true;
42             onReceiveInternal(context, intent);
43             notifyAll();
44         }
45 
onReceiveInternal(Context context, Intent intent)46         protected void onReceiveInternal(Context context, Intent intent) {
47         }
48 
waitForReceiver(long timeout)49         public synchronized void waitForReceiver(long timeout) throws InterruptedException {
50             if (!mCalledOnReceive) {
51                 wait(timeout);
52             }
53             assertThat(mCalledOnReceive).isTrue();
54         }
55     }
56 
57     @Test
testSendStatsdReadyBroadcast()58     public void testSendStatsdReadyBroadcast() throws Exception {
59         final Context context = InstrumentationRegistry.getInstrumentation().getContext();
60         final IntentFilter intentFilter = new IntentFilter(StatsManager.ACTION_STATSD_STARTED);
61         final int flags = 0x01000000; // Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND
62         MockReceiver receiver = new MockReceiver() {
63             @Override
64             protected void onReceiveInternal(Context context, Intent intent) {
65                 assertThat(intent.getAction()).isEqualTo(StatsManager.ACTION_STATSD_STARTED);
66                 assertThat(intent.getFlags() & flags).isEqualTo(flags);
67             }
68         };
69 
70         context.registerReceiver(receiver, intentFilter);
71 
72         StatsHelper.sendStatsdReadyBroadcast(context);
73 
74         receiver.waitForReceiver(2_000);
75     }
76 }
77