Home | History | Annotate | Download | only in managedprofile
      1 /*
      2  * Copyright (C) 2016 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.managedprofile;
     18 
     19 import static android.provider.Settings.Secure.SYNC_PARENT_SOUNDS;
     20 
     21 import android.content.ContentResolver;
     22 import android.content.Context;
     23 import android.media.RingtoneManager;
     24 import android.net.Uri;
     25 import android.provider.Settings;
     26 
     27 /**
     28  * Tests that managed profile ringtones work, as well as the "sync from parent profile" feature
     29  */
     30 public class RingtoneSyncTest extends BaseManagedProfileTest {
     31 
     32     private static final int[] RINGTONE_TYPES = {
     33             RingtoneManager.TYPE_RINGTONE, RingtoneManager.TYPE_NOTIFICATION,
     34             RingtoneManager.TYPE_ALARM
     35     };
     36 
     37     private ContentResolver mContentResolver;
     38 
     39     @Override
     40     protected void setUp() throws Exception {
     41         super.setUp();
     42         mContentResolver = mContext.getContentResolver();
     43     }
     44 
     45     /**
     46      * Checks that the ringtone set in the settings provider and the ringtone retrieved from
     47      * RingtoneManager are identical.
     48      *
     49      * Used to test that the ringtone sync setting is enabled by default, and that managed profile
     50      * ringtones are kept in sync with parent profile ringtones, despite the setting being kept in
     51      * another user from the caller.
     52      */
     53     public void testRingtoneSync() throws Exception {
     54         String defaultRingtone = Settings.System.getString(mContentResolver,
     55                 Settings.System.RINGTONE);
     56         String defaultNotification = Settings.System.getString(mContentResolver,
     57                 Settings.System.NOTIFICATION_SOUND);
     58         String defaultAlarm = Settings.System.getString(mContentResolver,
     59                 Settings.System.ALARM_ALERT);
     60 
     61         // RingtoneManager API should retrieve the same ringtones
     62         validateRingtoneManagerGetRingtone(defaultRingtone, RingtoneManager.TYPE_RINGTONE);
     63         validateRingtoneManagerGetRingtone(defaultNotification, RingtoneManager.TYPE_NOTIFICATION);
     64         validateRingtoneManagerGetRingtone(defaultAlarm, RingtoneManager.TYPE_ALARM);
     65     }
     66 
     67     private void validateRingtoneManagerGetRingtone(String expected, int type) {
     68         Uri expectedUri = (expected == null ? null : Utils.getUriWithoutUserId(
     69                 Uri.parse(expected)));
     70         Uri actualRingtoneUri = Utils.getUriWithoutUserId(
     71                 RingtoneManager.getActualDefaultRingtoneUri(mContext, type));
     72         assertEquals(expectedUri, actualRingtoneUri);
     73     }
     74 
     75     /*
     76      * Tests that setting a work ringtone disables Settings.Secure.SYNC_PARENT_SOUNDS.
     77      */
     78     private void testSoundDisableSync(int ringtoneType) throws Exception {
     79         Settings.Secure.putInt(mContentResolver, SYNC_PARENT_SOUNDS, 1);
     80 
     81         Uri originalUri = RingtoneManager.getActualDefaultRingtoneUri(mContext, ringtoneType);
     82 
     83         // Make sure we have the rights we need to set a new ringtone.
     84         assertTrue(Settings.System.canWrite(mContext));
     85 
     86         // Explicitly set a work sound, to stop syncing ringtones between profiles.
     87         assertEquals(1, Settings.Secure.getInt(mContentResolver, SYNC_PARENT_SOUNDS));
     88         try {
     89             RingtoneManager.setActualDefaultRingtoneUri(mContext, ringtoneType, null);
     90             assertEquals(0, Settings.Secure.getInt(mContentResolver, SYNC_PARENT_SOUNDS));
     91             validateRingtoneManagerGetRingtone(null, ringtoneType);
     92         } finally {
     93             // Reset the setting we just changed.
     94             Settings.Secure.putInt(mContentResolver, SYNC_PARENT_SOUNDS, 1);
     95         }
     96 
     97         // After re-unifying, the uri should be the same as the parent's uri.
     98         Uri postSyncUri = RingtoneManager.getActualDefaultRingtoneUri(mContext, ringtoneType);
     99         assertEquals(originalUri, postSyncUri);
    100 
    101         // Manually disabling sync again, without changing settings, should put the ringtone uri
    102         // back to its earlier value of null.
    103         try {
    104             Settings.Secure.putInt(mContentResolver, SYNC_PARENT_SOUNDS, 0);
    105             assertNull(RingtoneManager.getActualDefaultRingtoneUri(mContext, ringtoneType));
    106         } finally {
    107             Settings.Secure.putInt(mContentResolver, SYNC_PARENT_SOUNDS, 1);
    108         }
    109     }
    110 
    111     public void testRingtoneDisableSync() throws Exception {
    112         testSoundDisableSync(RingtoneManager.TYPE_RINGTONE);
    113     }
    114 
    115     public void testNotificationDisableSync() throws Exception {
    116         testSoundDisableSync(RingtoneManager.TYPE_NOTIFICATION);
    117     }
    118 
    119     public void testAlarmDisableSync() throws Exception {
    120         testSoundDisableSync(RingtoneManager.TYPE_ALARM);
    121     }
    122 }
    123