1 /*
2  * Copyright (C) 2008 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.app.activity;
18 
19 import android.app.AlarmManager;
20 import android.content.Context;
21 import android.test.AndroidTestCase;
22 import android.test.suitebuilder.annotation.LargeTest;
23 
24 import java.util.TimeZone;
25 
26 public class SetTimeZonePermissionsTest extends AndroidTestCase {
27 
28     private String[] mZones;
29     private String mCurrentZone;
30     private AlarmManager mAlarm;
31 
32     @Override
setUp()33     protected void setUp() throws Exception {
34         super.setUp();
35 
36         mZones = TimeZone.getAvailableIDs();
37         mCurrentZone = TimeZone.getDefault().getID();
38         mAlarm = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
39     }
40 
41     /**
42      * Verify that non-system processes cannot set the time zone.
43      */
44     @LargeTest
testSetTimeZonePermissions()45     public void testSetTimeZonePermissions() {
46         /**
47          * Attempt to set several predefined time zones, verifying that the system
48          * system default time zone has not actually changed from its prior state
49          * after each attempt.
50          */
51         int max = (mZones.length > 10) ? mZones.length : 10;
52         assertTrue("No system-defined time zones - test invalid", max > 0);
53 
54         for (int i = 0; i < max; i++) {
55             String tz = mZones[i];
56             try {
57                 mAlarm.setTimeZone(tz);
58             } catch (SecurityException se) {
59                 // Expected failure; no need to handle specially since we're
60                 // about to assert that the test invariant holds: no change
61                 // to the system time zone.
62             }
63 
64             String newZone = TimeZone.getDefault().getID();
65             assertEquals("AlarmManager.setTimeZone() succeeded despite lack of permission",
66                     mCurrentZone,
67                     newZone);
68         }
69     }
70 }
71