mirror of
https://github.com/flutter/samples.git
synced 2026-05-10 17:07:28 +00:00
Adds espresso testing for add_to_app/android_fullscreen (#323)
This commit is contained in:
@@ -4,25 +4,68 @@
|
||||
|
||||
package dev.flutter.example.androidfullscreen
|
||||
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.core.app.ActivityScenario
|
||||
import androidx.test.espresso.Espresso.onView
|
||||
import androidx.test.espresso.flutter.EspressoFlutter.onFlutterWidget
|
||||
import androidx.test.espresso.flutter.action.FlutterActions.click
|
||||
import androidx.test.espresso.flutter.assertion.FlutterAssertions.matches
|
||||
import androidx.test.espresso.flutter.matcher.FlutterMatchers
|
||||
import androidx.test.espresso.flutter.matcher.FlutterMatchers.withText
|
||||
import androidx.test.espresso.matcher.ViewMatchers.withId
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class ExampleInstrumentedTest {
|
||||
class MainActivityTest {
|
||||
@Before
|
||||
fun setUp() {
|
||||
ActivityScenario.launch(MainActivity::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun useAppContext() {
|
||||
// Context of the app under test.
|
||||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
||||
assertEquals("dev.flutter.example.androidfullscreen", appContext.packageName)
|
||||
fun flutterTextUpdatesOnClick() {
|
||||
// Launch Flutter module.
|
||||
onView(withId(R.id.launch_button)).perform(androidx.test.espresso.action.ViewActions.click())
|
||||
|
||||
// Verify state is inited correctly.
|
||||
onFlutterWidget(withText("Taps: 0"))
|
||||
.check(matches(FlutterMatchers.isExisting()))
|
||||
|
||||
// Verify the increment button works.
|
||||
onFlutterWidget(withText("Tap me!")).perform(click())
|
||||
onFlutterWidget(withText("Taps: 1"))
|
||||
.check(matches(FlutterMatchers.isExisting()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun nativeTextViewUpdatesOnClick() {
|
||||
// Verify Android TextView is inited correctly.
|
||||
onView(withId(R.id.counter_label)).check(
|
||||
androidx.test.espresso.assertion.ViewAssertions.matches(
|
||||
// TODO(redbrogdon): This should be a check for 0 rather than 1. Because our state
|
||||
// management is hardcoded into the Application object, though, and that object is
|
||||
// reused across tests, this test begins with a counter already incremented by the
|
||||
// previous one. This situation can be corrected via DI or a number of other
|
||||
// approaches.
|
||||
androidx.test.espresso.matcher.ViewMatchers.withText("Current count: 1")
|
||||
)
|
||||
)
|
||||
|
||||
// Launch Flutter module.
|
||||
onView(withId(R.id.launch_button)).perform(androidx.test.espresso.action.ViewActions.click())
|
||||
|
||||
// Increment count.
|
||||
onFlutterWidget(withText("Tap me!")).perform(click())
|
||||
|
||||
// Exit Flutter module and verify that the Android TextView is updated correctly.
|
||||
onFlutterWidget(withText("Exit this screen")).perform(click())
|
||||
onView(withId(R.id.counter_label)).check(
|
||||
androidx.test.espresso.assertion.ViewAssertions.matches(
|
||||
// TODO(redbrogdon): s/2/1
|
||||
androidx.test.espresso.matcher.ViewMatchers.withText("Current count: 2")
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="dev.flutter.example.androidfullscreen">
|
||||
|
||||
<!-- Include Cleartext traffic for the espresso package -->
|
||||
|
||||
<application android:usesCleartextTraffic="true" />
|
||||
|
||||
</manifest>
|
||||
@@ -13,17 +13,15 @@
|
||||
<activity android:name=".MainActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity
|
||||
android:name="io.flutter.embedding.android.FlutterActivity"
|
||||
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
|
||||
android:hardwareAccelerated="true"
|
||||
android:windowSoftInputMode="adjustResize"
|
||||
android:exported="true"
|
||||
/>
|
||||
android:hardwareAccelerated="true"
|
||||
android:windowSoftInputMode="adjustResize" />
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
</manifest>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
package dev.flutter.example.androidfullscreen
|
||||
|
||||
import android.app.Application
|
||||
import androidx.multidex.MultiDexApplication
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.embedding.engine.FlutterEngineCache
|
||||
import io.flutter.embedding.engine.dart.DartExecutor
|
||||
@@ -12,7 +12,7 @@ import io.flutter.plugin.common.MethodChannel
|
||||
|
||||
const val ENGINE_ID = "1"
|
||||
|
||||
class MyApplication : Application() {
|
||||
class MyApplication : MultiDexApplication() {
|
||||
var count = 0
|
||||
|
||||
private lateinit var channel: MethodChannel
|
||||
|
||||
Reference in New Issue
Block a user