From 8ce43f74a888ffbf3dd8e7abd16a195603a94220 Mon Sep 17 00:00:00 2001 From: Billy Brawner Date: Sat, 17 Aug 2019 13:51:37 -0500 Subject: [PATCH] Convert MainActivityTests to Kotlin --- .../simplemarkdown/MainActivityTests.java | 72 ------------------- .../simplemarkdown/MainActivityTests.kt | 65 +++++++++++++++++ 2 files changed, 65 insertions(+), 72 deletions(-) delete mode 100644 app/src/androidTest/java/com/wbrawner/simplemarkdown/MainActivityTests.java create mode 100644 app/src/androidTest/java/com/wbrawner/simplemarkdown/MainActivityTests.kt diff --git a/app/src/androidTest/java/com/wbrawner/simplemarkdown/MainActivityTests.java b/app/src/androidTest/java/com/wbrawner/simplemarkdown/MainActivityTests.java deleted file mode 100644 index a5d0ebc..0000000 --- a/app/src/androidTest/java/com/wbrawner/simplemarkdown/MainActivityTests.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.wbrawner.simplemarkdown; - -import android.content.Context; -import android.content.pm.ActivityInfo; - -import androidx.test.InstrumentationRegistry; -import androidx.test.rule.ActivityTestRule; -import androidx.test.runner.AndroidJUnit4; -import androidx.test.uiautomator.UiDevice; -import androidx.test.uiautomator.UiObject; -import androidx.test.uiautomator.UiScrollable; -import androidx.test.uiautomator.UiSelector; - -import com.wbrawner.simplemarkdown.view.activity.MainActivity; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -import static androidx.test.InstrumentationRegistry.getInstrumentation; -import static androidx.test.espresso.Espresso.onView; -import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu; -import static androidx.test.espresso.action.ViewActions.click; -import static androidx.test.espresso.matcher.ViewMatchers.withText; -import static org.junit.Assert.assertEquals; - -/** - * Instrumentation test, which will execute on an Android device. - * - * @see Testing documentation - */ -@RunWith(AndroidJUnit4.class) -public class MainActivityTests { - - @Rule - public ActivityTestRule mActivityRule = - new ActivityTestRule<>(MainActivity.class); - - @Before - public void setup() { - mActivityRule.getActivity() - .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - } - - @Test - public void openAppTest() throws Exception { - UiDevice mDevice = UiDevice.getInstance(getInstrumentation()); - mDevice.pressHome(); - // Bring up the default launcher by searching for a UI component - // that matches the content description for the launcher button. - UiObject allAppsButton = mDevice - .findObject(new UiSelector().description("Apps")); - - // Perform a click on the button to load the launcher. - allAppsButton.clickAndWaitForNewWindow(); - // Context of the app under test. - Context appContext = InstrumentationRegistry.getTargetContext(); - - assertEquals("com.wbrawner.simplemarkdown", appContext.getPackageName()); - UiScrollable appView = new UiScrollable(new UiSelector().scrollable(true)); - UiSelector simpleMarkdownSelector = new UiSelector().text("Simple Markdown"); - appView.scrollIntoView(simpleMarkdownSelector); - mDevice.findObject(simpleMarkdownSelector).clickAndWaitForNewWindow(); - } - - @Test - public void openFileWithoutFilesTest() { - openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext()); - onView(withText("Open")).perform(click()); - } -} diff --git a/app/src/androidTest/java/com/wbrawner/simplemarkdown/MainActivityTests.kt b/app/src/androidTest/java/com/wbrawner/simplemarkdown/MainActivityTests.kt new file mode 100644 index 0000000..3fbb88e --- /dev/null +++ b/app/src/androidTest/java/com/wbrawner/simplemarkdown/MainActivityTests.kt @@ -0,0 +1,65 @@ +package com.wbrawner.simplemarkdown + +import android.content.pm.ActivityInfo +import androidx.test.InstrumentationRegistry +import androidx.test.InstrumentationRegistry.getInstrumentation +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu +import androidx.test.espresso.action.ViewActions.click +import androidx.test.espresso.matcher.ViewMatchers.withText +import androidx.test.rule.ActivityTestRule +import androidx.test.runner.AndroidJUnit4 +import androidx.test.uiautomator.UiDevice +import androidx.test.uiautomator.UiScrollable +import androidx.test.uiautomator.UiSelector +import com.wbrawner.simplemarkdown.view.activity.MainActivity +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see [Testing documentation](http://d.android.com/tools/testing) + */ +@RunWith(AndroidJUnit4::class) +class MainActivityTests { + + @Rule + var mActivityRule = ActivityTestRule(MainActivity::class.java) + + @Before + fun setup() { + mActivityRule.activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT + } + + @Test + @Throws(Exception::class) + fun openAppTest() { + val mDevice = UiDevice.getInstance(getInstrumentation()) + mDevice.pressHome() + // Bring up the default launcher by searching for a UI component + // that matches the content description for the launcher button. + val allAppsButton = mDevice + .findObject(UiSelector().description("Apps")) + + // Perform a click on the button to load the launcher. + allAppsButton.clickAndWaitForNewWindow() + // Context of the app under test. + val appContext = InstrumentationRegistry.getTargetContext() + + assertEquals("com.wbrawner.simplemarkdown", appContext.packageName) + val appView = UiScrollable(UiSelector().scrollable(true)) + val simpleMarkdownSelector = UiSelector().text("Simple Markdown") + appView.scrollIntoView(simpleMarkdownSelector) + mDevice.findObject(simpleMarkdownSelector).clickAndWaitForNewWindow() + } + + @Test + fun openFileWithoutFilesTest() { + openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext()) + onView(withText("Open")).perform(click()) + } +}