diff --git a/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/RemoteMessagingConfigMatcher.kt b/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/RemoteMessagingConfigMatcher.kt index a0fc2fbbd8bb..0ffc0ee4579b 100644 --- a/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/RemoteMessagingConfigMatcher.kt +++ b/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/RemoteMessagingConfigMatcher.kt @@ -18,21 +18,21 @@ package com.duckduckgo.remote.messaging.impl import com.duckduckgo.remote.messaging.api.RemoteMessage import com.duckduckgo.remote.messaging.api.RemoteMessagingRepository -import com.duckduckgo.remote.messaging.impl.matchers.AndroidAppAttributeMatcher -import com.duckduckgo.remote.messaging.impl.matchers.DeviceAttributeMatcher -import com.duckduckgo.remote.messaging.impl.matchers.UserAttributeMatcher +import com.duckduckgo.remote.messaging.impl.di.DeviceAttrMatcher +import com.duckduckgo.remote.messaging.impl.di.UserAttrMatcher +import com.duckduckgo.remote.messaging.impl.matchers.AttributeMatcher +import com.duckduckgo.remote.messaging.impl.matchers.EvaluationResult import com.duckduckgo.remote.messaging.impl.matchers.toResult import com.duckduckgo.remote.messaging.impl.models.MatchingAttribute import com.duckduckgo.remote.messaging.impl.models.RemoteConfig -import com.duckduckgo.remote.messaging.impl.matchers.EvaluationResult import com.duckduckgo.remote.messaging.impl.models.MatchingAttribute.Unknown import timber.log.Timber class RemoteMessagingConfigMatcher( - val deviceAttributeMatcher: DeviceAttributeMatcher, - val androidAppAttributeMatcher: AndroidAppAttributeMatcher, + @DeviceAttrMatcher val deviceAttributeMatcher: AttributeMatcher, + @DeviceAttrMatcher val androidAppAttributeMatcher: AttributeMatcher, val remoteMessagingRepository: RemoteMessagingRepository, - val userAttributeMatcher: UserAttributeMatcher + @UserAttrMatcher val userAttributeMatcher: AttributeMatcher ) { private val matchers = listOf(deviceAttributeMatcher, androidAppAttributeMatcher, userAttributeMatcher) diff --git a/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/di/AndroidAppAttrMatcher.kt b/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/di/AndroidAppAttrMatcher.kt new file mode 100644 index 000000000000..e4461947ba24 --- /dev/null +++ b/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/di/AndroidAppAttrMatcher.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2020 DuckDuckGo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.duckduckgo.remote.messaging.impl.di + +import javax.inject.Qualifier + +/** Identifies a coroutine scope type that is scope to the app lifecycle */ +@Qualifier +@Retention(AnnotationRetention.RUNTIME) +annotation class AndroidAppAttrMatcher diff --git a/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/di/DeviceAttrMatcher.kt b/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/di/DeviceAttrMatcher.kt new file mode 100644 index 000000000000..b7879c6add34 --- /dev/null +++ b/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/di/DeviceAttrMatcher.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2020 DuckDuckGo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.duckduckgo.remote.messaging.impl.di + +import javax.inject.Qualifier + +/** Identifies a coroutine scope type that is scope to the app lifecycle */ +@Qualifier +@Retention(AnnotationRetention.RUNTIME) +annotation class DeviceAttrMatcher diff --git a/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/di/RemoteMessagingModule.kt b/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/di/RemoteMessagingModule.kt index 53a35d95b7fe..777672d71f80 100644 --- a/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/di/RemoteMessagingModule.kt +++ b/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/di/RemoteMessagingModule.kt @@ -30,6 +30,7 @@ import com.duckduckgo.remote.messaging.api.RemoteMessagingRepository import com.duckduckgo.remote.messaging.impl.* import com.duckduckgo.remote.messaging.impl.mappers.RemoteMessagingConfigJsonMapper import com.duckduckgo.remote.messaging.impl.matchers.AndroidAppAttributeMatcher +import com.duckduckgo.remote.messaging.impl.matchers.AttributeMatcher import com.duckduckgo.remote.messaging.impl.matchers.DeviceAttributeMatcher import com.duckduckgo.remote.messaging.impl.matchers.UserAttributeMatcher import com.duckduckgo.remote.messaging.impl.network.RemoteMessagingService @@ -129,37 +130,40 @@ class DataSourceModule { @Provides @SingleInstanceIn(AppScope::class) fun providesRemoteMessagingConfigMatcher( - deviceAttributeMatcher: DeviceAttributeMatcher, - androidAppAttributeMatcher: AndroidAppAttributeMatcher, + @DeviceAttrMatcher deviceAttributeMatcher: AttributeMatcher, + @AndroidAppAttrMatcher androidAppAttributeMatcher: AttributeMatcher, remoteMessagingRepository: RemoteMessagingRepository, - userAttributeMatcher: UserAttributeMatcher + @UserAttrMatcher userAttributeMatcher: AttributeMatcher ): RemoteMessagingConfigMatcher { return RemoteMessagingConfigMatcher(deviceAttributeMatcher, androidAppAttributeMatcher, remoteMessagingRepository, userAttributeMatcher) } @Provides @SingleInstanceIn(AppScope::class) + @AndroidAppAttrMatcher fun providesAndroidAppAttributeMatcher( appProperties: AppProperties, appBuildConfig: AppBuildConfig - ): AndroidAppAttributeMatcher { + ): AttributeMatcher { return AndroidAppAttributeMatcher(appProperties, appBuildConfig) } @Provides @SingleInstanceIn(AppScope::class) + @DeviceAttrMatcher fun providesDeviceAttributeMatcher( appBuildConfig: AppBuildConfig, appProperties: AppProperties - ): DeviceAttributeMatcher { + ): AttributeMatcher { return DeviceAttributeMatcher(appBuildConfig, appProperties) } @Provides @SingleInstanceIn(AppScope::class) + @UserAttrMatcher fun providesUserAttributeMatcher( userBrowserProperties: UserBrowserProperties - ): UserAttributeMatcher { + ): AttributeMatcher { return UserAttributeMatcher(userBrowserProperties) } diff --git a/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/di/UserAttrMatcher.kt b/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/di/UserAttrMatcher.kt new file mode 100644 index 000000000000..b1f0bf589492 --- /dev/null +++ b/remote-messaging/remote-messaging-impl/src/main/java/com/duckduckgo/remote/messaging/impl/di/UserAttrMatcher.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2020 DuckDuckGo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.duckduckgo.remote.messaging.impl.di + +import javax.inject.Qualifier + +/** Identifies a coroutine scope type that is scope to the app lifecycle */ +@Qualifier +@Retention(AnnotationRetention.RUNTIME) +annotation class UserAttrMatcher diff --git a/remote-messaging/remote-messaging-impl/src/test/java/com/duckduckgo/remote/messaging/impl/RealRemoteMessagingConfigProcessorTest.kt b/remote-messaging/remote-messaging-impl/src/test/java/com/duckduckgo/remote/messaging/impl/RealRemoteMessagingConfigProcessorTest.kt index cd4d83595468..fcd32a52483c 100644 --- a/remote-messaging/remote-messaging-impl/src/test/java/com/duckduckgo/remote/messaging/impl/RealRemoteMessagingConfigProcessorTest.kt +++ b/remote-messaging/remote-messaging-impl/src/test/java/com/duckduckgo/remote/messaging/impl/RealRemoteMessagingConfigProcessorTest.kt @@ -17,10 +17,12 @@ package com.duckduckgo.remote.messaging.impl import com.duckduckgo.app.CoroutineTestRule +import com.duckduckgo.appbuildconfig.api.AppBuildConfig import com.duckduckgo.remote.messaging.api.RemoteMessagingRepository import com.duckduckgo.remote.messaging.fixtures.JsonRemoteMessageOM.aJsonRemoteMessagingConfig import com.duckduckgo.remote.messaging.fixtures.RemoteMessagingConfigOM.aRemoteMessagingConfig import com.duckduckgo.remote.messaging.impl.mappers.RemoteMessagingConfigJsonMapper +import com.duckduckgo.remote.messaging.impl.matchers.AttributeMatcher import com.duckduckgo.remote.messaging.store.RemoteMessagingConfigRepository import com.nhaarman.mockitokotlin2.any import com.nhaarman.mockitokotlin2.mock @@ -29,25 +31,38 @@ import com.nhaarman.mockitokotlin2.verify import com.nhaarman.mockitokotlin2.whenever import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest +import org.junit.Before import org.junit.Rule import org.junit.Test import org.threeten.bp.LocalDateTime import org.threeten.bp.format.DateTimeFormatter +import java.util.* @ExperimentalCoroutinesApi class RealRemoteMessagingConfigProcessorTest { @get:Rule var coroutineRule = CoroutineTestRule() - private val remoteMessagingConfigJsonMapper = mock() + private val appBuildConfig: AppBuildConfig = mock() + private val remoteMessagingConfigJsonMapper = RemoteMessagingConfigJsonMapper(appBuildConfig) private val remoteMessagingConfigRepository = mock() private val remoteMessagingRepository = mock() - private val remoteMessagingConfigMatcher = mock() + private val remoteMessagingConfigMatcher = RemoteMessagingConfigMatcher( + mock(), + mock(), + mock(), + mock(), + ) private val testee = RealRemoteMessagingConfigProcessor( remoteMessagingConfigJsonMapper, remoteMessagingConfigRepository, remoteMessagingRepository, remoteMessagingConfigMatcher ) + @Before + fun setup() { + whenever(appBuildConfig.deviceLocale).thenReturn(Locale.US) + } + @Test fun whenNewVersionThenEvaluate() = runTest { whenever(remoteMessagingConfigRepository.get()).thenReturn( diff --git a/remote-messaging/remote-messaging-impl/src/test/java/com/duckduckgo/remote/messaging/impl/RemoteMessagingConfigMatcherTest.kt b/remote-messaging/remote-messaging-impl/src/test/java/com/duckduckgo/remote/messaging/impl/RemoteMessagingConfigMatcherTest.kt index e342dbe5145d..e7eea308a541 100644 --- a/remote-messaging/remote-messaging-impl/src/test/java/com/duckduckgo/remote/messaging/impl/RemoteMessagingConfigMatcherTest.kt +++ b/remote-messaging/remote-messaging-impl/src/test/java/com/duckduckgo/remote/messaging/impl/RemoteMessagingConfigMatcherTest.kt @@ -19,10 +19,8 @@ package com.duckduckgo.remote.messaging.impl import com.duckduckgo.remote.messaging.api.RemoteMessagingRepository import com.duckduckgo.remote.messaging.fixtures.RemoteMessageOM.aMediumMessage import com.duckduckgo.remote.messaging.fixtures.RemoteMessageOM.aSmallMessage -import com.duckduckgo.remote.messaging.impl.matchers.AndroidAppAttributeMatcher -import com.duckduckgo.remote.messaging.impl.matchers.DeviceAttributeMatcher +import com.duckduckgo.remote.messaging.impl.matchers.AttributeMatcher import com.duckduckgo.remote.messaging.impl.matchers.EvaluationResult -import com.duckduckgo.remote.messaging.impl.matchers.UserAttributeMatcher import com.duckduckgo.remote.messaging.impl.models.MatchingAttribute import com.duckduckgo.remote.messaging.impl.models.MatchingAttribute.Api import com.duckduckgo.remote.messaging.impl.models.MatchingAttribute.Bookmarks @@ -40,9 +38,9 @@ import org.junit.Test class RemoteMessagingConfigMatcherTest { - private val deviceAttributeMatcher: DeviceAttributeMatcher = mock() - private val androidAppAttributeMatcher: AndroidAppAttributeMatcher = mock() - private val userAttributeMatcher: UserAttributeMatcher = mock() + private val deviceAttributeMatcher: AttributeMatcher = mock() + private val androidAppAttributeMatcher: AttributeMatcher = mock() + private val userAttributeMatcher: AttributeMatcher = mock() private val remoteMessagingRepository: RemoteMessagingRepository = mock() private val testee = RemoteMessagingConfigMatcher(