package com.cottondb.sitesimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertTrueimport org.junit.Testimport java.io.File/** * ANDROID READY 2 A: Android blocks trackers as the CONTRACT's Trackers row says (`sites.trackers` as a page opens, * `sites.block` for each request from shouldInterceptRequest, an empty 204 for a tracker), and says so to the core * (`blocks_trackers`), so You's Trackers row says Blocked only where it is true. The three test pages * (resources/trackers) are the ones the emulator walk measured. */classSitesTrackersTest {privateval repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile }privatefun src(path: String) = File(repo, "apps/android/app/src/$path").readText()privateval list by lazy {File(repo, "crates/cotton-core/src/sites_trackers.txt").readLines().map(String::trim).filter { it.isNotEmpty() && !it.startsWith("#") } }privateval asked = mutableListOf<Pair<String, String>>()privateval core: (String, String) -> Boolean = { top, url -> asked += top to url; url.contains("doubleclick.net") } @Testfun only_a_blocking_page_asks_and_never_about_itself() {val top = "https://news.example/a" assertTrue(Trackers.stops(true, false, top, "https://ad.doubleclick.net/x", core)) assertFalse(Trackers.stops(true, false, top, "https://news.example/app.js", core)) assertEquals(2, asked.size) assertFalse("switched off: never asked", Trackers.stops(false, false, top, "https://ad.doubleclick.net/x", core)) assertFalse("the page itself is never stopped", Trackers.stops(true, true, top, "https://ad.doubleclick.net/x", core)) assertFalse("nothing off the web", Trackers.stops(true, false, top, "data:image/png;base64,AAAA", core)) assertFalse("no page address yet: nothing to be third-party to", Trackers.stops(true, false, null, "https://ad.doubleclick.net/x", core)) assertEquals(2, asked.size) } @Testfun each_request_goes_to_the_core_and_a_tracker_is_answered_empty() {val pages = src("sites/kotlin/com/cottondb/sites/Pages.kt")val client = pages.substring(pages.indexOf("private class Client("), pages.indexOf("override fun shouldOverrideUrlLoading")) assertTrue(client.contains("override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse?")) assertTrue(client.contains("Trackers.stops(page.blocking, false, page.top, url)")) assertTrue(client.contains("WebResourceResponse(\"text/plain\", \"utf-8\", 204, \"No Content\""))val state = src("sites/kotlin/com/cottondb/sites/SitesState.kt") assertTrue("sites.block on the web view's own thread, not the core's queue", state.contains("shell.core.now(\"sites.block\""))// sites.trackers before anything loads, for a site and for a throwaway place.valopen = state.substring(state.indexOf("fun open(row: SiteRow"), state.indexOf("private suspend fun trim(")) assertTrue(open.indexOf("blockIn(p, row.id, row.open)") in0 until open.indexOf("p.view.loadUrl"))val throwaway = state.substring(state.indexOf("private fun throwaway("), state.indexOf("fun toList()")) assertTrue(throwaway.indexOf("blockIn(p, null, url)") in0 until throwaway.indexOf("p.view.loadUrl")) assertTrue(state.contains("ask(\"sites.trackers\")"))// The site's switch moved while its page is live takes effect at once. assertTrue(src("sites/kotlin/com/cottondb/sites/Edit.kt").contains("edit({ state.blocking(id, on) }) { put(\"trackers\", on) }")) } @Testfun this_phone_tells_the_core_it_blocks() {val state = src("sites/kotlin/com/cottondb/sites/SitesState.kt") assertTrue(state.contains("if (method == \"sites.settings\" || method == \"sites.set\") put(\"blocks_trackers\", true)")) }/** The measured pages: real-world shapes, each asking hosts on the core's list, the site's own files, and hosts the list leaves alone. */ @Testfun the_three_pages_ask_listed_trackers_and_their_own_files() {val dir = File(repo, "apps/android/app/src/testSites/resources/trackers")val expected = mapOf("news.html" to 13, "shop.html" to 10, "blog.html" to 5)for ((name, trackers) in expected) {val html = File(dir, name).readText()val hosts = Regex("""(?:src|href)="https://([^/"]+)""").findAll(html).map { it.groupValues[1] }.toList()val listed = hosts.filter { h -> list.any { t -> h == t || h.endsWith(".$t") } } assertEquals("$name: requests to hosts on the list", trackers, listed.size) assertTrue("$name: hosts the list leaves alone", hosts.size > listed.size)val own = Regex("""(?:src|href)="(/[^"]+)"""").findAll(html).map { it.groupValues[1] }.toList() assertTrue("$name: the site's own files", own.isNotEmpty() && own.all { File(dir, it.removePrefix("/")).isFile }) } }}