Created
September 30, 2021 14:41
-
-
Save theapache64/21a32a5e9222057b8bf88ce03fc66755 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.content.pm.ActivityInfo | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.material.Button | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.platform.LocalConfiguration | |
import androidx.compose.ui.platform.LocalContext | |
import androidx.compose.ui.test.assertIsDisplayed | |
import androidx.compose.ui.test.junit4.createComposeRule | |
import androidx.compose.ui.test.onNodeWithText | |
import androidx.compose.ui.test.performClick | |
import org.junit.Rule | |
import org.junit.Test | |
class SoTest { | |
@get:Rule | |
val composeRule = createComposeRule() | |
@Test | |
fun test() { | |
composeRule.setContent { MyApp() } | |
// Starts with portrait | |
composeRule.onNodeWithText("P").assertIsDisplayed() | |
// Change the orientation to Landscape | |
composeRule.onNodeWithText("DO IT").performClick() | |
// Now the text should be `L` | |
composeRule.onNodeWithText("L").assertIsDisplayed() | |
} | |
@Composable | |
fun MyApp() { | |
val currentOrientation = LocalConfiguration.current.orientation | |
val orientation = if (currentOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { | |
"P" | |
} else { | |
"L" | |
} | |
val activity = LocalContext.current as Activity | |
Column { | |
Text(text = orientation) | |
Button(onClick = { | |
// change orientation to landscape | |
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE | |
}) { | |
Text(text = "DO IT") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment