From df31819baa5b91b4a50608d8ac1f08c7d737b15c Mon Sep 17 00:00:00 2001 From: Filip Filmar Date: Thu, 18 Jun 2020 09:56:18 -0700 Subject: [PATCH] [shell] Adds a shell test for timezone fetches (flutter/engine#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 ``` --- .../shell/common/fixtures/shell_test.dart | 12 +++++ .../flutter/shell/common/shell_unittests.cc | 49 +++++++++++++++++++ .../testing/fuchsia/meta/fuchsia_test.cmx | 3 +- 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/shell/common/fixtures/shell_test.dart b/engine/src/flutter/shell/common/fixtures/shell_test.dart index 8f8dcab38c..ceeee77a4e 100644 --- a/engine/src/flutter/shell/common/fixtures/shell_test.dart +++ b/engine/src/flutter/shell/common/fixtures/shell_test.dart @@ -138,3 +138,15 @@ void canDecompressImageFromAsset() { } List 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]); +} diff --git a/engine/src/flutter/shell/common/shell_unittests.cc b/engine/src/flutter/shell/common/shell_unittests.cc index 083393a4f6..6b0e370023 100644 --- a/engine/src/flutter/shell/common/shell_unittests.cc +++ b/engine/src/flutter/shell/common/shell_unittests.cc @@ -4,6 +4,7 @@ #define FML_USED_ON_EMBEDDER +#include #include #include #include @@ -1149,6 +1150,54 @@ TEST_F(ShellTest, CanConvertToAndFromMappings) { DestroyShell(std::move(shell)); } +// Compares local times as seen by the dart isolate and as seen by this test +// fixture, to a resolution of 1 hour. +// +// This verifies that (1) the isolate is able to get a timezone (doesn't lock +// up for example), and (2) that the host and the isolate agree on what the +// timezone is. +TEST_F(ShellTest, LocaltimesMatch) { + fml::AutoResetWaitableEvent latch; + std::string dart_isolate_time_str; + + // See fixtures/shell_test.dart, the callback NotifyLocalTime is declared + // there. + AddNativeCallback("NotifyLocalTime", CREATE_NATIVE_ENTRY([&](auto args) { + dart_isolate_time_str = + tonic::DartConverter::FromDart( + Dart_GetNativeArgument(args, 0)); + latch.Signal(); + })); + + auto settings = CreateSettingsForFixture(); + auto configuration = RunConfiguration::InferFromSettings(settings); + configuration.SetEntrypoint("localtimesMatch"); + std::unique_ptr shell = CreateShell(settings); + ASSERT_NE(shell.get(), nullptr); + RunEngine(shell.get(), std::move(configuration)); + latch.Wait(); + + char timestr[200]; + const time_t timestamp = time(nullptr); + const struct tm* local_time = localtime(×tamp); + ASSERT_NE(local_time, nullptr) + << "Could not get local time: errno=" << errno << ": " << strerror(errno); + // Example: "2020-02-26 14" for 2pm on February 26, 2020. + const size_t format_size = + strftime(timestr, sizeof(timestr), "%Y-%m-%d %H", local_time); + ASSERT_NE(format_size, 0UL) + << "strftime failed: host time: " << std::string(timestr) + << " dart isolate time: " << dart_isolate_time_str; + + const std::string host_local_time_str = timestr; + + ASSERT_EQ(dart_isolate_time_str, host_local_time_str) + << "Local times in the dart isolate and the local time seen by the test " + << "differ by more than 1 hour, but are expected to be about equal"; + + DestroyShell(std::move(shell)); +} + TEST_F(ShellTest, CanDecompressImageFromAsset) { fml::AutoResetWaitableEvent latch; AddNativeCallback("NotifyWidthHeight", CREATE_NATIVE_ENTRY([&](auto args) { diff --git a/engine/src/flutter/testing/fuchsia/meta/fuchsia_test.cmx b/engine/src/flutter/testing/fuchsia/meta/fuchsia_test.cmx index 9a01e290f2..c1f1fa71c4 100644 --- a/engine/src/flutter/testing/fuchsia/meta/fuchsia_test.cmx +++ b/engine/src/flutter/testing/fuchsia/meta/fuchsia_test.cmx @@ -17,7 +17,8 @@ "fuchsia.sysmem.Allocator", "fuchsia.vulkan.loader.Loader", "fuchsia.logger.LogSink", - "fuchsia.tracing.provider.Registry" + "fuchsia.tracing.provider.Registry", + "fuchsia.intl.PropertyProvider" ] } }