For #18040: no network access startup test

upstream-sync
Oana Horvath 3 years ago
parent 92f1014948
commit 67c8345364

@ -11,13 +11,16 @@ import android.net.Uri
import android.os.Build
import android.os.Environment
import androidx.preference.PreferenceManager
import androidx.test.espresso.Espresso
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.IdlingRegistry
import androidx.test.espresso.action.ViewActions.longClick
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiScrollable
import androidx.test.uiautomator.UiSelector
import androidx.test.uiautomator.Until
@ -26,6 +29,7 @@ import org.hamcrest.CoreMatchers
import org.hamcrest.CoreMatchers.allOf
import org.mozilla.fenix.R
import org.mozilla.fenix.helpers.ext.waitNotNull
import org.mozilla.fenix.helpers.idlingresource.NetworkConnectionIdlingResource
import org.mozilla.fenix.ui.robots.mDevice
import java.io.File
@ -117,4 +121,34 @@ object TestHelper {
}
}
}
fun setNetworkEnabled(enabled: Boolean) {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
val networkDisconnectedIdlingResource = NetworkConnectionIdlingResource(false)
val networkConnectedIdlingResource = NetworkConnectionIdlingResource(true)
when (enabled) {
true -> {
mDevice.executeShellCommand("svc data enable")
mDevice.executeShellCommand("svc wifi enable")
// Wait for network connection to be completely enabled
IdlingRegistry.getInstance().register(networkConnectedIdlingResource)
Espresso.onIdle {
IdlingRegistry.getInstance().unregister(networkConnectedIdlingResource)
}
}
false -> {
mDevice.executeShellCommand("svc data disable")
mDevice.executeShellCommand("svc wifi disable")
// Wait for network connection to be completely disabled
IdlingRegistry.getInstance().register(networkDisconnectedIdlingResource)
Espresso.onIdle {
IdlingRegistry.getInstance().unregister(networkDisconnectedIdlingResource)
}
}
}
}
}

@ -0,0 +1,48 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.helpers.idlingresource
import android.net.ConnectivityManager
import androidx.core.content.getSystemService
import androidx.test.espresso.IdlingResource
import androidx.test.platform.app.InstrumentationRegistry
import org.mozilla.fenix.ext.isOnline
/**
* An IdlingResource implementation that waits until the network connection is online or offline.
* The networkConnected parameter sets the expected connection status.
* Only after connecting/disconnecting has completed further actions will be performed.
*/
class NetworkConnectionIdlingResource(private val networkConnected: Boolean) : IdlingResource {
private var resourceCallback: IdlingResource.ResourceCallback? = null
private val connectionManager =
InstrumentationRegistry.getInstrumentation().context.getSystemService<ConnectivityManager>()
override fun getName(): String {
return this::javaClass.name
}
override fun isIdleNow(): Boolean {
val idle =
if (networkConnected) {
isOnline()
} else {
!isOnline()
}
if (idle)
resourceCallback?.onTransitionToIdle()
return idle
}
override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback?) {
if (callback != null)
resourceCallback = callback
}
private fun isOnline(): Boolean {
return connectionManager!!.isOnline()
}
}

@ -0,0 +1,102 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.ui
import androidx.core.net.toUri
import org.junit.After
import org.junit.Rule
import org.junit.Test
import org.mozilla.fenix.R
import org.mozilla.fenix.helpers.HomeActivityTestRule
import org.mozilla.fenix.helpers.TestHelper.packageName
import org.mozilla.fenix.helpers.TestHelper.setNetworkEnabled
import org.mozilla.fenix.helpers.TestHelper.verifyUrl
import org.mozilla.fenix.ui.robots.browserScreen
import org.mozilla.fenix.ui.robots.homeScreen
import org.mozilla.fenix.ui.robots.navigationToolbar
/**
* Tests to verify some main UI flows with Network connection off
*
*/
class NoNetworkAccessStartupTests {
@get:Rule
val activityTestRule = HomeActivityTestRule(launchActivity = false)
@After
fun tearDown() {
// Restoring network connection
setNetworkEnabled(true)
}
@Test
// Based on STR from https://github.com/mozilla-mobile/fenix/issues/16886
fun noNetworkConnectionStartupTest() {
setNetworkEnabled(false)
activityTestRule.launchActivity(null)
homeScreen {
}.dismissOnboarding()
homeScreen {
verifyHomeScreen()
}
}
@Test
// Based on STR from https://github.com/mozilla-mobile/fenix/issues/16886
fun networkInterruptedFromBrowserToHomeTest() {
val url = "example.com"
activityTestRule.launchActivity(null)
navigationToolbar {
}.enterURLAndEnterToBrowser(url.toUri()) {}
setNetworkEnabled(false)
browserScreen {
}.goToHomescreen {
verifyHomeScreen()
}
}
@Test
fun testPageReloadAfterNetworkInterrupted() {
val url = "example.com"
activityTestRule.launchActivity(null)
navigationToolbar {
}.enterURLAndEnterToBrowser(url.toUri()) {}
setNetworkEnabled(false)
browserScreen {
}.openThreeDotMenu {
}.refreshPage {}
}
@Test
fun testSignInPageWithNoNetworkConnection() {
setNetworkEnabled(false)
activityTestRule.launchActivity(null)
homeScreen {
}.openThreeDotMenu {
}.openSettings {
}.openTurnOnSyncMenu {
tapOnUseEmailToSignIn()
verifyUrl(
"firefox.com",
"$packageName:id/mozac_browser_toolbar_url_view",
R.id.mozac_browser_toolbar_url_view
)
}
}
}
Loading…
Cancel
Save