<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vorawut M on Medium]]></title>
        <description><![CDATA[Stories by Vorawut M on Medium]]></description>
        <link>https://medium.com/@vorawut.m?source=rss-4e46b22b300b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*kn9ygOi3e37u0oAY0IFOuQ.jpeg</url>
            <title>Stories by Vorawut M on Medium</title>
            <link>https://medium.com/@vorawut.m?source=rss-4e46b22b300b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 02:43:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vorawut.m/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Basics of UI Testing for Android Jetpack Compose]]></title>
            <link>https://medium.com/@vorawut.m/basics-of-ui-testing-for-android-jetpack-compose-b7421c94b3ce?source=rss-4e46b22b300b------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7421c94b3ce</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[jetpack-compose-testing]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[ui-testing]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <dc:creator><![CDATA[Vorawut M]]></dc:creator>
            <pubDate>Tue, 04 Jun 2024 15:38:34 GMT</pubDate>
            <atom:updated>2024-06-04T15:38:34.451Z</atom:updated>
            <content:encoded><![CDATA[<h3>(ลองทำ UI Test สำหรับ Jetpack Compose กันเถอะ)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*r3I0lmeRvmEbK0g7.png" /></figure><p>เชื่อว่าเหล่า Android dev ทั้งหลายคงรู้จัก Jetpack Compose กันดีอยู่ แต่อาจจะยังมีบางส่วนที่ไม่รู้ว่า เราสามารถทำ UI Test ที่ Jetpack Compose ได้แล้ว</p><p>ซึ่งในบทความนี้ผู้เขียนจะมาแนะนำการทำ UI Testing สำหรับ Jetpack Compose เพื่อ verify ผลรับว่าสิ่งที่แสดงอยู่ในหน้าจอนั้นเป็นตามที่เราต้องการ</p><p>ก่อนที่เราจะไปทำ Testing กัน ควรรู้จักคำสั่งพื้นฐานกันก่อน</p><h4>Compose rule</h4><pre>@get:Rule<br>val composeTestRule = createComposeRule()</pre><p>สำหรับการทำ UI Test จำเป็นต้องมี ComposeTestRule เพื่อไว้ใช้กำหนด content หรือแม้แต่การ find หา node (view) ต่างๆใน compose ui ของเรา</p><h4>Log</h4><pre>composeTestRule.onRoot().printToLog(&quot;TAG&quot;)</pre><p>หากเราต้องการดูโครงสร้างของ compose ui เราสามารถ log ออกมาได้ โดยมันจะ log ออกมาเป็น compose tree แสดงรายละเอียด semantics nodes การเรียงลำดับ และ childrens ต่างๆ ใน ui ของเรา</p><h4>Finders</h4><pre>// Finds a semantics node that matches the given condition.<br>composeTestRule.onNode(SemanticsMatcher)<br>composeTestRule.onNodeWithTag(&quot;Tag&quot;)<br>composeTestRule.onNodeWithText(&quot;Text&quot;)<br>composeTestRule.onNodeWithContentDescription(&quot;Description&quot;)<br><br>// Finds all semantics nodes that match the given condition.<br>composeTestRule.onAllNodes(SemanticsMatcher)<br>composeTestRule.onAllNodesWithTag(&quot;Tag&quot;)<br>composeTestRule.onAllNodesWithText(&quot;Text&quot;)<br>composeTestRule.onAllNodesWithContentDescription(&quot;Description&quot;)<br><br>// Finds the root semantics node of the Compose tree.<br>composeTestRule.onRoot()</pre><p>ในขั้นตอนการ Testing ก่อนที่เราจะ verify ค่าต่างๆ ตามที่เรา expected ไว้ เราจำเป็นต้องเลือก node (view) ที่ต้องการมาก่อน ซึ่งเราสามารถกำหนด condition ในการเลือกได้</p><p>onNode คือการเลือก node ที่ตรงเงื่อนไขมาตัวเดียว ซึ่งถ้าเจอ node ที่ตรงตามเงื่อนไขมากกว่า 1 ตัวก็จะทำให้ failed ได้</p><p>onAllNodes คือการเลือก nodes ที่ตรงตามเงื่อนไขมาทุกตัว</p><p>onRoot คือการเลือก root node (บนสุด) ของ compose</p><h4>Matchers</h4><pre>hasTestTag(&quot;Tag&quot;)<br>hasText(&quot;Text&quot;)<br>hasContentDescription(&quot;Description&quot;)<br>hasClickAction()<br>hasNoClickAction()<br>isFocused()<br>isNotFocused()<br>isEnabled()<br>isNotEnabled()<br>...</pre><p>Matcher มีไว้เพื่อตั้งเงื่อนไขในการ find node หรือ assert value ให้ match ตามที่เราต้องการ</p><h4>Actions</h4><pre>performClick()<br>performTextInput(&quot;Text&quot;)<br>performTextReplacement(&quot;Text&quot;)<br>performScrollToNode(SemanticsMatcher)<br>performTouchInput{ }<br>...</pre><p>action เป็นเหมือนคำสั่งที่มีไว้ให้ robot จำลอง behavior เดียวกันกับที่ user ทำได้เช่น click, swipe, enter text</p><h4>Assertions</h4><pre>assert(SemanticsMatcher)<br>assertIsDisplayed()<br>assertIsEnabled()<br>assertIsNotEnabled()<br>assertTextEquals(&quot;Text&quot;)<br>assertTextContains(&quot;Text&quot;)<br>assertContentDescriptionEquals(&quot;Description&quot;)<br>assertContentDescriptionContains(&quot;Description&quot;)<br>...</pre><p>assertion คือการ verify value ของ ui ว่าได้ตรงตามที่เรา expected ไว้ไหม</p><h4>มาเริ่มทำ Testing กันเถอะ</h4><pre>@Composable<br>fun TestScreen() {<br>    Column(<br>        modifier = Modifier.fillMaxSize(),<br>        verticalArrangement = Arrangement.SpaceAround,<br>        horizontalAlignment = Alignment.CenterHorizontally<br>    ) {<br>        Text(<br>            modifier = Modifier.testTag(&quot;title&quot;),<br>            text = &quot;Counter Application&quot;,<br>            fontSize = 24.sp<br>        )<br><br>        var counter by remember { mutableIntStateOf(0) }<br>        Text(<br>            modifier = Modifier.testTag(&quot;text_count&quot;),<br>            text = &quot;$counter&quot;,<br>            fontSize = 48.sp<br>        )<br><br>        Button(<br>            modifier = Modifier.testTag(&quot;button&quot;),<br>            onClick = { counter++ },<br>        ) {<br>            Text(text = &quot;Count&quot;)<br>        }<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*Kq3pkt1-qWk3YLs8POyOmg.gif" /><figcaption>Test Screen</figcaption></figure><p>ในตัวอย่างนี้เป็น screen ง่ายๆ ที่มีการนับการกด Count button</p><p>สังเกตได้ว่าผู้เขียนมีการเพิ่มคำสั่ง testTag ไว้เพื่อให้ง่ายในการทำ test โดยการ find หาจาก tag ที่เพิ่มไปนั้นเอง</p><p>ซึ่งถ้าไม่อยากเพิ่ม tag ก็สามารถใช้เงื่อนไขอื่นๆ ในการ match node (view) ได้เช่นกัน</p><p>มาเริ่ม Test จริงๆละ</p><pre>class TestScreenUiTest {<br><br>    @get:Rule<br>    val composeTestRule = createComposeRule()<br><br>    @Test<br>    fun testContent() {<br>        composeTestRule.setContent { TestScreen() }<br><br>        // Assertion<br>        composeTestRule.onNodeWithTag(&quot;title&quot;)<br>            .assertIsDisplayed()<br>            .assertTextEquals(&quot;Counter Application&quot;)<br><br>        composeTestRule.onNodeWithTag(&quot;text_count&quot;)<br>            .assertIsDisplayed()<br>            .assertTextEquals(&quot;0&quot;)<br><br>        composeTestRule.onNodeWithTag(&quot;button&quot;)<br>            .assertIsDisplayed()<br>            .assertHasClickAction()<br>            .assertTextEquals(&quot;Count&quot;)<br>    }<br><br>    @Test<br>    fun testCounter() {<br>        composeTestRule.setContent { TestScreen() }<br><br>        // Click<br>        composeTestRule.onNodeWithTag(&quot;button&quot;)<br>            .performClick()<br>        <br>        composeTestRule.onNodeWithTag(&quot;text_count&quot;)<br>            .assertIsDisplayed()<br>            .assertTextEquals(&quot;1&quot;)<br><br>        // Click<br>        composeTestRule.onNodeWithTag(&quot;button&quot;)<br>            .performClick()<br><br>        composeTestRule.onNodeWithTag(&quot;text_count&quot;)<br>            .assertIsDisplayed()<br>            .assertTextEquals(&quot;2&quot;)<br><br>        // Click 2 times<br>        composeTestRule.onNodeWithTag(&quot;button&quot;)<br>            .performClick()<br>            .performClick()<br><br>        composeTestRule.onNodeWithTag(&quot;text_count&quot;)<br>            .assertIsDisplayed()<br>            .assertTextEquals(&quot;4&quot;)<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*P46MQKTORCHJ6J0VCpx0yQ.gif" /><figcaption>Test Screen from UI Testing</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*mFqJjIB_m-L9Pn7eiEN3EQ.png" /></figure><p>จะเห็นได้ว่า Robot UI Test ของเราทำงานได้แล้ว และค่อยข้างรวดเร็วเลยทีเดียว โดยใน test case นี้ ผู้เขียนได้ทำการ verify content ที่ยังไม่ได้มีการ count กับอีก function ที่มี action click เพื่อ verify การ count นั้นเอง</p><h4>แล้วถ้าเป็น List (LazyColumn) ละ</h4><pre>@Composable<br>fun TestListScreen(list: List&lt;String&gt;) {<br>    Column(<br>        modifier = Modifier.fillMaxSize(),<br>    ) {<br>        Text(<br>            modifier = Modifier<br>                .fillMaxWidth()<br>                .padding(top = 32.dp)<br>                .testTag(&quot;list_title&quot;),<br>            text = &quot;Test List&quot;,<br>            fontSize = 20.sp,<br>            textAlign = TextAlign.Center<br>        )<br><br>        LazyColumn(<br>            modifier = Modifier<br>                .fillMaxWidth()<br>                .weight(1f)<br>                .padding(top = 16.dp)<br>                .testTag(&quot;list_section&quot;)<br>        ) {<br>            itemsIndexed(list) { index, item -&gt;<br>                Text(<br>                    modifier = Modifier<br>                        .padding(vertical = 16.dp, horizontal = 24.dp)<br>                        .testTag(&quot;list_item&quot;),<br>                    text = &quot;${index+1}. $item&quot;,<br>                    fontSize = 16.sp<br>                )<br>            }<br>        }<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*7zLCGGERwWoTG_AF8lpL3w.gif" /><figcaption>Test List Screen</figcaption></figure><p>ตัวอย่างนี้ คือหน้าจอแสดงรายชื่อบุคคลต่างๆ ซึ่งผู้เขียนใส่ tag สำหรับทำ Testing ไว้แล้ว</p><p>มาดูการ Testing กัน</p><pre>class TestListScreenUiTest {<br><br>    @get:Rule<br>    val composeTestRule = createComposeRule()<br><br>    @Test<br>    fun testList() {<br>        composeTestRule.setContent { TestListScreen(list = list)  }<br><br>        // Assert<br>        composeTestRule.onNodeWithTag(&quot;list_title&quot;)<br>            .assertIsDisplayed()<br>            .assertTextEquals(&quot;Test List&quot;)<br><br>        composeTestRule.onAllNodesWithTag(&quot;list_item&quot;)<br>            .onFirst()<br>            .assertIsDisplayed()<br>            .assertTextEquals(&quot;1. Nora Muller&quot;)<br><br>        composeTestRule.onAllNodesWithTag(&quot;list_item&quot;)<br>            .get(1)<br>            .assertIsDisplayed()<br>            .assertTextEquals(&quot;2. Violet Middleton&quot;)<br>    }<br><br>    private val list = listOf(<br>        &quot;Nora Muller&quot;,<br>        &quot;Violet Middleton&quot;,<br>        &quot;Allan Solis&quot;,<br>        &quot;Hope Hall&quot;,<br>        &quot;Gwen Lynch&quot;,<br>        &quot;Roberto Miranda&quot;,<br>        &quot;Silvia May&quot;,<br>        &quot;Tabitha Gillespie&quot;,<br>        &quot;Amy Franklin&quot;,<br>        &quot;Jacquelyn Meyer&quot;,<br>        &quot;Aida Massey&quot;,<br>        &quot;Ronda Welch&quot;,<br>        &quot;Rachel Donahue&quot;,<br>        &quot;Latoya Johnston&quot;,<br>        &quot;Gilbert Potts&quot;,<br>        &quot;Kristi Garrett&quot;,<br>        &quot;Brent Rodgers&quot;,<br>        &quot;Agnes Wiley&quot;,<br>        &quot;Doreen Salazar&quot;,<br>        &quot;Valerie Sampson&quot;,<br>    )<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*8BDIdrUIpiyxP1eRq9c_og.png" /></figure><p>ก็สามารถ Test ผ่านได้ปกติ แต่จะเห็นว่าเราต้องทำการเลือก index ใน list ของ node ด้วย เนื่องจาก item ของเรามี tag ที่ซ้ำกัน ทำให้ไม่สะดวกในการ find หา node ที่ต้องการ</p><p>จะดีกว่าไหมถ้าเราเซ็ตให้ tag ไม่ซ้ำกันไปเลย</p><pre>LazyColumn(<br>    ...<br>) {<br>    itemsIndexed(list) { index, item -&gt;<br>        Text(<br>            modifier = Modifier.padding(vertical = 16.dp, horizontal = 24.dp)<br>                .testTag(&quot;list_item$index&quot;),<br>            text = &quot;${index+1}. $item&quot;,<br>            fontSize = 16.sp<br>        )<br>    }<br>}</pre><p>เราสามารถใส่ index ของ item เข้าไปใน tag นั้นด้วยเพื่อให้ find หา node นั้นง่ายขึ้น</p><pre>@Test<br>fun testList() {<br>    composeTestRule.setContent { TestListScreen(list = list)  }<br><br>    // Assert<br>    composeTestRule.onNodeWithTag(&quot;list_title&quot;)<br>        .assertIsDisplayed()<br>        .assertTextEquals(&quot;Test List&quot;)<br><br>    composeTestRule.onNodeWithTag(&quot;list_item0&quot;)<br>        .assertIsDisplayed()<br>        .assertTextEquals(&quot;1. Nora Muller&quot;)<br><br>    composeTestRule.onNodeWithTag(&quot;list_item1&quot;)<br>        .assertIsDisplayed()<br>        .assertTextEquals(&quot;2. Violet Middleton&quot;)<br><br>    composeTestRule.onNodeWithTag(&quot;list_section&quot;)<br>        .performScrollToNode(hasTestTag(&quot;list_item19&quot;))<br><br>    composeTestRule.onNodeWithTag(&quot;list_item19&quot;)<br>        .assertIsDisplayed()<br>        .assertTextEquals(&quot;20. Valerie Sampson&quot;)<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*dUu0a52JCk-Wwp7uozlmHQ.gif" /><figcaption>Test List Screen from UI Testing</figcaption></figure><p>เรียบร้อยแล้ว find หาง่ายขึ้น scroll ไปหา node ที่ต้องการก็ง่ายอีกด้วย</p><h4>useUnmergedTree คืออะไรนะ?</h4><p>บางครั้ง child node ต่างๆ มีโอกาสถูก merged รวมกันได้ ซึ่งหน้าตา ui ไม่ได้เปลี่ยนไป แต่ก็อาจทำให้ testing ไม่ผ่าน</p><p>ตัวอย่าง</p><pre>// Compose UI<br>Button(<br>    modifier = Modifier.testTag(&quot;button_hello_world&quot;),<br>    onClick = {  },<br>) {<br>    Text(<br>        modifier = Modifier.testTag(&quot;button_text_hello&quot;),<br>        text = &quot;Hello&quot;<br>    )<br>    Text(<br>        modifier = Modifier.testTag(&quot;button_text_world&quot;),<br>        text = &quot; world&quot;<br>    )<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/1*mqPgh3Q0ZOisYO_YCcylMg.png" /><figcaption>Button Hello world</figcaption></figure><pre>// Test find by text<br>composeTestRule.onNodeWithText(&quot;Hello world&quot;)<br>            .assertIsDisplayed()<br><br>// or find by tag node button<br>composeTestRule.onNodeWithTag(&quot;button_hello_world&quot;)<br>            .assertIsDisplayed()<br>            .assertTextEquals(&quot;Hello world&quot;)<br><br>// or find by tag node text<br>composeTestRule.onNodeWithTag(&quot;button_text_hello&quot;)<br>            .assertIsDisplayed()<br>            .assertTextEquals(&quot;Hello&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*24qhwtriLXEE5dlqcRVaMg.png" /><figcaption>Error log 1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*kKtr-sYhKpAsUnc0IEmT3w.png" /><figcaption>Error log2</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*9HZkzbwrEJ0-lzH3j-Tx7A.png" /><figcaption>Error log3</figcaption></figure><p>ใน 3 ตัวอย่างการทดสอบนี้จะเห็นว่าไม่ผ่านทั้งหมด</p><p>ถ้าไม่ผ่านลักษณะนี้ ให้ลอง print log ออกมาดูก่อนว่า node ที่เรา test นั้นโดน merge ไปรึป่าว</p><pre>composeTestRule.onNodeWithTag(&quot;button_hello_world&quot;)<br>            .printToLog(&quot;hello_world&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*ggygcSpXOZIfuYJ8JP5SGw.png" /><figcaption>Print log</figcaption></figure><p>สังเกตได้ว่า Text โดยจับไปรวมกันแล้ว แม้ว่าเราจะ find node จาก tag ของ text ตัวแรก(ในตัวอย่าง error 3) ก็ยังไม่ผ่านอยู่ดี</p><p>แล้วถ้าเรา verify จาก text โดยตรง โดยการแยก verify เป็น 2 ตัว คือแยก Hello กับ world ออกจากกัน ให้เหมือนกับ node view ละ</p><pre>composeTestRule.onNodeWithText(&quot;Hello&quot;)<br>          .assertIsDisplayed()<br>composeTestRule.onNodeWithText(&quot; world&quot;)<br>          .assertIsDisplayed()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*ygKcpxcGx91m2QUmR5pprw.png" /></figure><p>ผ่านแล้ววว แต่บางครั้ง node ของเราไม่ได้เป็น text ที่จะ verify ง่ายๆ อย่างในเคสนี้นะ</p><p>ดังนั้นตัว Compose ui test เลยมี flag useUnmergedTree มาให้เราใช้ เพื่อให้แยก node ที่โดน merge ออกจากกันสำหรับทดสอบ</p><p>ใช้งานง่ายๆ โดยการเซ็ต useUnmergedTree เป็น true เข้าไปในตอนที่ find หา node</p><pre>composeTestRule.onNodeWithTag(&quot;button_hello_world&quot;, true)<br>            .printToLog(&quot;hello_world&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*rpBqeN-506oAlUEhcz61Vw.png" /><figcaption>Print log (useUnmergedTree is true)</figcaption></figure><pre>composeTestRule.onNodeWithTag(&quot;button_text_hello&quot;, true)<br>            .assertIsDisplayed()<br>            .assertTextEquals(&quot;Hello&quot;)<br><br>composeTestRule.onNodeWithTag(&quot;button_text_world&quot;, true)<br>            .assertIsDisplayed()<br>            .assertTextEquals(&quot; world&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*Qol1cnBCo6bLg24haI7XeQ.png" /></figure><p>เท่านี้ก็เรียบร้อยแล้ว 🎉🎉</p><h4>แถมให้นิดนึง</h4><p>ถ้าหากว่า app ของเรา มีพวก animation หรือ การ count down ต่างๆ แต่เราต้องการ skip มันเพื่อให้ test ของเราไวขึ้น เราสามารถเช็ต mainClock ให้ปิด synchonization และ skip ได้</p><pre>// Disable automatic synchronization<br>composeTestRule.mainClock.autoAdvance = false<br><br>// You can advance exactly one frame or by a specific duration<br>composeTestRule.mainClock.advanceTimeByFrame()<br>composeTestRule.mainClock.advanceTimeBy(milliseconds)</pre><p>จบแล้วพื้นฐาน UI Test สำหรับ Jetpack Compose ที่ผู้เขียนจัดเตรียมไว้ แล้วมาเพิ่ม Quality ของ Android app ด้วย UI Test กันเถอะ</p><p>หวังว่าบทความนี้จะเป็นประโยชน์นะครับ ขอบคุณทุกคนที่เข้ามาอ่านจ้าาาา</p><p>ส่วนใครที่ยังใช้ XML View อยู่ แล้วยังไม่ได้ลองเขียน UI Test สักที ผู้เขียนเองก็เคยเขียน UI Test ของ xml ไว้เหมือนกันน้า ไปตามอ่านกันได้</p><p><a href="https://medium.com/@vorawut.m/%E0%B8%A1%E0%B8%B2%E0%B8%97%E0%B8%B3-android-ui-test-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-kakao-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-4b6efd901de0">มาทำ Android UI Test ด้วย Kakao กันเถอะ</a></p><h4>Reference</h4><p><a href="https://developer.android.com/develop/ui/compose/testing/apis">https://developer.android.com/develop/ui/compose/testing/apis</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7421c94b3ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[EventBus รถเมล์สาย8 ในโลก Android]]></title>
            <link>https://medium.com/@vorawut.m/eventbus-%E0%B8%A3%E0%B8%96%E0%B9%80%E0%B8%A1%E0%B8%A5%E0%B9%8C%E0%B8%AA%E0%B8%B2%E0%B8%A28-%E0%B9%83%E0%B8%99%E0%B9%82%E0%B8%A5%E0%B8%81-android-6c04ab5fe6e?source=rss-4e46b22b300b------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c04ab5fe6e</guid>
            <category><![CDATA[eventbus]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Vorawut M]]></dc:creator>
            <pubDate>Sun, 25 Sep 2022 17:12:04 GMT</pubDate>
            <atom:updated>2022-10-10T17:23:25.444Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*L15VluJolKajx_WG.png" /></figure><p>ถ้าหากเพื่อนๆต้องการส่ง event หรือส่งค่าบางอย่างระหว่าง <strong>activity/fragment</strong> หรือ <strong>background thread</strong> แบบเรียบง่าย ไม่ซับซ้อน ผู้เขียนคิดว่า <strong>EventBus</strong> จะเป็นอีก 1 ทางเลือกให้เพื่อนๆได้พิจารณาเอามาใช้กัน</p><p>ดูรายละเอียดจากทีมพัฒนาได้ที่นี่ <a href="https://github.com/greenrobot/EventBus">Github</a></p><p><strong>Install</strong></p><pre>implementation(&quot;org.greenrobot:eventbus:&lt;latest version&gt;&quot;)</pre><h3>EventBus in 3 steps</h3><p><strong>1. กำหนด Event</strong></p><pre>class MessageEvent</pre><p>สามารถเพิ่ม field หรือกำหนดเป็น <strong>data class</strong> เพื่อให้ event ของเราสามารถแนบข้อมูลไปด้วยได้</p><p><strong>2. กำหนดตัว Subscriber</strong></p><pre>@Subscribe<br>fun onMessageEvent(event: MessageEvent) {<br>    // Do something<br>}</pre><p>สร้างฟังชันก์ไว้รับ event ซึ่งสามารถกำหนด <strong>threads mode</strong> ในการทำงานของฟังชันก์ได้</p><pre>override fun onCreate(savedInstanceState: Bundle?) {<br>    super.onCreate(savedInstanceState)<br>    EventBus.getDefault().register(this)<br>}</pre><pre>override fun onDestroy() {<br>    super.onDestroy()<br>    EventBus.getDefault().unregister(this)<br>}</pre><p>Register และ unregister ที่ตัว <strong>activity/fragment</strong> ของเรา และต้องทำให้สอดคล้องกับ <strong>lifecycle</strong> ด้วยนะ</p><p><strong>3. ส่ง Event</strong></p><pre>EventBus.getDefault().post(MessageEvent())</pre><p>ส่ง event ได้เลย โดยต้องเป็น <strong>class</strong> เดียวกับตัวที่เราตั้งรับไว้นะ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*4Q7pi6eOhWHKcfx4jFM1sA.gif" /><figcaption>ทดสอบโดยให้ second activity ส่ง event กลับมาให้ first activity</figcaption></figure><p><strong>ถ้ามี Subscriber มากกว่า 1 ที่ละ</strong></p><p>ถ้ารอรับ event เดียวกัน ไม่ว่าที่ไหนก็ตาม ก็จะได้รับ event นั้นๆเหมือนกัน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*kph6ucQlSEpO1a7J79POCQ.gif" /><figcaption>ทดสอบโดยให้ third activity ส่ง event กลับมาให้ first, second activity</figcaption></figure><p><strong>แล้วถ้าไม่มี Subscriber เลยละ</strong></p><p>มีการส่ง event ปกติ แค่ไม่มีตัวรับ ก็เหมือน event นั้นลอยหายไป หากเพื่อนๆคิดว่าอาจเกิดกรณีนี้ขึ้นได้แนะนำให้ config <strong>sendNoSubscriberEvent(false) </strong>เพิ่มเพื่อไม่ให้ส่ง event ในกรณีที่ไม่มี subscriber</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*bq_gIRclDTO-i97bKYyE8A.gif" /><figcaption>ทดสอบโดยให้ second activity ส่ง event กลับมาแต่ first activity ไม่ได้ register ไว้</figcaption></figure><p><strong>Threads mode</strong></p><p><strong>EventBus</strong> สามารถจัดการ threads สำหรับรับ event ได้ดังนี้</p><ul><li><strong>POSTING</strong> เป็น default ในการรับ event ซึ่งมันจะใช้ thread เดียวกับตัวที่ post event มานั้นเอง</li><li><strong>MAIN </strong>คือการใช้ main หรือ UI thread ในการรับ event หากใช้ main ในการรับ event ก็ไม่ควรให้ฟังชันก์มีการทำงานอะไรที่ช้า เพื่อไม่ให้ block main thead</li><li><strong>MAIN_ORDERED </strong>เหมือนกับ main thread เลย แต่หากมีการส่งหลายๆ event มาในเวลาใกล้เคียงกัน แล้ว event ที่ 2 ดันทำงานเสร็จก่อน event แรก ซึ่ง MAIN_ORDERED จะให้ event แรกทำงานเสร็จก่อนแล้วค่อยให้ event ที่ 2 ทำงาน</li><li><strong>BACKGROUND </strong>คือการใช้ background thread ในการรับ event ซึ่งไม่ควรทำทำงานอะไรที่เกี่ยวกับ UI</li><li><strong>ASYNC </strong>คือการแยกการทำงานแบบอิสระ ไม่มีการเช็คว่า event ทำงานเสร็จหรือยัง ส่วนใหญ่ใช้กับการทำงานที่มีการรอ</li></ul><pre>@Subscribe(threadMode = ThreadMode.<em>POSTING</em>)<br>fun onMessageEvent(event: MessageEvent) {<br>    // Do something<br>}</pre><h4>Sticky Events</h4><p>หากต้องการให้มีการเก็บ event ไว้ใน memory การ <strong>postSticky </strong>สามารถเก็บ event ไว้ได้</p><pre>EventBus.getDefault().postSticky(MessageEvent())</pre><p>โดยฟังชันก์ที่ subscribe ต้อง set <strong>sticky = true </strong>ไว้ด้วย</p><pre>@Subscribe(sticky = true)<br>fun onMessageEvent(event: MessageEvent) {<br>    // Do something<br>}</pre><p>และยังสามารถ get event จากที่อื่นได้อีกด้วยนะ</p><pre>val event = EventBus.getDefault()<br>    .getStickyEvent(MessageEvent::class.<em>java</em>)</pre><h4>ทำไมถึงต้องใช้ EventBus</h4><p>โดยทั่วไปแล้ว นักพัฒนาส่วนใหญ่จะไม่แนะนำให้ใช้ <strong>EventBus</strong> กันเพราะหากเป็น project ใหญ่ๆ มี event เยอะๆก็จะทำให้ไล่อ่านโค้ดหรือลำดับการทำงานได้ยาก นอกจากนี้ยังทำ test ไม่ได้อีกด้วย</p><p>แต่ด้วยความเรียบง่ายของตัว <strong>EventBus</strong> จึงทำให้หลายๆทีมเลือกนำมาใช้เพื่อลดเวลาในการ implement ลง</p><p>เช่น หากมี push notification มาแล้ว แอพจะต้องแสดง badge ที่ icon กระดิ่งภายในแอพ(ลองนึกถึง facebook) ถ้าเป็นท่าปกติอาจจะต้องใช้ <strong>Broadcast receiver</strong> ซึ่งก็มีการ implement ที่ยุ่งยากและต้องส่งผ่าน intent อีกด้วย แต่ถ้ามาใช้ <strong>EventBus</strong> ก็จะสามารถลดความ complex ของโค้ดลงได้</p><h4>ข้อควรระวัง</h4><ul><li>หากมีการ <strong>register</strong> ซ้ำซ้อนกันจะทำให้ <strong>application crash</strong> ได้โดยอาจเกิดจากการไม่เข้าใจ <strong>lifecycle</strong></li><li>หากมีการ <strong>register</strong> แต่ไม่มี <strong>subscribe function</strong> ในการรับ event ก็จะทำให้ <strong>application crash</strong> ได้เช่นกัน</li><li>ก่อนการ build <strong>release</strong> อย่าลืม set <strong>proguard-rules</strong> ด้วยละ ไม่งั้นรับรองว่าบึ้มแน่นอน</li></ul><p>สุดท้ายหวังว่าบทความนี้จะทำให้เพื่อนๆเข้าใจ <strong>EventBus</strong> มากขึ้น และสามารถนำไปปรับใช้ใน Project จริงๆได้นะ แต่ก็ใช้กันอย่างระมัดระวังด้วยละ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c04ab5fe6e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[มาทำ Android UI Test ด้วย Kakao กันเถอะ]]></title>
            <link>https://medium.com/@vorawut.m/%E0%B8%A1%E0%B8%B2%E0%B8%97%E0%B8%B3-android-ui-test-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-kakao-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-4b6efd901de0?source=rss-4e46b22b300b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b6efd901de0</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[ui-testing]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Vorawut M]]></dc:creator>
            <pubDate>Thu, 28 Jul 2022 22:21:02 GMT</pubDate>
            <atom:updated>2024-03-26T08:27:01.549Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/333/0*94LbfP1UFeaw391X.png" /></figure><p>ก่อนอื่นเลย ต้องบอกก่อนว่านี่เป็นบทความแรกของผู้เขียน หากผิดพลาดประการใดต้องขออภัยด้วยเด้ออออ</p><p>เพื่อนๆ ชาว Android dev น่าจะคุ้นเคยกับการทำ Unit/UI Test กันดีอยู่แล้ว เพื่อให้แน่ใจว่า Business Logic ที่เราทำไปนั้น ได้ผลลัพธ์ที่ถูกต้อง ซึ่งในบทความนี้ จะ focus ที่ UI Test นะครับบ</p><p>เข้าเรื่องกันเลยดีกว่า เพื่อนๆเคยคิดไหมว่า <strong>Espresso</strong> ที่เราใช้ๆกันอยู่นี้ มีความยุ่งยาก หรือ Syntax ที่มีรูปแบบซับซ้อนมากเกินไป วันนี้ผู้เขียนมีตัวช่วยมาแนะนำซึ่งก็คือ <strong>Kakao UI Test</strong> เป็นอีกหนี่งของดีที่ทาง <strong>Agoda</strong> พัฒนาขึ้นมาให้เราใช้กันนั่นเอง</p><p>สามารถเข้าไปดูรายละเอียดได้ที่ <a href="https://github.com/KakaoCup/Kakao"><strong><em>Github</em></strong></a></p><p><strong>Install</strong></p><pre>dependencies {<br>    androidTestImplementation(&quot;io.github.kakaocup:kakao:&lt;latest version&gt;&quot;)<br>}</pre><p>ก่อนที่เราจะทำการ test ต้องเตรียม 2 ส่วนหลักๆ คือ</p><ul><li>สร้าง Screen สำหรับหน้าที่ต้องการทดสอบ ในที่นี้คือการ extend class Screen เพื่อที่จะทดสอบ views ต่างๆที่อยู่ใน Screen นั้นๆต่อไป</li><li>สร้าง View ไว้ใน Screen เพื่อที่จะนำไปทดสอบ ซึ่งการสร้าง view นี่คือการเรียกใช้ KView โดยการอ้างอิงถึง view ที่ต้องการ</li></ul><p><strong>สร้าง Screen</strong></p><pre>object SomeScreen: Screen&lt;SomeScreen&gt;() {}</pre><p><strong>สร้าง View</strong></p><pre>object SomeScreen: Screen&lt;SomeScreen&gt;() {<br>    val title = KTextView { withId(R.id.title) }<br>    val logo = KImageView { withId(R.id.imageLogo) }<br>}</pre><p>Kakao มีการเตรียม view พื้นฐานไว้ให้แล้ว ดังนี้</p><ul><li>KView</li><li>KEditText</li><li>KTextView</li><li>KButton</li><li>KImageView</li><li>KWebView</li><li>KCheckbox</li><li>KViewPager</li><li>KSeekBar</li><li>KSwitch</li><li>and more</li></ul><p>การอ้างอิงถึง view นั้นสามารถทำได้หลายแบบ เช่น</p><ul><li>withId</li><li>withText</li><li>withContentDescription</li><li>withDrawable</li><li>withBackgroundColor</li><li>and more</li></ul><p><strong>มาเริ่มทดสอบกันเถอะ</strong></p><pre>@Test<br>fun displayTest() {<br>    launchActivity&lt;SomeActivity&gt;().use {<br>        SomeScreen {<br>            title {<br>                isDisplayed()<br>                hasText(&quot;Kakao&quot;)<br>            }<br>            logo {<br>                isDisplayed()<br>                hasDrawable(R.drawable.kakao)<br>            }<br>        }<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*dn1unfAIQk9ONObcIAh1hQ.png" /></figure><p><strong>ถ้าเป็น RecyclerView ต้องทำยังไง</strong></p><pre>val recycler = KRecyclerView(<br>    builder = { withId(R.id.recyclerView) },<br>    itemTypeBuilder = { itemType(::Item) }<br>)<br><br>class Item(parent: Matcher&lt;View&gt;): KRecyclerItem&lt;Item&gt;(parent) {<br>    val text = KTextView(parent) { withId(R.id.itemText) }<br>}</pre><p>โดยปกติแล้ว RecyclerView นั้นสามารถมี ViewHolder ได้หลายๆแบบ Kakao เลยมี itemTypeBuilder ไว้ให้เราใส่ itemType หลายๆแบบตามแต่ที่ design ไว้เลย</p><p>จุดสังเกต KView ใน Item มีการใส่ parent เข้าไปด้วย เพราะอาจมีหลายๆ view ที่มี <em>id</em> เหมือนกัน</p><pre>SomeScreen {<br>    /* ... */<br>    recycler {<br>        firstChild&lt;SomeScreen.Item&gt; {<br>            text {<br>                isDisplayed()<br>                hasText(&quot;Android dev&quot;)<br>            }<br>        }<br>    }<br>}</pre><p>การระบุตำแหน่งของ item ใน Adpater ทำได้ดังนี้</p><ul><li>childAt</li><li>firstChild</li><li>lastChild</li><li>childWith</li></ul><p><strong>แล้ว Custom view ละ</strong></p><p>สร้าง KCustomView ขึ้นมาเพื่อไว้ใช้ทดสอบตัว CustomView ของเรา</p><pre>class KCustomView: KBaseView&lt;KCustomView&gt; {<br>    val header: KTextView<br>    val description: KTextView<br><br>    constructor(function: ViewBuilder.() -&gt; Unit) : super(function) {<br>        header = KTextView {<br>            withId(R.id.header)<br>            isDescendantOfA(function)<br>        }<br>        description = KTextView {<br>            withId(R.id.description)<br>            isDescendantOfA(function)<br>        }<br>    }<br>    constructor(parent: Matcher&lt;View&gt;, function: ViewBuilder.() -&gt; Unit) : super(parent, function) {<br>        header = KTextView(parent) {<br>            withId(R.id.header)<br>            isDescendantOfA(function)<br>        }<br>        description = KTextView(parent) {<br>            withId(R.id.description)<br>            isDescendantOfA(function)<br>        }<br>    }<br>    constructor(parent: DataInteraction, function: ViewBuilder.() -&gt; Unit) : super(parent, function) {<br>        header = KTextView(parent) {<br>            withId(R.id.header)<br>            isDescendantOfA(function)<br>        }<br>        description = KTextView(parent) {<br>            withId(R.id.description)<br>            isDescendantOfA(function)<br>        }<br>    }<br>}</pre><p>นำ KCustomView ไปประกาศไว้ใน Screen</p><pre>val firstCustom = KCustomView { withId(R.id.firstCustom) }<br>val secondCustom = KCustomView { withId(R.id.secondCustom) }</pre><p>เท่านี้ก็นำไปทดสอบได้แล้ววววว</p><pre>firstCustom {<br>    isDisplayed()<br>    header { hasText(&quot;First&quot;) }<br>    description { hasText(&quot;Test&quot;) }<br>}<br>secondCustom {<br>    isDisplayed()<br>    header { hasText(&quot;Second&quot;) }<br>    description { hasText(&quot;Test&quot;) }<br>}</pre><p>นอกจากนี้ เรายังสามารถ Custom actions และ assertions ได้อีกด้วย เพื่อให้ใช้งานกับ Custom view ของเราได้สะดวกมากยิ่งขี้น</p><p>สุดท้ายนี้หวังว่าเพื่อนๆ จะทำ UI Test ได้ง่ายขี้น ส่วนใครที่ยังไม่เคยทำ ก็หวังว่าบทความนี้จะช่วยให้เริ่มต้นได้นะ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b6efd901de0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>