1 /*
<lambda>null2  * 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.view
18 
19 import android.graphics.Point
20 import android.graphics.Rect
21 import org.json.JSONObject
22 import platform.test.motion.golden.DataPointType
23 import platform.test.motion.golden.UnknownTypeException
24 
25 fun Rect.asDataPoint() = DataPointTypes.rect.makeDataPoint(this)
26 
27 fun Point.asDataPoint() = DataPointTypes.point.makeDataPoint(this)
28 
29 /** [DataPointType] implementations for core [View] related types. */
30 object DataPointTypes {
31     val point: DataPointType<Point> =
32         DataPointType(
33             "point",
34             jsonToValue = {
35                 with(it as? JSONObject ?: throw UnknownTypeException()) {
36                     Point(getInt("x"), getInt("y"))
37                 }
38             },
39             valueToJson = {
40                 JSONObject().apply {
41                     put("x", it.x)
42                     put("y", it.y)
43                 }
44             },
45             ensureImmutable = { Point(it) }
46         )
47 
48     val rect: DataPointType<Rect> =
49         DataPointType(
50             "rect",
51             jsonToValue = {
52                 with(it as? JSONObject ?: throw UnknownTypeException()) {
53                     Rect(getInt("left"), getInt("top"), getInt("right"), getInt("bottom"))
54                 }
55             },
56             valueToJson = {
57                 JSONObject().apply {
58                     put("left", it.left)
59                     put("top", it.top)
60                     put("right", it.right)
61                     put("bottom", it.bottom)
62                 }
63             },
64             ensureImmutable = { Rect(it) }
65         )
66 
67     /** [GradientDrawable] corner radii */
68     val cornerRadii: DataPointType<CornerRadii> =
69         DataPointType(
70             "cornerRadii",
71             jsonToValue = {
72                 with(it as? JSONObject ?: throw UnknownTypeException()) {
73                     CornerRadii(
74                         FloatArray(length()) { index ->
75                             getDouble(cornerRadiiPropertyNames[index]).toFloat()
76                         }
77                     )
78                 }
79             },
80             valueToJson = {
81                 JSONObject().apply {
82                     for (i in it.rawValues.indices) {
83                         put(cornerRadiiPropertyNames[i], it.rawValues[i])
84                     }
85                 }
86             }
87         )
88     // property names match order of val
89     private val cornerRadiiPropertyNames =
90         listOf(
91             "top_left_x",
92             "top_left_y",
93             "top_right_x",
94             "top_right_y",
95             "bottom_right_x",
96             "bottom_right_y",
97             "bottom_left_x",
98             "bottom_left_y",
99         )
100 
101     val allTypes = listOf(point, rect, cornerRadii)
102 }
103