Jelle Fresen | 8f0a785 | 2020-03-25 18:46:05 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package androidx.ui.test |
| 18 | |
| 19 | import android.view.ViewGroup |
| 20 | import android.widget.FrameLayout |
| 21 | import androidx.activity.ComponentActivity |
Adam Powell | c916742 | 2020-04-08 16:50:37 -0700 | [diff] [blame] | 22 | import androidx.compose.Recomposer |
Jelle Fresen | 8f0a785 | 2020-03-25 18:46:05 +0000 | [diff] [blame] | 23 | import androidx.test.espresso.Espresso.onIdle |
| 24 | import androidx.test.filters.MediumTest |
| 25 | import androidx.ui.core.Modifier |
| 26 | import androidx.ui.core.setContent |
| 27 | import androidx.ui.foundation.Box |
Nader Jawad | dcb7220 | 2020-05-05 20:03:50 -0700 | [diff] [blame^] | 28 | import androidx.ui.foundation.Canvas |
Jelle Fresen | 8f0a785 | 2020-03-25 18:46:05 +0000 | [diff] [blame] | 29 | import androidx.ui.graphics.Color |
Andrey Kulikov | 1526ea5 | 2020-04-07 18:43:27 +0100 | [diff] [blame] | 30 | import androidx.ui.layout.fillMaxSize |
Jelle Fresen | 8f0a785 | 2020-03-25 18:46:05 +0000 | [diff] [blame] | 31 | import androidx.ui.test.android.AndroidComposeTestRule |
| 32 | import com.google.common.truth.Truth.assertThat |
| 33 | import org.junit.Rule |
| 34 | import org.junit.Test |
| 35 | |
| 36 | @MediumTest |
| 37 | class FirstDrawTest { |
| 38 | |
| 39 | @get:Rule |
Jelle Fresen | c67b052 | 2020-04-09 12:03:55 +0100 | [diff] [blame] | 40 | val testRule = AndroidComposeTestRule<ComponentActivity>() |
Jelle Fresen | 8f0a785 | 2020-03-25 18:46:05 +0000 | [diff] [blame] | 41 | |
| 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 Jawad | dcb7220 | 2020-05-05 20:03:50 -0700 | [diff] [blame^] | 49 | Canvas(Modifier.fillMaxSize()) { |
Jelle Fresen | 8f0a785 | 2020-03-25 18:46:05 +0000 | [diff] [blame] | 50 | drawn = true |
| 51 | } |
| 52 | } |
| 53 | onIdle() |
| 54 | assertThat(drawn).isTrue() |
| 55 | } |
| 56 | |
| 57 | /** |
Jelle Fresen | 3b5a839 | 2020-04-16 15:59:36 +0100 | [diff] [blame] | 58 | * 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 Jawad | dcb7220 | 2020-05-05 20:03:50 -0700 | [diff] [blame^] | 64 | Canvas(Modifier.fillMaxSize()) { |
Jelle Fresen | 3b5a839 | 2020-04-16 15:59:36 +0100 | [diff] [blame] | 65 | drawn = true |
| 66 | } |
| 67 | } |
| 68 | // onIdle() shouldn't be necessary |
| 69 | assertThat(drawn).isTrue() |
| 70 | } |
| 71 | |
| 72 | /** |
Jelle Fresen | 8f0a785 | 2020-03-25 18:46:05 +0000 | [diff] [blame] | 73 | * 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 Powell | c916742 | 2020-04-08 16:50:37 -0700 | [diff] [blame] | 104 | outOfBoundsView.setContent(Recomposer.current()) { |
Jelle Fresen | 8f0a785 | 2020-03-25 18:46:05 +0000 | [diff] [blame] | 105 | // If you see this box when running the test, the test is setup incorrectly |
Adam Powell | b6d8db2 | 2020-04-02 12:40:03 -0700 | [diff] [blame] | 106 | Box(Modifier, backgroundColor = Color.Yellow) |
Nader Jawad | dcb7220 | 2020-05-05 20:03:50 -0700 | [diff] [blame^] | 107 | Canvas(Modifier) { |
Jelle Fresen | 8f0a785 | 2020-03-25 18:46:05 +0000 | [diff] [blame] | 108 | 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 | } |