Skip to content

Commit bb6ee33

Browse files
committed
Add RC flag to guard new API
1 parent 9ec7474 commit bb6ee33

File tree

5 files changed

+447
-13
lines changed

5 files changed

+447
-13
lines changed

content-scope-scripts/content-scope-scripts-impl/src/main/java/com/duckduckgo/contentscopescripts/impl/ContentScopeScriptsFeature.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ import com.duckduckgo.feature.toggles.api.Toggle.DefaultFeatureValue
2929
interface ContentScopeScriptsFeature {
3030
@Toggle.DefaultValue(DefaultFeatureValue.TRUE)
3131
fun self(): Toggle
32+
33+
@Toggle.DefaultValue(DefaultFeatureValue.TRUE)
34+
fun useNewWebCompatApis(): Toggle
3235
}

content-scope-scripts/content-scope-scripts-impl/src/main/java/com/duckduckgo/contentscopescripts/impl/ContentScopeScriptsJsInjectorPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ContentScopeScriptsJsInjectorPlugin @Inject constructor(
5959
it.remove()
6060
script = null
6161
}
62-
if (coreContentScopeScripts.isEnabled()) {
62+
if (adsJsContentScopeScripts.isEnabled()) {
6363
currentScriptString = scriptString
6464
script = webViewCompatWrapper.addDocumentStartJavaScript(webView, scriptString, setOf("*"))
6565
}

content-scope-scripts/content-scope-scripts-impl/src/main/java/com/duckduckgo/contentscopescripts/impl/RealAdsJsContentScopeScripts.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ interface AdsJsContentScopeScripts {
4141
activeExperiments: List<Toggle>,
4242
): String
4343

44-
fun isEnabled(): Boolean
44+
suspend fun isEnabled(): Boolean
4545

4646
val secret: String
4747
val javascriptInterface: String
@@ -111,8 +111,8 @@ class RealAdsJsContentScopeScripts @Inject constructor(
111111
return cachedAdsJS
112112
}
113113

114-
override fun isEnabled(): Boolean {
115-
return contentScopeScriptsFeature.self().isEnabled()
114+
override suspend fun isEnabled(): Boolean {
115+
return contentScopeScriptsFeature.self().isEnabled() && contentScopeScriptsFeature.useNewWebCompatApis().isEnabled()
116116
}
117117

118118
private fun getSecretKeyValuePair() = "\"messageSecret\":\"$secret\""

content-scope-scripts/content-scope-scripts-impl/src/test/java/com/duckduckgo/contentscopescripts/impl/ContentScopeScriptsJsInjectorPluginTest.kt

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ContentScopeScriptsJsInjectorPluginTest {
2727
var coroutineRule = CoroutineTestRule()
2828

2929
private val mockCoreContentScopeScripts: CoreContentScopeScripts = mock()
30-
private val adsJsContentScopeScripts: AdsJsContentScopeScripts = mock()
30+
private val mockAdsJsContentScopeScripts: AdsJsContentScopeScripts = mock()
3131
private val mockWebView: WebView = mock()
3232
private val mockContentScopeExperiments: ContentScopeExperiments = mock()
3333
private val mockWebViewCompatWrapper: WebViewCompatWrapper = mock()
@@ -41,7 +41,7 @@ class ContentScopeScriptsJsInjectorPluginTest {
4141
whenever(mockContentScopeExperiments.getActiveExperiments()).thenReturn(listOf(mockToggle))
4242
contentScopeScriptsJsInjectorPlugin = ContentScopeScriptsJsInjectorPlugin(
4343
mockCoreContentScopeScripts,
44-
adsJsContentScopeScripts,
44+
mockAdsJsContentScopeScripts,
4545
mockContentScopeExperiments,
4646
coroutineRule.testDispatcherProvider,
4747
mockWebViewCompatWrapper,
@@ -51,6 +51,7 @@ class ContentScopeScriptsJsInjectorPluginTest {
5151
@Test
5252
fun whenEnabledAndInjectContentScopeScriptsThenPopulateMessagingParameters() = runTest {
5353
whenever(mockCoreContentScopeScripts.isEnabled()).thenReturn(true)
54+
whenever(mockAdsJsContentScopeScripts.isEnabled()).thenReturn(true)
5455
whenever(mockCoreContentScopeScripts.getScript(null, listOf())).thenReturn("")
5556
contentScopeScriptsJsInjectorPlugin.onPageStarted(mockWebView, null, null)
5657

@@ -69,6 +70,7 @@ class ContentScopeScriptsJsInjectorPluginTest {
6970
@Test
7071
fun whenEnabledAndInjectContentScopeScriptsThenUseParams() = runTest {
7172
whenever(mockCoreContentScopeScripts.isEnabled()).thenReturn(true)
73+
whenever(mockAdsJsContentScopeScripts.isEnabled()).thenReturn(true)
7274
whenever(mockCoreContentScopeScripts.getScript(true, listOf())).thenReturn("")
7375
contentScopeScriptsJsInjectorPlugin.onPageStarted(mockWebView, null, true)
7476

@@ -98,6 +100,7 @@ class ContentScopeScriptsJsInjectorPluginTest {
98100
fun whenEnabledAndPageStartedWithInitJsAndActiveExperimentsChangedAfterwardsThenReturnActiveExperimentsFromInit() = runTest {
99101
val mockToggle2 = mock<Toggle>()
100102
whenever(mockCoreContentScopeScripts.isEnabled()).thenReturn(true)
103+
whenever(mockAdsJsContentScopeScripts.isEnabled()).thenReturn(true)
101104

102105
contentScopeScriptsJsInjectorPlugin.onInit(mockWebView)
103106

@@ -112,6 +115,7 @@ class ContentScopeScriptsJsInjectorPluginTest {
112115
fun whenInitJsActiveExperimentsUpdated() = runTest {
113116
val mockToggle2 = mock<Toggle>()
114117
whenever(mockCoreContentScopeScripts.isEnabled()).thenReturn(true)
118+
whenever(mockAdsJsContentScopeScripts.isEnabled()).thenReturn(true)
115119
contentScopeScriptsJsInjectorPlugin.onInit(mockWebView)
116120

117121
val result = contentScopeScriptsJsInjectorPlugin.onPageStarted(mockWebView, null, null)
@@ -130,6 +134,7 @@ class ContentScopeScriptsJsInjectorPluginTest {
130134
fun whenPageFinishedActiveExperimentsUpdated() = runTest {
131135
val mockToggle2 = mock<Toggle>()
132136
whenever(mockCoreContentScopeScripts.isEnabled()).thenReturn(true)
137+
whenever(mockAdsJsContentScopeScripts.isEnabled()).thenReturn(true)
133138
contentScopeScriptsJsInjectorPlugin.onInit(mockWebView)
134139

135140
val result = contentScopeScriptsJsInjectorPlugin.onPageStarted(mockWebView, null, null)
@@ -147,35 +152,59 @@ class ContentScopeScriptsJsInjectorPluginTest {
147152
@Test
148153
fun whenDocumentStartScriptSupportedAndInitCalledWithScriptChangedThenScriptInjected() = runTest {
149154
whenever(mockWebViewCompatWrapper.isDocumentStartScriptSupported()).thenReturn(true)
150-
whenever(mockCoreContentScopeScripts.isEnabled()).thenReturn(true)
151-
whenever(adsJsContentScopeScripts.getScript(any())).thenReturn("mockScript")
155+
whenever(mockAdsJsContentScopeScripts.isEnabled()).thenReturn(true)
156+
whenever(mockAdsJsContentScopeScripts.getScript(any())).thenReturn("mockScript")
152157

153158
contentScopeScriptsJsInjectorPlugin.onInit(mockWebView)
154159

155-
verify(adsJsContentScopeScripts).getScript(listOf(mockToggle))
160+
verify(mockAdsJsContentScopeScripts).getScript(listOf(mockToggle))
156161
verify(mockWebViewCompatWrapper).addDocumentStartJavaScript(any(), eq("mockScript"), any())
157162
}
158163

159164
@Test
160165
fun whenDocumentStartScriptNotSupportedAndInitCalledThenNoScriptInjected() = runTest {
161166
whenever(mockWebViewCompatWrapper.isDocumentStartScriptSupported()).thenReturn(false)
167+
whenever(mockAdsJsContentScopeScripts.isEnabled()).thenReturn(true)
168+
whenever(mockAdsJsContentScopeScripts.getScript(any())).thenReturn("mockScript")
169+
170+
contentScopeScriptsJsInjectorPlugin.onInit(mockWebView)
171+
172+
verifyNoInteractions(mockAdsJsContentScopeScripts)
173+
verify(mockWebViewCompatWrapper, never()).addDocumentStartJavaScript(any(), any(), any())
174+
}
175+
176+
@Test
177+
fun whenAdsjsIsNotEnabledAndInitCalledThenNoScriptInjected() = runTest {
178+
whenever(mockWebViewCompatWrapper.isDocumentStartScriptSupported()).thenReturn(true)
162179
whenever(mockCoreContentScopeScripts.isEnabled()).thenReturn(true)
163-
whenever(adsJsContentScopeScripts.getScript(any())).thenReturn("mockScript")
180+
whenever(mockAdsJsContentScopeScripts.isEnabled()).thenReturn(false)
181+
whenever(mockAdsJsContentScopeScripts.getScript(any())).thenReturn("mockScript")
164182

165183
contentScopeScriptsJsInjectorPlugin.onInit(mockWebView)
166184

167-
verifyNoInteractions(adsJsContentScopeScripts)
185+
verify(mockWebViewCompatWrapper, never()).addDocumentStartJavaScript(any(), any(), any())
186+
}
187+
188+
@Test
189+
fun whenAdsjsIsNotEnabledAndPageFinishedCalledThenNoScriptInjected() = runTest {
190+
whenever(mockWebViewCompatWrapper.isDocumentStartScriptSupported()).thenReturn(true)
191+
whenever(mockCoreContentScopeScripts.isEnabled()).thenReturn(true)
192+
whenever(mockAdsJsContentScopeScripts.isEnabled()).thenReturn(false)
193+
whenever(mockAdsJsContentScopeScripts.getScript(any())).thenReturn("mockScript")
194+
195+
contentScopeScriptsJsInjectorPlugin.onPageFinished(mockWebView, null)
196+
168197
verify(mockWebViewCompatWrapper, never()).addDocumentStartJavaScript(any(), any(), any())
169198
}
170199

171200
@Test
172201
fun whenDocumentStartScriptNotSupportedAndPageFinishedCalledThenNoScriptInjected() = runTest {
173202
whenever(mockWebViewCompatWrapper.isDocumentStartScriptSupported()).thenReturn(false)
174-
whenever(mockCoreContentScopeScripts.isEnabled()).thenReturn(true)
203+
whenever(mockAdsJsContentScopeScripts.isEnabled()).thenReturn(true)
175204

176205
contentScopeScriptsJsInjectorPlugin.onPageFinished(mockWebView, null)
177206

178-
verifyNoInteractions(adsJsContentScopeScripts)
207+
verifyNoInteractions(mockAdsJsContentScopeScripts)
179208
verify(mockWebViewCompatWrapper, never()).addDocumentStartJavaScript(any(), any(), any())
180209
}
181210
}

0 commit comments

Comments
 (0)