Filip Filmar 1551b74213
[shell] Adds a shell test for timezone fetches (#19108)
Adds a test that verifies that the view of the local time is the same in
the Dart isolate and the process that is running the test.

Specifically, this test is useful to verify that the code paths for
timezone retrieval do not break while the underlying FIDL protocols are
being refactored.

However, since the check is generally useful, the test is written as a
general flutter test.

Running this on Fuchsia required adding `fuchsia.intl.ProfileProvider`
to the CMX file that is used for all build Fuchsia packages.

Testing is a bit involved on Fuchsia.  You must build the Fuchsia
package `fluter/shell/common:shell_tests` and publish it to the dev
repository for your Fuchsia device.  It seems that a way to do so is to
modify the script `flutter/tools/fuchsia/build_fuchsia_artifacts.py` and
modify its function `GetTargetsToBuild` like so:

```
def GetTargetsToBuild(product=False):
  targets_to_build = [
    'flutter/shell/platform/fuchsia:fuchsia',
    'flutter/shell/common:shell_tests',
  ]
  return targets_to_build
```

Next, the Fuchsia packages need to be compiled and published.

Once done, the following `fx` invocation will run the test, assuming
that you have your Fuchsia setup:

```
fx shell run \
    fuchsia-pkg://fuchsia.com/shell_tests#meta/shell_tests.cmx \
    -- --gtest_filter=ShellTest.LocaltimesMatch
```
2020-06-18 09:56:18 -07:00

153 lines
4.3 KiB
Dart

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert' show utf8, json;
import 'dart:isolate';
import 'dart:typed_data';
import 'dart:ui';
void main() {}
void nativeReportTimingsCallback(List<int> timings) native 'NativeReportTimingsCallback';
void nativeOnBeginFrame(int microseconds) native 'NativeOnBeginFrame';
void nativeOnPointerDataPacket(List<int> sequences) native 'NativeOnPointerDataPacket';
@pragma('vm:entry-point')
void reportTimingsMain() {
window.onReportTimings = (List<FrameTiming> timings) {
List<int> timestamps = [];
for (FrameTiming t in timings) {
for (FramePhase phase in FramePhase.values) {
timestamps.add(t.timestampInMicroseconds(phase));
}
}
nativeReportTimingsCallback(timestamps);
};
}
@pragma('vm:entry-point')
void onBeginFrameMain() {
window.onBeginFrame = (Duration beginTime) {
nativeOnBeginFrame(beginTime.inMicroseconds);
};
}
@pragma('vm:entry-point')
void onPointerDataPacketMain() {
window.onPointerDataPacket = (PointerDataPacket packet) {
List<int> sequence= <int>[];
for (PointerData data in packet.data) {
sequence.add(PointerChange.values.indexOf(data.change));
}
nativeOnPointerDataPacket(sequence);
};
}
@pragma('vm:entry-point')
void emptyMain() {}
@pragma('vm:entry-point')
void dummyReportTimingsMain() {
window.onReportTimings = (List<FrameTiming> timings) {};
}
@pragma('vm:entry-point')
void fixturesAreFunctionalMain() {
sayHiFromFixturesAreFunctionalMain();
}
void sayHiFromFixturesAreFunctionalMain() native 'SayHiFromFixturesAreFunctionalMain';
void notifyNative() native 'NotifyNative';
void secondaryIsolateMain(String message) {
print('Secondary isolate got message: ' + message);
notifyNative();
}
@pragma('vm:entry-point')
void testCanLaunchSecondaryIsolate() {
Isolate.spawn(secondaryIsolateMain, 'Hello from root isolate.');
notifyNative();
}
@pragma('vm:entry-point')
void testSkiaResourceCacheSendsResponse() {
final PlatformMessageResponseCallback callback = (ByteData data) {
if (data == null) {
throw 'Response must not be null.';
}
final String response = utf8.decode(data.buffer.asUint8List());
final List<bool> jsonResponse = json.decode(response).cast<bool>();
if (jsonResponse[0] != true) {
throw 'Response was not true';
}
notifyNative();
};
const String jsonRequest = '''{
"method": "Skia.setResourceCacheMaxBytes",
"args": 10000
}''';
window.sendPlatformMessage(
'flutter/skia',
Uint8List.fromList(utf8.encode(jsonRequest)).buffer.asByteData(),
callback,
);
}
void notifyWidthHeight(int width, int height) native 'NotifyWidthHeight';
@pragma('vm:entry-point')
void canCreateImageFromDecompressedData() {
const int imageWidth = 10;
const int imageHeight = 10;
final Uint8List pixels = Uint8List.fromList(List<int>.generate(
imageWidth * imageHeight * 4,
(int i) => i % 4 < 2 ? 0x00 : 0xFF,
));
decodeImageFromPixels(
pixels, imageWidth, imageHeight, PixelFormat.rgba8888,
(Image image) {
notifyWidthHeight(image.width, image.height);
});
}
@pragma('vm:entry-point')
void canAccessIsolateLaunchData() {
notifyMessage(utf8.decode(window.getPersistentIsolateData().buffer.asUint8List()));
}
void notifyMessage(String string) native 'NotifyMessage';
@pragma('vm:entry-point')
void canConvertMappings() {
sendFixtureMapping(getFixtureMapping());
}
List<int> getFixtureMapping() native 'GetFixtureMapping';
void sendFixtureMapping(List<int> list) native 'SendFixtureMapping';
@pragma('vm:entry-point')
void canDecompressImageFromAsset() {
decodeImageFromList(Uint8List.fromList(getFixtureImage()), (Image result) {
notifyWidthHeight(result.width, result.height);
});
}
List<int> getFixtureImage() native 'GetFixtureImage';
void notifyLocalTime(String string) native 'NotifyLocalTime';
@pragma('vm:entry-point')
void localtimesMatch() {
final now = DateTime.now().toLocal();
// This is: "$y-$m-$d $h:$min:$sec.$ms$us";
final timeStr = now.toString();
// Forward only "$y-$m-$d $h" for timestamp comparison. Not using DateTime
// formatting since package:intl is not available.
notifyLocalTime(timeStr.split(":")[0]);
}