1 /*
2  * Copyright (C) 2021 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 com.android.systemui.qs
18 
19 import com.google.common.truth.Truth.assertThat
20 
21 import androidx.test.filters.SmallTest
22 
23 import android.testing.AndroidTestingRunner
24 import android.view.View
25 import android.view.ViewGroup
26 import android.widget.FrameLayout
27 import com.android.systemui.SysuiTestCase
28 import com.android.systemui.util.children
29 import org.junit.Before
30 import org.junit.Test
31 import org.junit.runner.RunWith
32 
33 @RunWith(AndroidTestingRunner::class)
34 @SmallTest
35 class QSPanelSwitchToParentTest : SysuiTestCase() {
36 
37     private lateinit var parent1: FrameLayout
38     private lateinit var parent2: FrameLayout
39 
40     private lateinit var movingView: View
41 
42     private lateinit var view1A: View
43     private lateinit var view1B: View
44     private lateinit var view1C: View
45 
46     private lateinit var view2A: View
47     private lateinit var view2B: View
48     private lateinit var view2C: View
49 
50     @Before
51     fun setUp() {
52         parent1 = FrameLayout(mContext)
53         parent2 = FrameLayout(mContext)
54 
55         movingView = View(mContext)
56 
57         view1A = View(mContext)
58         parent1.addView(view1A)
59         view1B = View(mContext)
60         parent1.addView(view1B)
61         view1C = View(mContext)
62         parent1.addView(view1C)
63 
64         view2A = View(mContext)
65         parent2.addView(view2A)
66         view2B = View(mContext)
67         parent2.addView(view2B)
68         view2C = View(mContext)
69         parent2.addView(view2C)
70     }
71 
72     @Test
73     fun testNullTargetNoInteractions() {
74         QSPanel.switchToParent(movingView, null, -1, "")
75 
76         assertThat(movingView.parent).isNull()
77     }
78 
79     @Test
80     fun testMoveToEndNoParent() {
81         QSPanel.switchToParent(movingView, parent2, -1, "")
82 
83         assertThat(parent1.childrenList).containsExactly(
84                 view1A, view1B, view1C
85         )
86 
87         assertThat(parent2.childrenList).containsExactly(
88                 view2A, view2B, view2C, movingView
89         )
90     }
91 
92     @Test
93     fun testMoveToEndDifferentParent() {
94         parent1.addView(movingView, 0)
95 
96         QSPanel.switchToParent(movingView, parent2, -1, "")
97 
98         assertThat(parent1.childrenList).containsExactly(
99                 view1A, view1B, view1C
100         )
101         assertThat(parent2.childrenList).containsExactly(
102                 view2A, view2B, view2C, movingView
103         )
104     }
105 
106     @Test
107     fun testMoveToEndSameParent() {
108         parent2.addView(movingView, 0)
109 
110         QSPanel.switchToParent(movingView, parent2, -1, "")
111 
112         assertThat(parent1.childrenList).containsExactly(
113                 view1A, view1B, view1C
114         )
115         assertThat(parent2.childrenList).containsExactly(
116                 view2A, view2B, view2C, movingView
117         )
118     }
119 
120     @Test
121     fun testMoveToMiddleFromNoParent() {
122         QSPanel.switchToParent(movingView, parent2, 1, "")
123 
124         assertThat(parent1.childrenList).containsExactly(
125                 view1A, view1B, view1C
126         )
127         assertThat(parent2.childrenList).containsExactly(
128                 view2A, movingView, view2B, view2C
129         )
130     }
131 
132     @Test
133     fun testMoveToMiddleDifferentParent() {
134         parent1.addView(movingView, 1)
135 
136         QSPanel.switchToParent(movingView, parent2, 2, "")
137 
138         assertThat(parent1.childrenList).containsExactly(
139                 view1A, view1B, view1C
140         )
141         assertThat(parent2.childrenList).containsExactly(
142                 view2A, view2B, movingView, view2C
143         )
144     }
145 
146     @Test
147     fun testMoveToMiddleSameParent() {
148         parent2.addView(movingView, 0)
149 
150         QSPanel.switchToParent(movingView, parent2, 1, "")
151 
152         assertThat(parent1.childrenList).containsExactly(
153                 view1A, view1B, view1C
154         )
155         assertThat(parent2.childrenList).containsExactly(
156                 view2A, movingView, view2B, view2C
157         )
158     }
159 
160     private val ViewGroup.childrenList: List<View>
161         get() = children.toList()
162 }