Skip to content

Commit 06c25b2

Browse files
committed
Merge branch 'release/4.0.4'
2 parents 74c9732 + 5cca76d commit 06c25b2

File tree

11 files changed

+138
-48
lines changed

11 files changed

+138
-48
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ apply plugin: 'kotlin-kapt'
66
apply from: '../versioning.gradle'
77

88
ext {
9-
VERSION_NAME = "4.0.3"
9+
VERSION_NAME = "4.0.4"
1010
}
1111

1212
android {

app/src/androidTest/java/com/duckduckgo/app/browser/BrowserViewModelTest.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,11 @@ class BrowserViewModelTest {
305305
}
306306

307307
@Test
308-
fun whenTrackerDetectedThenPrivacyGradeIsUpdated() {
308+
fun whenEnoughTrackersDetectedThenPrivacyGradeIsUpdated() {
309309
testee.urlChanged("https://example.com")
310-
testee.trackerDetected(TrackingEvent("https://example.com", "", null, false))
310+
for (i in 1..10) {
311+
testee.trackerDetected(TrackingEvent("https://example.com", "", null, false))
312+
}
311313
assertEquals(PrivacyGrade.C, testee.privacyGrade.value)
312314
}
313315

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2018 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.model
18+
19+
import org.junit.Assert.assertEquals
20+
import org.junit.Test
21+
22+
class PrivacyGradeTest {
23+
24+
@Test
25+
fun whenScoreIs0ThenGradeIsA() {
26+
assertEquals(PrivacyGrade.A, PrivacyGrade.gradeForScore(0))
27+
}
28+
29+
@Test
30+
fun whenScoreIs1ThenGradeIsB() {
31+
assertEquals(PrivacyGrade.B, PrivacyGrade.gradeForScore(1))
32+
}
33+
34+
@Test
35+
fun whenScoreIs2ThenGradeIsC() {
36+
assertEquals(PrivacyGrade.C, PrivacyGrade.gradeForScore(2))
37+
}
38+
39+
@Test
40+
fun whenScoreIs3ThenGradeIsD() {
41+
assertEquals(PrivacyGrade.D, PrivacyGrade.gradeForScore(3))
42+
}
43+
44+
@Test
45+
fun whenScoreIsAbove3ThenGradeIsD() {
46+
assertEquals(PrivacyGrade.D, PrivacyGrade.gradeForScore(4))
47+
}
48+
}

app/src/androidTest/java/com/duckduckgo/app/privacymonitor/model/PrivacyMonitorGradeExtensionTest.kt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,16 @@ class PrivacyMonitorGradeExtensionTest {
8585
}
8686

8787
@Test
88-
fun whenTermsScoreIsNegativeThenScoreIsDecrementedByOne() {
88+
fun whenTermsScoreIsNegativeThenScoreGreaterThanOneIsDecrementedByOne() {
89+
// http adds +1 and tos score removes 1 so we expect default score
90+
val privacyMonitor = monitor(terms = TermsOfService(score = -5), https = NONE)
91+
assertEquals(defaultScore, privacyMonitor.score)
92+
}
93+
94+
@Test
95+
fun whenTermsScoreIsNegativeThenScoreOfOneIsUnchanged() {
8996
val privacyMonitor = monitor(terms = TermsOfService(score = -5))
90-
assertEquals(defaultScore - 1, privacyMonitor.score)
97+
assertEquals(defaultScore, privacyMonitor.score)
9198
}
9299

93100
@Test
@@ -103,14 +110,20 @@ class PrivacyMonitorGradeExtensionTest {
103110
}
104111

105112
@Test
106-
fun whenOneTrackerThenScoreIsIncrementedByOne() {
113+
fun whenOneTrackerThenScoreNotIncremented() {
107114
val privacyMonitor = monitor(trackerCount = 1)
115+
assertEquals(defaultScore, privacyMonitor.score)
116+
}
117+
118+
@Test
119+
fun whenTenTrackersThenScoreIsIncrementedByOne() {
120+
val privacyMonitor = monitor(trackerCount = 10)
108121
assertEquals(defaultScore + 1, privacyMonitor.score)
109122
}
110123

111124
@Test
112-
fun whenElevenTrackersThenScoreIsIncrementedByTwo() {
113-
val privacyMonitor = monitor(trackerCount = 11)
125+
fun whenTwentyTrackersThenScoreIsIncrementedByTwo() {
126+
val privacyMonitor = monitor(trackerCount = 20)
114127
assertEquals(defaultScore + 2, privacyMonitor.score)
115128
}
116129

@@ -138,7 +151,7 @@ class PrivacyMonitorGradeExtensionTest {
138151
TrackerNetwork("", "", "", 5, true),
139152
TermsOfService(classification = "D"),
140153
NONE,
141-
5,
154+
11,
142155
true,
143156
true)
144157
assertEquals(defaultScore + 6, privacyMonitor.score)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import com.duckduckgo.app.privacymonitor.model.HttpsStatus
2323
import org.junit.Assert.assertEquals
2424
import org.junit.Test
2525

26-
class HttpsStatusRendererExtenstionTest {
26+
class HttpsStatusRendererExtensionTest {
2727

2828
private val context: Context = InstrumentationRegistry.getTargetContext()
2929

app/src/androidTest/java/com/duckduckgo/app/privacymonitor/ui/PrivacyDashboardViewModelTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,27 @@ class PrivacyDashboardViewModelTest {
109109
whenever(settingStore.privacyOn)
110110
.thenReturn(true)
111111
.thenReturn(false)
112-
assertTrue(testee.shouldReloadPage)
112+
assertTrue(testee.viewState.value!!.shouldReloadPage)
113113
}
114114

115115
@Test
116116
fun whenPrivacyInitiallyOnAndUnchangedThenShouldReloadIsFalse() {
117117
whenever(settingStore.privacyOn).thenReturn(true)
118-
assertFalse(testee.shouldReloadPage)
118+
assertFalse(testee.viewState.value!!.shouldReloadPage)
119119
}
120120

121121
@Test
122122
fun whenPrivacyInitiallyOffAndSwitchedOnThenShouldReloadIsTrue() {
123123
whenever(settingStore.privacyOn)
124124
.thenReturn(false)
125125
.thenReturn(true)
126-
assertTrue(testee.shouldReloadPage)
126+
assertTrue(testee.viewState.value!!.shouldReloadPage)
127127
}
128128

129129
@Test
130130
fun whenPrivacyInitiallyOffAndUnchangedThenShouldReloadIsFalse() {
131131
whenever(settingStore.privacyOn).thenReturn(false)
132-
assertFalse(testee.shouldReloadPage)
132+
assertFalse(testee.viewState.value!!.shouldReloadPage)
133133
}
134134

135135
@Test

app/src/main/java/com/duckduckgo/app/privacymonitor/model/PrivacyGrade.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,18 @@ enum class PrivacyGrade {
2222
B,
2323
C,
2424
D,
25-
UNKNOWN
25+
UNKNOWN;
26+
27+
companion object {
28+
29+
fun gradeForScore(score: Int): PrivacyGrade {
30+
return when (score) {
31+
0 -> PrivacyGrade.A
32+
1 -> PrivacyGrade.B
33+
2 -> PrivacyGrade.C
34+
else -> PrivacyGrade.D
35+
}
36+
}
37+
38+
}
2639
}

app/src/main/java/com/duckduckgo/app/privacymonitor/model/PrivacyMonitorGradeExtension.kt

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,42 @@
1717
package com.duckduckgo.app.privacymonitor.model
1818

1919
import com.duckduckgo.app.privacymonitor.PrivacyMonitor
20+
import timber.log.Timber
2021

2122

2223
val PrivacyMonitor.score: Int
2324
get() {
2425
var score = baseScore
25-
score += Math.ceil(trackerCount / 10.0).toInt()
26+
score += trackerCount / 10
2627
if (hasTrackerFromMajorNetwork) {
2728
score += 1
2829
}
2930
if (hasObscureTracker) {
3031
score += 1
3132
}
33+
if (score == 0 && termsOfService.classification != "A") {
34+
score = 1
35+
}
36+
Timber.v("""Calculating score {
37+
memberMajorNetworkPercentage: ${memberNetwork?.percentageOfPages}
38+
https: ${https}
39+
termsScore: ${termsOfService.gradingScore}
40+
trackerCount: $trackerCount
41+
hasTrackerFromMajorNetwork: $hasTrackerFromMajorNetwork
42+
hasObscureTracker: $hasObscureTracker
43+
score: $score
44+
""")
3245
return score
3346
}
3447

3548
val PrivacyMonitor.potentialScore: Int
36-
get() = baseScore
49+
get() {
50+
var score = baseScore
51+
if (score == 0 && termsOfService.classification != "A") {
52+
score = 1
53+
}
54+
return score
55+
}
3756

3857

3958
private val PrivacyMonitor.baseScore: Int
@@ -56,20 +75,10 @@ val PrivacyMonitor.improvedScore: Int
5675
}
5776

5877
val PrivacyMonitor.grade: PrivacyGrade
59-
get() = calculateGrade(score)
78+
get() = PrivacyGrade.gradeForScore(score)
6079

6180
val PrivacyMonitor.improvedGrade: PrivacyGrade
62-
get() = calculateGrade(improvedScore)
63-
64-
65-
private fun calculateGrade(score: Int): PrivacyGrade {
66-
return when {
67-
score <= 0 -> PrivacyGrade.A
68-
score == 1 -> PrivacyGrade.B
69-
score == 2 -> PrivacyGrade.C
70-
else -> PrivacyGrade.D
71-
}
72-
}
81+
get() = PrivacyGrade.gradeForScore(improvedScore)
7382

7483
val TermsOfService.gradingScore: Int
7584
get() {

app/src/main/java/com/duckduckgo/app/privacymonitor/ui/PrivacyDashboardActivity.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.duckduckgo.app.privacymonitor.ui
1818

19+
import android.app.Activity
1920
import android.arch.lifecycle.Observer
2021
import android.arch.lifecycle.ViewModelProviders
2122
import android.content.Context
@@ -43,6 +44,10 @@ class PrivacyDashboardActivity : DuckDuckGoActivity() {
4344
private val trackersRenderer = TrackersRenderer()
4445
private val upgradeRenderer = PrivacyUpgradeRenderer()
4546

47+
private val viewModel: PrivacyDashboardViewModel by lazy {
48+
ViewModelProviders.of(this, viewModelFactory).get(PrivacyDashboardViewModel::class.java)
49+
}
50+
4651
override fun onCreate(savedInstanceState: Bundle?) {
4752
super.onCreate(savedInstanceState)
4853
setContentView(R.layout.activity_privacy_dashboard)
@@ -65,22 +70,11 @@ class PrivacyDashboardActivity : DuckDuckGoActivity() {
6570
}
6671
}
6772

68-
private val viewModel: PrivacyDashboardViewModel by lazy {
69-
ViewModelProviders.of(this, viewModelFactory).get(PrivacyDashboardViewModel::class.java)
70-
}
71-
7273
private fun configureToolbar() {
7374
setSupportActionBar(toolbar)
7475
supportActionBar?.setDisplayHomeAsUpEnabled(true)
7576
}
7677

77-
override fun onBackPressed() {
78-
if (viewModel.shouldReloadPage) {
79-
setResult(RELOAD_RESULT_CODE)
80-
}
81-
super.onBackPressed()
82-
}
83-
8478
private fun render(viewState: ViewState) {
8579
if (isFinishing) {
8680
return
@@ -103,6 +97,7 @@ class PrivacyDashboardActivity : DuckDuckGoActivity() {
10397
networkTrackerSummaryPill1.render(viewState.networkTrackerSummaryName1, viewState.networkTrackerSummaryPercent1)
10498
networkTrackerSummaryPill2.render(viewState.networkTrackerSummaryName2, viewState.networkTrackerSummaryPercent2)
10599
networkTrackerSummaryPill3.render(viewState.networkTrackerSummaryName3, viewState.networkTrackerSummaryPercent3)
100+
updateActivityResult(viewState.shouldReloadPage)
106101
}
107102

108103
private fun renderToggle(enabled: Boolean) {
@@ -131,6 +126,14 @@ class PrivacyDashboardActivity : DuckDuckGoActivity() {
131126
}
132127
}
133128

129+
private fun updateActivityResult(shouldReload: Boolean) {
130+
if (shouldReload) {
131+
setResult(RELOAD_RESULT_CODE)
132+
} else {
133+
setResult(Activity.RESULT_OK)
134+
}
135+
}
136+
134137
companion object {
135138

136139
private const val REQUEST_CODE_PRIVACY_PRACTICES = 100

0 commit comments

Comments
 (0)