1 /*
2  * Copyright 2019 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 androidx.compose.runtime.mock
18 
19 import kotlin.test.assertEquals
20 import kotlin.test.assertTrue
21 
22 interface MockViewValidator {
23     val view: View
24 
nextnull25     fun next(): Boolean
26 }
27 
28 class MockViewListValidator(private val views: List<View>) : MockViewValidator {
29     override lateinit var view: View
30 
31     override fun next(): Boolean {
32         if (iterator.hasNext()) {
33             view = iterator.next()
34             return true
35         }
36         return false
37     }
38 
39     private val iterator by lazy { views.iterator() }
40 
41     fun validate(block: (MockViewValidator.() -> Unit)?) {
42         if (block != null) {
43             this.block()
44             val hasNext = next()
45             assertEquals(false, hasNext, "Expected children but none found")
46         } else {
47             assertEquals(0, views.size, "Not expecting children but some found")
48         }
49     }
50 
51     inline fun inlineValidate(block: MockViewListValidator.() -> Unit) {
52         this.block()
53         val hasNext = next()
54         assertEquals(false, hasNext, "Expected children but none found")
55     }
56 }
57 
viewnull58 fun MockViewValidator.view(name: String, block: (MockViewValidator.() -> Unit)? = null) {
59     val hasNext = next()
60     assertTrue(hasNext, "Expected a $name, but none found")
61     assertEquals(name, view.name)
62     MockViewListValidator(view.children).validate(block)
63 }
64 
inlineViewnull65 inline fun MockViewValidator.inlineView(name: String, block: MockViewValidator.() -> Unit) {
66     val hasNext = next()
67     assertTrue(hasNext, "Expected a $name, but none found")
68     assertEquals(name, view.name)
69     MockViewListValidator(view.children).inlineValidate(block)
70 }
71 
Repeatednull72 fun <T> MockViewValidator.Repeated(of: Iterable<T>, block: MockViewValidator.(value: T) -> Unit) {
73     for (value in of) {
74         block(value)
75     }
76 }
77 
MockViewValidatornull78 fun MockViewValidator.Linear() = view("linear", null)
79 
80 fun MockViewValidator.Linear(block: MockViewValidator.() -> Unit) = view("linear", block)
81 
82 inline fun MockViewValidator.InlineLinear(block: MockViewValidator.() -> Unit) =
83     inlineView("linear", block)
84 
85 fun MockViewValidator.box(block: MockViewValidator.() -> Unit) = view("box", block)
86 
87 fun MockViewValidator.Text(value: String) {
88     view("text")
89     assertEquals(value, view.attributes["text"])
90 }
91 
Editnull92 fun MockViewValidator.Edit(value: String) {
93     view("edit")
94     assertEquals(value, view.attributes["value"])
95 }
96 
SelectBoxnull97 fun MockViewValidator.SelectBox(selected: Boolean, block: MockViewValidator.() -> Unit) {
98     if (selected) {
99         box(block)
100     } else {
101         block()
102     }
103 }
104 
skipnull105 fun MockViewValidator.skip(times: Int = 1) {
106     repeat(times) {
107         val hasNext = next()
108         assertEquals(true, hasNext)
109     }
110 }
111