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 |
Louis Pullen-Freilich | ddda7be | 2020-07-17 18:28:12 +0100 | [diff] [blame] | 27 | import androidx.compose.foundation.Box |
| 28 | import androidx.compose.foundation.Canvas |
Jelle Fresen | 8f0a785 | 2020-03-25 18:46:05 +0000 | [diff] [blame] | 29 | import androidx.ui.graphics.Color |
Louis Pullen-Freilich | 623e405 | 2020-07-19 20:24:03 +0100 | [diff] [blame] | 30 | import androidx.compose.foundation.layout.fillMaxSize |
Filip Pavlis | b3943e1 | 2020-07-22 14:39:42 +0100 | [diff] [blame] | 31 | import androidx.ui.test.android.createAndroidComposeRule |
Jelle Fresen | 8f0a785 | 2020-03-25 18:46:05 +0000 | [diff] [blame] | 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 |
Filip Pavlis | b3943e1 | 2020-07-22 14:39:42 +0100 | [diff] [blame] | 40 | val testRule = createAndroidComposeRule<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 | |
Filip Pavlis | 607cc41 | 2020-06-11 21:56:49 +0100 | [diff] [blame] | 80 | testRule.activityRule.scenario.onActivity { activity -> |
Jelle Fresen | 8f0a785 | 2020-03-25 18:46:05 +0000 | [diff] [blame] | 81 | // 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 Powell | c916742 | 2020-04-08 16:50:37 -0700 | [diff] [blame] | 102 | outOfBoundsView.setContent(Recomposer.current()) { |
Jelle Fresen | 8f0a785 | 2020-03-25 18:46:05 +0000 | [diff] [blame] | 103 | // 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] | 104 | Box(Modifier, backgroundColor = Color.Yellow) |
Nader Jawad | dcb7220 | 2020-05-05 20:03:50 -0700 | [diff] [blame] | 105 | Canvas(Modifier) { |
Jelle Fresen | 8f0a785 | 2020-03-25 18:46:05 +0000 | [diff] [blame] | 106 | 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 | } |