[go: nahoru, domu]

blob: 1830d438fb5641f9cbd3f7a7146733ce30980e2b [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
Louis Pullen-Freilichddda7be2020-07-17 18:28:12 +010027import androidx.compose.foundation.Box
28import androidx.compose.foundation.Canvas
Jelle Fresen8f0a7852020-03-25 18:46:05 +000029import androidx.ui.graphics.Color
Louis Pullen-Freilich623e4052020-07-19 20:24:03 +010030import androidx.compose.foundation.layout.fillMaxSize
Filip Pavlisb3943e12020-07-22 14:39:42 +010031import androidx.ui.test.android.createAndroidComposeRule
Jelle Fresen8f0a7852020-03-25 18:46:05 +000032import com.google.common.truth.Truth.assertThat
33import org.junit.Rule
34import org.junit.Test
35
36@MediumTest
37class FirstDrawTest {
38
39 @get:Rule
Filip Pavlisb3943e12020-07-22 14:39:42 +010040 val testRule = createAndroidComposeRule<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
Filip Pavlis607cc412020-06-11 21:56:49 +010080 testRule.activityRule.scenario.onActivity { activity ->
Jelle Fresen8f0a7852020-03-25 18:46:05 +000081 // Set the compose content in a FrameLayout that is completely placed out of the
82 // screen, and enforce clipToPadding in case clipping will prevent the clipped
83 // content from being drawn.
84
85 val root = object : FrameLayout(activity) {
86 override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
87 // Place our child out of bounds
88 getChildAt(0).layout(-200, 0, -100, 100)
89 }
90 }.apply {
91 // Enforce clipping:
92 setPadding(1, 1, 1, 1)
93 clipToPadding = true
94 }
95
96 val outOfBoundsView = FrameLayout(activity).apply {
97 layoutParams = ViewGroup.MarginLayoutParams(100, 100)
98 }
99
100 root.addView(outOfBoundsView)
101 activity.setContentView(root)
Adam Powellc9167422020-04-08 16:50:37 -0700102 outOfBoundsView.setContent(Recomposer.current()) {
Jelle Fresen8f0a7852020-03-25 18:46:05 +0000103 // If you see this box when running the test, the test is setup incorrectly
Adam Powellb6d8db22020-04-02 12:40:03 -0700104 Box(Modifier, backgroundColor = Color.Yellow)
Nader Jawaddcb72202020-05-05 20:03:50 -0700105 Canvas(Modifier) {
Jelle Fresen8f0a7852020-03-25 18:46:05 +0000106 drawn = true
107 }
108 }
109 }
110
111 // onIdle shouldn't timeout
112 onIdle()
113 // The compose view was off-screen, so it hasn't drawn yet
114 assertThat(drawn).isFalse()
115 }
116}