Home | History | Annotate | Download | only in app
      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 com.example.android.apis.app;
     18 
     19 import com.example.android.apis.R;
     20 import com.example.android.apis.view.Focus2ActivityTest;
     21 
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.test.ActivityUnitTestCase;
     25 import android.test.suitebuilder.annotation.MediumTest;
     26 import android.widget.Button;
     27 
     28 /**
     29  * This demonstrates completely isolated "unit test" of an Activity class.
     30  *
     31  * <p>This model for testing creates the entire Activity (like {@link Focus2ActivityTest}) but does
     32  * not attach it to the system (for example, it cannot launch another Activity).  It allows you to
     33  * inject additional behaviors via the
     34  * {@link android.test.ActivityUnitTestCase#setActivityContext(Context)} and
     35  * {@link android.test.ActivityUnitTestCase#setApplication(android.app.Application)} methods.
     36  * It also allows you to more carefully test your Activity's performance
     37  * Writing unit tests in this manner requires more care and attention, but allows you to test
     38  * very specific behaviors, and can also be an easier way to test error conditions.
     39  *
     40  * <p>Because ActivityUnitTestCase creates the Activity under test completely outside of
     41  * the usual system, tests of layout and point-click UI interaction are much less useful
     42  * in this configuration.  It's more useful here to concentrate on tests that involve the
     43  * underlying data model, internal business logic, or exercising your Activity's life cycle.
     44  *
     45  * <p>See {@link com.example.android.apis.AllTests} for documentation on running
     46  * all tests and individual tests in this application.
     47  */
     48 public class ForwardingTest extends ActivityUnitTestCase<Forwarding> {
     49 
     50     private Intent mStartIntent;
     51     private Button mButton;
     52 
     53     public ForwardingTest() {
     54         super(Forwarding.class);
     55       }
     56 
     57     @Override
     58     protected void setUp() throws Exception {
     59         super.setUp();
     60 
     61         // In setUp, you can create any shared test data, or set up mock components to inject
     62         // into your Activity.  But do not call startActivity() until the actual test methods.
     63         mStartIntent = new Intent(Intent.ACTION_MAIN);
     64     }
     65 
     66     /**
     67      * The name 'test preconditions' is a convention to signal that if this
     68      * test doesn't pass, the test case was not set up properly and it might
     69      * explain any and all failures in other tests.  This is not guaranteed
     70      * to run before other tests, as junit uses reflection to find the tests.
     71      */
     72     @MediumTest
     73     public void testPreconditions() {
     74         startActivity(mStartIntent, null, null);
     75         mButton = (Button) getActivity().findViewById(R.id.go);
     76 
     77         assertNotNull(getActivity());
     78         assertNotNull(mButton);
     79     }
     80 
     81     /**
     82      * This test demonstrates examining the way that activity calls startActivity() to launch
     83      * other activities.
     84      */
     85     @MediumTest
     86     public void testSubLaunch() {
     87         Forwarding activity = startActivity(mStartIntent, null, null);
     88         mButton = (Button) activity.findViewById(R.id.go);
     89 
     90         // This test confirms that when you click the button, the activity attempts to open
     91         // another activity (by calling startActivity) and close itself (by calling finish()).
     92         mButton.performClick();
     93 
     94         assertNotNull(getStartedActivityIntent());
     95         assertTrue(isFinishCalled());
     96     }
     97 
     98     /**
     99      * This test demonstrates ways to exercise the Activity's life cycle.
    100      */
    101     @MediumTest
    102     public void testLifeCycleCreate() {
    103         Forwarding activity = startActivity(mStartIntent, null, null);
    104 
    105         // At this point, onCreate() has been called, but nothing else
    106         // Complete the startup of the activity
    107         getInstrumentation().callActivityOnStart(activity);
    108         getInstrumentation().callActivityOnResume(activity);
    109 
    110         // At this point you could test for various configuration aspects, or you could
    111         // use a Mock Context to confirm that your activity has made certain calls to the system
    112         // and set itself up properly.
    113 
    114         getInstrumentation().callActivityOnPause(activity);
    115 
    116         // At this point you could confirm that the activity has paused properly, as if it is
    117         // no longer the topmost activity on screen.
    118 
    119         getInstrumentation().callActivityOnStop(activity);
    120 
    121         // At this point, you could confirm that the activity has shut itself down appropriately,
    122         // or you could use a Mock Context to confirm that your activity has released any system
    123         // resources it should no longer be holding.
    124 
    125         // ActivityUnitTestCase.tearDown(), which is always automatically called, will take care
    126         // of calling onDestroy().
    127     }
    128 
    129 }
    130