Skip to content

Commit 9bfa19f

Browse files
committed
test: fix and enhance RotationTest with network permissions
- Add `AndroidManifest.xml` files for `androidTest` and `debug` build types to include `INTERNET`, `ACCESS_NETWORK_STATE`, and `ACCESS_LOCAL_NETWORK` permissions. - Re-enable `RotationTest` by removing the `@Ignore` annotation and implementing a runtime permission grant for `ACCESS_LOCAL_NETWORK` on supported Android versions. - Refactor `RotationTest` to use `onNodeWithTag` with `CREATE_NOTE_FAB_TAG` instead of string-based `onNodeWithContentDescription`. - Update test logic to use `runTest` and `awaitIdle()` for better synchronization during orientation changes. - Adjust `RuleChain` order in `RotationTest` to ensure `composeTestRule` is initialized correctly relative to the `ScreenOrientationRule`.
1 parent 487c448 commit 9bfa19f

3 files changed

Lines changed: 45 additions & 15 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<!-- Required by Espresso Device to connect from the test process to the emulator control service. -->
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
7+
<uses-permission android:name="android.permission.ACCESS_LOCAL_NETWORK" />
8+
9+
</manifest>
Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
package com.softartdev.notedelight.ui
22

3+
import android.os.Build
34
import androidx.compose.ui.test.assertIsDisplayed
45
import androidx.compose.ui.test.junit4.v2.createAndroidComposeRule
5-
import androidx.compose.ui.test.onNodeWithContentDescription
6+
import androidx.compose.ui.test.onNodeWithTag
67
import androidx.test.espresso.device.DeviceInteraction.Companion.setScreenOrientation
78
import androidx.test.espresso.device.EspressoDevice.Companion.onDevice
89
import androidx.test.espresso.device.action.ScreenOrientation
910
import androidx.test.espresso.device.rules.ScreenOrientationRule
1011
import androidx.test.ext.junit.runners.AndroidJUnit4
1112
import androidx.test.filters.LargeTest
13+
import androidx.test.platform.app.InstrumentationRegistry
14+
import co.touchlab.kermit.Logger
1215
import com.softartdev.notedelight.MainActivity
13-
import kotlinx.coroutines.runBlocking
16+
import com.softartdev.notedelight.util.CREATE_NOTE_FAB_TAG
17+
import kotlinx.coroutines.test.runTest
1418
import leakcanary.DetectLeaksAfterTestSuccess
1519
import leakcanary.TestDescriptionHolder
16-
import notedelight.core.ui.generated.resources.Res
17-
import notedelight.core.ui.generated.resources.create_note
18-
import org.jetbrains.compose.resources.getString
19-
import org.junit.Ignore
20+
import org.junit.Before
2021
import org.junit.Rule
2122
import org.junit.Test
2223
import org.junit.rules.RuleChain
@@ -25,30 +26,43 @@ import org.junit.runner.RunWith
2526
@LargeTest
2627
@RunWith(AndroidJUnit4::class)
2728
class RotationTest {
28-
29+
private val logger = Logger.withTag("RotationTest")
2930
private val composeTestRule = createAndroidComposeRule<MainActivity>()
3031

3132
@get:Rule
3233
val rules: RuleChain = RuleChain.outerRule(TestDescriptionHolder)
33-
.around(ScreenOrientationRule(ScreenOrientation.PORTRAIT))
3434
.around(composeTestRule)
35+
.around(ScreenOrientationRule(ScreenOrientation.PORTRAIT))
3536
.around(DetectLeaksAfterTestSuccess())
3637

37-
@Ignore("Unable to connect to Emulator gRPC port on CI")
38+
@Before
39+
fun grantLocalNetworkPermission() {
40+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CINNAMON_BUN) {
41+
val instrumentation = InstrumentationRegistry.getInstrumentation()
42+
val permission = "android.permission.ACCESS_LOCAL_NETWORK"
43+
sequenceOf(
44+
instrumentation.targetContext.packageName,
45+
instrumentation.context.packageName
46+
).forEach { packageName: String ->
47+
logger.d { "grant to $packageName the $permission" }
48+
instrumentation.uiAutomation.grantRuntimePermission(packageName, permission)
49+
}
50+
}
51+
}
52+
3853
@Test
39-
fun rotationTest() {
40-
val createNoteLabel = runBlocking { getString(Res.string.create_note) }
54+
fun rotationTest() = runTest {
55+
composeTestRule.awaitIdle()
4156

4257
composeTestRule
43-
.onNodeWithContentDescription(label = createNoteLabel)
58+
.onNodeWithTag(CREATE_NOTE_FAB_TAG)
4459
.assertIsDisplayed()
4560

4661
onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE)
62+
composeTestRule.awaitIdle()
4763

4864
composeTestRule
49-
.onNodeWithContentDescription(label = createNoteLabel)
65+
.onNodeWithTag(CREATE_NOTE_FAB_TAG)
5066
.assertIsDisplayed()
51-
52-
onDevice().setScreenOrientation(ScreenOrientation.PORTRAIT)
5367
}
5468
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<!-- Required by Espresso Device to connect from the test process to the emulator control service. -->
5+
<uses-permission android:name="android.permission.ACCESS_LOCAL_NETWORK" />
6+
7+
</manifest>

0 commit comments

Comments
 (0)