Merge "Make WM Jetpack unit tests mockable" into tm-dev

This commit is contained in:
Charles Chen
2022-03-31 03:35:00 +00:00
committed by Android (Google) Code Review
3 changed files with 91 additions and 3 deletions

View File

@@ -43,6 +43,8 @@ import android.window.WindowContainerTransaction;
import androidx.window.common.EmptyLifecycleCallbacksAdapter;
import com.android.internal.annotations.VisibleForTesting;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@@ -65,10 +67,12 @@ public class SplitController implements JetpackTaskFragmentOrganizer.TaskFragmen
* When the app is host of multiple Tasks, there can be multiple splits controlled by the same
* organizer.
*/
private final SparseArray<TaskContainer> mTaskContainers = new SparseArray<>();
@VisibleForTesting
final SparseArray<TaskContainer> mTaskContainers = new SparseArray<>();
// Callback to Jetpack to notify about changes to split states.
private @NonNull Consumer<List<SplitInfo>> mEmbeddingCallback;
@NonNull
private Consumer<List<SplitInfo>> mEmbeddingCallback;
private final List<SplitInfo> mLastReportedSplitStates = new ArrayList<>();
// We currently only support split activity embedding within the one root Task.
@@ -1029,7 +1033,8 @@ public class SplitController implements JetpackTaskFragmentOrganizer.TaskFragmen
}
/** Represents TaskFragments and split pairs below a Task. */
private static class TaskContainer {
@VisibleForTesting
static class TaskContainer {
final List<TaskFragmentContainer> mContainers = new ArrayList<>();
final List<SplitContainer> mSplitContainers = new ArrayList<>();
}

View File

@@ -46,6 +46,12 @@ android_test {
"android.test.runner",
],
// These are not normally accessible from apps so they must be explicitly included.
jni_libs: [
"libdexmakerjvmtiagent",
"libstaticjvmtiagent",
],
optimize: {
enabled: false,
},

View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 2022 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 androidx.window.extensions.embedding;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
import androidx.window.extensions.embedding.SplitController.TaskContainer;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@SmallTest
@RunWith(AndroidJUnit4.class)
public class SplitControllerTest {
private static final int TASK_ID = 10;
private SplitController mSplitController;
@Before
public void setUp() {
mSplitController = new SplitController();
spyOn(mSplitController);
}
@Test
public void testGetTopActiveContainer() {
TaskContainer taskContainer = new TaskContainer();
// tf3 is finished so is not active.
TaskFragmentContainer tf3 = mock(TaskFragmentContainer.class);
doReturn(true).when(tf3).isFinished();
// tf2 has running activity so is active.
TaskFragmentContainer tf2 = mock(TaskFragmentContainer.class);
doReturn(1).when(tf2).getRunningActivityCount();
// tf1 has no running activity so is not active.
TaskFragmentContainer tf1 = new TaskFragmentContainer(null, TASK_ID);
taskContainer.mContainers.add(tf3);
taskContainer.mContainers.add(tf2);
taskContainer.mContainers.add(tf1);
mSplitController.mTaskContainers.put(TASK_ID, taskContainer);
assertWithMessage("Must return tf2 because tf3 is not active.")
.that(mSplitController.getTopActiveContainer(TASK_ID)).isEqualTo(tf2);
taskContainer.mContainers.remove(tf1);
assertWithMessage("Must return tf2 because tf2 has running activity.")
.that(mSplitController.getTopActiveContainer(TASK_ID)).isEqualTo(tf2);
taskContainer.mContainers.remove(tf2);
assertWithMessage("Must return null because tf1 has no running activity.")
.that(mSplitController.getTopActiveContainer(TASK_ID)).isNull();
}
}