Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +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.core |
| 18 | |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 19 | import androidx.ui.core.LayoutNode.LayoutState |
George Mount | 17d6fe5 | 2020-05-15 08:08:23 -0700 | [diff] [blame] | 20 | import androidx.ui.util.fastForEach |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * There are some contracts between the tree of LayoutNodes and the state of AndroidComposeView |
| 24 | * which is hard to enforce but important to maintain. This method is intended to do the |
| 25 | * work only during our tests and will iterate through the tree to validate the states consistency. |
| 26 | */ |
George Mount | 5469e25 | 2020-06-17 10:01:42 -0700 | [diff] [blame] | 27 | @OptIn(ExperimentalLayoutNodeApi::class) |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 28 | internal class LayoutTreeConsistencyChecker( |
| 29 | private val root: LayoutNode, |
George Mount | 17d6fe5 | 2020-05-15 08:08:23 -0700 | [diff] [blame] | 30 | private val relayoutNodes: DepthSortedSet, |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 31 | private val postponedMeasureRequests: List<LayoutNode> |
| 32 | ) { |
| 33 | fun assertConsistent() { |
| 34 | val inconsistencyFound = !isTreeConsistent(root) |
| 35 | if (inconsistencyFound) { |
Andrey Kulikov | 387c76d | 2020-06-09 17:02:08 +0100 | [diff] [blame] | 36 | throw IllegalStateException("Inconsistency found! ${logTree()}") |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
George Mount | 17d6fe5 | 2020-05-15 08:08:23 -0700 | [diff] [blame] | 40 | private fun isTreeConsistent(node: LayoutNode): Boolean { |
| 41 | if (!node.consistentLayoutState()) { |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 42 | return false |
| 43 | } |
George Mount | 17d6fe5 | 2020-05-15 08:08:23 -0700 | [diff] [blame] | 44 | node.children.fastForEach { |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 45 | if (!isTreeConsistent(it)) { |
| 46 | return@isTreeConsistent false |
| 47 | } |
| 48 | } |
| 49 | return true |
| 50 | } |
| 51 | |
| 52 | private fun LayoutNode.consistentLayoutState(): Boolean { |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 53 | if (isPlaced) { |
| 54 | if (layoutState == LayoutState.NeedsRemeasure && |
| 55 | postponedMeasureRequests.contains(this) |
| 56 | ) { |
| 57 | // this node is waiting to be measured by parent or if this will not happen |
| 58 | // `onRequestMeasure` will be called for all items in `postponedMeasureRequests` |
| 59 | return true |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 60 | } |
Andrey Kulikov | 42d66a0 | 2020-06-22 17:38:53 +0100 | [diff] [blame] | 61 | // remeasure or relayout is scheduled |
| 62 | val parentLayoutState = parent?.layoutState |
| 63 | if (layoutState == LayoutState.NeedsRemeasure) { |
| 64 | return relayoutNodes.contains(this) || |
| 65 | parentLayoutState == LayoutState.NeedsRemeasure |
| 66 | } |
| 67 | if (layoutState == LayoutState.NeedsRelayout) { |
| 68 | return relayoutNodes.contains(this) || |
| 69 | parentLayoutState == LayoutState.NeedsRemeasure || |
| 70 | parentLayoutState == LayoutState.NeedsRelayout |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | return true |
| 74 | } |
| 75 | |
George Mount | 17d6fe5 | 2020-05-15 08:08:23 -0700 | [diff] [blame] | 76 | private fun nodeToString(node: LayoutNode): String { |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 77 | return with(StringBuilder()) { |
George Mount | 17d6fe5 | 2020-05-15 08:08:23 -0700 | [diff] [blame] | 78 | append(node) |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 79 | append("[${node.layoutState}]") |
George Mount | 17d6fe5 | 2020-05-15 08:08:23 -0700 | [diff] [blame] | 80 | if (!node.isPlaced) append("[!isPlaced]") |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 81 | append("[measuredByParent=${node.measuredByParent}]") |
George Mount | 17d6fe5 | 2020-05-15 08:08:23 -0700 | [diff] [blame] | 82 | if (!node.consistentLayoutState()) { |
| 83 | append("[INCONSISTENT]") |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 84 | } |
| 85 | toString() |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** Prints the nodes tree into the logs. */ |
Andrey Kulikov | 387c76d | 2020-06-09 17:02:08 +0100 | [diff] [blame] | 90 | private fun logTree(): String { |
| 91 | val stringBuilder = StringBuilder() |
George Mount | 17d6fe5 | 2020-05-15 08:08:23 -0700 | [diff] [blame] | 92 | fun printSubTree(node: LayoutNode, depth: Int) { |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 93 | var childrenDepth = depth |
| 94 | val nodeRepresentation = nodeToString(node) |
| 95 | if (nodeRepresentation.isNotEmpty()) { |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 96 | for (i in 0 until depth) { |
| 97 | stringBuilder.append("..") |
| 98 | } |
Jim Sproch | a88c07a | 2020-06-25 13:00:03 -0700 | [diff] [blame] | 99 | stringBuilder.appendLine(nodeRepresentation) |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 100 | childrenDepth += 1 |
| 101 | } |
George Mount | 17d6fe5 | 2020-05-15 08:08:23 -0700 | [diff] [blame] | 102 | node.children.fastForEach { printSubTree(it, childrenDepth) } |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 103 | } |
Jim Sproch | a88c07a | 2020-06-25 13:00:03 -0700 | [diff] [blame] | 104 | stringBuilder.appendLine("Tree state:") |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 105 | printSubTree(root, 0) |
Andrey Kulikov | 387c76d | 2020-06-09 17:02:08 +0100 | [diff] [blame] | 106 | return stringBuilder.toString() |
Andrey Kulikov | 8f43a95 | 2020-02-12 16:01:29 +0000 | [diff] [blame] | 107 | } |
| 108 | } |