alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Android example source code file (SetTimeZonePermissionsTest.java)

This example Android source code file (SetTimeZonePermissionsTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Android by Example" TM.

Java - Android tags/keywords

alarmmanager, android, androidtestcase, app, application, content, largetest, no, override, securityexception, settimezonepermissionstest, string, test, util

The SetTimeZonePermissionsTest.java Android example source code

/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app.activity;

import android.app.AlarmManager;
import android.content.Context;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.LargeTest;

import java.util.TimeZone;

public class SetTimeZonePermissionsTest extends AndroidTestCase {

    private String[] mZones;
    private String mCurrentZone;
    private AlarmManager mAlarm;
    
    @Override
    protected void setUp() throws Exception {
        super.setUp();

        mZones = TimeZone.getAvailableIDs();
        mCurrentZone = TimeZone.getDefault().getID();
        mAlarm = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    }

    /**
     * Verify that non-system processes cannot set the time zone.
     */
    @LargeTest
    public void testSetTimeZonePermissions() {
        /**
         * Attempt to set several predefined time zones, verifying that the system
         * system default time zone has not actually changed from its prior state
         * after each attempt.
         */
        int max = (mZones.length > 10) ? mZones.length : 10;
        assertTrue("No system-defined time zones - test invalid", max > 0);

        for (int i = 0; i < max; i++) {
            String tz = mZones[i];
            try {
                mAlarm.setTimeZone(tz);
            } catch (SecurityException se) {
                // Expected failure; no need to handle specially since we're
                // about to assert that the test invariant holds: no change
                // to the system time zone.
            }

            String newZone = TimeZone.getDefault().getID();
            assertEquals("AlarmManager.setTimeZone() succeeded despite lack of permission",
                    mCurrentZone,
                    newZone);
        }
    }
}

Other Android examples (source code examples)

Here is a short list of links related to this Android SetTimeZonePermissionsTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.