A fully functional Android app built entirely with Kotlin and Jetpack Compose
Grant `POST_NOTIFICATIONS` permission in more instrumented tests
Simon Marquis823c4db2013a
diff --git a/app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt b/app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationTest.ktindex 5aa3ab02e..036a2955c 100644--- a/app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt+++ b/app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt@@ -33,6 +33,7 @@ import androidx.test.espresso.Espressoimport androidx.test.espresso.NoActivityResumedExceptionimport com.google.samples.apps.nowinandroid.MainActivityimport com.google.samples.apps.nowinandroid.R+import com.google.samples.apps.nowinandroid.core.rules.GrantPostNotificationsPermissionRuleimport dagger.hilt.android.testing.BindValueimport dagger.hilt.android.testing.HiltAndroidRuleimport dagger.hilt.android.testing.HiltAndroidTest@@ -66,9 +67,15 @@ class NavigationTest {val tmpFolder: TemporaryFolder = TemporaryFolder.builder().assureDeletion().build()/**- * Use the primary activity to initialize the app normally.+ * Grant [android.Manifest.permission.POST_NOTIFICATIONS] permission.*/@get:Rule(order = 2)+ val postNotificationsPermission = GrantPostNotificationsPermissionRule()++ /**+ * Use the primary activity to initialize the app normally.+ */+ @get:Rule(order = 3)val composeTestRule = createAndroidComposeRule<MainActivity>()private fun AndroidComposeTestRule<*, *>.stringResource(@StringRes resId: Int) =diff --git a/app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationUiTest.kt b/app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationUiTest.ktindex cd4b40a50..d92390918 100644--- a/app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationUiTest.kt+++ b/app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationUiTest.kt@@ -27,6 +27,7 @@ import androidx.compose.ui.unit.dpimport com.google.accompanist.testharness.TestHarnessimport com.google.samples.apps.nowinandroid.core.data.repository.CompositeUserNewsResourceRepositoryimport com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor+import com.google.samples.apps.nowinandroid.core.rules.GrantPostNotificationsPermissionRuleimport com.google.samples.apps.nowinandroid.core.testing.repository.TestNewsRepositoryimport com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepositoryimport com.google.samples.apps.nowinandroid.uitesthiltmanifest.HiltComponentActivity@@ -61,9 +62,15 @@ class NavigationUiTest {val tmpFolder: TemporaryFolder = TemporaryFolder.builder().assureDeletion().build()/**- * Use a test activity to set the content on.+ * Grant [android.Manifest.permission.POST_NOTIFICATIONS] permission.*/@get:Rule(order = 2)+ val postNotificationsPermission = GrantPostNotificationsPermissionRule()++ /**+ * Use a test activity to set the content on.+ */+ @get:Rule(order = 3)val composeTestRule = createAndroidComposeRule<HiltComponentActivity>()val userNewsResourceRepository = CompositeUserNewsResourceRepository(diff --git a/core/testing/src/main/java/com/google/samples/apps/nowinandroid/core/rules/GrantPostNotificationsPermissionRule.kt b/core/testing/src/main/java/com/google/samples/apps/nowinandroid/core/rules/GrantPostNotificationsPermissionRule.ktnew file mode 100644index 000000000..512399d85--- /dev/null+++ b/core/testing/src/main/java/com/google/samples/apps/nowinandroid/core/rules/GrantPostNotificationsPermissionRule.kt@@ -0,0 +1,29 @@+/*+ * Copyright 2023 The Android Open Source Project+ *+ * 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+ *+ * https://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.google.samples.apps.nowinandroid.core.rules++import android.Manifest.permission.POST_NOTIFICATIONS+import android.os.Build.VERSION.SDK_INT+import android.os.Build.VERSION_CODES.TIRAMISU+import androidx.test.rule.GrantPermissionRule.grant+import org.junit.rules.TestRule++/**+ * [TestRule] granting [POST_NOTIFICATIONS] permission if running on [SDK_INT] greater than [TIRAMISU].+ */+class GrantPostNotificationsPermissionRule :+ TestRule by if (SDK_INT >= TIRAMISU) grant(POST_NOTIFICATIONS) else grant()diff --git a/feature/foryou/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreenTest.kt b/feature/foryou/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreenTest.ktindex b138cba06..8dcdef6be 100644--- a/feature/foryou/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreenTest.kt+++ b/feature/foryou/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreenTest.kt@@ -16,9 +16,6 @@package com.google.samples.apps.nowinandroid.feature.foryou-import android.Manifest-import android.os.Build.VERSION.SDK_INT-import android.os.Build.VERSION_CODES.TIRAMISUimport androidx.activity.ComponentActivityimport androidx.compose.foundation.layout.BoxWithConstraintsimport androidx.compose.ui.test.assertHasClickAction@@ -31,8 +28,7 @@ import androidx.compose.ui.test.onFirstimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performScrollToNode-import androidx.test.rule.GrantPermissionRule-import androidx.test.rule.GrantPermissionRule.grant+import com.google.samples.apps.nowinandroid.core.rules.GrantPostNotificationsPermissionRuleimport com.google.samples.apps.nowinandroid.core.testing.data.followableTopicTestDataimport com.google.samples.apps.nowinandroid.core.testing.data.userNewsResourcesTestDataimport com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState@@ -41,15 +37,10 @@ import org.junit.Testclass ForYouScreenTest {- @get:Rule- val permissionTestRule: GrantPermissionRule =- if (SDK_INT >= TIRAMISU) {- grant(Manifest.permission.POST_NOTIFICATIONS)- } else {- grant()- }+ @get:Rule(order = 0)+ val postNotificationsPermission = GrantPostNotificationsPermissionRule()- @get:Rule+ @get:Rule(order = 1)val composeTestRule = createAndroidComposeRule<ComponentActivity>()private val doneButtonMatcher by lazy {