Skip to content

Commit

Permalink
Merge pull request #95 from AkYML/feature/merge_experiments
Browse files Browse the repository at this point in the history
merge from experiments
  • Loading branch information
dkk009 authored Jun 1, 2023
2 parents 56e7235 + 973fb6f commit da9603a
Show file tree
Hide file tree
Showing 45 changed files with 4,480 additions and 674 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: set up JDK 11
- name: set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '17'
distribution: 'temurin'
cache: gradle

- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
- name: Clean and Build with Gradle
run: ./gradlew clean build
1 change: 1 addition & 0 deletions YChartsLib/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@Suppress("DSL_SCOPE_VIOLATION") // scope violation issue: work around suggested from: https://github.com/gradle/gradle/issues/22797
plugins {
id("ycharts.android.library")
id("ycharts.android.library.compose")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package co.yml.charts.barchart

import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.unit.dp
import co.yml.charts.axis.AxisData
import co.yml.charts.common.extensions.getMaxElementInYAxis
import co.yml.charts.common.model.Point
import co.yml.charts.ui.barchart.StackedBarChart
import co.yml.charts.ui.barchart.models.*
import org.junit.Rule
import org.junit.Test

class StackedBarChartTest {
@get:Rule
val composeTestRule = createComposeRule()

private val barSize = 3
private val listSize = 5
private val groupBarData = getGroupBarChartTestData(listSize, barSize)
private val yStepSize = 10

private val xAxisData = AxisData.Builder()
.axisStepSize(30.dp)
.steps(listSize - 1)
.startDrawPadding(48.dp)
.labelData { index -> "C $index" }
.build()

private val yAxisData = AxisData.Builder()
.steps(yStepSize)
.labelAndAxisLinePadding(20.dp)
.axisOffset(20.dp)
.labelData { index ->
val valueList = mutableListOf<Float>()
groupBarData.map { groupBar ->
var yMax = 0f
groupBar.barList.forEach {
yMax += it.point.y
}
valueList.add(yMax)
}
val maxElementInYAxis = getMaxElementInYAxis(valueList.maxOrNull() ?: 0f, yStepSize)

(index * (maxElementInYAxis / yStepSize)).toString()
}
.topPadding(36.dp)
.build()

private val colorPaletteList = listOf(Color.Black, Color.Yellow, Color.Blue)
private val groupBarPlotData = BarPlotData(
groupBarList = groupBarData,
barStyle = BarStyle(
barWidth = 35.dp,
selectionHighlightData = SelectionHighlightData(
isHighlightFullBar = true,
groupBarPopUpLabel = { name, value ->
"Name"
}
)
),
barColorPaletteList = colorPaletteList
)
private val groupBarChartData = GroupBarChartData(
barPlotData = groupBarPlotData,
xAxisData = xAxisData,
yAxisData = yAxisData,
paddingBetweenStackedBars = 4.dp
)

private fun startStackedBarChart(isTalkBackEnabled: Boolean = false) {
composeTestRule.setContent {
StackedBarChart(
modifier = Modifier,
groupBarChartData = groupBarChartData,
isTalkBackEnabled
)
}
}

@Test
fun assertIfStackedBarChartIsDisplayed() {

startStackedBarChart()
composeTestRule.onNodeWithTag("stacked_bar_chart", useUnmergedTree = true)
.assertIsDisplayed()
}

@Test
fun assertIfCanvasIsDisplayed() {
startStackedBarChart()

composeTestRule.onNodeWithTag("chart_canvas", useUnmergedTree = true)
.assertExists()
}

@Test
fun assertIfXAxisIsDisplayed() {
startStackedBarChart()

composeTestRule.onNodeWithTag("x_axis", useUnmergedTree = true)
.assertExists()
}

@Test
fun assertIfYAxisIsDisplayed() {
startStackedBarChart()

composeTestRule.onNodeWithTag("y_axis", useUnmergedTree = true)
.assertExists()
}


@Test
fun assertIfStackedBarClicked() {
startStackedBarChart()

composeTestRule.onNode(hasAnyChild(hasClickAction()), useUnmergedTree = true)
.onChildren()
.onFirst()
.performClick()
}

@Test
fun assertIfStackedBarChartHasCorrectBarData() {
startStackedBarChart(isTalkBackEnabled = true)

composeTestRule.onNodeWithText("Bar at 0 with label B0 has value 20.00")
.assertExists()
}


@Test
fun assertIfStackedBarHasChartHasCorrectXAxisLabel() {
startStackedBarChart(isTalkBackEnabled = true)

composeTestRule.onNodeWithText("X Axis label C 0")
.assertExists()
}

@Test
fun assertIfStackedBarHasChartHasCorrectPrimaryCategoryData() {
startStackedBarChart(isTalkBackEnabled = true)

composeTestRule.onNodeWithTag("AccessibilityBottomSheet List")
.onChildren()
.assertCountEquals(5)
}

@Test
fun assertIfPrimaryCategoryHasCorrectSecondaryVariableData() {
startStackedBarChart(isTalkBackEnabled = true)

composeTestRule.onNodeWithText("X Axis label C 0")
.onSiblings()
.assertCountEquals(4)

}
}


fun getGroupBarChartTestData(listSize: Int, barSize: Int): List<GroupBar> {
val list = mutableListOf<GroupBar>()
for (index in 0 until listSize) {
val barList = mutableListOf<BarData>()
for (i in 0 until barSize) {
val barValue = (((i + 1) * 10) + ((index + 1) * 10)).toFloat()
barList.add(
BarData(
Point(
index.toFloat(),
barValue
),
label = "B$i",
description = "Bar at $index with label B$i has value ${
String.format(
"%.2f", barValue
)
}"
)
)
}
list.add(GroupBar(index.toString(), barList))
}
return list
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package co.yml.charts.wavechart

import android.graphics.Typeface
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.unit.dp
import co.yml.charts.axis.AxisData
import co.yml.charts.common.extensions.formatToSinglePrecision
import co.yml.charts.common.model.Point
import co.yml.charts.ui.linechart.getMappingPointsToGraph
import co.yml.charts.ui.linechart.model.LineStyle
import co.yml.charts.ui.linechart.model.LineType
import co.yml.charts.ui.linechart.model.SelectionHighlightPoint
import co.yml.charts.ui.linechart.model.SelectionHighlightPopUp
import co.yml.charts.ui.linechart.model.ShadowUnderLine
import co.yml.charts.ui.wavechart.WaveChart
import co.yml.charts.ui.wavechart.model.Wave
import co.yml.charts.ui.wavechart.model.WaveChartData
import co.yml.charts.ui.wavechart.model.WavePlotData
import org.junit.Assert
import org.junit.Rule
import org.junit.Test

class WaveChartTest {
@get:Rule
val composeTestRule = createComposeRule()

private val pointsData = listOf(
Point(0f, 0f),
Point(1f, -3f),
Point(2f, -5f),
Point(3f, -2f),
Point(4f, 2f),
Point(5f, 5f),
Point(6f, 4f),
Point(7f, 0f),
Point(8f, -1f),
Point(9f, -3f),
Point(10f, 0f),
)
private val steps = 10
private val xAxisData = AxisData.Builder()
.axisStepSize(40.dp)
.steps(pointsData.size - 1)
.labelData { i -> i.toString() }
.labelAndAxisLinePadding(15.dp)
.axisLineColor(Color.Red)
.build()
private val yAxisData = AxisData.Builder()
.steps(steps)
.labelData { i ->
val yMin = pointsData.minOf { it.y }
val yMax = pointsData.maxOf { it.y }
val yScale = (yMax - yMin) / steps
((i * yScale) + yMin).formatToSinglePrecision()
}
.axisLineColor(Color.Red)
.labelAndAxisLinePadding(20.dp)
.bottomPadding(15.dp)
.build()
private val data = WaveChartData(
wavePlotData = WavePlotData(
lines = listOf(
Wave(
dataPoints = pointsData,
waveStyle = LineStyle(
lineType = LineType.SmoothCurve(isDotted = true),
color = Color.Green
),
shadowUnderLine = ShadowUnderLine(),
selectionHighlightPoint = SelectionHighlightPoint(
color = Color.Green
),
selectionHighlightPopUp = SelectionHighlightPopUp(
backgroundColor = Color.Black,
backgroundStyle = Stroke(2f),
labelColor = Color.Red,
labelTypeface = Typeface.DEFAULT_BOLD
)
)
)
),
xAxisData = xAxisData,
yAxisData = yAxisData
)

@Test
fun assertIfWaveChartIsDisplayed() {
composeTestRule.setContent {
WaveChart(modifier = Modifier, waveChartData = data)
}
composeTestRule.onNodeWithTag("wave_chart").assertIsDisplayed()
}

@Test
fun assertIfScrollableContainerIsDisplayed() {
composeTestRule.setContent {
WaveChart(modifier = Modifier, waveChartData = data)
}
composeTestRule.onNodeWithTag("scrollable_container").assertIsDisplayed()
}

@Test
fun getMappingPointsToGraphTest() {
val mappedPoints = getMappingPointsToGraph(
pointsData, 0f, 1f, 0f, 0f, 0f, 10f, 1f
)
val expectedResult = mutableListOf<Offset>()
pointsData.forEach {
expectedResult.add(Offset(it.x, (10f - it.y)))
}
Assert.assertEquals(expectedResult, mappedPoints)
}

}
2 changes: 1 addition & 1 deletion YChartsLib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="co.yml.charts.components"/>
<manifest/>
Loading

0 comments on commit da9603a

Please sign in to comment.