AndroidTest for YBKChart - YunByungKwan/YBKChart GitHub Wiki

I did AndroidTest on my device through helper functions as below.

Testing library: Espresso

Test Case (19/34)

Test name Result
fun should_be_divided_into_1_equally() X
fun should_be_divided_into_5_equally() X
fun should_be_divided_into_10_equally() X
fun should_be_divided_into_100_equally() X
fun should_be_divided_into_1000_equally() X
fun should_turn_red_PieChart_top_face() O
fun should_turn_blue_PieChart_top_face() O
fun should_turn_green_PieChart_top_face() O
fun should_turn_red_PieChart_side_face() O
fun should_turn_blue_PieChart_side_face() O
fun should_turn_green_PieChart_side_face() O
fun should_be_1_PieChart_border_width() O
fun should_be_5_PieChart_border_width() O
fun should_be_10_PieChart_border_width() O
fun should_be_1000_PieChart_border_width() X
fun should_be_throw_exception_
when_PieChart_border_width_set_negative()
X
fun should_turn_red_PieChart_border() O
fun should_turn_green_PieChart_border() O
fun should_turn_blue_PieChart_border() O
fun should_be_changed_1_text_size() O
fun should_be_changed_5_text_size() O
fun should_be_changed_10_text_size() O
fun should_be_changed_20_text_size() O
fun should_be_changed_100_text_size() X
fun should_be_changed_1000_text_size() X
fun should_turn_red_text_color() O
fun should_turn_green_text_color() O
fun should_turn_blue_text_color() O
fun should_be_1_PieChart_height() X
fun should_be_5_PieChart_height() X
fun should_be_10_PieChart_height() X
fun should_be_100_PieChart_height() X
fun should_be_1000_PieChart_height() X
fun should_be_10000_PieChart_height() X
Test code

@RunWith(AndroidJUnit4::class)
class MainActivityTest {

    @get:Rule
    val intentsTestRule = ActivityScenarioRule(MainActivity::class.java)

    /** Test setItems() */
    @Test
    fun should_be_divided_into_1_equally() {
        val items = mutableListOf<Item>()
        for(i in 1..1) {
            items.add(Item("", 1F))
        }
        onView(withId(R.id.pieChart)).perform(setItems(items))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_divided_into_5_equally() {
        val items = mutableListOf<Item>()
        for(i in 1..5) {
            items.add(Item("", 1F))
        }
        onView(withId(R.id.pieChart)).perform(setItems(items))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_divided_into_10_equally() {
        val items = mutableListOf<Item>()
        for(i in 1..10) {
            items.add(Item("", 1F))
        }
        onView(withId(R.id.pieChart)).perform(setItems(items))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_divided_into_100_equally() {
        val items = mutableListOf<Item>()
        for(i in 1..100) {
            items.add(Item("", 1F))
        }
        onView(withId(R.id.pieChart)).perform(setItems(items))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_divided_into_1000_equally() {
        val items = mutableListOf<Item>()
        for(i in 1..1000) {
            items.add(Item("", 1F))
        }
        onView(withId(R.id.pieChart)).perform(setItems(items))
        Thread.sleep(1000)
    }

    /** Test setCircleColor() */
    @Test
    fun should_turn_red_PieChart_top_face() {
        onView(withId(R.id.pieChart)).perform(setCircleColor(Color.RED))
        Thread.sleep(1000)
    }

    @Test
    fun should_turn_blue_PieChart_top_face() {
        onView(withId(R.id.pieChart)).perform(setCircleColor(Color.BLUE))
        Thread.sleep(1000)
    }

    @Test
    fun should_turn_green_PieChart_top_face() {
        onView(withId(R.id.pieChart)).perform(setCircleColor(Color.GREEN))
        Thread.sleep(1000)
    }

    /** Test setSideColor() */
    @Test
    fun should_turn_red_PieChart_side_face() {
        onView(withId(R.id.pieChart)).perform(setSideColor(Color.RED))
        Thread.sleep(1000)
    }

    @Test
    fun should_turn_blue_PieChart_side_face() {
        onView(withId(R.id.pieChart)).perform(setSideColor(Color.BLUE))
        Thread.sleep(1000)
    }

    @Test
    fun should_turn_green_PieChart_side_face() {
        onView(withId(R.id.pieChart)).perform(setSideColor(Color.GREEN))
        Thread.sleep(1000)
    }

    /** Test setBorderWidth() */
    @Test
    fun should_be_1_PieChart_border_width() {
        onView(withId(R.id.pieChart)).perform(setBorderWidth(1F))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_5_PieChart_border_width() {
        onView(withId(R.id.pieChart)).perform(setBorderWidth(5F))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_10_PieChart_border_width() {
        onView(withId(R.id.pieChart)).perform(setBorderWidth(10F))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_1000_PieChart_border_width() {
        onView(withId(R.id.pieChart)).perform(setBorderWidth(1000F))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_throw_exception_when_PieChart_border_width_set_negative() {
        onView(withId(R.id.pieChart)).perform(setBorderWidth(-1F))
        Thread.sleep(1000)
    }
    
    /** Test setBorderColor() */
    @Test
    fun should_turn_red_PieChart_border() {
        onView(withId(R.id.pieChart)).perform(setBorderColor(Color.RED))
        Thread.sleep(1000)
    }

    @Test
    fun should_turn_green_PieChart_border() {
        onView(withId(R.id.pieChart)).perform(setBorderColor(Color.GREEN))
        Thread.sleep(1000)
    }

    @Test
    fun should_turn_blue_PieChart_border() {
        onView(withId(R.id.pieChart)).perform(setBorderColor(Color.BLUE))
        Thread.sleep(1000)
    }

    /** Test setItemTextSize() */
    @Test
    fun should_be_changed_1_text_size() {
        onView(withId(R.id.pieChart)).perform(setItemTextSize(1F))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_changed_5_text_size() {
        onView(withId(R.id.pieChart)).perform(setItemTextSize(5F))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_changed_10_text_size() {
        onView(withId(R.id.pieChart)).perform(setItemTextSize(10F))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_changed_20_text_size() {
        onView(withId(R.id.pieChart)).perform(setItemTextSize(20F))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_changed_100_text_size() {
        onView(withId(R.id.pieChart)).perform(setItemTextSize(100F))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_changed_1000_text_size() {
        onView(withId(R.id.pieChart)).perform(setItemTextSize(1000F))
        Thread.sleep(1000)
    }

    /** Test setItemTextColor() */
    @Test
    fun should_turn_red_text_color() {
        onView(withId(R.id.pieChart)).perform(setItemTextColor(Color.RED))
        Thread.sleep(1000)
    }

    @Test
    fun should_turn_green_text_color() {
        onView(withId(R.id.pieChart)).perform(setItemTextColor(Color.GREEN))
        Thread.sleep(1000)
    }

    @Test
    fun should_turn_blue_text_color() {
        onView(withId(R.id.pieChart)).perform(setItemTextColor(Color.BLUE))
        Thread.sleep(1000)
    }

    /** Test setHeight() */
    @Test
    fun should_be_1_PieChart_height() {
        onView(withId(R.id.pieChart)).perform(setHeight(1F))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_5_PieChart_height() {
        onView(withId(R.id.pieChart)).perform(setHeight(5F))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_10_PieChart_height() {
        onView(withId(R.id.pieChart)).perform(setHeight(10F))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_100_PieChart_height() {
        onView(withId(R.id.pieChart)).perform(setHeight(100F))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_1000_PieChart_height() {
        onView(withId(R.id.pieChart)).perform(setHeight(1000F))
        Thread.sleep(1000)
    }

    @Test
    fun should_be_10000_PieChart_height() {
        onView(withId(R.id.pieChart)).perform(setHeight(10000F))
        Thread.sleep(1000)
    }

    /**================================= Helper function =========================================*/
    private fun setItems(list: MutableList<Item>): ViewAction {
        return object : ViewAction {
            override fun getConstraints(): Matcher<View> {
                return CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.isAssignableFrom(
                    com.kwancorp.ybkchart.piechart.PieChart::class.java))
            }

            override fun perform(uiController: UiController, view: View) {
                (view as com.kwancorp.ybkchart.piechart.PieChart).setItems(list)
            }

            override fun getDescription(): String {
                return "setItems"
            }
        }
    }

    private fun setCircleColor(color: Int): ViewAction {
        return object : ViewAction {
            override fun getConstraints(): Matcher<View> {
                return CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.isAssignableFrom(
                    com.kwancorp.ybkchart.piechart.PieChart::class.java))
            }

            override fun perform(uiController: UiController, view: View) {
                (view as com.kwancorp.ybkchart.piechart.PieChart).setCircleColor(color)
            }

            override fun getDescription(): String {
                return "setCircleColor"
            }
        }
    }

    private fun setSideColor(color: Int): ViewAction {
        return object : ViewAction {
            override fun getConstraints(): Matcher<View> {
                return CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.isAssignableFrom(
                    com.kwancorp.ybkchart.piechart.PieChart::class.java))
            }

            override fun perform(uiController: UiController, view: View) {
                (view as com.kwancorp.ybkchart.piechart.PieChart).setSideColor(color)
            }

            override fun getDescription(): String {
                return "setSideColor"
            }
        }
    }

    private fun setBorderWidth(stroke: Float): ViewAction {
        return object : ViewAction {
            override fun getConstraints(): Matcher<View> {
                return CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.isAssignableFrom(
                    com.kwancorp.ybkchart.piechart.PieChart::class.java))
            }

            override fun perform(uiController: UiController, view: View) {
                (view as com.kwancorp.ybkchart.piechart.PieChart).setBorderWidth(stroke)
            }

            override fun getDescription(): String {
                return "setBorderWidth"
            }
        }
    }

    private fun setBorderColor(color: Int): ViewAction {
        return object : ViewAction {
            override fun getConstraints(): Matcher<View> {
                return CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.isAssignableFrom(
                    com.kwancorp.ybkchart.piechart.PieChart::class.java))
            }

            override fun perform(uiController: UiController, view: View) {
                (view as com.kwancorp.ybkchart.piechart.PieChart).setBorderColor(color)
            }

            override fun getDescription(): String {
                return "setBorderColor"
            }
        }
    }

    private fun setItemTextSize(size: Float): ViewAction {
        return object : ViewAction {
            override fun getConstraints(): Matcher<View> {
                return CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.isAssignableFrom(
                    com.kwancorp.ybkchart.piechart.PieChart::class.java))
            }

            override fun perform(uiController: UiController, view: View) {
                (view as com.kwancorp.ybkchart.piechart.PieChart).setItemTextSize(size)
            }

            override fun getDescription(): String {
                return "setItemTextSize"
            }
        }
    }

    private fun setItemTextColor(color: Int): ViewAction {
        return object : ViewAction {
            override fun getConstraints(): Matcher<View> {
                return CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.isAssignableFrom(
                    com.kwancorp.ybkchart.piechart.PieChart::class.java))
            }

            override fun perform(uiController: UiController, view: View) {
                (view as com.kwancorp.ybkchart.piechart.PieChart).setItemTextColor(color)
            }

            override fun getDescription(): String {
                return "setItemTextColor"
            }
        }
    }

    private fun setHeight(height: Float): ViewAction {
        return object : ViewAction {
            override fun getConstraints(): Matcher<View> {
                return CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.isAssignableFrom(
                    com.kwancorp.ybkchart.piechart.PieChart::class.java))
            }

            override fun perform(uiController: UiController, view: View) {
                (view as com.kwancorp.ybkchart.piechart.PieChart).setHeight(height)
            }

            override fun getDescription(): String {
                return "setHeight"
            }
        }
    }
}

⚠️ **GitHub.com Fallback** ⚠️