|
| 1 | +/* |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +package com.microsoft.device.display.samples.sourceeditor |
| 8 | + |
| 9 | +import androidx.test.espresso.Espresso.onView |
| 10 | +import androidx.test.espresso.action.ViewActions.click |
| 11 | +import androidx.test.espresso.action.ViewActions.replaceText |
| 12 | +import androidx.test.espresso.matcher.ViewMatchers.withId |
| 13 | +import androidx.test.espresso.web.assertion.WebViewAssertions.webMatches |
| 14 | +import androidx.test.espresso.web.sugar.Web.onWebView |
| 15 | +import androidx.test.espresso.web.webdriver.DriverAtoms.findElement |
| 16 | +import androidx.test.espresso.web.webdriver.DriverAtoms.getText |
| 17 | +import androidx.test.espresso.web.webdriver.Locator |
| 18 | +import androidx.test.filters.MediumTest |
| 19 | +import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner |
| 20 | +import androidx.test.rule.ActivityTestRule |
| 21 | +import com.microsoft.device.display.samples.sourceeditor.utils.ScreenInfoListenerImpl |
| 22 | +import com.microsoft.device.display.samples.sourceeditor.utils.moveToLeft |
| 23 | +import com.microsoft.device.display.samples.sourceeditor.utils.moveToRight |
| 24 | +import com.microsoft.device.display.samples.sourceeditor.utils.spanFromLeft |
| 25 | +import com.microsoft.device.display.samples.sourceeditor.utils.spanFromRight |
| 26 | +import com.microsoft.device.display.samples.sourceeditor.utils.switchFromSingleToDualScreen |
| 27 | +import com.microsoft.device.display.samples.sourceeditor.utils.unspanToLeft |
| 28 | +import com.microsoft.device.display.samples.sourceeditor.utils.unspanToRight |
| 29 | +import com.microsoft.device.dualscreen.ScreenManagerProvider |
| 30 | +import org.hamcrest.core.StringContains.containsString |
| 31 | +import org.junit.After |
| 32 | +import org.junit.Assert.assertFalse |
| 33 | +import org.junit.Assert.assertTrue |
| 34 | +import org.junit.Before |
| 35 | +import org.junit.Rule |
| 36 | +import org.junit.Test |
| 37 | +import org.junit.runner.RunWith |
| 38 | + |
| 39 | +@MediumTest |
| 40 | +@RunWith(AndroidJUnit4ClassRunner::class) |
| 41 | +class PreviewTest { |
| 42 | + @get:Rule |
| 43 | + val activityRule = ActivityTestRule(MainActivity::class.java, false, false) |
| 44 | + private var screenInfoListener = ScreenInfoListenerImpl() |
| 45 | + private val testString = "Testing in a different browser" |
| 46 | + |
| 47 | + @Before |
| 48 | + fun setup() { |
| 49 | + val screenManager = ScreenManagerProvider.getScreenManager() |
| 50 | + screenManager.addScreenInfoListener(screenInfoListener) |
| 51 | + activityRule.launchActivity(null) |
| 52 | + } |
| 53 | + |
| 54 | + @After |
| 55 | + fun tearDown() { |
| 56 | + val screenManager = ScreenManagerProvider.getScreenManager() |
| 57 | + screenManager.removeScreenInfoListener(screenInfoListener) |
| 58 | + screenInfoListener.resetScreenInfo() |
| 59 | + screenInfoListener.resetScreenInfoCounter() |
| 60 | + activityRule.finishActivity() |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun previewTextInSingleScreenMode() { |
| 65 | + screenInfoListener.waitForScreenInfoChanges() |
| 66 | + |
| 67 | + onView(withId(R.id.btn_switch_to_preview)).perform(click()) |
| 68 | + onWebView() |
| 69 | + .withElement(findElement(Locator.TAG_NAME, "h1")) |
| 70 | + .check(webMatches(getText(), containsString("Testing in a browser"))) |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + fun previewTextInDualScreenMode() { |
| 75 | + screenInfoListener.waitForScreenInfoChanges() |
| 76 | + |
| 77 | + onView(withId(R.id.textinput_code)).perform(replaceText("<h1>$testString</h1>")) |
| 78 | + switchFromSingleToDualScreen() |
| 79 | + assert(isSpanned()) |
| 80 | + onWebView() |
| 81 | + .withElement(findElement(Locator.TAG_NAME, "h1")) |
| 82 | + .check(webMatches(getText(), containsString(testString))) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun testSpanning() { |
| 87 | + screenInfoListener.waitForScreenInfoChanges() |
| 88 | + screenInfoListener.resetScreenInfo() |
| 89 | + |
| 90 | + spanFromLeft() |
| 91 | + waitForScreenInfoAndAssert { assertTrue(isSpanned()) } |
| 92 | + unspanToRight() |
| 93 | + waitForScreenInfoAndAssert { assertFalse(isSpanned()) } |
| 94 | + spanFromRight() |
| 95 | + waitForScreenInfoAndAssert { assertTrue(isSpanned()) } |
| 96 | + unspanToLeft() |
| 97 | + waitForScreenInfoAndAssert { assertFalse(isSpanned()) } |
| 98 | + moveToRight() |
| 99 | + moveToLeft() |
| 100 | + } |
| 101 | + |
| 102 | + private fun waitForScreenInfoAndAssert(assert: () -> Unit) { |
| 103 | + screenInfoListener.waitForScreenInfoChanges() |
| 104 | + assert() |
| 105 | + screenInfoListener.resetScreenInfo() |
| 106 | + } |
| 107 | + |
| 108 | + private fun isSpanned(): Boolean { |
| 109 | + return screenInfoListener.screenInfo?.isDualMode() == true |
| 110 | + } |
| 111 | +} |
0 commit comments