From 331e8cffaca5f7fae93267c9d25a7231106fb6bf Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Fri, 19 May 2023 11:45:38 +0100 Subject: [PATCH 1/3] Grant `POST_NOTIFICATIONS` permission in instrumented tests --- .../apps/nowinandroid/feature/foryou/ForYouScreenTest.kt | 7 +++++++ 1 file changed, 7 insertions(+) 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.kt index eb27473bb..404ad55b9 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,6 +16,7 @@ package com.google.samples.apps.nowinandroid.feature.foryou +import android.Manifest import androidx.activity.ComponentActivity import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.ui.test.assertHasClickAction @@ -28,6 +29,8 @@ import androidx.compose.ui.test.onFirst import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performScrollToNode +import androidx.test.rule.GrantPermissionRule +import androidx.test.rule.GrantPermissionRule.grant import com.google.samples.apps.nowinandroid.core.testing.data.followableTopicTestData import com.google.samples.apps.nowinandroid.core.testing.data.userNewsResourcesTestData import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState @@ -35,6 +38,10 @@ import org.junit.Rule import org.junit.Test class ForYouScreenTest { + + @get:Rule + val permissionTestRule: GrantPermissionRule = grant(Manifest.permission.POST_NOTIFICATIONS) + @get:Rule val composeTestRule = createAndroidComposeRule() From 69879ff874161253b255de4e5c7bdf5fb024f228 Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Fri, 19 May 2023 12:28:01 +0100 Subject: [PATCH 2/3] Grant permission only if TIRAMISU or above --- .../apps/nowinandroid/feature/foryou/ForYouScreenTest.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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.kt index 404ad55b9..7a750a13d 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 @@ -17,6 +17,8 @@ package com.google.samples.apps.nowinandroid.feature.foryou import android.Manifest +import android.os.Build.VERSION.SDK_INT +import android.os.Build.VERSION_CODES.TIRAMISU import androidx.activity.ComponentActivity import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.ui.test.assertHasClickAction @@ -40,7 +42,9 @@ import org.junit.Test class ForYouScreenTest { @get:Rule - val permissionTestRule: GrantPermissionRule = grant(Manifest.permission.POST_NOTIFICATIONS) + val permissionTestRule: GrantPermissionRule = + if (SDK_INT < TIRAMISU) grant() + else grant(Manifest.permission.POST_NOTIFICATIONS) @get:Rule val composeTestRule = createAndroidComposeRule() From a93cce92f712a8d067751f751e7ffce0de544c2f Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Fri, 19 May 2023 12:51:35 +0100 Subject: [PATCH 3/3] Update ForYouScreenTest.kt --- .../apps/nowinandroid/feature/foryou/ForYouScreenTest.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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.kt index 7a750a13d..b138cba06 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 @@ -43,8 +43,11 @@ class ForYouScreenTest { @get:Rule val permissionTestRule: GrantPermissionRule = - if (SDK_INT < TIRAMISU) grant() - else grant(Manifest.permission.POST_NOTIFICATIONS) + if (SDK_INT >= TIRAMISU) { + grant(Manifest.permission.POST_NOTIFICATIONS) + } else { + grant() + } @get:Rule val composeTestRule = createAndroidComposeRule()