[go: nahoru, domu]

blob: fa112c6727fe457ac874e61256bdf5546f860a16 [file] [log] [blame]
Jelle Fresen8f0a7852020-03-25 18:46:05 +00001/*
2 * Copyright 2020 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
17package androidx.ui.test
18
19import android.view.ViewGroup
20import android.widget.FrameLayout
21import androidx.activity.ComponentActivity
Adam Powellc9167422020-04-08 16:50:37 -070022import androidx.compose.Recomposer
Jelle Fresen8f0a7852020-03-25 18:46:05 +000023import androidx.test.espresso.Espresso.onIdle
24import androidx.test.filters.MediumTest
25import androidx.ui.core.Modifier
26import androidx.ui.core.setContent
27import androidx.ui.foundation.Box
Nader Jawaddcb72202020-05-05 20:03:50 -070028import androidx.ui.foundation.Canvas
Jelle Fresen8f0a7852020-03-25 18:46:05 +000029import androidx.ui.graphics.Color
Andrey Kulikov1526ea52020-04-07 18:43:27 +010030import androidx.ui.layout.fillMaxSize
Jelle Fresen8f0a7852020-03-25 18:46:05 +000031import androidx.ui.test.android.AndroidComposeTestRule
32import com.google.common.truth.Truth.assertThat
33import org.junit.Rule
34import org.junit.Test
35
36@MediumTest
37class FirstDrawTest {
38
39 @get:Rule
Jelle Fresenc67b0522020-04-09 12:03:55 +010040 val testRule = AndroidComposeTestRule<ComponentActivity>()
Jelle Fresen8f0a7852020-03-25 18:46:05 +000041
42 /**
43 * Tests that the compose tree has been drawn at least once when [onIdle] finishes.
44 */
45 @Test
46 fun waitsForFirstDraw() {
47 var drawn = false
48 testRule.setContent {
Nader Jawaddcb72202020-05-05 20:03:50 -070049 Canvas(Modifier.fillMaxSize()) {
Jelle Fresen8f0a7852020-03-25 18:46:05 +000050 drawn = true
51 }
52 }
53 onIdle()
54 assertThat(drawn).isTrue()
55 }
56
57 /**
Jelle Fresen3b5a8392020-04-16 15:59:36 +010058 * Tests that the compose tree has been drawn at least once when [onIdle] finishes.
59 */
60 @Test
61 fun waitsForFirstDraw_withoutOnIdle() {
62 var drawn = false
63 testRule.setContent {
Nader Jawaddcb72202020-05-05 20:03:50 -070064 Canvas(Modifier.fillMaxSize()) {
Jelle Fresen3b5a8392020-04-16 15:59:36 +010065 drawn = true
66 }
67 }
68 // onIdle() shouldn't be necessary
69 assertThat(drawn).isTrue()
70 }
71
72 /**
Jelle Fresen8f0a7852020-03-25 18:46:05 +000073 * Tests that [onIdle] doesn't timeout when the compose tree is completely off-screen and
74 * will hence not be drawn.
75 */
76 @Test
77 fun waitsForOutOfBoundsComposeView() {
78 var drawn = false
79
80 val activity = testRule.activityTestRule.activity
81 activity.runOnUiThread {
82
83 // Set the compose content in a FrameLayout that is completely placed out of the
84 // screen, and enforce clipToPadding in case clipping will prevent the clipped
85 // content from being drawn.
86
87 val root = object : FrameLayout(activity) {
88 override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
89 // Place our child out of bounds
90 getChildAt(0).layout(-200, 0, -100, 100)
91 }
92 }.apply {
93 // Enforce clipping:
94 setPadding(1, 1, 1, 1)
95 clipToPadding = true
96 }
97
98 val outOfBoundsView = FrameLayout(activity).apply {
99 layoutParams = ViewGroup.MarginLayoutParams(100, 100)
100 }
101
102 root.addView(outOfBoundsView)
103 activity.setContentView(root)
Adam Powellc9167422020-04-08 16:50:37 -0700104 outOfBoundsView.setContent(Recomposer.current()) {
Jelle Fresen8f0a7852020-03-25 18:46:05 +0000105 // If you see this box when running the test, the test is setup incorrectly
Adam Powellb6d8db22020-04-02 12:40:03 -0700106 Box(Modifier, backgroundColor = Color.Yellow)
Nader Jawaddcb72202020-05-05 20:03:50 -0700107 Canvas(Modifier) {
Jelle Fresen8f0a7852020-03-25 18:46:05 +0000108 drawn = true
109 }
110 }
111 }
112
113 // onIdle shouldn't timeout
114 onIdle()
115 // The compose view was off-screen, so it hasn't drawn yet
116 assertThat(drawn).isFalse()
117 }
118}