forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Android: ensuring onDestroy called between each test
The previous onDestroy in tests change only blocked until onDestroy with certain subclasses of ChromeActivityTestRule which called launchActivity. This change blocks all tests on onDestroy. Bug: 908174 Change-Id: I074054c28d002e012d0f6ebc95a53aadf4175eaa Reviewed-on: https://chromium-review.googlesource.com/c/1473330 Commit-Queue: Sam Maier <smaier@chromium.org> Auto-Submit: Sam Maier <smaier@chromium.org> Reviewed-by: Andrew Grieve <agrieve@chromium.org> Cr-Commit-Position: refs/heads/master@{#636023}
- Loading branch information
Sam Maier
authored and
Commit Bot
committed
Feb 27, 2019
1 parent
c5b820e
commit 68320b8
Showing
8 changed files
with
112 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
base/test/android/javatests/src/org/chromium/base/test/DestroyActivitiesRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2019 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.base.test; | ||
|
||
import android.app.Activity; | ||
import android.os.Build; | ||
|
||
import org.junit.rules.ExternalResource; | ||
|
||
import org.chromium.base.ActivityState; | ||
import org.chromium.base.ApplicationStatus; | ||
import org.chromium.base.Log; | ||
import org.chromium.base.ThreadUtils; | ||
import org.chromium.base.test.util.CallbackHelper; | ||
|
||
import java.util.concurrent.TimeoutException; | ||
|
||
/** | ||
* This is to ensure all calls to onDestroy() are performed before starting the next test. | ||
* We could probably remove this when crbug.com/932130 is fixed. | ||
*/ | ||
public class DestroyActivitiesRule extends ExternalResource { | ||
private static final String TAG = "DestroyActivities"; | ||
@Override | ||
public void after() { | ||
if (ApplicationStatus.isInitialized()) { | ||
CallbackHelper allDestroyedCalledback = new CallbackHelper(); | ||
ApplicationStatus.ActivityStateListener activityStateListener = | ||
(activity, newState) -> { | ||
switch (newState) { | ||
case ActivityState.DESTROYED: | ||
if (ApplicationStatus.isEveryActivityDestroyed()) { | ||
allDestroyedCalledback.notifyCalled(); | ||
} | ||
break; | ||
case ActivityState.CREATED: | ||
if (!activity.isFinishing()) { | ||
activity.finish(); | ||
} | ||
break; | ||
} | ||
}; | ||
ApplicationStatus.registerStateListenerForAllActivities(activityStateListener); | ||
ThreadUtils.runOnUiThread(() -> { | ||
for (Activity a : ApplicationStatus.getRunningActivities()) { | ||
if (!a.isFinishing()) { | ||
a.finish(); | ||
} | ||
} | ||
}); | ||
try { | ||
if (!ApplicationStatus.isEveryActivityDestroyed()) { | ||
allDestroyedCalledback.waitForCallback(); | ||
} | ||
} catch (InterruptedException | TimeoutException e) { | ||
// There appears to be a framework bug on KitKat where onStop and onDestroy are not | ||
// called for a handful of tests. | ||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { | ||
Log.w(TAG, "Activity failed to be destroyed after a test"); | ||
for (Activity a : ApplicationStatus.getRunningActivities()) { | ||
ApplicationStatus.onStateChangeForTesting(a, ActivityState.DESTROYED); | ||
} | ||
throw new RuntimeException(e); | ||
} | ||
} finally { | ||
ApplicationStatus.unregisterActivityStateListener(activityStateListener); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters