|
| 1 | +/* |
| 2 | + * Paintroid: An image manipulation application for Android. |
| 3 | + * Copyright (C) 2010-2023 The Catrobat Team |
| 4 | + * (<http://developer.catrobat.org/credits>) |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation, either version 3 of the |
| 9 | + * License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.catrobat.paintroid.test.espresso |
| 21 | + |
| 22 | +import android.graphics.Color |
| 23 | +import android.webkit.WebView |
| 24 | +import androidx.test.espresso.Espresso.onView |
| 25 | +import androidx.test.espresso.IdlingRegistry |
| 26 | +import androidx.test.espresso.IdlingResource |
| 27 | +import androidx.test.espresso.assertion.ViewAssertions |
| 28 | +import androidx.test.espresso.matcher.ViewMatchers.withId |
| 29 | +import androidx.test.espresso.web.sugar.Web.onWebView |
| 30 | +import androidx.test.espresso.web.webdriver.DriverAtoms.webClick |
| 31 | +import androidx.test.espresso.web.webdriver.DriverAtoms.findElement |
| 32 | +import androidx.test.espresso.web.webdriver.Locator |
| 33 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 34 | +import androidx.test.rule.ActivityTestRule |
| 35 | +import kotlinx.coroutines.delay |
| 36 | +import kotlinx.coroutines.runBlocking |
| 37 | +import org.catrobat.paintroid.MainActivity |
| 38 | +import org.catrobat.paintroid.R |
| 39 | +import org.catrobat.paintroid.test.espresso.util.BitmapLocationProvider |
| 40 | +import org.catrobat.paintroid.test.espresso.util.wrappers.DrawingSurfaceInteraction |
| 41 | +import org.catrobat.paintroid.test.espresso.util.wrappers.ImportToolOptionsViewInteraction.Companion.onImportToolOptionsViewInteraction |
| 42 | +import org.catrobat.paintroid.test.espresso.util.wrappers.ToolBarViewInteraction.Companion.onToolBarView |
| 43 | +import org.catrobat.paintroid.test.espresso.util.wrappers.TopBarViewInteraction |
| 44 | +import org.catrobat.paintroid.test.testsuites.annotations.MediaGalleryTests |
| 45 | +import org.catrobat.paintroid.test.utils.ScreenshotOnFailRule |
| 46 | +import org.catrobat.paintroid.tools.ToolType |
| 47 | +import org.junit.After |
| 48 | +import org.junit.Before |
| 49 | +import org.junit.Rule |
| 50 | +import org.junit.Test |
| 51 | +import org.junit.experimental.categories.Category |
| 52 | +import org.junit.runner.RunWith |
| 53 | + |
| 54 | +@RunWith(AndroidJUnit4::class) |
| 55 | +@Category(MediaGalleryTests::class) |
| 56 | +class MediaGalleryWebViewClientIntegrationTest { |
| 57 | + companion object { |
| 58 | + private const val TOP_APP_BAR_TITLE = "top-app-bar__title" |
| 59 | + private const val TOP_APP_BAR_BUTTON_SIDEBAR_TOGGLE = "top-app-bar__btn-sidebar-toggle" |
| 60 | + private const val LOGO = "logo" |
| 61 | + private const val MEDIA_FILE = "mediafile-335" |
| 62 | + } |
| 63 | + |
| 64 | + @get:Rule |
| 65 | + val launchActivityRule = ActivityTestRule( |
| 66 | + MainActivity::class.java |
| 67 | + ) |
| 68 | + |
| 69 | + @get:Rule |
| 70 | + var screenshotOnFailRule = ScreenshotOnFailRule() |
| 71 | + |
| 72 | + private lateinit var mainActivity: MainActivity |
| 73 | + private lateinit var idlingResource: IdlingResource |
| 74 | + |
| 75 | + @Before |
| 76 | + fun setUp() { |
| 77 | + mainActivity = launchActivityRule.activity |
| 78 | + idlingResource = mainActivity.idlingResource |
| 79 | + IdlingRegistry.getInstance().register(idlingResource) |
| 80 | + } |
| 81 | + |
| 82 | + @After |
| 83 | + fun tearDown() { |
| 84 | + IdlingRegistry.getInstance().unregister(idlingResource) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun testClickingOnCatrobatCommunityClosesWebView() { |
| 89 | + onToolBarView().performSelectTool(ToolType.IMPORTPNG) |
| 90 | + onImportToolOptionsViewInteraction().performOpenStickers() |
| 91 | + clickElementInWebView(120_000, Locator.ID, TOP_APP_BAR_TITLE) |
| 92 | + checkIfViewDisappears(120_000, R.id.webview) |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun testClickingOnCatrobatLogoClosesWebView() { |
| 97 | + onToolBarView().performSelectTool(ToolType.IMPORTPNG) |
| 98 | + onImportToolOptionsViewInteraction().performOpenStickers() |
| 99 | + clickElementInWebView(120_000, Locator.ID, TOP_APP_BAR_BUTTON_SIDEBAR_TOGGLE) |
| 100 | + clickElementInWebView(120_000, Locator.CLASS_NAME, LOGO) |
| 101 | + checkIfViewDisappears(120_000, R.id.webview) |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun testImportSticker() { |
| 106 | + runBlocking { |
| 107 | + delay(3000) |
| 108 | + } |
| 109 | + DrawingSurfaceInteraction.onDrawingSurfaceView().checkPixelColor(Color.TRANSPARENT, BitmapLocationProvider.MIDDLE) |
| 110 | + onToolBarView().performSelectTool(ToolType.IMPORTPNG) |
| 111 | + onImportToolOptionsViewInteraction().performOpenStickers() |
| 112 | + clickElementInWebView(120_000, Locator.ID, MEDIA_FILE) |
| 113 | + checkIfViewDisappears(120_000, R.id.webview) |
| 114 | + runBlocking { |
| 115 | + delay(5000) |
| 116 | + } |
| 117 | + TopBarViewInteraction.onTopBarView().performClickCheckmark() |
| 118 | + DrawingSurfaceInteraction.onDrawingSurfaceView().checkPixelColorIsNotTransparent(BitmapLocationProvider.MIDDLE) |
| 119 | + } |
| 120 | + |
| 121 | + @SuppressWarnings("SwallowedException") |
| 122 | + private fun clickElementInWebView(maxWaitingTimeMs: Int, locator: Locator, name: String) { |
| 123 | + val endTime = System.currentTimeMillis() + maxWaitingTimeMs |
| 124 | + do { |
| 125 | + try { |
| 126 | + onWebView().withElement(findElement(locator, name)).perform(webClick()) |
| 127 | + return |
| 128 | + } catch (e: java.lang.RuntimeException) { |
| 129 | + runBlocking { |
| 130 | + delay(1000) |
| 131 | + } |
| 132 | + continue |
| 133 | + } |
| 134 | + } while (System.currentTimeMillis() <= endTime) |
| 135 | + onWebView().withElement(findElement(locator, name)).perform(webClick()) |
| 136 | + } |
| 137 | + |
| 138 | + private fun checkIfViewDisappears(maxWaitingTimeMs: Int, viewId: Int) { |
| 139 | + val endTime = System.currentTimeMillis() + maxWaitingTimeMs |
| 140 | + do { |
| 141 | + val view = mainActivity.findViewById<WebView>(viewId) |
| 142 | + } while (view != null && System.currentTimeMillis() <= endTime) |
| 143 | + onView(withId(viewId)).check(ViewAssertions.doesNotExist()) |
| 144 | + } |
| 145 | +} |
0 commit comments