|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 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.duckchat.impl.ui |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import com.duckduckgo.common.test.CoroutineTestRule |
| 21 | +import com.duckduckgo.js.messaging.api.JsCallbackData |
| 22 | +import com.duckduckgo.js.messaging.api.JsMessaging |
| 23 | +import com.duckduckgo.navigation.api.GlobalActivityStarter |
| 24 | +import com.duckduckgo.subscriptions.api.SubscriptionScreens.RestoreSubscriptionScreenWithParams |
| 25 | +import com.duckduckgo.subscriptions.api.SubscriptionScreens.SubscriptionScreenNoParams |
| 26 | +import com.duckduckgo.subscriptions.api.SubscriptionScreens.SubscriptionsSettingsScreenWithEmptyParams |
| 27 | +import com.duckduckgo.subscriptions.api.SubscriptionsJSHelper |
| 28 | +import kotlinx.coroutines.test.runTest |
| 29 | +import org.json.JSONObject |
| 30 | +import org.junit.Before |
| 31 | +import org.junit.Rule |
| 32 | +import org.junit.Test |
| 33 | +import org.mockito.Mock |
| 34 | +import org.mockito.MockitoAnnotations |
| 35 | +import org.mockito.kotlin.any |
| 36 | +import org.mockito.kotlin.never |
| 37 | +import org.mockito.kotlin.verify |
| 38 | +import org.mockito.kotlin.whenever |
| 39 | + |
| 40 | +class SubscriptionsHandlerTest { |
| 41 | + |
| 42 | + @get:Rule |
| 43 | + var coroutineRule = CoroutineTestRule() |
| 44 | + |
| 45 | + private val dispatcherProvider = coroutineRule.testDispatcherProvider |
| 46 | + |
| 47 | + @Mock |
| 48 | + private lateinit var subscriptionsJSHelper: SubscriptionsJSHelper |
| 49 | + |
| 50 | + @Mock |
| 51 | + private lateinit var globalActivityStarter: GlobalActivityStarter |
| 52 | + |
| 53 | + @Mock |
| 54 | + private lateinit var context: Context |
| 55 | + |
| 56 | + @Mock |
| 57 | + private lateinit var contentScopeScripts: JsMessaging |
| 58 | + |
| 59 | + private lateinit var subscriptionsHandler: SubscriptionsHandler |
| 60 | + |
| 61 | + @Before |
| 62 | + fun setUp() { |
| 63 | + MockitoAnnotations.openMocks(this) |
| 64 | + |
| 65 | + subscriptionsHandler = SubscriptionsHandler( |
| 66 | + subscriptionsJSHelper, |
| 67 | + globalActivityStarter, |
| 68 | + dispatcherProvider, |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun `handleSubscriptionsFeature processes js callback and responds when response is not null`() = runTest { |
| 74 | + val featureName = "subscriptions" |
| 75 | + val method = "someMethod" |
| 76 | + val id = "testId" |
| 77 | + val data = JSONObject() |
| 78 | + val response = JsCallbackData(JSONObject(), featureName, method, id) |
| 79 | + whenever(subscriptionsJSHelper.processJsCallbackMessage(featureName, method, id, data)) |
| 80 | + .thenReturn(response) |
| 81 | + |
| 82 | + subscriptionsHandler.handleSubscriptionsFeature( |
| 83 | + featureName, |
| 84 | + method, |
| 85 | + id, |
| 86 | + data, |
| 87 | + context, |
| 88 | + coroutineRule.testScope, |
| 89 | + contentScopeScripts, |
| 90 | + ) |
| 91 | + |
| 92 | + verify(subscriptionsJSHelper).processJsCallbackMessage(featureName, method, id, data) |
| 93 | + verify(contentScopeScripts).onResponse(response) |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun `handleSubscriptionsFeature processes js callback but does not respond when response is null`() = runTest { |
| 98 | + val featureName = "subscriptions" |
| 99 | + val method = "someMethod" |
| 100 | + val id = "testId" |
| 101 | + val data = JSONObject() |
| 102 | + whenever(subscriptionsJSHelper.processJsCallbackMessage(featureName, method, id, data)) |
| 103 | + .thenReturn(null) |
| 104 | + |
| 105 | + subscriptionsHandler.handleSubscriptionsFeature( |
| 106 | + featureName, |
| 107 | + method, |
| 108 | + id, |
| 109 | + data, |
| 110 | + context, |
| 111 | + coroutineRule.testScope, |
| 112 | + contentScopeScripts, |
| 113 | + ) |
| 114 | + |
| 115 | + verify(subscriptionsJSHelper).processJsCallbackMessage(featureName, method, id, data) |
| 116 | + verify(contentScopeScripts, never()).onResponse(any()) |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + fun `handleSubscriptionsFeature launches settings screen when method is backToSettings`() = runTest { |
| 121 | + val featureName = "subscriptions" |
| 122 | + val method = "backToSettings" |
| 123 | + val id = "testId" |
| 124 | + val data = JSONObject() |
| 125 | + val response = JsCallbackData(JSONObject(), featureName, method, id) |
| 126 | + whenever(subscriptionsJSHelper.processJsCallbackMessage(featureName, method, id, data)) |
| 127 | + .thenReturn(response) |
| 128 | + |
| 129 | + subscriptionsHandler.handleSubscriptionsFeature( |
| 130 | + featureName, |
| 131 | + method, |
| 132 | + id, |
| 133 | + data, |
| 134 | + context, |
| 135 | + coroutineRule.testScope, |
| 136 | + contentScopeScripts, |
| 137 | + ) |
| 138 | + |
| 139 | + verify(globalActivityStarter).start(context, SubscriptionsSettingsScreenWithEmptyParams) |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + fun `handleSubscriptionsFeature launches subscription activation screen when method is openSubscriptionActivation`() = runTest { |
| 144 | + val featureName = "subscriptions" |
| 145 | + val method = "openSubscriptionActivation" |
| 146 | + val id = "testId" |
| 147 | + val data = JSONObject() |
| 148 | + val response = JsCallbackData(JSONObject(), featureName, method, id) |
| 149 | + whenever(subscriptionsJSHelper.processJsCallbackMessage(featureName, method, id, data)) |
| 150 | + .thenReturn(response) |
| 151 | + |
| 152 | + subscriptionsHandler.handleSubscriptionsFeature( |
| 153 | + featureName, |
| 154 | + method, |
| 155 | + id, |
| 156 | + data, |
| 157 | + context, |
| 158 | + coroutineRule.testScope, |
| 159 | + contentScopeScripts, |
| 160 | + ) |
| 161 | + |
| 162 | + verify(globalActivityStarter).start(context, RestoreSubscriptionScreenWithParams(isOriginWeb = true)) |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + fun `handleSubscriptionsFeature launches subscription purchase screen when method is openSubscriptionPurchase`() = runTest { |
| 167 | + val featureName = "subscriptions" |
| 168 | + val method = "openSubscriptionPurchase" |
| 169 | + val id = "testId" |
| 170 | + val data = JSONObject() |
| 171 | + val response = JsCallbackData(JSONObject(), featureName, method, id) |
| 172 | + whenever(subscriptionsJSHelper.processJsCallbackMessage(featureName, method, id, data)) |
| 173 | + .thenReturn(response) |
| 174 | + |
| 175 | + subscriptionsHandler.handleSubscriptionsFeature( |
| 176 | + featureName, |
| 177 | + method, |
| 178 | + id, |
| 179 | + data, |
| 180 | + context, |
| 181 | + coroutineRule.testScope, |
| 182 | + contentScopeScripts, |
| 183 | + ) |
| 184 | + |
| 185 | + verify(globalActivityStarter).start(context, SubscriptionScreenNoParams) |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + fun `handleSubscriptionsFeature handles null data parameter`() = runTest { |
| 190 | + val featureName = "subscriptions" |
| 191 | + val method = "backToSettings" |
| 192 | + val id = "testId" |
| 193 | + val data: JSONObject? = null |
| 194 | + val response = JsCallbackData(JSONObject(), featureName, method, id) |
| 195 | + whenever(subscriptionsJSHelper.processJsCallbackMessage(featureName, method, id, data)) |
| 196 | + .thenReturn(response) |
| 197 | + |
| 198 | + subscriptionsHandler.handleSubscriptionsFeature( |
| 199 | + featureName, |
| 200 | + method, |
| 201 | + id, |
| 202 | + data, |
| 203 | + context, |
| 204 | + coroutineRule.testScope, |
| 205 | + contentScopeScripts, |
| 206 | + ) |
| 207 | + |
| 208 | + verify(subscriptionsJSHelper).processJsCallbackMessage(featureName, method, id, data) |
| 209 | + verify(globalActivityStarter).start(context, SubscriptionsSettingsScreenWithEmptyParams) |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + fun `handleSubscriptionsFeature handles null id parameter`() = runTest { |
| 214 | + val featureName = "subscriptions" |
| 215 | + val method = "openSubscriptionPurchase" |
| 216 | + val id: String? = null |
| 217 | + val data = JSONObject() |
| 218 | + val response = JsCallbackData(JSONObject(), featureName, method, "") |
| 219 | + whenever(subscriptionsJSHelper.processJsCallbackMessage(featureName, method, id, data)) |
| 220 | + .thenReturn(response) |
| 221 | + |
| 222 | + subscriptionsHandler.handleSubscriptionsFeature( |
| 223 | + featureName, |
| 224 | + method, |
| 225 | + id, |
| 226 | + data, |
| 227 | + context, |
| 228 | + coroutineRule.testScope, |
| 229 | + contentScopeScripts, |
| 230 | + ) |
| 231 | + |
| 232 | + verify(subscriptionsJSHelper).processJsCallbackMessage(featureName, method, id, data) |
| 233 | + verify(globalActivityStarter).start(context, SubscriptionScreenNoParams) |
| 234 | + } |
| 235 | +} |
0 commit comments