Make tests more robust

Extract switchToParent logic and make some tests more robusts.

This change is to minimize divergence between branches

Bug: 195104944
Test: atest QSPanelTest QSPanelSwitchToParentTest
Change-Id: Ibc85cd5501010fcf9752d8af103500479c6f28c2
This commit is contained in:
Fabian Kozynski
2021-07-30 15:01:14 -04:00
parent d31dbcf198
commit e52042c878
3 changed files with 203 additions and 10 deletions

View File

@@ -27,12 +27,15 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import androidx.annotation.VisibleForTesting;
import com.android.internal.logging.UiEventLogger;
import com.android.internal.widget.RemeasuringLinearLayout;
import com.android.systemui.R;
@@ -386,13 +389,7 @@ public class QSPanel extends LinearLayout implements Tunable {
}
private void switchToParent(View child, ViewGroup parent, int index) {
ViewGroup currentParent = (ViewGroup) child.getParent();
if (currentParent != parent || currentParent.indexOfChild(child) != index) {
if (currentParent != null) {
currentParent.removeView(child);
}
parent.addView(child, index);
}
switchToParent(child, parent, index, getDumpableTag());
}
/** Call when orientation has changed and MediaHost needs to be adjusted. */
@@ -766,4 +763,29 @@ public class QSPanel extends LinearLayout implements Tunable {
interface OnConfigurationChangedListener {
void onConfigurationChange(Configuration newConfig);
}
@VisibleForTesting
static void switchToParent(View child, ViewGroup parent, int index, String tag) {
if (parent == null) {
Log.w(tag, "Trying to move view to null parent",
new IllegalStateException());
return;
}
ViewGroup currentParent = (ViewGroup) child.getParent();
if (currentParent != parent) {
if (currentParent != null) {
currentParent.removeView(child);
}
parent.addView(child, index);
return;
}
// Same parent, we are just changing indices
int currentIndex = parent.indexOfChild(child);
if (currentIndex == index) {
// We want to be in the same place. Nothing to do here
return;
}
parent.removeView(child);
parent.addView(child, index);
}
}

View File

@@ -0,0 +1,162 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.qs
import com.google.common.truth.Truth.assertThat
import androidx.test.filters.SmallTest
import android.testing.AndroidTestingRunner
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.android.systemui.SysuiTestCase
import com.android.systemui.util.children
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidTestingRunner::class)
@SmallTest
class QSPanelSwitchToParentTest : SysuiTestCase() {
private lateinit var parent1: FrameLayout
private lateinit var parent2: FrameLayout
private lateinit var movingView: View
private lateinit var view1A: View
private lateinit var view1B: View
private lateinit var view1C: View
private lateinit var view2A: View
private lateinit var view2B: View
private lateinit var view2C: View
@Before
fun setUp() {
parent1 = FrameLayout(mContext)
parent2 = FrameLayout(mContext)
movingView = View(mContext)
view1A = View(mContext)
parent1.addView(view1A)
view1B = View(mContext)
parent1.addView(view1B)
view1C = View(mContext)
parent1.addView(view1C)
view2A = View(mContext)
parent2.addView(view2A)
view2B = View(mContext)
parent2.addView(view2B)
view2C = View(mContext)
parent2.addView(view2C)
}
@Test
fun testNullTargetNoInteractions() {
QSPanel.switchToParent(movingView, null, -1, "")
assertThat(movingView.parent).isNull()
}
@Test
fun testMoveToEndNoParent() {
QSPanel.switchToParent(movingView, parent2, -1, "")
assertThat(parent1.childrenList).containsExactly(
view1A, view1B, view1C
)
assertThat(parent2.childrenList).containsExactly(
view2A, view2B, view2C, movingView
)
}
@Test
fun testMoveToEndDifferentParent() {
parent1.addView(movingView, 0)
QSPanel.switchToParent(movingView, parent2, -1, "")
assertThat(parent1.childrenList).containsExactly(
view1A, view1B, view1C
)
assertThat(parent2.childrenList).containsExactly(
view2A, view2B, view2C, movingView
)
}
@Test
fun testMoveToEndSameParent() {
parent2.addView(movingView, 0)
QSPanel.switchToParent(movingView, parent2, -1, "")
assertThat(parent1.childrenList).containsExactly(
view1A, view1B, view1C
)
assertThat(parent2.childrenList).containsExactly(
view2A, view2B, view2C, movingView
)
}
@Test
fun testMoveToMiddleFromNoParent() {
QSPanel.switchToParent(movingView, parent2, 1, "")
assertThat(parent1.childrenList).containsExactly(
view1A, view1B, view1C
)
assertThat(parent2.childrenList).containsExactly(
view2A, movingView, view2B, view2C
)
}
@Test
fun testMoveToMiddleDifferentParent() {
parent1.addView(movingView, 1)
QSPanel.switchToParent(movingView, parent2, 2, "")
assertThat(parent1.childrenList).containsExactly(
view1A, view1B, view1C
)
assertThat(parent2.childrenList).containsExactly(
view2A, view2B, movingView, view2C
)
}
@Test
fun testMoveToMiddleSameParent() {
parent2.addView(movingView, 0)
QSPanel.switchToParent(movingView, parent2, 1, "")
assertThat(parent1.childrenList).containsExactly(
view1A, view1B, view1C
)
assertThat(parent2.childrenList).containsExactly(
view2A, movingView, view2B, view2C
)
}
private val ViewGroup.childrenList: List<View>
get() = children.toList()
}

View File

@@ -69,6 +69,8 @@ class QSPanelTest : SysuiTestCase() {
@Mock
private lateinit var mQSTileView: QSTileView
private lateinit var mFooter: View
@Before
@Throws(Exception::class)
fun setup() {
@@ -81,7 +83,8 @@ class QSPanelTest : SysuiTestCase() {
mQsPanel = QSPanel(mContext, null)
mQsPanel.initialize()
// QSPanel inflates a footer inside of it, mocking it here
mQsPanel.addView(LinearLayout(mContext).apply { id = R.id.qs_footer })
mFooter = LinearLayout(mContext).apply { id = R.id.qs_footer }
mQsPanel.addView(mFooter)
mQsPanel.onFinishInflate()
mQsPanel.setSecurityFooter(View(mContext), false)
mQsPanel.setHeaderContainer(LinearLayout(mContext))
@@ -125,7 +128,10 @@ class QSPanelTest : SysuiTestCase() {
mQsPanel.isExpanded = true
}
assertThat(mQsPanel.indexOfChild(mQsPanel.mSecurityFooter)).isEqualTo(2)
// After mFooter
assertThat(mQsPanel.indexOfChild(mQsPanel.mSecurityFooter)).isEqualTo(
mQsPanel.indexOfChild(mFooter) + 1
)
}
@Test
@@ -137,7 +143,10 @@ class QSPanelTest : SysuiTestCase() {
mQsPanel.isExpanded = true
}
assertThat(mQsPanel.indexOfChild(mQsPanel.mSecurityFooter)).isEqualTo(2)
// After mFooter
assertThat(mQsPanel.indexOfChild(mQsPanel.mSecurityFooter)).isEqualTo(
mQsPanel.indexOfChild(mFooter) + 1
)
}
@Test