Skip to content

Commit 7826b4f

Browse files
authored
Merge pull request #748 from SimonMarquis/fix/permissions
Grant `POST_NOTIFICATIONS` permission in more instrumented tests
2 parents 885dae7 + 823c4db commit 7826b4f

File tree

4 files changed

+49
-15
lines changed

4 files changed

+49
-15
lines changed

app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import androidx.test.espresso.Espresso
3333
import androidx.test.espresso.NoActivityResumedException
3434
import com.google.samples.apps.nowinandroid.MainActivity
3535
import com.google.samples.apps.nowinandroid.R
36+
import com.google.samples.apps.nowinandroid.core.rules.GrantPostNotificationsPermissionRule
3637
import dagger.hilt.android.testing.BindValue
3738
import dagger.hilt.android.testing.HiltAndroidRule
3839
import dagger.hilt.android.testing.HiltAndroidTest
@@ -66,9 +67,15 @@ class NavigationTest {
6667
val tmpFolder: TemporaryFolder = TemporaryFolder.builder().assureDeletion().build()
6768

6869
/**
69-
* Use the primary activity to initialize the app normally.
70+
* Grant [android.Manifest.permission.POST_NOTIFICATIONS] permission.
7071
*/
7172
@get:Rule(order = 2)
73+
val postNotificationsPermission = GrantPostNotificationsPermissionRule()
74+
75+
/**
76+
* Use the primary activity to initialize the app normally.
77+
*/
78+
@get:Rule(order = 3)
7279
val composeTestRule = createAndroidComposeRule<MainActivity>()
7380

7481
private fun AndroidComposeTestRule<*, *>.stringResource(@StringRes resId: Int) =

app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationUiTest.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import androidx.compose.ui.unit.dp
2727
import com.google.accompanist.testharness.TestHarness
2828
import com.google.samples.apps.nowinandroid.core.data.repository.CompositeUserNewsResourceRepository
2929
import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
30+
import com.google.samples.apps.nowinandroid.core.rules.GrantPostNotificationsPermissionRule
3031
import com.google.samples.apps.nowinandroid.core.testing.repository.TestNewsRepository
3132
import com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository
3233
import com.google.samples.apps.nowinandroid.uitesthiltmanifest.HiltComponentActivity
@@ -61,9 +62,15 @@ class NavigationUiTest {
6162
val tmpFolder: TemporaryFolder = TemporaryFolder.builder().assureDeletion().build()
6263

6364
/**
64-
* Use a test activity to set the content on.
65+
* Grant [android.Manifest.permission.POST_NOTIFICATIONS] permission.
6566
*/
6667
@get:Rule(order = 2)
68+
val postNotificationsPermission = GrantPostNotificationsPermissionRule()
69+
70+
/**
71+
* Use a test activity to set the content on.
72+
*/
73+
@get:Rule(order = 3)
6774
val composeTestRule = createAndroidComposeRule<HiltComponentActivity>()
6875

6976
val userNewsResourceRepository = CompositeUserNewsResourceRepository(
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2023 The Android Open Source Project
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+
* https://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.google.samples.apps.nowinandroid.core.rules
18+
19+
import android.Manifest.permission.POST_NOTIFICATIONS
20+
import android.os.Build.VERSION.SDK_INT
21+
import android.os.Build.VERSION_CODES.TIRAMISU
22+
import androidx.test.rule.GrantPermissionRule.grant
23+
import org.junit.rules.TestRule
24+
25+
/**
26+
* [TestRule] granting [POST_NOTIFICATIONS] permission if running on [SDK_INT] greater than [TIRAMISU].
27+
*/
28+
class GrantPostNotificationsPermissionRule :
29+
TestRule by if (SDK_INT >= TIRAMISU) grant(POST_NOTIFICATIONS) else grant()

feature/foryou/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreenTest.kt

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package com.google.samples.apps.nowinandroid.feature.foryou
1818

19-
import android.Manifest
20-
import android.os.Build.VERSION.SDK_INT
21-
import android.os.Build.VERSION_CODES.TIRAMISU
2219
import androidx.activity.ComponentActivity
2320
import androidx.compose.foundation.layout.BoxWithConstraints
2421
import androidx.compose.ui.test.assertHasClickAction
@@ -31,8 +28,7 @@ import androidx.compose.ui.test.onFirst
3128
import androidx.compose.ui.test.onNodeWithContentDescription
3229
import androidx.compose.ui.test.onNodeWithText
3330
import androidx.compose.ui.test.performScrollToNode
34-
import androidx.test.rule.GrantPermissionRule
35-
import androidx.test.rule.GrantPermissionRule.grant
31+
import com.google.samples.apps.nowinandroid.core.rules.GrantPostNotificationsPermissionRule
3632
import com.google.samples.apps.nowinandroid.core.testing.data.followableTopicTestData
3733
import com.google.samples.apps.nowinandroid.core.testing.data.userNewsResourcesTestData
3834
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState
@@ -41,15 +37,10 @@ import org.junit.Test
4137

4238
class ForYouScreenTest {
4339

44-
@get:Rule
45-
val permissionTestRule: GrantPermissionRule =
46-
if (SDK_INT >= TIRAMISU) {
47-
grant(Manifest.permission.POST_NOTIFICATIONS)
48-
} else {
49-
grant()
50-
}
40+
@get:Rule(order = 0)
41+
val postNotificationsPermission = GrantPostNotificationsPermissionRule()
5142

52-
@get:Rule
43+
@get:Rule(order = 1)
5344
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
5445

5546
private val doneButtonMatcher by lazy {

0 commit comments

Comments
 (0)