Skip to content

Commit bb2c2cd

Browse files
brettchabotcopybara-androidxtest
authored andcommitted
Use PlatformTestStorage instead of hardcoding TestStorage in Bitmap.writeToTestStorage.
This will unlock usage in environments where TestStorage is not available. PiperOrigin-RevId: 605044658
1 parent 46b1037 commit bb2c2cd

File tree

11 files changed

+50
-105
lines changed

11 files changed

+50
-105
lines changed

core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
**API Changes**
1414

1515
* Added ApplicationInfoBuilder.setFlags(int)
16+
* Make Bitmap.writeToTestStorage use the registered PlatformTestStorage instead of hardcoding TestStorage
1617

1718
**Breaking API Changes**
1819

core/java/androidx/test/core/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ kt_android_library(
4949
"//annotation",
5050
"//opensource/androidx:annotation",
5151
"//runner/monitor",
52-
"//services/storage",
5352
"@maven//:androidx_concurrent_concurrent_futures",
5453
"@maven//:androidx_lifecycle_lifecycle_common",
5554
"@maven//:androidx_tracing_tracing",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
// Signature format: 3.0
2+
package androidx.test.core.graphics {
3+
4+
public final class BitmapStorage {
5+
method @Deprecated @RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP) public static void writeToTestStorage(android.graphics.Bitmap, androidx.test.platform.io.PlatformTestStorage testStorage, String name) throws java.io.IOException;
6+
}
7+
8+
}
9+

core/java/androidx/test/core/api/current_public.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ package androidx.test.core.content.pm {
5050

5151
}
5252

53+
package androidx.test.core.graphics {
54+
55+
public final class BitmapStorage {
56+
}
57+
58+
}
59+
5360
package androidx.test.core.os {
5461

5562
public final class Parcelables {

core/java/androidx/test/core/graphics/BitmapStorageExt.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,42 @@
1919
package androidx.test.core.graphics
2020

2121
import android.graphics.Bitmap
22+
import androidx.annotation.RestrictTo
2223
import androidx.test.annotation.ExperimentalTestApi
2324
import androidx.test.platform.io.PlatformTestStorage
24-
import androidx.test.services.storage.TestStorage
25+
import androidx.test.platform.io.PlatformTestStorageRegistry
2526
import java.io.IOException
2627

2728
/**
28-
* Writes the contents of the [Bitmap] to a compressed png file on [TestStorage]
29+
* Writes the contents of the [Bitmap] to a compressed png file on [PlatformTestStorage]
2930
*
3031
* @param name a descriptive base name for the resulting file. '.png' will be appended to this name.
3132
* @throws IOException if bitmap could not be compressed or written to ds
3233
*/
3334
@ExperimentalTestApi
3435
@Throws(IOException::class)
3536
fun Bitmap.writeToTestStorage(name: String) {
36-
writeToTestStorage(TestStorage(), name)
37+
writeToTestStorage(PlatformTestStorageRegistry.getInstance(), name)
3738
}
3839

3940
/**
40-
* Writes the contents of the [Bitmap] to a compressed png file to the given [PlatformTestStorage]
41-
*
42-
* @param testStorage the [PlatformTestStorage] to use
43-
* @param name a descriptive base name for the resulting file
44-
* @throws IOException if bitmap could not be compressed or written to storage
41+
* @deprecated
42+
* @hide
4543
*/
46-
@ExperimentalTestApi
44+
@Deprecated(
45+
"use PlatformTestStorageRegistry.setInstance in the rare cases where you want to override the PlatformTestStorage to use",
46+
replaceWith = ReplaceWith("writeToTestStorage()"),
47+
)
48+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) // legacy - used by espresso 3.5.0 DefaultFailureHandler
4749
@Throws(IOException::class)
4850
fun Bitmap.writeToTestStorage(testStorage: PlatformTestStorage, name: String) {
4951
testStorage.openOutputFile("$name.png").use {
50-
if (!this.compress(
52+
if (
53+
!this.compress(
5154
Bitmap.CompressFormat.PNG,
5255
/** PNG is lossless, so quality is ignored. */
5356
0,
54-
it
57+
it,
5558
)
5659
) {
5760
throw IOException("Failed to compress bitmap")

core/javatests/androidx/test/core/graphics/BitmapStorageTest.kt

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,7 @@
1616
package androidx.test.core.graphics
1717

1818
import android.graphics.Bitmap
19-
import android.net.Uri
2019
import androidx.test.ext.junit.runners.AndroidJUnit4
21-
import androidx.test.platform.io.PlatformTestStorage
22-
import java.io.IOException
23-
import java.io.InputStream
24-
import java.io.OutputStream
25-
import java.io.Serializable
26-
import org.junit.Assert.assertThrows
2720
import org.junit.Test
2821
import org.junit.runner.RunWith
2922

@@ -35,62 +28,4 @@ class BitmapStorageTest {
3528
val bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)
3629
bitmap.writeToTestStorage("test")
3730
}
38-
39-
@Test
40-
fun writeToTestStorage_throws() {
41-
val bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)
42-
assertThrows(IOException::class.java) {
43-
bitmap.writeToTestStorage(ThrowingPlatformTestStorage(), "test")
44-
}
45-
}
46-
47-
class ThrowingPlatformTestStorage : PlatformTestStorage {
48-
override fun openInputFile(pathname: String?): InputStream {
49-
TODO("Not yet implemented")
50-
}
51-
52-
override fun getInputArg(argName: String?): String {
53-
TODO("Not yet implemented")
54-
}
55-
56-
override fun getInputArgs(): MutableMap<String, String> {
57-
TODO("Not yet implemented")
58-
}
59-
60-
override fun openOutputFile(pathname: String?): OutputStream {
61-
throw IOException("error")
62-
}
63-
64-
override fun openOutputFile(pathname: String?, append: Boolean): OutputStream {
65-
TODO("Not yet implemented")
66-
}
67-
68-
override fun addOutputProperties(properties: MutableMap<String, Serializable>?) {
69-
TODO("Not yet implemented")
70-
}
71-
72-
override fun getOutputProperties(): MutableMap<String, Serializable> {
73-
TODO("Not yet implemented")
74-
}
75-
76-
override fun openInternalInputFile(pathname: String?): InputStream {
77-
TODO("Not yet implemented")
78-
}
79-
80-
override fun openInternalOutputFile(pathname: String?): OutputStream {
81-
TODO("Not yet implemented")
82-
}
83-
84-
override fun getInputFileUri2(pathname: String): Uri {
85-
TODO("Not yet implemented")
86-
}
87-
88-
override fun getOutputFileUri2(pathname: String): Uri {
89-
TODO("Not yet implemented")
90-
}
91-
92-
override fun isTestStorageFilePath(pathname: String): Boolean {
93-
TODO("Not yet implemented")
94-
}
95-
}
9631
}

espresso/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The following artifacts were released:
1717
**Bug Fixes**
1818

1919
* Fix slow inRoot operations in Robolectric
20+
* Use PlatformTestStorageRegistry.getInstance consistently instead of passing a reference around
2021

2122
**New Features**
2223

espresso/core/java/androidx/test/espresso/base/BaseLayerModule.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import androidx.test.internal.platform.ServiceLoaderWrapper;
3131
import androidx.test.internal.platform.os.ControlledLooper;
3232
import androidx.test.platform.app.InstrumentationRegistry;
33-
import androidx.test.platform.io.PlatformTestStorage;
3433
import androidx.test.platform.tracing.Tracing;
3534
import androidx.test.runner.lifecycle.ActivityLifecycleMonitor;
3635
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
@@ -173,9 +172,8 @@ FailureHandler provideFailureHander(DefaultFailureHandler impl) {
173172
}
174173

175174
@Provides
176-
DefaultFailureHandler provideDefaultFailureHander(
177-
@TargetContext Context context, PlatformTestStorage testStorage) {
178-
return new DefaultFailureHandler(context, testStorage, true);
175+
DefaultFailureHandler provideDefaultFailureHander(@TargetContext Context context) {
176+
return new DefaultFailureHandler(context, true);
179177
}
180178

181179
@Provides

espresso/core/java/androidx/test/espresso/base/DefaultFailureHandler.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,11 @@
3131
import androidx.test.espresso.base.ViewHierarchyExceptionHandler.Truncater;
3232
import androidx.test.espresso.internal.inject.TargetContext;
3333
import androidx.test.internal.platform.util.TestOutputEmitter;
34-
import androidx.test.platform.io.PlatformTestStorage;
35-
import androidx.test.platform.io.PlatformTestStorageRegistry;
3634
import java.io.IOException;
3735
import java.util.ArrayList;
3836
import java.util.Arrays;
3937
import java.util.List;
4038
import java.util.concurrent.atomic.AtomicInteger;
41-
import javax.inject.Inject;
4239
import junit.framework.AssertionFailedError;
4340
import org.hamcrest.Matcher;
4441

@@ -50,23 +47,15 @@ public final class DefaultFailureHandler implements FailureHandler {
5047

5148
private static final AtomicInteger failureCount = new AtomicInteger(0);
5249
private final List<FailureHandler> handlers = new ArrayList<>();
53-
private final PlatformTestStorage testStorage;
5450
private final boolean captureScreenshotOnFailure;
5551

5652
public DefaultFailureHandler(@TargetContext Context appContext) {
57-
this(appContext, PlatformTestStorageRegistry.getInstance(), true);
53+
this(appContext, true);
5854
}
5955

6056
public DefaultFailureHandler(
6157
@TargetContext Context appContext, boolean captureScreenshotOnFailure) {
62-
this(appContext, PlatformTestStorageRegistry.getInstance(), captureScreenshotOnFailure);
63-
}
6458

65-
@Inject
66-
DefaultFailureHandler(
67-
@TargetContext Context appContext,
68-
PlatformTestStorage testStorage,
69-
boolean captureScreenshotOnFailure) {
7059
// Adds a chain of exception handlers.
7160
// Order matters and a matching failure handler in the chain will throw after the exception is
7261
// handled. Always adds the handler of the child class ahead of its superclasses to make sure
@@ -77,17 +66,14 @@ public DefaultFailureHandler(
7766
// PerformException ---------> EspressoException
7867
// ---------> Throwable
7968
// AssertionError ----------->
80-
this.testStorage = testStorage;
8169
this.captureScreenshotOnFailure = captureScreenshotOnFailure;
8270
handlers.add(
8371
new ViewHierarchyExceptionHandler<>(
84-
testStorage,
8572
failureCount,
8673
NoMatchingViewException.class,
8774
getNoMatchingViewExceptionTruncater()));
8875
handlers.add(
8976
new ViewHierarchyExceptionHandler<>(
90-
testStorage,
9177
failureCount,
9278
AmbiguousViewMatcherException.class,
9379
getAmbiguousViewMatcherExceptionTruncater()));
@@ -141,8 +127,7 @@ private void takeScreenshot(String outputName) {
141127
}
142128
try {
143129
if (DeviceCapture.canTakeScreenshot()) {
144-
BitmapStorage.writeToTestStorage(
145-
DeviceCapture.takeScreenshotNoSync(), testStorage, outputName);
130+
BitmapStorage.writeToTestStorage(DeviceCapture.takeScreenshotNoSync(), outputName);
146131
} else {
147132
TestOutputEmitter.takeScreenshot(outputName + ".png");
148133
}

espresso/core/java/androidx/test/espresso/base/ViewHierarchyExceptionHandler.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package androidx.test.espresso.base;
1717

1818
import static androidx.test.espresso.util.Throwables.throwIfUnchecked;
19-
import static androidx.test.internal.util.Checks.checkNotNull;
2019

2120
import android.util.Log;
2221
import android.view.View;
@@ -25,6 +24,7 @@
2524
import androidx.test.espresso.base.DefaultFailureHandler.TypedFailureHandler;
2625
import androidx.test.espresso.util.HumanReadables;
2726
import androidx.test.platform.io.PlatformTestStorage;
27+
import androidx.test.platform.io.PlatformTestStorageRegistry;
2828
import androidx.test.services.storage.TestStorageException;
2929
import java.io.IOException;
3030
import java.io.OutputStream;
@@ -44,7 +44,6 @@ class ViewHierarchyExceptionHandler<T extends Throwable & RootViewException>
4444
private static final int MAX_MSG_SIZE = (64 - 2) * 1024;
4545
private static final String VIEW_HIERARCHY_CHAR_LIMIT = "view_hierarchy_char_limit";
4646

47-
private final PlatformTestStorage testStorage;
4847
private final AtomicInteger failureCount;
4948
private final Truncater<T> truncater;
5049

@@ -53,12 +52,10 @@ interface Truncater<T> {
5352
}
5453

5554
public ViewHierarchyExceptionHandler(
56-
PlatformTestStorage testStorage,
5755
AtomicInteger failureCount,
5856
Class<T> expectedType,
5957
Truncater<T> truncater) {
6058
super(expectedType);
61-
this.testStorage = checkNotNull(testStorage);
6259
this.failureCount = failureCount;
6360
this.truncater = truncater;
6461
}
@@ -79,6 +76,7 @@ public void handleSafely(T exception, Matcher<View> viewMatcher) {
7976
}
8077

8178
private int getMsgLen() {
79+
PlatformTestStorage testStorage = PlatformTestStorageRegistry.getInstance();
8280
try {
8381
if (testStorage.getInputArgs().containsKey(VIEW_HIERARCHY_CHAR_LIMIT)) {
8482
String limit = testStorage.getInputArg(VIEW_HIERARCHY_CHAR_LIMIT);
@@ -116,7 +114,7 @@ private String dumpFullViewHierarchyToFile(T error) {
116114
}
117115

118116
private void addOutputFile(String filename, String content) throws IOException {
119-
try (OutputStream out = testStorage.openOutputFile(filename)) {
117+
try (OutputStream out = PlatformTestStorageRegistry.getInstance().openOutputFile(filename)) {
120118
out.write(content.getBytes());
121119
}
122120
}

0 commit comments

Comments
 (0)