Skip to content

Commit 0088d90

Browse files
Ensure TestStorage library is multi-linux-user compatible.
PiperOrigin-RevId: 853403437
1 parent 477a52e commit 0088d90

File tree

6 files changed

+55
-37
lines changed

6 files changed

+55
-37
lines changed

services/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
{{date}}
44

5+
<!-- disableFinding(LINE_OVER_80) -->
56
`androidx.test.services:test-services:{version}` `androidx.test.services:storage:{version}` are released.
67

78
**Bug Fixes**
89

10+
* Ensure TestStorage library is multi-linux-user compatible.
11+
912
**New Features**
1013

1114
**Breaking Changes**
1215

16+
* The location where TestStorage stores files has changed. This is non-breaking
17+
if using the TestStorage API, but breaking if tests depended on the explicit
18+
location of the files (e.g., by reading them without using TestStorage).
19+
1320
**API Changes**
1421

1522
* Update to minSdkVersion 23 and remove all related logic for SDKs < 23

services/storage/java/androidx/test/services/storage/file/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ android_library(
1515
name = "file",
1616
srcs = glob(["*.java"]),
1717
deps = [
18-
"//opensource/androidx:annotation",
1918
"//runner/monitor",
2019
"//services/storage/java/androidx/test/services/storage:test_storage_constants",
2120
],

services/storage/java/androidx/test/services/storage/file/HostedFile.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@
1818
import android.content.Context;
1919
import android.net.Uri;
2020
import android.os.Environment;
21-
import android.os.UserManager;
2221
import android.provider.OpenableColumns;
23-
import android.util.Log;
2422
import androidx.test.services.storage.TestStorageConstants;
2523
import java.io.File;
26-
import java.util.concurrent.atomic.AtomicBoolean;
2724

2825
import androidx.annotation.RestrictTo;
2926
import androidx.annotation.RestrictTo.Scope;
@@ -38,8 +35,6 @@ public final class HostedFile {
3835

3936
private static final String TAG = "HostedFile";
4037

41-
private static final AtomicBoolean loggedOutputDir = new AtomicBoolean(false);
42-
4338
/** An enum of the columns returned by the hosted file service. */
4439
public enum HostedFileColumn {
4540
NAME("name", String.class, 3 /* Cursor.FIELD_TYPE_STRING since api 11 */, 0),
@@ -152,21 +147,8 @@ public static File getInputRootDirectory(Context context) {
152147
}
153148

154149
public static File getOutputRootDirectory(Context context) {
155-
UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
156-
if (userManager.isSystemUser()) {
157-
return Environment.getExternalStorageDirectory();
158-
} else {
159-
// using legacy external storage for output in automotive devices where tests run as
160-
// a secondary user has been flaky. So use local storage instead.
161-
if (!loggedOutputDir.getAndSet(true)) {
162-
// limit log spam by only logging choice once
163-
Log.d(
164-
TAG,
165-
"Secondary user detected. Choosing local storage as output root dir: "
166-
+ context.getCacheDir().getAbsolutePath());
167-
}
168-
return context.getCacheDir();
169-
}
150+
// Use a reliably self-writable directory.
151+
return context.getExternalFilesDir(null);
170152
}
171153

172154
private static <T> T checkNotNull(T reference) {

services/storage/java/androidx/test/services/storage/provider/InternalUseOnlyFilesContentProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ public final class InternalUseOnlyFilesContentProvider extends AbstractFileConte
2525

2626
@Override
2727
protected File getHostedDirectory(Context context) {
28-
// use input root directory here, as TestArgsContentProvider also uses this directory
28+
// Uses the output root directory since the provider is Read/Write and only the output directory
29+
// is guaranteed to be writable.
2930
return new File(
30-
HostedFile.getInputRootDirectory(context),
31+
HostedFile.getOutputRootDirectory(context),
3132
TestStorageConstants.ON_DEVICE_PATH_INTERNAL_USE);
3233
}
3334

services/storage/javatests/androidx/test/services/storage/BUILD

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Tests for the test storage.
33

44
load("//build_extensions:android_library_test.bzl", "axt_android_library_test")
5-
load("//build_extensions:phone_devices.bzl", "devices")
5+
load("//build_extensions:phone_devices.bzl", "apis", "devices")
66

77
package(
88
default_applicable_licenses = ["//services:license"],
@@ -30,10 +30,51 @@ axt_android_library_test(
3030
],
3131
deps = [
3232
"//core/java/androidx/test/core",
33-
"//runner/monitor/java/androidx/test:monitor",
3433
"//services/storage/java/androidx/test/services/storage",
3534
"//services/storage/java/androidx/test/services/storage/file",
3635
"@maven//:com_google_truth_truth",
3736
"@maven//:junit_junit",
3837
],
3938
)
39+
40+
[
41+
axt_android_library_test(
42+
name = "TestStorageHsumTest_{user}".format(user = user),
43+
size = "large",
44+
srcs = [
45+
"TestStorageTest.java",
46+
],
47+
args = [
48+
"--install_test_services=true",
49+
"--test_args=arg1=value1,arg2=value2,arg3=value3",
50+
"--instrumentation_user={user}".format(user = user),
51+
],
52+
data = [
53+
":testinput.txt",
54+
],
55+
device_list =
56+
custom_devices(
57+
apis(min_api = 36),
58+
"hsum_phone",
59+
"google_hsum",
60+
"x86_64",
61+
),
62+
support_apps = [
63+
"//services:test_services",
64+
],
65+
tags = ["requires-mem:20g"],
66+
test_class = "androidx.test.services.storage.TestStorageTest",
67+
deps = [
68+
"//core/java/androidx/test/core",
69+
"//runner/monitor/java/androidx/test:monitor",
70+
"//services/storage/java/androidx/test/services/storage",
71+
"//services/storage/java/androidx/test/services/storage/file",
72+
"@maven//:com_google_truth_truth",
73+
"@maven//:junit_junit",
74+
],
75+
)
76+
for user in [
77+
"0",
78+
"10",
79+
]
80+
]

services/storage/javatests/androidx/test/services/storage/TestStorageTest.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static junit.framework.TestCase.fail;
2121
import static org.junit.Assert.assertEquals;
22-
import static org.junit.Assume.assumeTrue;
2322

24-
import android.content.Context;
2523
import android.net.Uri;
26-
import android.os.UserManager;
2724
import androidx.test.services.storage.file.HostedFile;
2825
import androidx.test.services.storage.internal.TestStorageUtil;
2926
import java.io.BufferedReader;
@@ -113,20 +110,11 @@ public void addOutputProperties() throws Exception {
113110

114111
@Test
115112
public void writeInternalFile() throws IOException {
116-
// known not to work in multi-user mode
117-
assumeTrue(isSystemUser());
118113
try (OutputStream output = testStorage.openInternalOutputFile("path/to/file")) {
119114
output.write(new byte[] {'h', 'e', 'l', 'l', 'o'});
120115
}
121116
}
122117

123-
private static boolean isSystemUser() {
124-
125-
UserManager um =
126-
((UserManager) getApplicationContext().getSystemService(Context.USER_SERVICE));
127-
return um.isSystemUser();
128-
}
129-
130118
@Test
131119
public void readWriteOverwriteReadFile() throws IOException {
132120
try (OutputStream output = testStorage.openOutputFile("path/to/file")) {

0 commit comments

Comments
 (0)