Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
Constraints and json parser fixes
Browse files Browse the repository at this point in the history
Fixes the constraints parser for studio not getting the ID for barriers
and guidelines.

Fixes json custom properties not being properly set on initialization.

Fixes json parser when color is more than signed Int allows.
  • Loading branch information
oscar-ad committed Nov 9, 2021
1 parent d782fcb commit facedd6
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 97 deletions.
4 changes: 3 additions & 1 deletion constraintlayout/compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
}

def compose_version = '1.0.0-rc02'
def kotlin_version = "1.5.10"

android {
compileSdkVersion 30
Expand Down Expand Up @@ -34,7 +35,7 @@ android {
}

composeOptions {
kotlinCompilerVersion "1.5.10"
kotlinCompilerVersion kotlin_version
kotlinCompilerExtensionVersion compose_version
}

Expand All @@ -51,6 +52,7 @@ android {
implementation "androidx.compose.foundation:foundation:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation project(':core')
androidTestImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
androidTestImplementation 'junit:junit:4.13.1'
androidTestImplementation "androidx.compose.ui:ui-test:$compose_version"
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2021 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
*
* 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 androidx.constraintlayout.compose

import androidx.constraintlayout.core.state.Transition
import org.intellij.lang.annotations.Language
import org.junit.Test

internal class ConstraintSetParserKtTest {

@Test
fun parseCustomColor() {
val coreTransition = Transition()

@Language("JSON5")
val content = """
{
id1: {
custom: {
color: '#ffaabbcc' // color that requires u_int or long parsing
}
}
}
""".trimIndent()
parseJSON(content, coreTransition, 0) // Should finish successfully
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class DesignInfoProviderTest {
}

@Test
@Ignore
fun withConstraintSet() {
rule.setWithConstraintSet()

Expand All @@ -97,7 +96,6 @@ class DesignInfoProviderTest {
}

@Test
@Ignore
fun withDsl() {
rule.setWithDsl()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2021 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
*
* 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 androidx.constraintlayout.compose

import android.content.Context
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.platform.isDebugInspectorInfoEnabled
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.core.parser.CLParser
import androidx.constraintlayout.core.parser.CLParsingException
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.assertFailsWith


@MediumTest
@RunWith(AndroidJUnit4::class)
internal class MotionParserTest {
@get:Rule
val rule = createComposeRule()

var displaySize: IntSize = IntSize.Zero

// region sizing tests

@Before
fun before() {
isDebugInspectorInfoEnabled = true
displaySize = ApplicationProvider
.getApplicationContext<Context>().resources.displayMetrics.let {
IntSize(it.widthPixels, it.heightPixels)
}
}

@After
fun after() {
isDebugInspectorInfoEnabled = false
}

@Test
fun testTransitionParseFailsSilently() {
// We don't want applications to hard-crash when the parser sees an error
var coreTransition = androidx.constraintlayout.core.state.Transition()
val transitionContent = """
{
from: "start",
to: "end",
KeyFrames: {
KeyAttributes: [{
target: ['id1'],
frames: [10, 20],
alpha: [0.5,],
}],
}
}
""".trimIndent()
// Parsing transition throws an exception but the Composable should not crash the app
assertFailsWith<CLParsingException> {
parseTransition(CLParser.parse(transitionContent), coreTransition)
}
coreTransition = androidx.constraintlayout.core.state.Transition()
rule.setContent {
val transition = Transition(content = transitionContent)
MotionLayout(
modifier = Modifier.size(100.dp),
start = ConstraintSet(
"""
{
id1: {
width: 10, height: 10,
centerVertically: 'parent', start: ['parent', 'start', 0]
}
}
""".trimIndent()
),
end = ConstraintSet(
"""
{
id1: {
width: 10, height: 10,
centerVertically: 'parent', end: ['parent', 'end', 0]
}
}
""".trimIndent()
),
transition = transition,
progress = 0f
) {
Box(modifier = Modifier.layoutId("id1"))
}
}
// Test should finish without exceptions
rule.waitForIdle()
}
}
Loading

0 comments on commit facedd6

Please sign in to comment.