Home | History | Annotate | Download | only in receiver
      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 
     17 package com.android.cts.intent.receiver;
     18 
     19 import android.app.admin.DevicePolicyManager;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.SharedPreferences;
     23 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
     24 import android.test.InstrumentationTestCase;
     25 
     26 import java.util.concurrent.Semaphore;
     27 import java.util.concurrent.TimeUnit;
     28 import java.lang.InterruptedException;
     29 
     30 public 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         // We're relying on background broadcast intents, which can take a long time.
     65         assertTrue(mPreferenceChanged.tryAcquire(2, TimeUnit.MINUTES));
     66         assertTrue(mPreferences.getBoolean(
     67                 BroadcastIntentReceiver.OWNER_CHANGED_BROADCAST_RECEIVED_KEY, false));
     68     }
     69 
     70     public void testOwnerChangedBroadcastNotReceived() {
     71         assertFalse(mPreferences.getBoolean(
     72                 BroadcastIntentReceiver.OWNER_CHANGED_BROADCAST_RECEIVED_KEY, false));
     73     }
     74 }
     75