Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@

package com.example.android.testing.espresso.BasicSample

import androidx.test.ext.junit.rules.activityScenarioRule
import android.app.Activity
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.launchActivity
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.closeSoftKeyboard
import androidx.test.espresso.action.ViewActions.typeText
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith


/**
* The kotlin equivalent to the ChangeTextBehaviorTest, that
* showcases simple view matchers and actions like [ViewMatchers.withId],
Expand All @@ -56,10 +54,9 @@ class ChangeTextBehaviorKtTest {

@Test
fun changeText_sameActivity() {

// Type text and then press the button.
onView(withId(R.id.editTextUserInput))
.perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard())
.perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard())
onView(withId(R.id.changeTextBt)).perform(click())

// Check that the text was changed.
Expand All @@ -69,8 +66,10 @@ class ChangeTextBehaviorKtTest {
@Test
fun changeText_newActivity() {
// Type text and then press the button.
onView(withId(R.id.editTextUserInput)).perform(typeText(STRING_TO_BE_TYPED),
closeSoftKeyboard())
onView(withId(R.id.editTextUserInput)).perform(
typeText(STRING_TO_BE_TYPED),
closeSoftKeyboard()
)
onView(withId(R.id.activityChangeTextBtn)).perform(click())

// This view is in a different Activity, no need to tell Espresso.
Expand All @@ -81,4 +80,4 @@ class ChangeTextBehaviorKtTest {

val STRING_TO_BE_TYPED = "Espresso"
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2015, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.testing.espresso.BasicSample

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView

/**
* An [Activity] that gets a text string from the user and displays it back when the user
* clicks on one of the two buttons. The first one shows it in the same activity and the second
* one opens another activity and displays the message.
*/
class MainActivity : Activity(), View.OnClickListener {
// The TextView used to display the message inside the Activity.
private var mTextView: TextView? = null

// The EditText where the user types the message.
private var mEditText: EditText? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Set the listeners for the buttons.
findViewById<View>(R.id.changeTextBt).setOnClickListener(this)
findViewById<View>(R.id.activityChangeTextBtn).setOnClickListener(this)
mTextView = findViewById<View>(R.id.textToBeChanged) as TextView
mEditText = findViewById<View>(R.id.editTextUserInput) as EditText
}

override fun onClick(view: View) {
// Get the text from the EditText view.
val text = mEditText!!.text.toString()
val changeTextBtId = R.id.changeTextBt
val activityChangeTextBtnId = R.id.activityChangeTextBtn
if (view.id == changeTextBtId) {
// First button's interaction: set a text in a text view.
mTextView!!.text = text
} else if (view.id == activityChangeTextBtnId) {
// Second button's interaction: start an activity and send a message to it.
val intent = ShowTextActivity
.newStartIntent(this, text)
startActivity(intent)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2015, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.testing.espresso.BasicSample

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.google.common.base.Strings

/**
* A simple [Activity] that shows a message.
*/
class ShowTextActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_show_text)

// Get the message from the Intent.
val intent = intent
val message = Strings.nullToEmpty(intent.getStringExtra(KEY_EXTRA_MESSAGE))

// Show message.
(findViewById<View>(R.id.show_text_view) as TextView).text = message
}

companion object {
// The name of the extra data sent through an {@link Intent}.
const val KEY_EXTRA_MESSAGE = "com.example.android.testing.espresso.basicsample.MESSAGE"

/**
* Creates an [Intent] for [ShowTextActivity] with the message to be displayed.
* @param context the [Context] where the [Intent] will be used
* @param message a [String] with text to be displayed
* @return an [Intent] used to start [ShowTextActivity]
*/
fun newStartIntent(context: Context?, message: String?): Intent {
val newIntent = Intent(context, ShowTextActivity::class.java)
newIntent.putExtra(KEY_EXTRA_MESSAGE, message)
return newIntent
}
}
}

This file was deleted.

Loading