Merge "Fixing flakiness in robo tests" into main

This commit is contained in:
Sunny Goyal
2024-09-26 16:58:20 +00:00
committed by Android (Google) Code Review
3 changed files with 34 additions and 41 deletions

View File

@@ -108,6 +108,25 @@ public class MainThreadInitializedObject<T extends SafeCloseable> {
*/
<T extends SafeCloseable> T getObject(MainThreadInitializedObject<T> object);
/**
* Put a value into cache, can be used to put mocked MainThreadInitializedObject
* instances.
*/
<T extends SafeCloseable> void putObject(MainThreadInitializedObject<T> object, T value);
/**
* Returns whether this context should cleanup all objects when its destroyed or leave it
* to the GC.
* These objects can have listeners attached to the system server and mey not be able to get
* GCed themselves when running on a device.
* Some environments like Robolectric tear down the whole system at the end of the test,
* so manual cleanup may not be required.
*/
default boolean shouldCleanUpOnDestroy() {
return true;
}
@UiThread
default <T extends SafeCloseable> T createObject(MainThreadInitializedObject<T> object) {
return object.mProvider.get((Context) this);
@@ -138,7 +157,19 @@ public class MainThreadInitializedObject<T extends SafeCloseable> {
return this;
}
@Override
public boolean shouldCleanUpOnDestroy() {
return (getBaseContext().getApplicationContext() instanceof SandboxApplication sa)
? sa.shouldCleanUpOnDestroy() : true;
}
public void onDestroy() {
if (shouldCleanUpOnDestroy()) {
cleanUpObjects();
}
}
protected void cleanUpObjects() {
getAppComponent().getDaggerSingletonTracker().close();
synchronized (mDestroyLock) {
// Destroy in reverse order
@@ -174,10 +205,7 @@ public class MainThreadInitializedObject<T extends SafeCloseable> {
}
}
/**
* Put a value into mObjectMap, can be used to put mocked MainThreadInitializedObject
* instances into SandboxContext.
*/
@Override
public <T extends SafeCloseable> void putObject(
MainThreadInitializedObject<T> object, T value) {
mObjectMap.put(object, value);

View File

@@ -1,35 +0,0 @@
/*
* Copyright (C) 2024 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.launcher3
import com.android.launcher3.util.MainThreadInitializedObject
import com.android.launcher3.util.MainThreadInitializedObject.SandboxApplication
import com.android.launcher3.util.SafeCloseable
/**
* Initializes [MainThreadInitializedObject] instances for Robolectric tests.
*
* Unlike instrumentation tests, Robolectric creates a new application instance for each test, which
* could cause the various static objects defined in [MainThreadInitializedObject] to leak. Thus, a
* [SandboxApplication] for Robolectric tests can implement this interface to limit the lifecycle of
* these objects to a single test.
*/
interface RoboObjectInitializer {
/** Overrides an object with [type] to [value]. */
fun <T : SafeCloseable> initializeObject(type: MainThreadInitializedObject<T>, value: T)
}

View File

@@ -283,11 +283,11 @@ public class LauncherModelHelper {
}
@Override
public void onDestroy() {
protected void cleanUpObjects() {
if (deleteContents(mDbDir)) {
mDbDir.delete();
}
super.onDestroy();
super.cleanUpObjects();
}
@Override