-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also refactor one of the existing tests to use activity recreation instead of orientation change (which is not as reliable) Bug: 37930078 Bug: 35368213 Test: ./gradlew support-design:connectedCheck --info --daemon -Pandroid.testInstrumentationRunnerArguments.package=android.support.design.widget Change-Id: I1b9165fb11e03b9c855e118f31dee27e7dac8149 PiperOrigin-RevId: 177198734
- Loading branch information
1 parent
e7f01c6
commit 68c77f0
Showing
10 changed files
with
311 additions
and
43 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
64 changes: 64 additions & 0 deletions
64
testing/java/android/support/design/testapp/base/RecreatableAppCompatActivity.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,64 @@ | ||
/* | ||
* Copyright (C) 2017 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 android.support.design.testapp.base; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.support.v7.app.AppCompatActivity; | ||
import java.util.concurrent.CountDownLatch; | ||
|
||
/** | ||
* Activity that keeps track of resume / destroy lifecycle events, as well as of the last instance | ||
* of itself. | ||
*/ | ||
public class RecreatableAppCompatActivity extends AppCompatActivity { | ||
// These must be cleared after each test using clearState() | ||
@SuppressLint("StaticFieldLeak") // Not an issue because this is test-only and gets cleared | ||
public static RecreatableAppCompatActivity activity; | ||
|
||
public static CountDownLatch resumedLatch; | ||
public static CountDownLatch destroyedLatch; | ||
|
||
public static void clearState() { | ||
activity = null; | ||
resumedLatch = null; | ||
destroyedLatch = null; | ||
} | ||
|
||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
activity = this; | ||
} | ||
|
||
@Override | ||
protected void onResume() { | ||
super.onResume(); | ||
if (resumedLatch != null) { | ||
resumedLatch.countDown(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
if (destroyedLatch != null) { | ||
destroyedLatch.countDown(); | ||
} | ||
} | ||
} |
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
87 changes: 87 additions & 0 deletions
87
tests/javatests/android/support/design/testutils/ActivityUtils.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,87 @@ | ||
/* | ||
* Copyright (C) 2017 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 android.support.design.testutils; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import android.os.Looper; | ||
import android.support.design.testapp.base.RecreatableAppCompatActivity; | ||
import android.support.test.rule.ActivityTestRule; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** Utility methods for testing activities. */ | ||
public class ActivityUtils { | ||
private static final Runnable DO_NOTHING = | ||
new Runnable() { | ||
@Override | ||
public void run() {} | ||
}; | ||
|
||
public static void waitForExecution( | ||
final ActivityTestRule<? extends RecreatableAppCompatActivity> rule) { | ||
// Wait for two cycles. When starting a postponed transition, it will post to | ||
// the UI thread and then the execution will be added onto the queue after that. | ||
// The two-cycle wait makes sure fragments have the opportunity to complete both | ||
// before returning. | ||
try { | ||
rule.runOnUiThread(DO_NOTHING); | ||
rule.runOnUiThread(DO_NOTHING); | ||
} catch (Throwable throwable) { | ||
throw new RuntimeException(throwable); | ||
} | ||
} | ||
|
||
private static void runOnUiThreadRethrow( | ||
ActivityTestRule<? extends RecreatableAppCompatActivity> rule, Runnable r) { | ||
if (Looper.getMainLooper() == Looper.myLooper()) { | ||
r.run(); | ||
} else { | ||
try { | ||
rule.runOnUiThread(r); | ||
} catch (Throwable t) { | ||
throw new RuntimeException(t); | ||
} | ||
} | ||
} | ||
/** | ||
* Restarts the RecreatedAppCompatActivity and waits for the new activity to be resumed. | ||
* | ||
* @return The newly-restarted RecreatedAppCompatActivity | ||
*/ | ||
@SuppressWarnings("unchecked") // The type of the recreated activity is guaranteed to be T | ||
public static <T extends RecreatableAppCompatActivity> T recreateActivity( | ||
ActivityTestRule<? extends RecreatableAppCompatActivity> rule, final T activity) | ||
throws InterruptedException { | ||
// Now switch the orientation | ||
RecreatableAppCompatActivity.resumedLatch = new CountDownLatch(1); | ||
RecreatableAppCompatActivity.destroyedLatch = new CountDownLatch(1); | ||
runOnUiThreadRethrow( | ||
rule, | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
activity.recreate(); | ||
} | ||
}); | ||
assertTrue(RecreatableAppCompatActivity.resumedLatch.await(1, TimeUnit.SECONDS)); | ||
assertTrue(RecreatableAppCompatActivity.destroyedLatch.await(1, TimeUnit.SECONDS)); | ||
T newActivity = (T) RecreatableAppCompatActivity.activity; | ||
waitForExecution(rule); | ||
RecreatableAppCompatActivity.clearState(); | ||
return newActivity; | ||
} | ||
} |
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
Oops, something went wrong.