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.golden
18 
19 import androidx.test.ext.junit.runners.AndroidJUnit4
20 import com.google.common.truth.Truth.assertThat
21 import org.json.JSONObject
22 import org.junit.Test
23 import org.junit.runner.RunWith
24 import platform.test.motion.golden.DataPoint.Companion.notFound
25 import platform.test.motion.golden.DataPoint.Companion.nullValue
26 
27 @RunWith(AndroidJUnit4::class)
28 class DataPointTypeTest {
29     data class Native(val id: String)
30 
31     private val subject =
32         DataPointType(
33             "native",
34             jsonToValue = { jsonValue ->
35                 jsonToValueInvocations++
36                 if (jsonValue is String) Native(jsonValue) else throw UnknownTypeException()
37             },
38             valueToJson = {
39                 valueToJsonInvocations++
40                 it.id
41             }
42         )
43     private var jsonToValueInvocations = 0
44     private var valueToJsonInvocations = 0
45 
46     @Test
47     fun makeDataPoint_ofNull_createsNullValue() {
48         assertThat(subject.makeDataPoint(null)).isEqualTo(nullValue<Native>())
49     }
50 
51     @Test
52     fun makeDataPoint_ofInstance_createsValueDataPoint() {
53         val nativeValue = Native("one")
54         val dataPoint = subject.makeDataPoint(nativeValue)
55 
56         assertThat(dataPoint).isInstanceOf(ValueDataPoint::class.java)
57         val valueDataPoint = dataPoint as ValueDataPoint
58         assertThat(valueDataPoint.value).isSameInstanceAs(nativeValue)
59         assertThat(valueDataPoint.type).isSameInstanceAs(subject)
60     }
61 
62     @Test
63     fun fromJson_ofNull_returnsNullValue() {
64         val dataPoint = subject.fromJson(JSONObject.NULL)
65 
66         assertThat(dataPoint).isEqualTo(nullValue<Native>())
67         assertThat(jsonToValueInvocations).isEqualTo(0)
68     }
69 
70     @Test
71     fun fromJson_ofNotFound_returnsNotFound() {
72         val dataPoint = subject.fromJson(NotFoundDataPoint.instance.asJson())
73 
74         assertThat(dataPoint).isEqualTo(notFound<Native>())
75         assertThat(jsonToValueInvocations).isEqualTo(0)
76     }
77 
78     @Test
79     fun fromJson_ofValue_returnsValueDataPoint() {
80         val dataPoint = subject.fromJson("one")
81 
82         assertThat(dataPoint).isInstanceOf(ValueDataPoint::class.java)
83         val valueDataPoint = dataPoint as ValueDataPoint
84         assertThat(valueDataPoint.value).isEqualTo(Native("one"))
85         assertThat(valueDataPoint.type).isSameInstanceAs(subject)
86         assertThat(jsonToValueInvocations).isEqualTo(1)
87     }
88 
89     @Test
90     fun toJson_delegatesToConverter() {
91         val json = subject.toJson(Native("one"))
92 
93         assertThat(json).isEqualTo("one")
94         assertThat(valueToJsonInvocations).isEqualTo(1)
95     }
96 }
97