[dart:io] Allow setting overrides in the root Zone

Otherwise there is no way to install overrides that affect the whole
program.

related https://github.com/flutter/flutter/issues/13602

Change-Id: I905b7067e79f11051e8204ebb296915d495fd13f
Reviewed-on: https://dart-review.googlesource.com/30100
Reviewed-by: Florian Loitsch <floitsch@google.com>
Commit-Queue: Zach Anderson <zra@google.com>
This commit is contained in:
Zachary Anderson 2017-12-18 22:46:07 +00:00 committed by commit-bot@chromium.org
parent 7aeb94423f
commit 971a15795e
4 changed files with 82 additions and 2 deletions

View file

@ -30,8 +30,19 @@ const _asyncRunZoned = runZoned;
/// }
/// ```
abstract class HttpOverrides {
static HttpOverrides _global;
static HttpOverrides get current {
return Zone.current[_httpOverridesToken];
return Zone.current[_httpOverridesToken] ?? _global;
}
/// The [HttpOverrides] to use in the root [Zone].
///
/// These are the [HttpOverrides] that will be used in the root Zone, and in
/// Zone's that do not set [HttpOverrides] and whose ancestors up to the root
/// Zone do not set [HttpOverrides].
static set global(HttpOverrides overrides) {
_global = overrides;
}
/// Runs [body] in a fresh [Zone] using the provided overrides.

View file

@ -32,8 +32,19 @@ const _asyncRunZoned = runZoned;
/// }
/// ```
abstract class IOOverrides {
static IOOverrides _global;
static IOOverrides get current {
return Zone.current[_ioOverridesToken];
return Zone.current[_ioOverridesToken] ?? _global;
}
/// The [IOOverrides] to use in the root [Zone].
///
/// These are the [IOOverrides] that will be used in the root Zone, and in
/// Zone's that do not set [IOOverrides] and whose ancestors up to the root
/// Zone do not set [IOOverrides].
static set global(IOOverrides overrides) {
_global = overrides;
}
/// Runs [body] in a fresh [Zone] using the provided overrides.

View file

@ -171,9 +171,39 @@ zonedWithHttpOverridesTest() {
}, new MyHttpOverrides());
}
globalHttpOverridesTest() {
HttpOverrides.global = new MyHttpOverrides();
var httpClient = new HttpClient();
Expect.isNotNull(httpClient);
Expect.isTrue(httpClient is MyHttpClient1);
Expect.equals((new MyHttpClient1(null)).userAgent, httpClient.userAgent);
HttpOverrides.global = null;
httpClient = new HttpClient();
Expect.isTrue(httpClient is HttpClient);
Expect.isTrue(httpClient is! MyHttpClient1);
}
globalHttpOverridesZoneTest() {
HttpOverrides.global = new MyHttpOverrides();
runZoned(() {
runZoned(() {
var httpClient = new HttpClient();
Expect.isNotNull(httpClient);
Expect.isTrue(httpClient is MyHttpClient1);
Expect.equals((new MyHttpClient1(null)).userAgent, httpClient.userAgent);
});
});
HttpOverrides.global = null;
var httpClient = new HttpClient();
Expect.isTrue(httpClient is HttpClient);
Expect.isTrue(httpClient is! MyHttpClient1);
}
main() {
withHttpOverridesTest();
nestedWithHttpOverridesTest();
nestedDifferentOverridesTest();
zonedWithHttpOverridesTest();
globalHttpOverridesTest();
globalHttpOverridesZoneTest();
}

View file

@ -196,6 +196,34 @@ Future<Null> ioOverridesRunTest() async {
await f;
}
class MyIOOverrides extends IOOverrides {
Directory createDirectory(String path) => DirectoryMock.createDirectory(path);
}
globalIOOverridesTest() {
IOOverrides.global = new MyIOOverrides();
Expect.isTrue(new Directory("directory") is DirectoryMock);
IOOverrides.global = null;
Directory dir = new Directory("directory");
Expect.isTrue(dir is! DirectoryMock);
Expect.isTrue(dir is Directory);
}
globalIOOverridesZoneTest() {
IOOverrides.global = new MyIOOverrides();
runZoned(() {
runZoned(() {
Expect.isTrue(new Directory("directory") is DirectoryMock);
});
});
IOOverrides.global = null;
Directory dir = new Directory("directory");
Expect.isTrue(dir is! DirectoryMock);
Expect.isTrue(dir is Directory);
}
main() async {
await ioOverridesRunTest();
globalIOOverridesTest();
globalIOOverridesZoneTest();
}