Home | History | Annotate | Download | only in deviceowner
      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 
     17 package com.android.cts.deviceowner;
     18 
     19 import static android.provider.Settings.Global.AIRPLANE_MODE_ON;
     20 
     21 import android.content.BroadcastReceiver;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.content.IntentFilter;
     25 import android.os.UserManager;
     26 import android.provider.Settings;
     27 import android.provider.Settings.SettingNotFoundException;
     28 
     29 import java.util.concurrent.CountDownLatch;
     30 import java.util.concurrent.TimeUnit;
     31 
     32 /**
     33  * Test interaction between {@link UserManager#DISALLOW_AIRPLANE_MODE} user restriction and
     34  * {@link Settings.Global#AIRPLANE_MODE_ON}.
     35  */
     36 public class AirplaneModeRestrictionTest extends BaseDeviceOwnerTest {
     37     private static final int TIMEOUT_SEC = 5;
     38 
     39     @Override
     40     protected void setUp() throws Exception {
     41         super.setUp();
     42         mDevicePolicyManager.clearUserRestriction(getWho(), UserManager.DISALLOW_AIRPLANE_MODE);
     43     }
     44 
     45     @Override
     46     protected void tearDown() throws Exception {
     47         mDevicePolicyManager.clearUserRestriction(getWho(), UserManager.DISALLOW_AIRPLANE_MODE);
     48         super.tearDown();
     49     }
     50 
     51     public void testAirplaneModeTurnedOffWhenRestrictionSet() throws Exception {
     52         final CountDownLatch latch = new CountDownLatch(1);
     53         // Using array so that it can be modified in broadcast receiver.
     54         boolean value[] = new boolean[1];
     55         BroadcastReceiver receiver = new BroadcastReceiver() {
     56             @Override
     57             public void onReceive(Context context, Intent intent) {
     58                 value[0] = intent.getBooleanExtra("state", true);
     59                 latch.countDown();
     60             }
     61         };
     62         mContext.registerReceiver(receiver, new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
     63 
     64         try {
     65             Settings.Global.putInt(mContext.getContentResolver(), AIRPLANE_MODE_ON, 1);
     66             mDevicePolicyManager.addUserRestriction(getWho(), UserManager.DISALLOW_AIRPLANE_MODE);
     67             assertTrue(latch.await(TIMEOUT_SEC, TimeUnit.SECONDS));
     68             assertFalse(value[0]);
     69             assertEquals(0, Settings.Global.getInt(
     70                     mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON));
     71         } finally {
     72             mContext.unregisterReceiver(receiver);
     73         }
     74     }
     75 
     76     public void testAirplaneModeCannotBeTurnedOnWithRestrictionOn()
     77             throws SettingNotFoundException {
     78         mDevicePolicyManager.addUserRestriction(getWho(), UserManager.DISALLOW_AIRPLANE_MODE);
     79         Settings.Global.putInt(mContext.getContentResolver(), AIRPLANE_MODE_ON, 1);
     80         assertEquals(0, Settings.Global.getInt(
     81                 mContext.getContentResolver(), AIRPLANE_MODE_ON));
     82     }
     83 
     84     public void testAirplaneModeCanBeTurnedOnWithRestrictionOff() throws SettingNotFoundException {
     85         mDevicePolicyManager.clearUserRestriction(getWho(), UserManager.DISALLOW_AIRPLANE_MODE);
     86         Settings.Global.putInt(mContext.getContentResolver(), AIRPLANE_MODE_ON, 1);
     87         assertEquals(1, Settings.Global.getInt(
     88                 mContext.getContentResolver(), AIRPLANE_MODE_ON));
     89     }
     90 }
     91