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

Android example source code file (SingleLaunchActivityTestCase.java)

This example Android source code file (SingleLaunchActivityTestCase.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

android, app, application, class, exception, instrumentationtestcase, os, override, singlelaunchactivitytestcase, string, ui, view

The SingleLaunchActivityTestCase.java Android example source code

/*
 * Copyright (C) 2007 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.test;

import android.app.Activity;
import android.view.IWindowManager;
import android.os.ServiceManager;

/**
 * If you would like to test a single activity with an
 * {@link android.test.InstrumentationTestCase}, this provides some of the boiler plate to
 * launch and finish the activity in {@link #setUp} and {@link #tearDown}.
 * 
 * This launches the activity only once for the entire class instead of doing it 
 * in every setup / teardown call.
 */
public abstract class SingleLaunchActivityTestCase<T extends Activity>
        extends InstrumentationTestCase {
    
    String mPackage;
    Class<T> mActivityClass;
    private static int sTestCaseCounter = 0;
    private static boolean sActivityLaunchedFlag = false;

    /**
     * <b>NOTE: The parameter pkg must refer to the package identifier of the
     * package hosting the activity to be launched, which is specified in the AndroidManifest.xml
     * file.  This is not necessarily the same as the java package name.
     *
     * @param pkg The package hosting the activity to be launched.
     * @param activityClass The activity to test.
     */
    public SingleLaunchActivityTestCase(String pkg, Class<T> activityClass) {
        mPackage = pkg;
        mActivityClass = activityClass;        
        sTestCaseCounter ++;                
    }
    
    /**
     * The activity that will be set up for use in each test method.
     */
    private static Activity sActivity;

    public T getActivity() {
        return (T) sActivity;
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        // If it is the first test case, launch the activity.
        if (!sActivityLaunchedFlag) {
            // by default, not in touch mode
            getInstrumentation().setInTouchMode(false);
            sActivity = launchActivity(mPackage, mActivityClass, null);
            sActivityLaunchedFlag = true;
        }                        
    }

    @Override
    protected void tearDown() throws Exception {
        // If it is the last test case, call finish on the activity.
        sTestCaseCounter --;
        if (sTestCaseCounter == 1) {
            sActivity.finish();
        }        
        super.tearDown();
    }

    public void testActivityTestCaseSetUpProperly() throws Exception {
        assertNotNull("activity should be launched successfully", sActivity);
    }
}

Other Android examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.