Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to MDC theme interop module #1

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 82 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on: [push, pull_request]

jobs:
build:
# Skip build if head commit contains 'skip ci'
if: "!contains(github.event.head_commit.message, 'skip ci')"

runs-on: ubuntu-latest
env:
JAVA_TOOL_OPTIONS: -Xmx5120m
Expand All @@ -27,16 +30,50 @@ jobs:
- uses: actions/cache@v1
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('checksum.txt') }}
key: gradle-${{ hashFiles('checksum.txt') }}

- name: Build, lint and spotless
run: ./gradlew spotlessCheck build lintDebug --scan
run: ./gradlew spotlessCheck assemble lintDebug --scan

- name: Deploy to Sonatype
run: ./gradlew uploadArchives --no-parallel
env:
SONATYPE_NEXUS_USERNAME: ${{ secrets.SONATYPE_NEXUS_USERNAME }}
SONATYPE_NEXUS_PASSWORD: ${{ secrets.SONATYPE_NEXUS_PASSWORD }}
- name: Clean secrets
if: always()
run: release/signing-cleanup.sh

test:
runs-on: macOS-latest
needs: build
strategy:
matrix:
api-level: [21, 23, 26, 29]
env:
JAVA_TOOL_OPTIONS: -Xmx3072m
GRADLE_OPTS: -Dorg.gradle.daemon=false -Dorg.gradle.workers.max=2 -Dkotlin.incremental=false -Dkotlin.compiler.execution.strategy=in-process
TERM: dumb

steps:
- uses: actions/checkout@v2

- name: set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11

- name: Decrypt secrets
run: release/signing-setup.sh ${{ secrets.ENCRYPT_KEY }}

- name: Generate cache key
run: ./checksum.sh checksum.txt

- uses: actions/cache@v1
with:
path: ~/.gradle/caches
key: gradle-${{ hashFiles('checksum.txt') }}

- name: Run tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: ${{ matrix.api-level }}
script: ./gradlew connectedCheck

- name: Clean secrets
if: always()
Expand All @@ -47,10 +84,47 @@ jobs:
run: |
mkdir -p junit
find . -type f -regex ".*/build/test-results/.*xml" -exec cp {} junit/ \;

- name: Upload test results
if: always()
uses: actions/upload-artifact@v1
with:
name: junit-results
path: junit

deploy:
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
needs: [build, test]
env:
JAVA_TOOL_OPTIONS: -Xmx5120m
GRADLE_OPTS: -Dorg.gradle.daemon=false -Dorg.gradle.workers.max=2 -Dkotlin.incremental=false -Dkotlin.compiler.execution.strategy=in-process
TERM: dumb

steps:
- uses: actions/checkout@v2

- name: set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11

- name: Decrypt secrets
run: release/signing-setup.sh ${{ secrets.ENCRYPT_KEY }}

- name: Generate cache key
run: ./checksum.sh checksum.txt

- uses: actions/cache@v1
with:
path: ~/.gradle/caches
key: gradle-${{ hashFiles('checksum.txt') }}

- name: Deploy to Sonatype
run: ./gradlew uploadArchives --no-parallel
env:
SONATYPE_NEXUS_USERNAME: ${{ secrets.SONATYPE_NEXUS_USERNAME }}
SONATYPE_NEXUS_PASSWORD: ${{ secrets.SONATYPE_NEXUS_PASSWORD }}

- name: Clean secrets
if: always()
run: release/signing-cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@ object Libs {

const val gradleMavenPublishPlugin = "com.vanniktech:gradle-maven-publish-plugin:0.11.1"

const val junit = "junit:junit:4.12"
const val junit = "junit:junit:4.13"

object Kotlin {
private const val version = "1.3.70"
private const val version = "1.3.72"
const val stdlib = "org.jetbrains.kotlin:kotlin-stdlib:$version"
const val gradlePlugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:$version"
}

object AndroidX {
object Test {
private const val version = "1.2.0"
const val core = "androidx.test:core:$version"
const val runner = "androidx.test:runner:$version"
const val rules = "androidx.test:rules:$version"

Expand All @@ -58,6 +57,9 @@ object Libs {
const val animation = "androidx.ui:ui-animation:$version"
const val tooling = "androidx.ui:ui-tooling:$version"
const val livedata = "androidx.ui:ui-livedata:$version"

const val platform = "androidx.ui:ui-platform:$version"
const val test = "androidx.ui:ui-test:$version"
}

const val core = "androidx.core:core:1.2.0"
Expand Down
9 changes: 5 additions & 4 deletions mdc-theme/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ android {
lintOptions {
textReport true
textOutput 'stdout'
// We run a full lint analysis as build part in CI, so skip vital checks for assemble tasks.
// We run a full lint analysis as build part in CI, so skip vital checks for assemble tasks
checkReleaseBuilds false
}

Expand All @@ -66,12 +66,13 @@ dependencies {
implementation Libs.AndroidX.UI.composeRuntime
implementation Libs.AndroidX.UI.material
implementation Libs.mdc
implementation Libs.Kotlin.stdlib

androidTestImplementation Libs.Kotlin.stdlib
androidTestImplementation Libs.junit
androidTestImplementation Libs.AndroidX.Test.core
androidTestImplementation Libs.AndroidX.Test.ext
androidTestImplementation Libs.AndroidX.UI.test
androidTestImplementation Libs.AndroidX.UI.platform
androidTestImplementation Libs.AndroidX.Test.rules
androidTestImplementation Libs.AndroidX.Test.runner
}

apply plugin: "com.vanniktech.maven.publish"
8 changes: 5 additions & 3 deletions mdc-theme/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.chrisbanes.accompanist">
package="dev.chrisbanes.accompanist.mdctheme">

<application>

<activity
android:name="android.app.Activity"
android:theme="@android:style/Theme.DeviceDefault.NoActionBar" />
android:name=".MdcActivity"
android:theme="@style/Theme.MdcThemeTest" />

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.chrisbanes.accompanist.mdctheme

import androidx.test.filters.MediumTest
import androidx.ui.core.DensityAmbient
import androidx.ui.foundation.shape.corner.CornerSize
import androidx.ui.foundation.shape.corner.CutCornerShape
import androidx.ui.foundation.shape.corner.RoundedCornerShape
import androidx.ui.material.MaterialTheme
import androidx.ui.res.colorResource
import androidx.ui.test.android.AndroidComposeTestRule
import androidx.ui.text.font.asFontFamily
import androidx.ui.text.font.font
import androidx.ui.unit.Density
import androidx.ui.unit.Dp
import androidx.ui.unit.Px
import androidx.ui.unit.PxSize
import androidx.ui.unit.TextUnit
import androidx.ui.unit.dp
import androidx.ui.unit.em
import androidx.ui.unit.px
import androidx.ui.unit.sp
import dev.chrisbanes.accompanist.mdctheme.test.R
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@MediumTest
@RunWith(JUnit4::class)
class MaterialThemeTest {
@get:Rule
val composeTestRule = AndroidComposeTestRule<MdcActivity>()

@Test
fun colors() = composeTestRule.setContent {
MaterialThemeFromAndroidMdcTheme {
val color = MaterialTheme.colors

assertEquals(colorResource(R.color.Aquamarine), color.primary)
assertEquals(colorResource(R.color.RoyalBlue), color.primaryVariant)
assertEquals(colorResource(R.color.MidnightBlue), color.onPrimary)

assertEquals(colorResource(R.color.DarkGoldenrod), color.secondary)
assertEquals(colorResource(R.color.BlueViolet), color.secondaryVariant)
assertEquals(colorResource(R.color.SlateGray), color.onSecondary)

assertEquals(colorResource(R.color.MediumSpringGreen), color.surface)
assertEquals(colorResource(R.color.Navy), color.onSurface)

assertEquals(colorResource(R.color.DarkSalmon), color.error)
assertEquals(colorResource(R.color.Beige), color.onError)

assertEquals(colorResource(R.color.IndianRed), color.background)
assertEquals(colorResource(R.color.Orchid), color.onBackground)
}
}

@Test
fun shapes() = composeTestRule.setContent {
MaterialThemeFromAndroidMdcTheme {
val shapes = MaterialTheme.shapes
val density = DensityAmbient.current

shapes.small.run {
assertTrue(this is CutCornerShape)
assertEquals(4.px, topLeft.toPx(density))
assertEquals(9.dp.scaleToPx(density), topRight.toPx(density))
assertEquals(5.px, bottomRight.toPx(density))
assertEquals(3.dp.scaleToPx(density), bottomLeft.toPx(density))
}
shapes.medium.run {
assertTrue(this is RoundedCornerShape)
assertEquals(12.dp.scaleToPx(density), topLeft.toPx(density))
assertEquals(12.dp.scaleToPx(density), topRight.toPx(density))
assertEquals(12.dp.scaleToPx(density), bottomRight.toPx(density))
assertEquals(12.dp.scaleToPx(density), bottomLeft.toPx(density))
}
shapes.large.run {
assertTrue(this is CutCornerShape)
assertEquals(0.px, topLeft.toPx(density))
assertEquals(0.px, topRight.toPx(density))
assertEquals(0.px, bottomRight.toPx(density))
assertEquals(0.px, bottomLeft.toPx(density))
}
}
}

@Test
fun type() = composeTestRule.setContent {
MaterialThemeFromAndroidMdcTheme {
val typography = MaterialTheme.typography
val density = DensityAmbient.current

val rubik300 = font(R.font.rubik_300).asFontFamily()
val rubik400 = font(R.font.rubik_400).asFontFamily()
val rubik500 = font(R.font.rubik_500).asFontFamily()

typography.h1.run {
assertTextUnitEquals(97.54.sp, fontSize, density)
assertTextUnitEquals((-0.0015).em, letterSpacing, density)
assertEquals(rubik300, fontFamily)
}

assertNotNull(typography.h2.shadow)
typography.h2.shadow!!.run {
assertEquals(colorResource(R.color.OliveDrab), color)
assertEquals(4.43f, offset.dx)
assertEquals(8.19f, offset.dy)
assertEquals(2.13.px, blurRadius)
}

typography.body1.run {
assertTextUnitEquals(16.26.sp, fontSize, density)
assertTextUnitEquals(0.005.em, letterSpacing, density)
assertEquals(rubik400, fontFamily)
assertNull(shadow)
}

typography.button.run {
assertEquals(rubik500, fontFamily)
}
}
}
}

private fun Dp.scaleToPx(density: Density): Px {
val dp = this
return with(density) { dp.toPx() }
}

private fun assertTextUnitEquals(expected: TextUnit, actual: TextUnit, density: Density) {
if (expected.javaClass == actual.javaClass) {
// If the expected and actual are the same type, compare the raw values with a
// delta to account for float inaccuracy
assertEquals(expected.value, actual.value, 0.001f)
} else {
// Otherwise we need to flatten to a px to compare the values. Again using a
// delta to account for float inaccuracy
with(density) { assertEquals(expected.toPx().value, actual.toPx().value, 0.001f) }
}
}

private fun CornerSize.toPx(density: Density) = toPx(PxSize.Companion.UnspecifiedSize, density)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.chrisbanes.accompanist.mdctheme

import androidx.appcompat.app.AppCompatActivity

class MdcActivity : AppCompatActivity()
Binary file added mdc-theme/src/androidTest/res/font/rubik_300.ttf
Binary file not shown.
Binary file added mdc-theme/src/androidTest/res/font/rubik_400.ttf
Binary file not shown.
Binary file added mdc-theme/src/androidTest/res/font/rubik_500.ttf
Binary file not shown.
Binary file added mdc-theme/src/androidTest/res/font/rubik_700.ttf
Binary file not shown.
Loading