Skip to content

Commit

Permalink
Bug 1638904 - [1.0] Add a private mode content blocking exception tes…
Browse files Browse the repository at this point in the history
…t. r=agi,geckoview-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D75822
  • Loading branch information
Eugen Sawin committed May 18, 2020
1 parent 87cfab9 commit d4eadbd
Showing 1 changed file with 79 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ 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
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
Expand All @@ -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 {
Expand All @@ -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())

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))
}

sessionRule.session.reload()
session1.reload()
sessionRule.waitForPageStop()

sessionRule.forCallbacksDuringWait(
Expand All @@ -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 {
Expand All @@ -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() {
Expand All @@ -106,22 +137,22 @@ 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.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()
Expand All @@ -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()

Expand All @@ -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"))
}
})
}
Expand All @@ -172,27 +203,27 @@ 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))
sessionRule.runtime.contentBlockingController.restoreExceptionList(importJson)

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()
Expand All @@ -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))
}
}
})
Expand Down

0 comments on commit d4eadbd

Please sign in to comment.