Skip to content

Commit

Permalink
Update various libraries and migrate to JDK 17 (#343)
Browse files Browse the repository at this point in the history
* Clean up dependencies and simplify things using version catalogs

* Update to modern library versions and fix build/lint issues

* Update Scala libraries and migrate to Scala 2.13

* Validate changes and switch to JDK 17 for the GH workflow

* Convert example to AGP 8 for verification of Gradle/AGP 8

* Fix issue with gradle.properties

* Re-record screenshots for updated sample projects

* Add tolerance to compensate for ARM vs x86 emulator differences

* Missed one
  • Loading branch information
Kurt-Bonatz committed Jul 7, 2023
1 parent 8ad3a47 commit 2dec201
Show file tree
Hide file tree
Showing 114 changed files with 1,402 additions and 985 deletions.
16 changes: 4 additions & 12 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Start emulator
run: shot-consumer/scripts/start_emulator.sh
timeout-minutes: 20
- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 11
java-version: 17
- name: Check code formatting
run: ./gradlew clean checkScalaFmtAll ktlintCheck
- name: Shot unit tests
Expand Down Expand Up @@ -74,17 +70,13 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Start emulator
run: shot-consumer/scripts/start_emulator.sh
timeout-minutes: 20
- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 11
java-version: 17
- name: Disable artifacts signing
run: echo "RELEASE_SIGNING_ENABLED=false" >> gradle.properties
- name: Build and install Shot
Expand Down
14 changes: 6 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ buildscript {
google()
}

ext.kotlin_version = "1.4.32"

dependencies {
classpath "gradle.plugin.com.github.maiflai:gradle-scalatest:0.31"
classpath "gradle.plugin.cz.alenkacz:gradle-scalafmt:1.16.2"
classpath "com.android.tools.build:gradle:4.1.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jlleitschuh.gradle:ktlint-gradle:10.0.0"
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.15.1'
classpath(libs.scalatest.gradle.plugin)
classpath(libs.scalafmt.gradle.plugin)
classpath(libs.android.gradle.plugin)
classpath(libs.kotlin.gradle.plugin)
classpath(libs.ktlint.gradle.plugin)
classpath(libs.vanniktech.publish.gradle.plugin)
}
}
16 changes: 7 additions & 9 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ repositories {
}

dependencies {
implementation 'org.scala-lang:scala-library:2.12.13'
implementation 'org.scala-lang.modules:scala-xml_2.12:1.3.0'
implementation 'com.sksamuel.scrimage:scrimage-core_2.12:2.1.8'
implementation 'io.github.bitstorm:tinyzip-core:1.0.0'
implementation 'org.json4s:json4s-native_2.12:3.6.11'
implementation 'org.json4s:json4s-jackson_2.12:3.6.11'
implementation libs.bundles.scala
implementation libs.bundles.scrimage
implementation libs.tinyzip
implementation libs.bundles.json4s
implementation libs.commons.io

testImplementation 'org.scalatest:scalatest_2.12:3.2.6'
testRuntimeOnly "com.vladsch.flexmark:flexmark-profile-pegdown:0.36.8" // https://github.com/scalatest/scalatest/issues/1736
testImplementation 'org.scalamock:scalamock-scalatest-support_2.12:3.5.0'
testImplementation libs.bundles.scala.test
testRuntimeOnly libs.flexmark.profile.pegdown // https://github.com/scalatest/scalatest/issues/1736
}

sourceSets {
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/scala/com/karumi/shot/Shot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import scala.collection.convert.ImplicitConversions.{
`collection asJava`
}
import scala.collection.immutable.Stream.Empty
import scala.collection.parallel.CollectionConverters._
import scala.Console.YELLOW

class Shot(
adb: Adb,
Expand Down Expand Up @@ -150,7 +152,7 @@ class Shot(
} else {
fileList = files.listFilesInFolder(composeFolder)
}
fileList.forEach { file: File =>
fileList.forEach { (file: File) =>
val rawFilePath = file.getAbsolutePath
val newFilePath = shotFolder.pulledScreenshotsFolder() + file.getName
files.rename(rawFilePath, newFilePath)
Expand Down Expand Up @@ -311,7 +313,7 @@ class Shot(
try {
FileUtils.deleteDirectory(file)
} catch {
case e: Throwable => println(Console.YELLOW + s"Failed to delete directory: $e")
case e: Throwable => println(YELLOW + s"Failed to delete directory: $e")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.karumi.shot.json

import com.drew.metadata.Metadata
import com.karumi.shot.domain.{Dimension, Screenshot}
import com.karumi.shot.domain.model.{Folder, ScreenshotsSuite}
import org.json4s._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ExecutionReporter {

private def generateRecordSummaryTableBody(screenshots: ScreenshotsSuite): String = {
screenshots
.map { screenshot: Screenshot =>
.map { (screenshot: Screenshot) =>
val testClass = screenshot.testClass
val testName = screenshot.testName
val originalScreenshot = "./images/recorded/" + screenshot.name + ".png"
Expand Down Expand Up @@ -111,7 +111,7 @@ class ExecutionReporter {

private def getSortedByResultScreenshots(comparison: ScreenshotsComparisionResult) =
comparison.screenshots
.map { screenshot: Screenshot =>
.map { (screenshot: Screenshot) =>
val error = findError(screenshot, comparison.errors)
(screenshot, error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ package com.karumi.shot.screenshots
import java.io.File

import com.karumi.shot.domain.Screenshot
import com.sksamuel.scrimage.{AwtImage, Color, Image}
import com.sksamuel.scrimage.color.Colors
import com.sksamuel.scrimage.{AwtImage, ImmutableImage}

object ScreenshotComposer {

private val tileSize = 512

private[screenshots] def composeNewScreenshot(screenshot: Screenshot): Image = {
private[screenshots] def composeNewScreenshot(screenshot: Screenshot): ImmutableImage = {
val width = screenshot.screenshotDimension.width
val height = screenshot.screenshotDimension.height
if (screenshot.recordedPartsPaths.size == 1) {
Image.fromFile(new File(screenshot.recordedPartsPaths.head))
ImmutableImage.loader().fromFile(new File(screenshot.recordedPartsPaths.head))
} else {
var composedImage = Image.filled(width, height, Color.Transparent)
var composedImage = ImmutableImage.filled(width, height, Colors.Transparent.awt())
var partIndex = 0
for (
x <- 0 until screenshot.tilesDimension.width;
y <- 0 until screenshot.tilesDimension.height
) {
val partFile = new File(screenshot.recordedPartsPaths(partIndex))
val part = Image.fromFile(partFile).awt
val part = ImmutableImage.loader().fromFile(partFile).awt
val xPosition = x * tileSize
val yPosition = y * tileSize
composedImage = composedImage.overlay(new AwtImage(part), xPosition, yPosition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import java.io.File

import com.karumi.shot.domain._
import com.karumi.shot.domain.model.ScreenshotsSuite
import com.sksamuel.scrimage.Image
import com.sksamuel.scrimage.ImmutableImage
import scala.collection.parallel.CollectionConverters._

class ScreenshotsComparator {

Expand All @@ -23,7 +24,7 @@ class ScreenshotsComparator {
Some(ScreenshotNotFound(screenshot))
} else {
val oldScreenshot =
Image.fromFile(recordedScreenshotFile)
ImmutableImage.loader().fromFile(recordedScreenshotFile)
val newScreenshot = ScreenshotComposer.composeNewScreenshot(screenshot)
if (!haveSameDimensions(newScreenshot, oldScreenshot)) {
val originalDimension =
Expand All @@ -40,8 +41,8 @@ class ScreenshotsComparator {

private def imagesAreDifferent(
screenshot: Screenshot,
oldScreenshot: Image,
newScreenshot: Image,
oldScreenshot: ImmutableImage,
newScreenshot: ImmutableImage,
tolerance: Double
) = {
if (oldScreenshot == newScreenshot) {
Expand All @@ -67,7 +68,10 @@ class ScreenshotsComparator {
}
}

private def haveSameDimensions(newScreenshot: Image, recordedScreenshot: Image): Boolean =
private def haveSameDimensions(
newScreenshot: ImmutableImage,
recordedScreenshot: ImmutableImage
): Boolean =
newScreenshot.width == recordedScreenshot.width && newScreenshot.height == recordedScreenshot.height

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.karumi.shot.screenshots

import java.io.File

import com.karumi.shot.base64.Base64Encoder
import com.karumi.shot.domain.model.ScreenshotComparisionErrors
import com.karumi.shot.domain.{DifferentScreenshots, ScreenshotsComparisionResult}
import com.sksamuel.scrimage.Image
import com.sksamuel.scrimage.ImmutableImage
import com.sksamuel.scrimage.composite.RedComposite
import com.sksamuel.scrimage.nio.PngWriter

import java.awt.image.BufferedImage
import scala.collection.parallel.CollectionConverters._

class ScreenshotsDiffGenerator(base64Encoder: Base64Encoder) {

Expand All @@ -32,11 +35,15 @@ class ScreenshotsDiffGenerator(base64Encoder: Base64Encoder) {
val screenshot = error.screenshot
val originalImagePath = screenshot.recordedScreenshotPath
val newImagePath = screenshot.temporalScreenshotPath
val originalImage = Image.fromFile(new File(originalImagePath))
val newImage = Image.fromFile(new File(newImagePath))
val diff = newImage.composite(new RedComposite(1d), originalImage)
val outputFilePath = screenshot.getDiffScreenshotPath(outputFolder)
diff.output(outputFilePath)
val originalImage = ImmutableImage
.loader()
.fromFile(new File(originalImagePath))
.copy(BufferedImage.TYPE_INT_ARGB)
val newImage =
ImmutableImage.loader().fromFile(new File(newImagePath)).copy(BufferedImage.TYPE_INT_ARGB)
val diff = newImage.composite(new RedComposite(1d), originalImage)
val outputFilePath = screenshot.getDiffScreenshotPath(outputFolder)
diff.output(PngWriter.MaxCompression, outputFilePath)
if (generateBase64Diff) {
error.copy(base64Diff = base64Encoder.base64FromFile(outputFilePath))
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package com.karumi.shot.screenshots

import java.io.File
import com.karumi.shot.domain.{
Config,
DifferentImageDimensions,
DifferentScreenshots,
Dimension,
Screenshot,
ScreenshotNotFound,
ScreenshotsComparisionResult,
ShotFolder
}
import com.karumi.shot.domain.{Dimension, Screenshot, ScreenshotsComparisionResult, ShotFolder}
import com.karumi.shot.domain.model.{FilePath, Folder, ScreenshotsSuite}
import com.sksamuel.scrimage.Image
import com.sksamuel.scrimage.ImmutableImage
import com.sksamuel.scrimage.nio.PngWriter
import org.apache.commons.io.FileUtils

class ScreenshotsSaver {
Expand Down Expand Up @@ -71,7 +63,7 @@ class ScreenshotsSaver {
screenshot: Screenshot
): Dimension = {
val screenshotPath = shotFolder.pulledScreenshotsFolder() + screenshot.name + ".png"
val image = Image.fromFile(new File(screenshotPath))
val image = ImmutableImage.loader().fromFile(new File(screenshotPath))
Dimension(image.width, image.height)
}

Expand All @@ -97,7 +89,7 @@ class ScreenshotsSaver {
outputFile.createNewFile()
}
val image = ScreenshotComposer.composeNewScreenshot(screenshot)
image.output(outputFile)
image.output(PngWriter.MaxCompression, outputFile)
}
}

Expand Down
9 changes: 5 additions & 4 deletions core/src/main/scala/com/karumi/shot/ui/Console.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.karumi.shot.ui
import scala.Console.{CYAN, GREEN, RED, RESET, YELLOW}

class Console {

type Message = String

def show(message: Message): Unit =
print(Console.CYAN + message + Console.RESET)
print(CYAN + message + RESET)

def showSuccess(message: Message): Unit =
print(Console.GREEN + message + Console.RESET)
print(GREEN + message + RESET)

def showWarning(message: Message): Unit =
print(Console.YELLOW + message + Console.RESET)
print(YELLOW + message + RESET)

def showError(message: Message): Unit =
print(Console.RED + message + Console.RESET)
print(RED + message + RESET)

def lineBreak(): Unit = show("\n")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ object ScreenshotsSuiteJsonParser {
testName,
tilesDimension,
viewHierarchy,
absoluteFileNames,
relativeFileNames,
relativeFileNames.map(temporalScreenshotsFolder + _),
absoluteFileNames.toSeq,
relativeFileNames.toSeq,
relativeFileNames.map(temporalScreenshotsFolder + _).toSeq,
Dimension(0, 0)
)
}
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_DEVELOPER_ID=pedrovgs
POM_DEVELOPER_NAME=Pedro Vicente Gomez Sanchez
android.useAndroidX=true
Loading

0 comments on commit 2dec201

Please sign in to comment.