1+ /*
2+ * Copyright (c) 2017 DuckDuckGo
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package com.duckduckgo.app.privacymonitor
18+
19+ import com.duckduckgo.app.privacymonitor.HttpsStatus.*
20+ import com.duckduckgo.app.privacymonitor.model.TermsOfService
21+ import com.duckduckgo.app.privacymonitor.ui.improvedScore
22+ import com.duckduckgo.app.privacymonitor.ui.potentialScore
23+ import com.duckduckgo.app.privacymonitor.ui.score
24+ import com.duckduckgo.app.trackerdetection.model.TrackerNetwork
25+ import com.nhaarman.mockito_kotlin.mock
26+ import com.nhaarman.mockito_kotlin.whenever
27+ import org.junit.Assert.assertEquals
28+ import org.junit.Test
29+
30+ class PrivacyMonitorGradeExtensionTest {
31+
32+ companion object {
33+ val defaultScore = 1
34+ }
35+
36+ @Test
37+ fun whenHttpsMixedThenScoreIsIncrementedByOne () {
38+ val privacyMonitor = monitor(https = MIXED )
39+ assertEquals(defaultScore + 1 , privacyMonitor.score)
40+ }
41+
42+ @Test
43+ fun whenHttpThenScoreIsIncrementedByOne () {
44+ val privacyMonitor = monitor(https = NONE )
45+ assertEquals(defaultScore + 1 , privacyMonitor.score)
46+ }
47+
48+ @Test
49+ fun whenHttpsThenScoreIsUnchanged () {
50+ val privacyMonitor = monitor(https = SECURE )
51+ assertEquals(defaultScore, privacyMonitor.score)
52+ }
53+
54+ @Test
55+ fun whenTermsClassificationIsAThenScoreIsDecrementedByOne () {
56+ val privacyMonitor = monitor(terms = TermsOfService (classification = " A" ))
57+ assertEquals(defaultScore - 1 , privacyMonitor.score)
58+ }
59+
60+ @Test
61+ fun whenTermsClassificationIsBThenScoreIsUnchanged () {
62+ val privacyMonitor = monitor(terms = TermsOfService (classification = " B" ))
63+ assertEquals(defaultScore, privacyMonitor.score)
64+ }
65+
66+ @Test
67+ fun whenTermsClassificationIsCThenScoreIsUnchanged () {
68+ val privacyMonitor = monitor(terms = TermsOfService (classification = " C" ))
69+ assertEquals(defaultScore, privacyMonitor.score)
70+ }
71+
72+ @Test
73+ fun whenTermsClassificationIsDThenScoreIsIncrementedByOne () {
74+ val privacyMonitor = monitor(terms = TermsOfService (classification = " D" ))
75+ assertEquals(defaultScore + 1 , privacyMonitor.score)
76+ }
77+
78+ @Test
79+ fun whenTermsClassificationIsEThenScoreIsIncrementedByTwo () {
80+ val privacyMonitor = monitor(terms = TermsOfService (classification = " E" ))
81+ assertEquals(defaultScore + 2 , privacyMonitor.score)
82+ }
83+
84+ @Test
85+ fun whenTermsScoreIsPositiveThenScoreIsIncrementedByOne () {
86+ val privacyMonitor = monitor(terms = TermsOfService (score = 5 ))
87+ assertEquals(defaultScore + 1 , privacyMonitor.score)
88+ }
89+
90+ @Test
91+ fun whenTermsScoreIsNegativeThenScoreIsDecrementedByOne () {
92+ val privacyMonitor = monitor(terms = TermsOfService (score = - 5 ))
93+ assertEquals(defaultScore - 1 , privacyMonitor.score)
94+ }
95+
96+ @Test
97+ fun whenTermsScoreIsZeroThenScoreIsUnchanged () {
98+ val privacyMonitor = monitor(terms = TermsOfService (score = 0 ))
99+ assertEquals(defaultScore, privacyMonitor.score)
100+ }
101+
102+ @Test
103+ fun whenZeroTrackersThenScoreIsDefault () {
104+ val privacyMonitor = monitor(trackerCount = 0 )
105+ assertEquals(defaultScore, privacyMonitor.score)
106+ }
107+
108+ @Test
109+ fun whenOneTrackerThenScoreIsIncrementedByOne () {
110+ val privacyMonitor = monitor(trackerCount = 1 )
111+ assertEquals(defaultScore + 1 , privacyMonitor.score)
112+ }
113+
114+ @Test
115+ fun whenElevenTrackersThenScoreIsIncrementedByTwo () {
116+ val privacyMonitor = monitor(trackerCount = 11 )
117+ assertEquals(defaultScore + 2 , privacyMonitor.score)
118+ }
119+
120+ @Test
121+ fun whenSiteIsMajorNetworkMemberThenScoreIsIncrementedByPercentage () {
122+ val privacyMonitor = monitor(memberNetwork = TrackerNetwork (" " , " " , 45 , true ))
123+ assertEquals(defaultScore + 5 , privacyMonitor.score)
124+ }
125+
126+ @Test
127+ fun whenHasMajorTrackerThenScoreIsIncrementedByOne () {
128+ val privacyMonitor = monitor(majorTrackerCount = 1 )
129+ assertEquals(defaultScore + 1 , privacyMonitor.score)
130+ }
131+
132+ @Test
133+ fun whenHasObscureTrackerThenScoreIsIncrementedByOne () {
134+ val privacyMonitor = monitor(hasObscureTracker = true )
135+ assertEquals(defaultScore + 1 , privacyMonitor.score)
136+ }
137+
138+ @Test
139+ fun whenPotentialScoreThenTrackerMetricsIgnored () {
140+ val privacyMonitor = monitor(
141+ TrackerNetwork (" " , " " , 5 , true ),
142+ TermsOfService (classification = " D" ),
143+ NONE ,
144+ 5 ,
145+ 2 ,
146+ true )
147+ assertEquals(defaultScore + 6 , privacyMonitor.score)
148+ assertEquals(defaultScore + 3 , privacyMonitor.potentialScore)
149+ }
150+
151+ @Test
152+ fun whenAllTrackersBlockedThenImporvedScoreIsEqualToPotentialScore () {
153+ val privacyMonitor = monitor(
154+ TrackerNetwork (" " , " " , 5 , true ),
155+ TermsOfService (classification = " D" ),
156+ NONE ,
157+ 5 ,
158+ 2 ,
159+ true ,
160+ allTrackerBlocked = true )
161+ assertEquals(privacyMonitor.potentialScore, privacyMonitor.improvedScore)
162+ }
163+
164+ @Test
165+ fun whenNotAllTrackersBlockedThenImprovedScoreIsEqualToScore () {
166+ val privacyMonitor = monitor(
167+ TrackerNetwork (" " , " " , 5 , true ),
168+ TermsOfService (classification = " D" ),
169+ NONE ,
170+ 5 ,
171+ 2 ,
172+ true ,
173+ allTrackerBlocked = false )
174+ assertEquals(privacyMonitor.score, privacyMonitor.improvedScore)
175+ }
176+
177+ private fun monitor (memberNetwork : TrackerNetwork ? = null,
178+ terms : TermsOfService = TermsOfService (),
179+ https : HttpsStatus = SECURE ,
180+ trackerCount : Int = 0,
181+ majorTrackerCount : Int = 0,
182+ hasObscureTracker : Boolean = false,
183+ allTrackerBlocked : Boolean = true): PrivacyMonitor {
184+ val monitor: PrivacyMonitor = mock()
185+ whenever(monitor.memberNetwork).thenReturn(memberNetwork)
186+ whenever(monitor.termsOfService).thenReturn(terms)
187+ whenever(monitor.trackerCount).thenReturn(trackerCount)
188+ whenever(monitor.majorNetworkCount).thenReturn(majorTrackerCount)
189+ whenever(monitor.hasObscureTracker).thenReturn(hasObscureTracker)
190+ whenever(monitor.https).thenReturn(https)
191+ whenever(monitor.allTrackersBlocked).thenReturn(allTrackerBlocked)
192+ return monitor
193+ }
194+ }
0 commit comments