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 feature flag for new resource loading mechanism #764

Merged
merged 1 commit into from
May 3, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,40 @@ class PaparazziPluginTest {
assertThat(result.output).contains("Objects still linked from the DelegateManager:")
}

@Test
fun flagNewResourceLoadingIsOff() {
val fixtureRoot = File("src/test/projects/flag-new-resource-loading-off")

val result = gradleRunner
.withArguments("testDebug", "--stacktrace")
.runFixture(fixtureRoot) { build() }

assertThat(result.task(":preparePaparazziDebugResources")).isNotNull()
assertThat(result.task(":testDebugUnitTest")).isNotNull()

val snapshotsDir = File(fixtureRoot, "build/reports/paparazzi/images")
val snapshots = snapshotsDir.listFiles()
assertThat(snapshots!!).hasLength(1)

val snapshotImage = snapshots[0]
val goldenImage = File(fixtureRoot, "src/test/resources/launch.png")
assertThat(snapshotImage).isSimilarTo(goldenImage).withDefaultThreshold()
}

@Test
fun flagNewResourceLoadingIsOn() {
val fixtureRoot = File("src/test/projects/flag-new-resource-loading-on")

val result = gradleRunner
.withArguments("testDebug", "--stacktrace")
.forwardOutput()
.runFixture(fixtureRoot) { buildAndFail() }

assertThat(result.output).contains(
"An operation is not implemented: New resource loading coming soon"
)
}

@Test
fun cacheable() {
val fixtureRoot = File("src/test/projects/cacheable")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'app.cash.paparazzi'
}

repositories {
maven {
url "file://${projectDir.absolutePath}/../../../../../build/localMaven"
}
mavenCentral()
//mavenLocal()
google()
}

android {
namespace 'app.cash.paparazzi.plugin.test'
compileSdk libs.versions.compileSdk.get() as int
defaultConfig {
minSdk libs.versions.minSdk.get() as int
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:background="@color/launchBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/camera"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/cameraBody"
android:text="paparazzi"
android:textSize="70dp"
android:textFontWeight="100"
/>

</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="launchBackground">#dcdccc</color>
<color name="cameraBody">#332F21</color>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2019 Square, Inc.
*
* 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 app.cash.paparazzi.plugin.test

import android.widget.LinearLayout
import app.cash.paparazzi.Paparazzi
import org.junit.Rule
import org.junit.Test

class LaunchViewTest {
@get:Rule
val paparazzi = Paparazzi()

@Test
fun testViews() {
val launch = paparazzi.inflate<LinearLayout>(R.layout.launch)
paparazzi.snapshot(launch, "launch")
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'app.cash.paparazzi'
}

repositories {
maven {
url "file://${projectDir.absolutePath}/../../../../../build/localMaven"
}
mavenCentral()
//mavenLocal()
google()
}

android {
namespace 'app.cash.paparazzi.plugin.test'
compileSdk libs.versions.compileSdk.get() as int
defaultConfig {
minSdk libs.versions.minSdk.get() as int
}
}

tasks.withType(Test).configureEach {
testLogging {
exceptionFormat 'FULL'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
android.useAndroidX=true
app.cash.paparazzi.new.resource.loading=true
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:background="@color/launchBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/camera"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/cameraBody"
android:text="paparazzi"
android:textSize="70dp"
android:textFontWeight="100"
/>

</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="launchBackground">#dcdccc</color>
<color name="cameraBody">#332F21</color>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2019 Square, Inc.
*
* 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 app.cash.paparazzi.plugin.test

import android.widget.LinearLayout
import app.cash.paparazzi.Paparazzi
import org.junit.Rule
import org.junit.Test

class LaunchViewTest {
@get:Rule
val paparazzi = Paparazzi()

@Test
fun testViews() {
val launch = paparazzi.inflate<LinearLayout>(R.layout.launch)
paparazzi.snapshot(launch, "launch")
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package app.cash.paparazzi

object Flags {
const val DEBUG_LINKED_OBJECTS = "app.cash.paparazzi.debug.linked.objects"
const val NEW_RESOURCE_LOADING = "app.cash.paparazzi.new.resource.loading"
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,28 @@ internal class Renderer(
/** Initialize the bridge and the resource maps. */
fun prepare(): SessionParamsBuilder {
val platformDataResDir = File("${environment.platformDir}/data/res")
val frameworkResources = FrameworkResources(FolderWrapper(platformDataResDir)).apply {
loadResources()
loadPublicResources(logger)

val useNewResourceLoading = System.getProperty(Flags.NEW_RESOURCE_LOADING).toBoolean()

val frameworkResources = if (!useNewResourceLoading) {
FrameworkResources(FolderWrapper(platformDataResDir))
.apply {
loadResources()
loadPublicResources(logger)
}
} else {
TODO("New resource loading coming soon")
}

val projectResources = object : ResourceRepository(FolderWrapper(environment.resDir), false) {
override fun createResourceItem(name: String): ResourceItem {
return ResourceItem(name)
}
val projectResources = if (!useNewResourceLoading) {
object : ResourceRepository(FolderWrapper(environment.resDir), false) {
override fun createResourceItem(name: String): ResourceItem {
return ResourceItem(name)
}
}.apply { loadResources() }
} else {
TODO("New resource loading coming soon")
}
projectResources.loadResources()

sessionParamsBuilder = SessionParamsBuilder(
layoutlibCallback = layoutlibCallback,
Expand Down