diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..79edaf9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +*.pyc +*/build/ +.gradle +/repo/ +/build +repo +examples/**/build +examples/**/.idea/* +.idea/* +!.idea/codeStyleSettings.xml +*.iml +local.properties +plugin/src/generated diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..b8db41c --- /dev/null +++ b/build.gradle @@ -0,0 +1,90 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * 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 + * + * http://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. + */ + +buildscript { + apply from: rootProject.file('versions.gradle') + repositories { + mavenLocal() + jcenter() + google() + } + + dependencies { + classpath plugs.agp + classpath plugs.screenshot + classpath plugs.kotlin + classpath "io.screenshotbot:plugin:0.1.0" + } +} + +allprojects { + repositories { + mavenLocal() + jcenter() + google() + } +} + +apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' +apply plugin: 'kotlin-kapt' +apply plugin: 'com.facebook.testing.screenshot' +apply plugin: 'io.screenshotbot.plugin' + +android { + compileSdkVersion versions.compileSdk + defaultConfig { + applicationId "com.facebook.testing.screenshot.example" + minSdkVersion 15 + targetSdkVersion versions.targetSdk + versionCode 1 + versionName "1.0" + testInstrumentationRunner "com.facebook.testing.screenshot.sample.ScreenshotTestRunner" + } +} + +dependencies { + implementation deps.kotlinStdlib + + implementation deps.supportAppCompat + implementation deps.supportDesign + + implementation deps.lithoCore + implementation deps.lithoWidget + implementation deps.lithoAnnotations + kapt deps.lithoProcessor + + implementation deps.soLoader + + androidTestImplementation deps.screenshotTestCommon + androidTestImplementation deps.screenshotTestLitho + androidTestImplementation deps.junit + androidTestImplementation(deps.espresso) { + exclude group: 'com.android.support', module: 'support-annotations' + exclude group: 'com.google.code.findbugs', module: 'jsr305' + } + androidTestImplementation deps.androidTestRules +} + +screenshots { + multipleDevices true +} + +screenshotbot { + channelName "this-is-a-channel" + githubRepo "https://github.com/facebook/screenshot-tests-for-android" +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..1dcb8f5 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,3 @@ +org.gradle.configureondemand=true +android.useAndroidX = true +android.enableJetifier = false diff --git a/gradle/gradle-mvn-push.gradle b/gradle/gradle-mvn-push.gradle new file mode 100644 index 0000000..aa0a132 --- /dev/null +++ b/gradle/gradle-mvn-push.gradle @@ -0,0 +1,139 @@ +/* + * Copyright 2013 Chris Banes + * + * 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 + * + * http://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. + */ + +apply plugin: 'maven' +apply plugin: 'signing' + +def isReleaseBuild() { + return !VERSION_NAME.contains("SNAPSHOT") +} + +def getRepositoryUsername() { + return findProperty('repositoryUsername') ? property('repositoryUsername') : "" +} + +def getRepositoryPassword() { + return findProperty('repositoryPassword') ? property('repositoryPassword') : "" +} + +def configurePom(pom) { + pom.groupId = GROUP + pom.artifactId = POM_ARTIFACT_ID + pom.version = VERSION_NAME + + pom.project { + name POM_NAME + packaging POM_PACKAGING + description POM_DESCRIPTION + url POM_URL + + scm { + url POM_SCM_URL + connection POM_SCM_CONNECTION + developerConnection POM_SCM_DEV_CONNECTION + } + + licenses { + license { + name POM_LICENCE_NAME + url POM_LICENCE_URL + distribution POM_LICENCE_DIST + } + } + + developers { + developer { + id POM_DEVELOPER_ID + name POM_DEVELOPER_NAME + } + } + } +} + +afterEvaluate { project -> + uploadArchives { + repositories { + mavenDeployer { + beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } + + repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { + authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) + } + snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { + authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) + } + + configurePom(pom) + } + } + } + + task installArchives(type: Upload) { + configuration = configurations.archives + repositories { + mavenDeployer { + repository url: "file://${System.properties['user.home']}/.m2/repository" + configurePom(pom) + } + } + } + + signing { + required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") } + sign configurations.archives + } + + if (property('POM_PACKAGING') == 'aar') { + task androidJavadocs(type: Javadoc) { + source = android.sourceSets.main.java.srcDirs + classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) + exclude "**/BUCK" + if (JavaVersion.current().isJava8Compatible()) { + options.addStringOption('Xdoclint:none', '-quiet') + } + } + + task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) { + classifier = 'javadoc' + from androidJavadocs.destinationDir + } + + task androidSourcesJar(type: Jar) { + classifier = 'sources' + from android.sourceSets.main.java.sourceFiles + } + + artifacts { + archives androidSourcesJar + archives androidJavadocsJar + } + } else { + task sourcesJar(type: Jar, dependsOn: classes) { + classifier = 'sources' + from sourceSets.main.allSource + } + + task javadocJar(type: Jar, dependsOn: javadoc) { + classifier = 'javadoc' + from javadoc.destinationDir + } + + artifacts { + archives sourcesJar + archives javadocJar + } + } +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..5c2d1cf Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..4a6ebce --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.1-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..83f2acf --- /dev/null +++ b/gradlew @@ -0,0 +1,188 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# 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. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.ExampleScreenshotTest_testDefault.png b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.ExampleScreenshotTest_testDefault.png new file mode 100644 index 0000000..9708669 Binary files /dev/null and b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.ExampleScreenshotTest_testDefault.png differ diff --git a/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.ImageRowScreenshotTest_testDefault.png b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.ImageRowScreenshotTest_testDefault.png new file mode 100644 index 0000000..bf4d126 Binary files /dev/null and b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.ImageRowScreenshotTest_testDefault.png differ diff --git a/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_errorTextShouldBeRed.png b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_errorTextShouldBeRed.png new file mode 100644 index 0000000..3702727 Binary files /dev/null and b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_errorTextShouldBeRed.png differ diff --git a/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_mainActivityTestSettingsOpen.png b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_mainActivityTestSettingsOpen.png new file mode 100644 index 0000000..d5baa70 Binary files /dev/null and b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_mainActivityTestSettingsOpen.png differ diff --git a/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_okTextShouldBeGreen.png b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_okTextShouldBeGreen.png new file mode 100644 index 0000000..b4d7719 Binary files /dev/null and b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_okTextShouldBeGreen.png differ diff --git a/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_testScreenshotEntireActivity.png b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_testScreenshotEntireActivity.png new file mode 100644 index 0000000..b42e58a Binary files /dev/null and b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_testScreenshotEntireActivity.png differ diff --git a/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_testScreenshotEntireActivityWithoutAccessibilityMetadata.png b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_testScreenshotEntireActivityWithoutAccessibilityMetadata.png new file mode 100644 index 0000000..b42e58a Binary files /dev/null and b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_testScreenshotEntireActivityWithoutAccessibilityMetadata.png differ diff --git a/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_warningTextShouldBeYellow.png b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_warningTextShouldBeYellow.png new file mode 100644 index 0000000..956f608 Binary files /dev/null and b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.MainActivityTest_warningTextShouldBeYellow.png differ diff --git a/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.StandardAndroidViewTest_testChinese.png b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.StandardAndroidViewTest_testChinese.png new file mode 100644 index 0000000..1daad0f Binary files /dev/null and b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.StandardAndroidViewTest_testChinese.png differ diff --git a/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.StandardAndroidViewTest_testLongText.png b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.StandardAndroidViewTest_testLongText.png new file mode 100644 index 0000000..ea869d3 Binary files /dev/null and b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.StandardAndroidViewTest_testLongText.png differ diff --git a/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.StandardAndroidViewTest_testRendering.png b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.StandardAndroidViewTest_testRendering.png new file mode 100644 index 0000000..f9802db Binary files /dev/null and b/screenshots/API_25_GP_XHDPI_768x1280_x86/com.facebook.testing.screenshot.sample.StandardAndroidViewTest_testRendering.png differ diff --git a/screenshots/API_25_GP_XHDPI_768x1280_x86/fab.png b/screenshots/API_25_GP_XHDPI_768x1280_x86/fab.png new file mode 100644 index 0000000..58b32a5 Binary files /dev/null and b/screenshots/API_25_GP_XHDPI_768x1280_x86/fab.png differ diff --git a/src/androidTest/java/com/facebook/testing/screenshot/sample/ExampleScreenshotTest.java b/src/androidTest/java/com/facebook/testing/screenshot/sample/ExampleScreenshotTest.java new file mode 100644 index 0000000..8d37819 --- /dev/null +++ b/src/androidTest/java/com/facebook/testing/screenshot/sample/ExampleScreenshotTest.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * 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 + * + * http://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 com.facebook.testing.screenshot.sample; + +import android.content.Context; +import android.view.LayoutInflater; +import androidx.test.platform.app.InstrumentationRegistry; +import com.facebook.litho.LithoView; +import com.facebook.testing.screenshot.Screenshot; +import com.facebook.testing.screenshot.ViewHelpers; +import org.junit.Test; + +public class ExampleScreenshotTest { + @Test + public void testDefault() { + Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + LayoutInflater inflater = LayoutInflater.from(targetContext); + LithoView view = (LithoView) inflater.inflate(R.layout.litho_view, null, false); + + view.setComponent(Example.create(view.getComponentContext()).build()); + + ViewHelpers.setupView(view).setExactWidthDp(300).layout(); + Screenshot.snap(view).record(); + } +} diff --git a/src/androidTest/java/com/facebook/testing/screenshot/sample/ImageRowScreenshotTest.java b/src/androidTest/java/com/facebook/testing/screenshot/sample/ImageRowScreenshotTest.java new file mode 100644 index 0000000..4ded650 --- /dev/null +++ b/src/androidTest/java/com/facebook/testing/screenshot/sample/ImageRowScreenshotTest.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * 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 + * + * http://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 com.facebook.testing.screenshot.sample; + +import android.content.Context; +import android.view.LayoutInflater; +import androidx.test.platform.app.InstrumentationRegistry; +import com.facebook.litho.LithoView; +import com.facebook.testing.screenshot.Screenshot; +import com.facebook.testing.screenshot.ViewHelpers; +import org.junit.Test; + +public class ImageRowScreenshotTest { + @Test + public void testDefault2() { + Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + LayoutInflater inflater = LayoutInflater.from(targetContext); + LithoView view = (LithoView) inflater.inflate(R.layout.litho_view, null, false); + + view.setComponent(ImageRow.create(view.getComponentContext()).build()); + + ViewHelpers.setupView(view).setExactWidthDp(300).layout(); + Screenshot.snap(view).record(); + } +} diff --git a/src/androidTest/java/com/facebook/testing/screenshot/sample/MainActivityTest.kt b/src/androidTest/java/com/facebook/testing/screenshot/sample/MainActivityTest.kt new file mode 100644 index 0000000..92a5c53 --- /dev/null +++ b/src/androidTest/java/com/facebook/testing/screenshot/sample/MainActivityTest.kt @@ -0,0 +1,87 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * 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 + * + * http://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 com.facebook.testing.screenshot.sample + + +import android.view.View +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu +import androidx.test.espresso.action.ViewActions.click +import androidx.test.espresso.matcher.ViewMatchers.isDisplayed +import androidx.test.espresso.matcher.ViewMatchers.withId +import androidx.test.rule.ActivityTestRule +import com.facebook.testing.screenshot.Screenshot +import org.hamcrest.core.AllOf.allOf +import org.junit.Rule +import org.junit.Test + +class MainActivityTest { + @get:Rule + var activityTestRule = ActivityTestRule(MainActivity::class.java, false, false) + + @Test + fun testScreenshotEntireActivity() { + val activity = activityTestRule.launchActivity(null) + Screenshot.snapActivity(activity).record() + } + + @Test + fun mainActivityTestSettingsOpen() { + val activity = activityTestRule.launchActivity(null) + val floatingActionButton = onView(allOf(withId(R.id.fab), isDisplayed())) + floatingActionButton.perform(click()) + + openActionBarOverflowOrOptionsMenu(activity) + Screenshot.snapActivity(activity).record() + } + + @Test + fun mainActivityTestFabWithEspresso() { + activityTestRule.launchActivity(null) + onView(withId(R.id.fab)).perform(screenshot("fab")) + } + + @Test + fun errorTextShouldBeRed() { + val intent = MainActivity.intent(MainActivity.Status.ERROR) + val activity = activityTestRule.launchActivity(intent) + + Screenshot.snapActivity(activity).record() + } + + @Test + fun warningTextShouldBeYellow() { + val intent = MainActivity.intent(MainActivity.Status.WARNING) + val activity = activityTestRule.launchActivity(intent) + + Screenshot.snapActivity(activity).record() + } + + @Test + fun okTextShouldBeGreen() { + val intent = MainActivity.intent(MainActivity.Status.OK) + val activity = activityTestRule.launchActivity(intent) + + Screenshot.snapActivity(activity).record() + } + + @Test + fun testScreenshotEntireActivityWithoutAccessibilityMetadata() { + val activity = activityTestRule.launchActivity(null) + Screenshot.snapActivity(activity).setIncludeAccessibilityInfo(false).record() + } +} diff --git a/src/androidTest/java/com/facebook/testing/screenshot/sample/ScreenshotTestRunner.kt b/src/androidTest/java/com/facebook/testing/screenshot/sample/ScreenshotTestRunner.kt new file mode 100644 index 0000000..0292303 --- /dev/null +++ b/src/androidTest/java/com/facebook/testing/screenshot/sample/ScreenshotTestRunner.kt @@ -0,0 +1,46 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * 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 + * + * http://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 com.facebook.testing.screenshot.sample + +import android.os.Bundle +import androidx.test.runner.AndroidJUnitRunner +import com.facebook.litho.config.ComponentsConfiguration +import com.facebook.testing.screenshot.ScreenshotRunner +import com.facebook.testing.screenshot.layouthierarchy.LayoutHierarchyDumper +import com.facebook.testing.screenshot.layouthierarchy.litho.LithoAttributePlugin +import com.facebook.testing.screenshot.layouthierarchy.litho.LithoHierarchyPlugin + +class ScreenshotTestRunner : AndroidJUnitRunner() { + companion object { + init { + ComponentsConfiguration.isDebugModeEnabled = true + LayoutHierarchyDumper.addGlobalHierarchyPlugin(LithoHierarchyPlugin.getInstance()) + LayoutHierarchyDumper.addGlobalAttributePlugin(LithoAttributePlugin.getInstance()) + } + } + + override fun onCreate(arguments: Bundle) { + ScreenshotRunner.onCreate(this, arguments) + super.onCreate(arguments) + } + + override fun finish(resultCode: Int, results: Bundle) { + ScreenshotRunner.onDestroy() + super.finish(resultCode, results) + } +} + diff --git a/src/androidTest/java/com/facebook/testing/screenshot/sample/ScreenshotViewAction.kt b/src/androidTest/java/com/facebook/testing/screenshot/sample/ScreenshotViewAction.kt new file mode 100644 index 0000000..9893500 --- /dev/null +++ b/src/androidTest/java/com/facebook/testing/screenshot/sample/ScreenshotViewAction.kt @@ -0,0 +1,43 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * 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 + * + * http://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 com.facebook.testing.screenshot.sample + + +import android.view.View +import androidx.test.espresso.UiController +import androidx.test.espresso.ViewAction +import androidx.test.espresso.matcher.ViewMatchers.isDisplayed +import com.facebook.testing.screenshot.Screenshot +import org.hamcrest.Matcher + +fun screenshot(name: String): ViewAction { + return ScreenshotViewAction(name) +} + +class ScreenshotViewAction internal constructor(private val name: String) : ViewAction { + override fun getConstraints(): Matcher { + return isDisplayed() + } + + override fun getDescription(): String { + return name + } + + override fun perform(uiController: UiController, view: View) { + Screenshot.snap(view).setName(name).record() + } +} diff --git a/src/androidTest/java/com/facebook/testing/screenshot/sample/StandardAndroidViewTest.kt b/src/androidTest/java/com/facebook/testing/screenshot/sample/StandardAndroidViewTest.kt new file mode 100644 index 0000000..dd6e566 --- /dev/null +++ b/src/androidTest/java/com/facebook/testing/screenshot/sample/StandardAndroidViewTest.kt @@ -0,0 +1,72 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * 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 + * + * http://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 com.facebook.testing.screenshot.sample + +import android.view.LayoutInflater +import android.view.View +import android.widget.TextView +import androidx.test.platform.app.InstrumentationRegistry +import com.facebook.testing.screenshot.Screenshot +import com.facebook.testing.screenshot.ViewHelpers +import org.junit.Test + +class StandardAndroidViewTest { + @Test + @Throws(Throwable::class) + fun testRendering() { + val targetContext = InstrumentationRegistry.getInstrumentation().targetContext + val inflater = LayoutInflater.from(targetContext) + val view = inflater.inflate(R.layout.search_bar, null, false) + + ViewHelpers.setupView(view).setExactWidthDp(300).layout() + Screenshot.snap(view).record() + } + + @Test + @Throws(Throwable::class) + fun testLongText() { + val targetContext = InstrumentationRegistry.getInstrumentation().targetContext + val inflater = LayoutInflater.from(targetContext) + val view = inflater.inflate(R.layout.search_bar, null, false) + + val tv = view.findViewById(R.id.search_box) as TextView + + tv.text = "This is a really long text and should overflow" + ViewHelpers.setupView(view).setExactWidthDp(300).layout() + + Screenshot.snap(view).record() + } + + @Test + @Throws(Throwable::class) + fun testChinese() { + val targetContext = InstrumentationRegistry.getInstrumentation().targetContext + val inflater = LayoutInflater.from(targetContext) + val view = inflater.inflate(R.layout.search_bar, null, false) + + val tv = view.findViewById(R.id.search_box) as TextView + val btn = view.findViewById(R.id.button) as TextView + + tv.hint = "搜索世界" + btn.text = "搜" + + ViewHelpers.setupView(view).setExactWidthDp(300).layout() + + Screenshot.snap(view).record() + } +} + diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml new file mode 100644 index 0000000..9223f20 --- /dev/null +++ b/src/main/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + diff --git a/src/main/java/com/facebook/testing/screenshot/sample/App.kt b/src/main/java/com/facebook/testing/screenshot/sample/App.kt new file mode 100644 index 0000000..e7266c0 --- /dev/null +++ b/src/main/java/com/facebook/testing/screenshot/sample/App.kt @@ -0,0 +1,27 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * 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 + * + * http://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 com.facebook.testing.screenshot.sample + +import android.app.Application +import com.facebook.soloader.SoLoader + +class App : Application() { + override fun onCreate() { + super.onCreate() + SoLoader.init(this, false) + } +} \ No newline at end of file diff --git a/src/main/java/com/facebook/testing/screenshot/sample/ExampleSpec.kt b/src/main/java/com/facebook/testing/screenshot/sample/ExampleSpec.kt new file mode 100644 index 0000000..995a394 --- /dev/null +++ b/src/main/java/com/facebook/testing/screenshot/sample/ExampleSpec.kt @@ -0,0 +1,44 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * 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 + * + * http://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 com.facebook.testing.screenshot.sample + +import com.facebook.litho.Border +import com.facebook.litho.Column +import com.facebook.litho.Component +import com.facebook.litho.ComponentContext +import com.facebook.litho.annotations.LayoutSpec +import com.facebook.litho.annotations.OnCreateLayout +import com.facebook.litho.widget.Text +import com.facebook.yoga.YogaEdge + +@LayoutSpec +object ExampleSpec { + @OnCreateLayout + fun onCreateLayout(c: ComponentContext): Component = + Column.create(c) + .child(ImageRow.create(c)) + .child( + Text.create(c) + .textRes(R.string.large_text)) + .paddingDip(YogaEdge.ALL, 16f) + .border( + Border.create(c) + .colorRes(YogaEdge.ALL, R.color.colorPrimary) + .widthDip(YogaEdge.ALL, 8f) + .build()) + .build() +} diff --git a/src/main/java/com/facebook/testing/screenshot/sample/ImageRowSpec.kt b/src/main/java/com/facebook/testing/screenshot/sample/ImageRowSpec.kt new file mode 100644 index 0000000..ae783c3 --- /dev/null +++ b/src/main/java/com/facebook/testing/screenshot/sample/ImageRowSpec.kt @@ -0,0 +1,51 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * 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 + * + * http://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 com.facebook.testing.screenshot.sample + +import com.facebook.litho.Component +import com.facebook.litho.ComponentContext +import com.facebook.litho.Row +import com.facebook.litho.annotations.LayoutSpec +import com.facebook.litho.annotations.OnCreateLayout +import com.facebook.litho.widget.Image +import com.facebook.yoga.YogaEdge + +@LayoutSpec +object ImageRowSpec { + @OnCreateLayout + fun onCreateLayout(c: ComponentContext): Component = + Row.create(c) + .child( + Image.create(c) + .drawableRes(R.drawable.ic_launcher_background) + .widthDip(64f) + .heightDip(64f) + .paddingDip(YogaEdge.ALL, 4f)) + .child( + Image.create(c) + .drawableRes(R.drawable.ic_launcher_background) + .widthDip(128f) + .heightDip(128f) + .paddingDip(YogaEdge.ALL, 4f)) + .child( + Image.create(c) + .drawableRes(R.drawable.ic_launcher_background) + .widthDip(32f) + .heightDip(32f) + .paddingDip(YogaEdge.ALL, 4f)) + .build() +} diff --git a/src/main/java/com/facebook/testing/screenshot/sample/MainActivity.kt b/src/main/java/com/facebook/testing/screenshot/sample/MainActivity.kt new file mode 100644 index 0000000..c2de17f --- /dev/null +++ b/src/main/java/com/facebook/testing/screenshot/sample/MainActivity.kt @@ -0,0 +1,92 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * 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 + * + * http://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 com.facebook.testing.screenshot.sample + +import android.content.Context +import android.content.Intent +import android.os.Bundle +import androidx.annotation.ColorRes +import com.google.android.material.floatingactionbutton.FloatingActionButton +import com.google.android.material.snackbar.Snackbar +import androidx.core.content.ContextCompat +import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.widget.Toolbar +import android.view.Menu +import android.view.View +import android.widget.TextView +import com.facebook.litho.LithoView + +class MainActivity : AppCompatActivity() { + companion object { + private const val STATUS = "status" + + fun intent(status: Status) = Intent().apply { + putExtra(STATUS, status.name) + } + } + + enum class Status { + OK, + WARNING, + ERROR + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + setSupportActionBar(findViewById(R.id.toolbar)) + + val textView = findViewById(R.id.text_view) + val status = Status.valueOf(intent.string(STATUS, Status.OK.name)) + when (status) { + Status.OK -> textView.run { + setTextColor(context.color(R.color.ok)) + text = "Status is OK" + } + Status.WARNING -> textView.run { + setTextColor(context.color(R.color.warning)) + text = "Status is WARNING" + } + Status.ERROR -> textView.run { + setTextColor(context.color(R.color.error)) + text = "Status is ERROR" + } + } + + findViewById(R.id.fab).setOnClickListener { view -> + Snackbar.make(view, "This is a snackbar", Snackbar.LENGTH_LONG) + .setAction("Action", null) + .show() + } + } + + override fun onCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.menu_main, menu) + return true + } + + private fun Intent.string(name: String, defValue: String): String { + if (hasExtra(name)) { + return getStringExtra(name) + } + return defValue + } + + private fun Context.color(@ColorRes color: Int): Int { + return ContextCompat.getColor(this, color) + } +} diff --git a/src/main/res/drawable/ic_launcher_background.xml b/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..7fc88a0 --- /dev/null +++ b/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/res/layout/activity_main.xml b/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..8d0a480 --- /dev/null +++ b/src/main/res/layout/activity_main.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + diff --git a/src/main/res/layout/litho_view.xml b/src/main/res/layout/litho_view.xml new file mode 100644 index 0000000..9058246 --- /dev/null +++ b/src/main/res/layout/litho_view.xml @@ -0,0 +1,6 @@ + + diff --git a/src/main/res/layout/search_bar.xml b/src/main/res/layout/search_bar.xml new file mode 100644 index 0000000..5e5435c --- /dev/null +++ b/src/main/res/layout/search_bar.xml @@ -0,0 +1,19 @@ + + + +