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!"));
   
}
}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. androd 事件分发机制的初步理解
  2. 在Android 2.3中如何使用native_activity
  3. Android Weekly - 第 175 期
  4. 成功解决 Android 下载中文文件名
  5. 记录Android中@Parcelize的一个小坑
  6. 图解Android - Zygote, System Server 启
  7. Android之动画(Java代码实现)1
  8. android之cursor
  9. 控件(2)toast
  10. 【Android】远程服务(Remote Service)的