diff options
| author | 2021-07-30 15:01:14 -0400 | |
|---|---|---|
| committer | 2021-07-30 16:29:34 -0400 | |
| commit | e52042c87881fd093960287356c571af05faa7ab (patch) | |
| tree | 7c11521ef0f0a86b799abb93cd6100f36ee1c218 | |
| parent | d31dbcf198c962e4d7e2442ac80cd9819af1a3ac (diff) | |
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
3 files changed, 203 insertions, 10 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java index 0c655103d888..63124ab2d80d 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java +++ b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java @@ -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); + } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/QSPanelSwitchToParentTest.kt b/packages/SystemUI/tests/src/com/android/systemui/qs/QSPanelSwitchToParentTest.kt new file mode 100644 index 000000000000..56f2905e834f --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/qs/QSPanelSwitchToParentTest.kt @@ -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() +}
\ No newline at end of file diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/QSPanelTest.kt b/packages/SystemUI/tests/src/com/android/systemui/qs/QSPanelTest.kt index a83a5e1f5484..3500c183de39 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/qs/QSPanelTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/qs/QSPanelTest.kt @@ -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 |