1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package platform.test.motion.compose
18
19 import androidx.compose.ui.geometry.Offset
20 import androidx.compose.ui.geometry.isFinite
21 import androidx.compose.ui.geometry.isUnspecified
22 import androidx.compose.ui.unit.Dp
23 import androidx.compose.ui.unit.DpOffset
24 import androidx.compose.ui.unit.DpSize
25 import androidx.compose.ui.unit.IntOffset
26 import androidx.compose.ui.unit.IntSize
27 import androidx.compose.ui.unit.dp
28 import java.lang.reflect.Array.getDouble
29 import org.json.JSONObject
30 import platform.test.motion.golden.DataPointType
31 import platform.test.motion.golden.UnknownTypeException
32
Dpnull33 fun Dp.asDataPoint() = DataPointTypes.dp.makeDataPoint(this)
34
35 fun IntSize.asDataPoint() = DataPointTypes.intSize.makeDataPoint(this)
36
37 fun Offset.asDataPoint() = DataPointTypes.offset.makeDataPoint(this)
38
39 fun DpSize.asDataPoint() = DataPointTypes.dpSize.makeDataPoint(this)
40
41 fun DpOffset.asDataPoint() = DataPointTypes.dpOffset.makeDataPoint(this)
42
43 object DataPointTypes {
44
45 val dp: DataPointType<Dp> =
46 DataPointType(
47 "dp",
48 jsonToValue = {
49 when (it) {
50 is Float -> it.dp
51 is Number -> it.toFloat().dp
52 is String -> it.toFloatOrNull()?.dp ?: throw UnknownTypeException()
53 else -> throw UnknownTypeException()
54 }
55 },
56 valueToJson = { it.value },
57 )
58
59 val intSize: DataPointType<IntSize> =
60 DataPointType(
61 "intSize",
62 jsonToValue = {
63 with(it as? JSONObject ?: throw UnknownTypeException()) {
64 IntSize(getInt("width"), getInt("height"))
65 }
66 },
67 valueToJson = {
68 JSONObject().apply {
69 put("width", it.width)
70 put("height", it.height)
71 }
72 },
73 )
74
75 val intOffset: DataPointType<IntOffset> =
76 DataPointType(
77 "intOffset",
78 jsonToValue = {
79 with(it as? JSONObject ?: throw UnknownTypeException()) {
80 IntOffset(getInt("x"), getInt("y"))
81 }
82 },
83 valueToJson = {
84 JSONObject().apply {
85 put("x", it.x)
86 put("y", it.y)
87 }
88 },
89 )
90
91 val dpSize: DataPointType<DpSize> =
92 DataPointType(
93 "dpSize",
94 jsonToValue = {
95 with(it as? JSONObject ?: throw UnknownTypeException()) {
96 DpSize(getDouble("width").dp, getDouble("height").dp)
97 }
98 },
99 valueToJson = {
100 JSONObject().apply {
101 put("width", it.width.value)
102 put("height", it.height.value)
103 }
104 },
105 )
106
107 val dpOffset: DataPointType<DpOffset> =
108 DataPointType(
109 "dpOffset",
110 jsonToValue = {
111 with(it as? JSONObject ?: throw UnknownTypeException()) {
112 DpOffset(getDouble("x").dp, getDouble("y").dp)
113 }
114 },
115 valueToJson = {
116 JSONObject().apply {
117 put("x", it.x.value)
118 put("y", it.y.value)
119 }
120 },
121 )
122
123 val offset: DataPointType<Offset> =
124 DataPointType(
125 "offset",
126 jsonToValue = {
127 when (it) {
128 "unspecified" -> Offset.Unspecified
129 "infinite" -> Offset.Infinite
130 is JSONObject ->
131 Offset(it.getDouble("x").toFloat(), it.getDouble("y").toFloat())
132 else -> throw UnknownTypeException()
133 }
134 },
135 valueToJson = {
136 when {
137 it.isUnspecified -> "unspecified"
138 !it.isFinite -> "infinite"
139 else ->
140 JSONObject().apply {
141 put("x", it.x)
142 put("y", it.y)
143 }
144 }
145 },
146 )
147 }
148