Skip to content

Instantly share code, notes, and snippets.

@igor-brishkoski
igor-brishkoski / kotlin_result_to_java.kt
Created September 30, 2021 13:31
Kotlin Result to Java
////////// Kotlin //////////////////////
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val t = JavaTest()
t.test()
}
}
class Solution {
// Return tree depth in O(d) time.
public int computeDepth(TreeNode node) {
int d = 0;
while (node.left != null) {
node = node.left;
++d;
}
return d;
}
@RunWith(AndroidJUnit4::class)
class CreatePostActivityTest : BaseTest() {
@Rule
@JvmField
var activityTestRule = ActivityTestRule(CreatePostActivity::class.java)
@Test
fun loging_and_createPost_WithText_Success() {
login {
class CreatePostRobot : BaseRobot() {
fun enterPostText(text: String) =
postText().typeText(text)
fun submitPost() =
postImageView().click()
fun matchResultText(expectedText: String) =
postSubmitResultText().isTextDisplayed(expectedText)
@RunWith(AndroidJUnit4::class)
class CreatePostActivityTest : BaseTest() {
@Rule
@JvmField
var activityTestRule = ActivityTestRule(CreatePostActivity::class.java)
@Test
fun createPost_WithText_Success() {
createPost {
class CreatePostRobot : BaseRobot() {
fun enterPostText(text: String) =
postText().typeText(text)
fun submitPost() =
postButton().click()
fun matchResultText(expectedText: String) =
postSubmitResultText().isTextDisplayed(expectedText)
abstract class BaseTest {
val createPost = robotRunner(CreatePostRobot::class)
private fun <T : BaseRobot> robotRunner(cls: KClass<T>) = { func: T.() -> Unit ->
cls.createInstance().apply {
func()
}
}
}
fun ViewInteraction.click(): ViewInteraction =
perform(ViewActions.click())
fun ViewInteraction.typeText(text: String): ViewInteraction =
perform(ViewActions.typeText(text), ViewActions.closeSoftKeyboard())
fun ViewInteraction.isTextDisplayed(text: String): ViewInteraction =
check(ViewAssertions.matches(ViewMatchers.withText(text)))
class CreatePostActivityTest {
@Test
fun void createPost_WithText_Success() {
// Find the edit text and perform typing text action
// close the keyboard after text has been entered
onView(withId(R.id.post_text))
.perform(ViewActions.typeText("This is a post"),ViewActions.closeSoftKeyboard))
// Find the submit post button and perform a click
onView(withId(R.id.post_button))
.perform(ViewActions.click())
@Test
fun mainActivityTest() {
val bottomNavigationItemView = onView(
allOf(
withId(R.id.navigation_dashboard), withContentDescription("Dashboard"),
childAtPosition(
childAtPosition(
withId(R.id.navigation),
0