1717package com.duckduckgo.app.trackerdetection
1818
1919import com.duckduckgo.app.privacymonitor.store.PrivacySettingsStore
20+ import com.duckduckgo.app.trackerdetection.Client.ClientName.EASYLIST
21+ import com.duckduckgo.app.trackerdetection.Client.ClientName.EASYPRIVACY
2022import com.duckduckgo.app.trackerdetection.model.*
2123import com.nhaarman.mockito_kotlin.any
2224import com.nhaarman.mockito_kotlin.mock
2325import com.nhaarman.mockito_kotlin.whenever
24- import org.junit.Assert.assertEquals
25- import org.junit.Assert.assertNull
26+ import org.junit.Assert.*
2627import org.junit.Test
2728import org.mockito.ArgumentMatchers.anyString
2829
@@ -38,23 +39,52 @@ class TrackerDetectorTest {
3839 private val network = " Network"
3940 }
4041
42+ @Test
43+ fun whenThereAreNoClientsThenClientCountIsZero () {
44+ assertEquals(0 , trackerDetector.clientCount)
45+ }
46+
47+ @Test
48+ fun whenClientAddedThenClientCountIsOne () {
49+ trackerDetector.addClient(alwaysMatchingClient(EASYLIST ))
50+ assertEquals(1 , trackerDetector.clientCount)
51+ }
52+
53+ @Test
54+ fun whenTwoClientsWithDifferentNamesAddedThenCountIsTwo () {
55+ trackerDetector.addClient(alwaysMatchingClient(EASYLIST ))
56+ trackerDetector.addClient(alwaysMatchingClient(EASYPRIVACY ))
57+ assertEquals(2 , trackerDetector.clientCount)
58+ }
59+
60+ @Test
61+ fun whenTwoClientsWithSameNameAddedThenClientIsReplacedAndCountIsStillOne () {
62+ trackerDetector.addClient(alwaysMatchingClient(EASYLIST ))
63+ assertEquals(1 , trackerDetector.clientCount)
64+ assertNotNull(trackerDetector.evaluate(" http://thirdparty.com/update.js" , " http://example.com/index.com" , resourceType))
65+
66+ trackerDetector.addClient(neverMatchingClient(EASYLIST ))
67+ assertEquals(1 , trackerDetector.clientCount)
68+ assertNull(trackerDetector.evaluate(" http://thirdparty.com/update.js" , " http://example.com/index.com" , resourceType))
69+ }
70+
4171 @Test
4272 fun whenThereAreNoClientsThenEvaluateReturnsNull () {
4373 assertNull(trackerDetector.evaluate(" http://thirdparty.com/update.js" , " http://example.com/index.com" , resourceType))
4474 }
4575
4676 @Test
4777 fun whenAllClientsFailToMatchThenEvaluateReturnsNull () {
48- trackerDetector.addClient(neverMatchingClient())
49- trackerDetector.addClient(neverMatchingClient())
78+ trackerDetector.addClient(neverMatchingClient(EASYLIST ))
79+ trackerDetector.addClient(neverMatchingClient(EASYPRIVACY ))
5080 assertNull(trackerDetector.evaluate(" http://thirdparty.com/update.js" , " http://example.com/index.com" , resourceType))
5181 }
5282
5383 @Test
5484 fun whenPrivacyOnAndAllClientsMatchThenEvaluateReturnsBlockedTrackingEvent () {
5585 whenever(settingStore.privacyOn).thenReturn(true )
56- trackerDetector.addClient(alwaysMatchingClient())
57- trackerDetector.addClient(alwaysMatchingClient())
86+ trackerDetector.addClient(alwaysMatchingClient(EASYLIST ))
87+ trackerDetector.addClient(alwaysMatchingClient(EASYPRIVACY ))
5888 val expected = TrackingEvent (" http://example.com/index.com" , " http://thirdparty.com/update.js" , null , true )
5989 val actual = trackerDetector.evaluate(" http://thirdparty.com/update.js" , " http://example.com/index.com" , resourceType)
6090 assertEquals(expected, actual)
@@ -63,8 +93,8 @@ class TrackerDetectorTest {
6393 @Test
6494 fun whenPrivacyOffAndAllClientsMatchThenEvaluateReturnsUnblockedTrackingEvent () {
6595 whenever(settingStore.privacyOn).thenReturn(false )
66- trackerDetector.addClient(alwaysMatchingClient())
67- trackerDetector.addClient(alwaysMatchingClient())
96+ trackerDetector.addClient(alwaysMatchingClient(EASYLIST ))
97+ trackerDetector.addClient(alwaysMatchingClient(EASYPRIVACY ))
6898 val expected = TrackingEvent (" http://example.com/index.com" , " http://thirdparty.com/update.js" , null , false )
6999 val actual = trackerDetector.evaluate(" http://thirdparty.com/update.js" , " http://example.com/index.com" , resourceType)
70100 assertEquals(expected, actual)
@@ -73,8 +103,8 @@ class TrackerDetectorTest {
73103 @Test
74104 fun whenPrivacyOnAndSomeClientsMatchThenEvaluateReturnsBlockedTrackingEvent () {
75105 whenever(settingStore.privacyOn).thenReturn(true )
76- trackerDetector.addClient(neverMatchingClient())
77- trackerDetector.addClient(alwaysMatchingClient())
106+ trackerDetector.addClient(neverMatchingClient(EASYLIST ))
107+ trackerDetector.addClient(alwaysMatchingClient(EASYPRIVACY ))
78108 val expected = TrackingEvent (" http://example.com/index.com" , " http://thirdparty.com/update.js" , null , true )
79109 val actual = trackerDetector.evaluate(" http://thirdparty.com/update.js" , " http://example.com/index.com" , resourceType)
80110 assertEquals(expected, actual)
@@ -83,8 +113,8 @@ class TrackerDetectorTest {
83113 @Test
84114 fun whenPrivacyOffAndSomeClientsMatchThenEvaluateReturnsUnblockedTrackingEvent () {
85115 whenever(settingStore.privacyOn).thenReturn(false )
86- trackerDetector.addClient(neverMatchingClient())
87- trackerDetector.addClient(alwaysMatchingClient())
116+ trackerDetector.addClient(neverMatchingClient(EASYLIST ))
117+ trackerDetector.addClient(alwaysMatchingClient(EASYPRIVACY ))
88118 val expected = TrackingEvent (" http://example.com/index.com" , " http://thirdparty.com/update.js" , null , false )
89119 val actual = trackerDetector.evaluate(" http://thirdparty.com/update.js" , " http://example.com/index.com" , resourceType)
90120 assertEquals(expected, actual)
@@ -97,7 +127,7 @@ class TrackerDetectorTest {
97127 )
98128 whenever(settingStore.privacyOn).thenReturn(true )
99129 networkTrackers.updateData(networks)
100- trackerDetector.addClient(alwaysMatchingClient())
130+ trackerDetector.addClient(alwaysMatchingClient(EASYLIST ))
101131
102132 val network = TrackerNetwork (network, " http://network.com" , category = " category" )
103133 val expected = TrackingEvent (" http://example.com/index.com" , " http://thirdparty.com/update.js" , network, true )
@@ -107,19 +137,19 @@ class TrackerDetectorTest {
107137
108138 @Test
109139 fun whenUrlHasSameDomainAsDocumentThenEvaluateReturnsNull () {
110- trackerDetector.addClient(alwaysMatchingClient())
140+ trackerDetector.addClient(alwaysMatchingClient(EASYLIST ))
111141 assertNull(trackerDetector.evaluate(" http://example.com/update.js" , " http://example.com/index.com" , resourceType))
112142 }
113143
114144 @Test
115145 fun whenUrlIsSubdomainOfDocumentThenEvaluateReturnsNull () {
116- trackerDetector.addClient(alwaysMatchingClient())
146+ trackerDetector.addClient(alwaysMatchingClient(EASYLIST ))
117147 assertNull(trackerDetector.evaluate(" http://mobile.example.com/update.js" , " http://example.com/index.com" , resourceType))
118148 }
119149
120150 @Test
121151 fun whenUrlIsParentOfDocumentThenEvaluateReturnsNull () {
122- trackerDetector.addClient(alwaysMatchingClient())
152+ trackerDetector.addClient(alwaysMatchingClient(EASYLIST ))
123153 assertNull(trackerDetector.evaluate(" http://example.com/update.js" , " http://mobile.example.com/index.com" , resourceType))
124154 }
125155
@@ -147,16 +177,17 @@ class TrackerDetectorTest {
147177 assertNull(trackerDetector.evaluate(" http://thirdparty.com/update.js" , " http://example.com/index.com" , resourceType))
148178 }
149179
150- private fun alwaysMatchingClient (): Client {
180+ private fun alwaysMatchingClient (name : Client . ClientName ): Client {
151181 val client: Client = mock()
182+ whenever(client.name).thenReturn(name)
152183 whenever(client.matches(anyString(), anyString(), any())).thenReturn(true )
153184 return client
154185 }
155186
156- private fun neverMatchingClient (): Client {
187+ private fun neverMatchingClient (name : Client . ClientName ): Client {
157188 val client: Client = mock()
189+ whenever(client.name).thenReturn(name)
158190 whenever(client.matches(anyString(), anyString(), any())).thenReturn(false )
159191 return client
160192 }
161-
162193}
0 commit comments