1 /*
<lambda>null2  * Copyright (C) 2022 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 com.android.compose.test.subjects
18 
19 import androidx.compose.ui.test.assertIsEqualTo
20 import androidx.compose.ui.unit.Dp
21 import androidx.compose.ui.unit.DpOffset
22 import com.google.common.truth.FailureMetadata
23 import com.google.common.truth.Subject
24 import com.google.common.truth.Subject.Factory
25 import com.google.common.truth.Truth.assertAbout
26 
27 /** Assert on a [DpOffset]. */
28 fun assertThat(dpOffset: DpOffset): DpOffsetSubject {
29     return assertAbout(DpOffsetSubject.dpOffsets()).that(dpOffset)
30 }
31 
32 /** A Truth subject to assert on [DpOffset] with some tolerance. Inspired by FloatSubject. */
33 class DpOffsetSubject(metadata: FailureMetadata, private val actual: DpOffset) :
34     Subject(metadata, actual) {
isWithinnull35     fun isWithin(tolerance: Dp): TolerantDpOffsetComparison {
36         return object : TolerantDpOffsetComparison {
37             override fun of(expected: DpOffset) {
38                 actual.x.assertIsEqualTo(expected.x, "offset.x", tolerance)
39                 actual.y.assertIsEqualTo(expected.y, "offset.y", tolerance)
40             }
41         }
42     }
43 
44     interface TolerantDpOffsetComparison {
ofnull45         fun of(expected: DpOffset)
46     }
47 
48     companion object {
49         val DefaultTolerance = Dp(.5f)
50 
51         fun dpOffsets() =
52             Factory<DpOffsetSubject, DpOffset> { metadata, actual ->
53                 DpOffsetSubject(metadata, actual)
54             }
55     }
56 }
57