mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ContentBlockingControllerTest.kt
author Dylan Roeh <droeh@mozilla.com>
Fri, 20 Sep 2019 22:09:37 +0000
changeset 494339 899aa346b320a180dca88be1485f19cee20d50c4
parent 493461 426128a9c71b541a2a17235a57edb703704c0111
child 494616 b4ec351c24c65dd4680771affe7a5a7e54f52e42
permissions -rw-r--r--
Bug 1564920 - Add browser.safebrowsing.debug to test environment prefs. r=geckoview-reviewers,agi Differential Revision: https://phabricator.services.mozilla.com/D46658

/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

package org.mozilla.geckoview.test

import android.support.test.filters.MediumTest
import android.support.test.runner.AndroidJUnit4
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.GeckoSession
import org.mozilla.geckoview.test.rule.GeckoSessionTestRule
import org.mozilla.geckoview.test.util.Callbacks

@RunWith(AndroidJUnit4::class)
@MediumTest
class ContentBlockingControllerTest : BaseSessionTest() {
    @GeckoSessionTestRule.Setting(key = GeckoSessionTestRule.Setting.Key.USE_TRACKING_PROTECTION, value = "true")
    @Test
    fun trackingProtectionException() {
        val category = ContentBlocking.AntiTracking.TEST
        sessionRule.runtime.settings.contentBlocking.setAntiTracking(category)
        sessionRule.session.loadTestPath(TRACKERS_PATH)

        sessionRule.waitUntilCalled(
                object : Callbacks.ContentBlockingDelegate {
                    @GeckoSessionTestRule.AssertCalled(count=3)
                    override fun onContentBlocked(session: GeckoSession,
                                                  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"))
                    }
                })

        sessionRule.runtime.contentBlockingController.addException(sessionRule.session)

        sessionRule.runtime.contentBlockingController.checkException(sessionRule.session).accept {
            assertThat("Site should be on exceptions list", it, Matchers.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.uris.size, Matchers.equalTo(1))

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

        sessionRule.forCallbacksDuringWait(
                object : Callbacks.ContentBlockingDelegate {
                    @GeckoSessionTestRule.AssertCalled(false)
                    override fun onContentBlocked(session: GeckoSession,
                                                  event: ContentBlocking.BlockEvent) {
                    }
                })

        sessionRule.runtime.contentBlockingController.removeException(sessionRule.session)

        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.uris.size, Matchers.equalTo(0))

        sessionRule.session.reload()

        sessionRule.waitUntilCalled(
                object : Callbacks.ContentBlockingDelegate {
                    @GeckoSessionTestRule.AssertCalled(count=3)
                    override fun onContentBlocked(session: GeckoSession,
                                                  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"))
                    }
                })
    }

    @Test
    fun importExportExceptions() {
        val category = ContentBlocking.AntiTracking.TEST
        sessionRule.runtime.settings.contentBlocking.setAntiTracking(category)
        sessionRule.session.loadTestPath(TRACKERS_PATH)

        sessionRule.waitForPageStop()

        sessionRule.runtime.contentBlockingController.addException(sessionRule.session)

        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.uris.size, Matchers.equalTo(1))

        val exportStr = export.toString()
        assertThat("Exported string must not be null", exportStr, Matchers.notNullValue())

        val exportJson = export.toJson()
        assertThat("Exported JSON must not be null", exportJson, Matchers.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.uris.size, Matchers.equalTo(0))

        // Restore from String
        val importStr = sessionRule.runtime.contentBlockingController.ExceptionList(exportStr)
        sessionRule.runtime.contentBlockingController.restoreExceptionList(importStr)

        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.uris.size, Matchers.equalTo(1))

        // 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.uris.size, Matchers.equalTo(0))

        // Restore from JSON
        val importJson = sessionRule.runtime.contentBlockingController.ExceptionList(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.uris.size, Matchers.equalTo(1))

        // Wipe so as not to break other tests.
        sessionRule.runtime.contentBlockingController.clearExceptionList()
    }

    @Test
    fun getLog() {
        val category = ContentBlocking.AntiTracking.TEST
        sessionRule.runtime.settings.contentBlocking.setAntiTracking(category)
        sessionRule.session.settings.useTrackingProtection = true
        sessionRule.session.loadTestPath(TRACKERS_PATH)

        sessionRule.waitForPageStop()

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