OwnerChangedBroadcastTest.java revision fffb95a287d7abd204f300da76f5cabd6bd3887f
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
17package com.android.cts.intent.receiver;
18
19import android.app.admin.DevicePolicyManager;
20import android.content.Context;
21import android.content.Intent;
22import android.content.SharedPreferences;
23import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
24import android.test.InstrumentationTestCase;
25
26import java.util.concurrent.Semaphore;
27import java.util.concurrent.TimeUnit;
28import java.lang.InterruptedException;
29
30public class OwnerChangedBroadcastTest extends InstrumentationTestCase {
31
32    private SharedPreferences mPreferences;
33
34    @Override
35    public void setUp() throws Exception {
36        super.setUp();
37        Context context = getInstrumentation().getTargetContext();
38        mPreferences = context.getSharedPreferences(
39                BroadcastIntentReceiver.PREFERENCES_NAME, Context.MODE_PRIVATE);
40    }
41
42    // We can't just register a broadcast receiver in the code because the broadcast
43    // may have been sent before this test is run. So we have a manifest receiver
44    // listening to the broadcast and writing to a shared preference when it receives it.
45    public void testOwnerChangedBroadcastReceived() throws InterruptedException {
46        final Semaphore mPreferenceChanged = new Semaphore(0);
47
48        OnSharedPreferenceChangeListener listener = new OnSharedPreferenceChangeListener() {
49            @Override
50            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
51                if (key.equals(BroadcastIntentReceiver.OWNER_CHANGED_BROADCAST_RECEIVED_KEY)) {
52                    mPreferenceChanged.release();
53                }
54            }
55        };
56        mPreferences.registerOnSharedPreferenceChangeListener(listener);
57
58        if (mPreferences.getBoolean(BroadcastIntentReceiver.OWNER_CHANGED_BROADCAST_RECEIVED_KEY,
59                false)) {
60            // The broadcast intent has already been received? good
61            // Otherwise, we'll wait until we receive it.
62            return;
63        }
64        assertTrue(mPreferenceChanged.tryAcquire(40, TimeUnit.SECONDS));
65        assertTrue(mPreferences.getBoolean(
66                BroadcastIntentReceiver.OWNER_CHANGED_BROADCAST_RECEIVED_KEY, false));
67    }
68
69    public void testOwnerChangedBroadcastNotReceived() {
70        assertFalse(mPreferences.getBoolean(
71                BroadcastIntentReceiver.OWNER_CHANGED_BROADCAST_RECEIVED_KEY, false));
72    }
73}
74