Home | History | Annotate | Download | only in lesson4
      1 /*
      2  * Copyright (C) 2013 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 package com.example.android.testingfun.tests.lesson4;
     17 
     18 import com.example.android.testingfun.R;
     19 import com.example.android.testingfun.lesson4.LaunchActivity;
     20 import com.example.android.testingfun.lesson4.NextActivity;
     21 
     22 import android.content.Intent;
     23 import android.test.ActivityUnitTestCase;
     24 import android.test.suitebuilder.annotation.MediumTest;
     25 import android.widget.Button;
     26 
     27 /**
     28  * Tests LaunchActivity in isolation from the system.
     29  */
     30 public class LaunchActivityTest extends ActivityUnitTestCase<LaunchActivity> {
     31 
     32     private Intent mLaunchIntent;
     33 
     34     public LaunchActivityTest() {
     35         super(LaunchActivity.class);
     36     }
     37 
     38 
     39     @Override
     40     protected void setUp() throws Exception {
     41         super.setUp();
     42         //Create an intent to launch target Activity
     43         mLaunchIntent = new Intent(getInstrumentation().getTargetContext(),
     44                 LaunchActivity.class);
     45     }
     46 
     47     /**
     48      * Tests the preconditions of this test fixture.
     49      */
     50     @MediumTest
     51     public void testPreconditions() {
     52         //Start the activity under test in isolation, without values for savedInstanceState and
     53         //lastNonConfigurationInstance
     54         startActivity(mLaunchIntent, null, null);
     55         final Button launchNextButton = (Button) getActivity().findViewById(R.id.launch_next_activity_button);
     56 
     57         assertNotNull("mLaunchActivity is null", getActivity());
     58         assertNotNull("mLaunchNextButton is null", launchNextButton);
     59     }
     60 
     61 
     62     @MediumTest
     63     public void testLaunchNextActivityButton_labelText() {
     64         startActivity(mLaunchIntent, null, null);
     65         final Button launchNextButton = (Button) getActivity().findViewById(R.id.launch_next_activity_button);
     66 
     67         final String expectedButtonText = getActivity().getString(R.string.label_launch_next);
     68         assertEquals("Unexpected button label text", expectedButtonText,
     69                 launchNextButton.getText());
     70     }
     71 
     72     @MediumTest
     73     public void testNextActivityWasLaunchedWithIntent() {
     74         startActivity(mLaunchIntent, null, null);
     75         final Button launchNextButton = (Button) getActivity().findViewById(R.id.launch_next_activity_button);
     76         //Because this is an isolated ActivityUnitTestCase we have to directly click the
     77         //button from code
     78         launchNextButton.performClick();
     79 
     80         // Get the intent for the next started activity
     81         final Intent launchIntent = getStartedActivityIntent();
     82         //Verify the intent was not null.
     83         assertNotNull("Intent was null", launchIntent);
     84         //Verify that LaunchActivity was finished after button click
     85         assertTrue(isFinishCalled());
     86 
     87 
     88         final String payload = launchIntent.getStringExtra(NextActivity.EXTRAS_PAYLOAD_KEY);
     89         //Verify that payload data was added to the intent
     90         assertEquals("Payload is empty", LaunchActivity.STRING_PAYLOAD
     91                 , payload);
     92     }
     93 }