• Demo1


    Activity代码:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.yimo.myapplication.MainActivity">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/textView" />

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Enter your name"
    android:ems="10"
    android:hint="Enter your name"
    android:id="@+id/editText" />

    <Button
    android:text="sayHello"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText"
    android:layout_toEndOf="@+id/textView"
    android:layout_gravity="center"
    android:onClick="sayHello"
    android:id="@+id/button" />
    </LinearLayout>



    Java代码:
    public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    }

    public void sayHello(View view){
    TextView textView=(TextView) findViewById(R.id.textView);
    EditText editText=(EditText)findViewById(R.id.editText);
    textView.setText("Hello,"+editText.getText().toString()+"!");
    }


    }


    Test代码:
    package com.example.yimo.myapplication;

    import android.support.test.espresso.ViewAssertion;
    import android.support.test.rule.ActivityTestRule;
    import android.view.View;

    import org.hamcrest.Matcher;
    import org.junit.Rule;
    import org.junit.Test;

    import static android.support.test.espresso.Espresso.onView;
    import static android.support.test.espresso.action.ViewActions.click;
    import static android.support.test.espresso.action.ViewActions.typeText;
    import static android.support.test.espresso.assertion.ViewAssertions.matches;
    import static android.support.test.espresso.matcher.ViewMatchers.withId;
    import static android.support.test.espresso.matcher.ViewMatchers.withText;
    import static org.junit.Assert.*;

    /**
    * Created by yimo on 2017/3/18.
    */
    public class MainActivityTest {
    private static final String STRING_TO_BE_TYPED="Peter";
    @Rule
    public ActivityTestRule<MainActivity> mActivityRule=new ActivityTestRule<>(MainActivity.class);

    @Test
    public void sayHello(){
    onView(withId(R.id.editText)).perform(typeText(STRING_TO_BE_TYPED));

    onView(withId(R.id.button)).perform(click());
    String expectedText="Hello,"+STRING_TO_BE_TYPED+"!";
    onView(withId(R.id.textView)).check(matches(withText(expectedText)));
    }




    }
  • 相关阅读:
    python中namedtuple介绍
    导入mysql数据的时候提示Field * doesn't have a default value解决方法
    Django中多表查询思路
    使用questionsModel.values()后不能获取模型中的属性对应的外键属性值的解决方式
    使用django UWSGI 出现 Bad Request (400)
    reverse函数实现指定页面跳转
    Model中内部类meta详解
    models中的pk主键用法
    binlog的几种复制形式
    Mysqldump参数大全(参数来源于mysql5.5.19源码)
  • 原文地址:https://www.cnblogs.com/distanc/p/6579352.html
Copyright © 2020-2023  润新知