android testing (三)————关于Android test 中涉及多个Activity的问题

来源:互联网 发布:阿里云短信发送 编辑:程序博客网 时间:2024/06/06 11:43
package com.mycompany;

import android.app.*;
import android.content.*;
import android.test.*;
import android.test.suitebuilder.annotation.*;
import android.util.*;
import android.view.*;
import android.widget.*;

import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsNull.*;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.*;
import static com.mycompany.R.id.*;

public class LoginTests extends InstrumentationTestCase {

   
@MediumTest
   
public void testAValidUserCanLogIn() {

     
Instrumentation instrumentation = getInstrumentation();

     
// Register we are interested in the authentication activiry...
     
Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(AuthenticateActivity.class.getName(), null, false);

     
// Start the authentication activity as the first activity...
     
Intent intent = new Intent(Intent.ACTION_MAIN);
      intent
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent
.setClassName(instrumentation.getTargetContext(), AuthenticateActivity.class.getName());
      instrumentation
.startActivitySync(intent);

     
// Wait for it to start...
     
Activity currentActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 5);
      assertThat
(currentActivity, is(notNullValue()));

     
// Type into the username field...
     
View currentView = currentActivity.findViewById(username_field);
      assertThat
(currentView, is(notNullValue()));
      assertThat
(currentView, instanceOf(EditText.class));
     
TouchUtils.clickView(this, currentView);
      instrumentation
.sendStringSync("MyUsername");

     
// Type into the password field...
      currentView
= currentActivity.findViewById(password_field);
      assertThat
(currentView, is(notNullValue()));
      assertThat
(currentView, instanceOf(EditText.class));
     
TouchUtils.clickView(this, currentView);
      instrumentation
.sendStringSync("MyPassword");

     
// Register we are interested in the welcome activity...
     
// this has to be done before we do something that will send us to that
     
// activity...
      instrumentation
.removeMonitor(monitor);
      monitor
= instrumentation.addMonitor(WelcomeActivity.class.getName(), null, false);

     
// Click the login button...
      currentView
= currentActivity.findViewById(login_button;
      assertThat
(currentView, is(notNullValue()));
      assertThat
(currentView, instanceOf(Button.class));
     
TouchUtils.clickView(this, currentView);

     
// Wait for the welcome page to start...
      currentActivity
= getInstrumentation().waitForMonitorWithTimeout(monitor, 5);
      assertThat
(currentActivity, is(notNullValue()));

     
// Make sure we are logged in...
      currentView
= currentActivity.findViewById(welcome_message);
      assertThat
(currentView, is(notNullValue()));
      assertThat
(currentView, instanceOf(TextView.class));
      assertThat
(((TextView)currentView).getText().toString(), is("Welcome, MyUsername!"));
   
}
}
原创粉丝点击