diff --git a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ContentBlockingControllerTest.kt b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ContentBlockingControllerTest.kt index c7adb85dee09..a590deaea0c7 100644 --- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ContentBlockingControllerTest.kt +++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ContentBlockingControllerTest.kt @@ -6,8 +6,7 @@ package org.mozilla.geckoview.test import androidx.test.filters.MediumTest import androidx.test.ext.junit.runners.AndroidJUnit4 -import org.hamcrest.Matchers -import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.* import org.junit.Ignore import org.junit.Test import org.junit.runner.RunWith @@ -15,6 +14,7 @@ import org.mozilla.geckoview.ContentBlocking import org.mozilla.geckoview.ContentBlockingController import org.mozilla.geckoview.ContentBlockingController.ContentBlockingException import org.mozilla.geckoview.GeckoSession +import org.mozilla.geckoview.GeckoSessionSettings import org.mozilla.geckoview.test.rule.GeckoSessionTestRule import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.AssertCalled import org.mozilla.geckoview.test.util.Callbacks @@ -23,14 +23,12 @@ import org.junit.Assume.assumeThat @RunWith(AndroidJUnit4::class) @MediumTest class ContentBlockingControllerTest : BaseSessionTest() { - @GeckoSessionTestRule.Setting(key = GeckoSessionTestRule.Setting.Key.USE_TRACKING_PROTECTION, value = "true") - @Test - fun trackingProtectionException() { - // disable test on debug for frequently failing #Bug 1580223 - assumeThat(sessionRule.env.isDebugBuild, equalTo(false)) + private fun testTrackingProtectionException(baseSettings: GeckoSessionSettings) { val category = ContentBlocking.AntiTracking.TEST sessionRule.runtime.settings.contentBlocking.setAntiTracking(category) - sessionRule.session.loadTestPath(TRACKERS_PATH) + + val session1 = sessionRule.createOpenSession(baseSettings) + session1.loadTestPath(TRACKERS_PATH) sessionRule.waitUntilCalled( object : Callbacks.ContentBlockingDelegate { @@ -39,24 +37,35 @@ class ContentBlockingControllerTest : BaseSessionTest() { event: ContentBlocking.BlockEvent) { assertThat("Category should be set", event.antiTrackingCategory, - Matchers.equalTo(category)) - assertThat("URI should not be null", event.uri, Matchers.notNullValue()) - assertThat("URI should match", event.uri, Matchers.endsWith("tracker.js")) + equalTo(category)) + assertThat("URI should not be null", event.uri, notNullValue()) + assertThat("URI should match", event.uri, endsWith("tracker.js")) } }) // Add exception for this site. - sessionRule.runtime.contentBlockingController.addException(sessionRule.session) + sessionRule.runtime.contentBlockingController.addException(session1) - sessionRule.runtime.contentBlockingController.checkException(sessionRule.session).accept { - assertThat("Site should be on exceptions list", it, Matchers.equalTo(true)) + sessionRule.runtime.contentBlockingController.checkException(session1).accept { + assertThat("Site should be on exceptions list", it, equalTo(true)) } var list = sessionRule.waitForResult(sessionRule.runtime.contentBlockingController.saveExceptionList()) - assertThat("Exceptions list should not be null", list, Matchers.notNullValue()) - assertThat("Exceptions list should have one entry", list.size, Matchers.equalTo(1)) + assertThat("Exceptions list should not be null", list, notNullValue()) - sessionRule.session.reload() + if (baseSettings.usePrivateMode) { + assertThat( + "Exceptions list should be empty", + list.size, + equalTo(0)) + } else { + assertThat( + "Exceptions list should have one entry", + list.size, + equalTo(1)) + } + + session1.reload() sessionRule.waitForPageStop() sessionRule.forCallbacksDuringWait( @@ -68,13 +77,14 @@ class ContentBlockingControllerTest : BaseSessionTest() { }) // Remove exception for this site by passing GeckoSession. - sessionRule.runtime.contentBlockingController.removeException(sessionRule.session) + sessionRule.runtime.contentBlockingController.removeException(session1) - list = sessionRule.waitForResult(sessionRule.runtime.contentBlockingController.saveExceptionList()) - assertThat("Exceptions list should not be null", list, Matchers.notNullValue()) - assertThat("Exceptions list should have one entry", list.size, Matchers.equalTo(0)) + list = sessionRule.waitForResult( + sessionRule.runtime.contentBlockingController.saveExceptionList()) + assertThat("Exceptions list should not be null", list, notNullValue()) + assertThat("Exceptions list should be empty", list.size, equalTo(0)) - sessionRule.session.reload() + session1.reload() sessionRule.waitUntilCalled( object : Callbacks.ContentBlockingDelegate { @@ -83,13 +93,34 @@ class ContentBlockingControllerTest : BaseSessionTest() { event: ContentBlocking.BlockEvent) { assertThat("Category should be set", event.antiTrackingCategory, - Matchers.equalTo(category)) - assertThat("URI should not be null", event.uri, Matchers.notNullValue()) - assertThat("URI should match", event.uri, Matchers.endsWith("tracker.js")) + equalTo(category)) + assertThat("URI should not be null", event.uri, notNullValue()) + assertThat("URI should match", event.uri, endsWith("tracker.js")) } }) } + @GeckoSessionTestRule.Setting(key = GeckoSessionTestRule.Setting.Key.USE_TRACKING_PROTECTION, value = "true") + @Test + fun trackingProtectionExceptionPrivateMode() { + // disable test on debug for frequently failing #Bug 1580223 + assumeThat(sessionRule.env.isDebugBuild, equalTo(false)) + + testTrackingProtectionException( + GeckoSessionSettings.Builder(mainSession.settings) + .usePrivateMode(true) + .build()) + } + + @GeckoSessionTestRule.Setting(key = GeckoSessionTestRule.Setting.Key.USE_TRACKING_PROTECTION, value = "true") + @Test + fun trackingProtectionException() { + // disable test on debug for frequently failing #Bug 1580223 + assumeThat(sessionRule.env.isDebugBuild, equalTo(false)) + + testTrackingProtectionException(mainSession.settings) + } + @GeckoSessionTestRule.Setting(key = GeckoSessionTestRule.Setting.Key.USE_TRACKING_PROTECTION, value = "true") @Test fun trackingProtectionExceptionRemoveByException() { @@ -106,9 +137,9 @@ class ContentBlockingControllerTest : BaseSessionTest() { event: ContentBlocking.BlockEvent) { assertThat("Category should be set", event.antiTrackingCategory, - Matchers.equalTo(category)) - assertThat("URI should not be null", event.uri, Matchers.notNullValue()) - assertThat("URI should match", event.uri, Matchers.endsWith("tracker.js")) + equalTo(category)) + assertThat("URI should not be null", event.uri, notNullValue()) + assertThat("URI should match", event.uri, endsWith("tracker.js")) } }) @@ -116,12 +147,12 @@ class ContentBlockingControllerTest : BaseSessionTest() { sessionRule.runtime.contentBlockingController.addException(sessionRule.session) sessionRule.runtime.contentBlockingController.checkException(sessionRule.session).accept { - assertThat("Site should be on exceptions list", it, Matchers.equalTo(true)) + assertThat("Site should be on exceptions list", it, equalTo(true)) } var list = sessionRule.waitForResult(sessionRule.runtime.contentBlockingController.saveExceptionList()) - assertThat("Exceptions list should not be null", list, Matchers.notNullValue()) - assertThat("Exceptions list should have one entry", list.size, Matchers.equalTo(1)) + assertThat("Exceptions list should not be null", list, notNullValue()) + assertThat("Exceptions list should have one entry", list.size, equalTo(1)) sessionRule.session.reload() sessionRule.waitForPageStop() @@ -138,8 +169,8 @@ class ContentBlockingControllerTest : BaseSessionTest() { sessionRule.runtime.contentBlockingController.removeException(list.get(0)) list = sessionRule.waitForResult(sessionRule.runtime.contentBlockingController.saveExceptionList()) - assertThat("Exceptions list should not be null", list, Matchers.notNullValue()) - assertThat("Exceptions list should have one entry", list.size, Matchers.equalTo(0)) + assertThat("Exceptions list should not be null", list, notNullValue()) + assertThat("Exceptions list should have one entry", list.size, equalTo(0)) sessionRule.session.reload() @@ -150,9 +181,9 @@ class ContentBlockingControllerTest : BaseSessionTest() { event: ContentBlocking.BlockEvent) { assertThat("Category should be set", event.antiTrackingCategory, - Matchers.equalTo(category)) - assertThat("URI should not be null", event.uri, Matchers.notNullValue()) - assertThat("URI should match", event.uri, Matchers.endsWith("tracker.js")) + equalTo(category)) + assertThat("URI should not be null", event.uri, notNullValue()) + assertThat("URI should match", event.uri, endsWith("tracker.js")) } }) } @@ -172,18 +203,18 @@ class ContentBlockingControllerTest : BaseSessionTest() { var export = sessionRule.waitForResult(sessionRule.runtime.contentBlockingController .saveExceptionList()) - assertThat("Exported list must not be null", export, Matchers.notNullValue()) - assertThat("Exported list must contain one entry", export.size, Matchers.equalTo(1)) + assertThat("Exported list must not be null", export, notNullValue()) + assertThat("Exported list must contain one entry", export.size, equalTo(1)) val exportJson = export.get(0).toJson() - assertThat("Exported JSON must not be null", exportJson, Matchers.notNullValue()) + assertThat("Exported JSON must not be null", exportJson, notNullValue()) // Wipe sessionRule.runtime.contentBlockingController.clearExceptionList() export = sessionRule.waitForResult(sessionRule.runtime.contentBlockingController .saveExceptionList()) - assertThat("Exported list must not be null", export, Matchers.notNullValue()) - assertThat("Exported list must contain zero entries", export.size, Matchers.equalTo(0)) + assertThat("Exported list must not be null", export, notNullValue()) + assertThat("Exported list must contain zero entries", export.size, equalTo(0)) // Restore from JSON val importJson = listOf(ContentBlockingException.fromJson(exportJson)) @@ -191,8 +222,8 @@ class ContentBlockingControllerTest : BaseSessionTest() { export = sessionRule.waitForResult(sessionRule.runtime.contentBlockingController .saveExceptionList()) - assertThat("Exported list must not be null", export, Matchers.notNullValue()) - assertThat("Exported list must contain one entry", export.size, Matchers.equalTo(1)) + assertThat("Exported list must not be null", export, notNullValue()) + assertThat("Exported list must contain one entry", export.size, equalTo(1)) // Wipe so as not to break other tests. sessionRule.runtime.contentBlockingController.clearExceptionList() @@ -214,14 +245,14 @@ class ContentBlockingControllerTest : BaseSessionTest() { }) sessionRule.waitForResult(sessionRule.runtime.contentBlockingController.getLog(sessionRule.session).accept { - assertThat("Log must not be null", it, Matchers.notNullValue()) - assertThat("Log must have at least one entry", it?.size, Matchers.not(0)) + assertThat("Log must not be null", it, notNullValue()) + assertThat("Log must have at least one entry", it?.size, not(0)) it?.forEach { it.blockingData.forEach { assertThat("Category must match", it.category, - Matchers.equalTo(ContentBlockingController.Event.BLOCKED_TRACKING_CONTENT)) - assertThat("Blocked must be true", it.blocked, Matchers.equalTo(true)) - assertThat("Count must be at least 1", it.count, Matchers.not(0)) + equalTo(ContentBlockingController.Event.BLOCKED_TRACKING_CONTENT)) + assertThat("Blocked must be true", it.blocked, equalTo(true)) + assertThat("Count must be at least 1", it.count, not(0)) } } })