From 8b200432726241370f374071448f13bd4f5bf94d Mon Sep 17 00:00:00 2001 From: Filip Filmar Date: Tue, 25 Aug 2020 16:24:25 -0700 Subject: [PATCH] [fuchsia] Adds a test for clock change (flutter/engine#19993) This is a Fuchsia-only shell test, confirming that when the timezone setting is changed on the realm via Fuchsia's FIDL interface `fuchsia.settings.Intl`, the Dart isolate running in that realm receives the change and starts reporting the appropriate timestamp. We already have a [similar test][tst] that verifies the timezones in the dart VM matches the local timezone at start, but there was no test that also verified timezone changes. [tst]: https://github.com/flutter/engine/blob/master/shell/common/shell_unittests.cc#L1166 See issue #61284 --- .../ci/licenses_golden/licenses_flutter | 1 + engine/src/flutter/shell/common/BUILD.gn | 11 + .../shell/common/fixtures/shell_test.dart | 23 +- .../shell/common/shell_fuchsia_unittests.cc | 198 ++++++++++++++++++ .../testing/fuchsia/meta/fuchsia_test.cmx | 9 +- 5 files changed, 235 insertions(+), 7 deletions(-) create mode 100644 engine/src/flutter/shell/common/shell_fuchsia_unittests.cc diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 99124c9eff..fae466af1f 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -624,6 +624,7 @@ FILE: ../../../flutter/shell/common/serialization_callbacks.h FILE: ../../../flutter/shell/common/shell.cc FILE: ../../../flutter/shell/common/shell.h FILE: ../../../flutter/shell/common/shell_benchmarks.cc +FILE: ../../../flutter/shell/common/shell_fuchsia_unittests.cc FILE: ../../../flutter/shell/common/shell_io_manager.cc FILE: ../../../flutter/shell/common/shell_io_manager.h FILE: ../../../flutter/shell/common/shell_test.cc diff --git a/engine/src/flutter/shell/common/BUILD.gn b/engine/src/flutter/shell/common/BUILD.gn index f377d643ff..c6b7a56e9a 100644 --- a/engine/src/flutter/shell/common/BUILD.gn +++ b/engine/src/flutter/shell/common/BUILD.gn @@ -263,5 +263,16 @@ if (enable_unittests) { "//flutter/shell/version", "//third_party/googletest:gmock", ] + + if (is_fuchsia) { + sources += [ "shell_fuchsia_unittests.cc" ] + + deps += [ + "$fuchsia_sdk_root/fidl:fuchsia.intl", + "$fuchsia_sdk_root/fidl:fuchsia.settings", + "$fuchsia_sdk_root/pkg:fidl_cpp", + "$fuchsia_sdk_root/pkg:sys_cpp", + ] + } } } diff --git a/engine/src/flutter/shell/common/fixtures/shell_test.dart b/engine/src/flutter/shell/common/fixtures/shell_test.dart index ceeee77a4e..464f76f79a 100644 --- a/engine/src/flutter/shell/common/fixtures/shell_test.dart +++ b/engine/src/flutter/shell/common/fixtures/shell_test.dart @@ -141,12 +141,29 @@ List getFixtureImage() native 'GetFixtureImage'; void notifyLocalTime(String string) native 'NotifyLocalTime'; -@pragma('vm:entry-point') -void localtimesMatch() { +void waitFixture() native 'WaitFixture'; + +// Return local date-time as a string, to an hour resolution. So, "2020-07-23 +// 14:03:22" will become "2020-07-23 14". +String localTimeAsString() { 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]); + return timeStr.split(":")[0]; +} + +@pragma('vm:entry-point') +void localtimesMatch() { + notifyLocalTime(localTimeAsString()); +} + +@pragma('vm:entry-point') +void timezonesChange() { + notifyLocalTime(localTimeAsString()); + waitFixture(); + notifyLocalTime(localTimeAsString()); + waitFixture(); + notifyLocalTime(localTimeAsString()); } diff --git a/engine/src/flutter/shell/common/shell_fuchsia_unittests.cc b/engine/src/flutter/shell/common/shell_fuchsia_unittests.cc new file mode 100644 index 0000000000..f754520327 --- /dev/null +++ b/engine/src/flutter/shell/common/shell_fuchsia_unittests.cc @@ -0,0 +1,198 @@ +// 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. + +// A fuchsia-specific shell test. +// +// This test is only supposed to be ran on Fuchsia OS, as it exercises +// Fuchsia-specific functionality for which no equivalent exists elsewhere. + +#define FML_USED_ON_EMBEDDER + +#include +#include + +#include + +#include +#include +#include + +#include "flutter/fml/synchronization/count_down_latch.h" +#include "flutter/runtime/dart_vm.h" +#include "flutter/shell/common/shell_test.h" + +namespace flutter { +namespace testing { + +using fuchsia::intl::TimeZoneId; +using fuchsia::settings::Intl_Set_Result; +using fuchsia::settings::IntlSettings; + +class FuchsiaShellTest : public ShellTest { + protected: + FuchsiaShellTest() + : ctx_(sys::ComponentContext::CreateAndServeOutgoingDirectory()), + intl_() { + ctx_->svc()->Connect(intl_.NewRequest()); + } + + ~FuchsiaShellTest() { + // Restore the time zoe that matche that of the test harness. This is + // the default. + const std::string local_timezone = GetLocalTimezone(); + SetTimezone(local_timezone); + AssertTimezone(local_timezone, GetSettings()); + } + + // Gets the international settings from this Fuchsia realm. + IntlSettings GetSettings() { + IntlSettings settings; + zx_status_t status = intl_->Watch2(&settings); + EXPECT_EQ(status, ZX_OK); + return settings; + } + + // Sets the timezone of this Fuchsia realm to `timezone_name`. + void SetTimezone(const std::string& timezone_name) { + fuchsia::settings::IntlSettings settings; + settings.set_time_zone_id(TimeZoneId{.id = timezone_name}); + Intl_Set_Result result; + zx_status_t status = intl_->Set(std::move(settings), &result); + ASSERT_EQ(status, ZX_OK); + } + + std::string GetLocalTimezone() { + const time_t timestamp = time(nullptr); + const struct tm* local_time = localtime(×tamp); + EXPECT_NE(local_time, nullptr) + << "Could not get local time: errno=" << errno << ": " + << strerror(errno); + return std::string(local_time->tm_zone); + } + + // Checks that the timezone name in the `settings` matches what is `expected`. + void AssertTimezone(const std::string& expected, + const IntlSettings& settings) { + ASSERT_EQ(expected, settings.time_zone_id().id); + } + + std::unique_ptr ctx_; + fuchsia::settings::IntlSyncPtr intl_; + + fuchsia::settings::IntlSettings save_settings_; +}; + +static bool ValidateShell(Shell* shell) { + if (!shell) { + return false; + } + + if (!shell->IsSetup()) { + return false; + } + + ShellTest::PlatformViewNotifyCreated(shell); + + { + fml::AutoResetWaitableEvent latch; + fml::TaskRunner::RunNowOrPostTask( + shell->GetTaskRunners().GetPlatformTaskRunner(), [shell, &latch]() { + shell->GetPlatformView()->NotifyDestroyed(); + latch.Signal(); + }); + latch.Wait(); + } + + return true; +} + +// Verifies that changing the Fuchsia settings timezone through the FIDL +// settings interface results in a change of the reported local time in the +// isolate. +// +// The test is as follows: +// +// - Set an initial timezone, then get a timestamp from the isolate rounded down +// to the nearest hour. The assumption is as long as this test doesn't run +// very near the whole hour, which should be very unlikely, the nearest hour +// will vary depending on the time zone. +// - Set a different timezone. Get a timestamp from the isolate again and +// confirm that this time around the timestamps are different. +// - Set the initial timezone again, and get the timestamp. This time, the +// timestamp rounded down to whole hour should match the timestamp we got +// in the initial step. +TEST_F(FuchsiaShellTest, LocaltimesVaryOnTimezoneChanges) { + // Start with a set timezone. System timezone will be restored at test + // teardown. + SetTimezone("Europe/Amsterdam"); + AssertTimezone("Europe/Amsterdam", GetSettings()); + + // See fixtures/shell_test.dart, the callback NotifyLocalTime is declared + // there. + fml::AutoResetWaitableEvent latch; + std::string dart_isolate_time_str; + AddNativeCallback("NotifyLocalTime", CREATE_NATIVE_ENTRY([&](auto args) { + dart_isolate_time_str = + tonic::DartConverter::FromDart( + Dart_GetNativeArgument(args, 0)); + latch.Signal(); + })); + + fml::AutoResetWaitableEvent fixture_latch; + AddNativeCallback("WaitFixture", CREATE_NATIVE_ENTRY([&](auto args) { + // Wait for the test fixture to advance. + fixture_latch.Wait(); + })); + + auto settings = CreateSettingsForFixture(); + auto configuration = RunConfiguration::InferFromSettings(settings); + configuration.SetEntrypoint("timezonesChange"); + + std::unique_ptr shell = CreateShell(settings); + ASSERT_NE(shell.get(), nullptr); + ASSERT_TRUE(ValidateShell(shell.get())); + RunEngine(shell.get(), std::move(configuration)); + latch.Wait(); + + // Save the first reported local time. + const std::string initial_dart_isolate_time_str = dart_isolate_time_str; + + SetTimezone("America/New_York"); + AssertTimezone("America/New_York", GetSettings()); + + // Allow the dart program to advance. + fixture_latch.Signal(); + // Wait until the isolate processes the timezone update. + latch.Wait(); + + const std::string modified_dart_isolate_time_str = dart_isolate_time_str; + + ASSERT_NE(initial_dart_isolate_time_str, modified_dart_isolate_time_str) + << "Local time clocks in Europe/Amsterdam and America/New_York " + << "should never be the same but this test says they are; time zone " + << "handling in the dart VM is likely broken."; + + // Set the timezone back to the original one we started from, and verify + // that the timestamp (to the resolution of 1 hour) match. + SetTimezone("Europe/Amsterdam"); + AssertTimezone("Europe/Amsterdam", GetSettings()); + + // Allow the dart program to advance. + fixture_latch.Signal(); + // Wait until the isolate processes the timezone update. + latch.Wait(); + + const std::string final_dart_isolate_time_str = dart_isolate_time_str; + + ASSERT_EQ(initial_dart_isolate_time_str, final_dart_isolate_time_str) + << "Local time in Europe/Amsterdam rounded down to nearest hour read " + "twice" + << "should be the same but this test says they are not; time zone " + << "handling in the dart VM is likely broken."; + + DestroyShell(std::move(shell)); +} + +} // namespace testing +} // namespace flutter diff --git a/engine/src/flutter/testing/fuchsia/meta/fuchsia_test.cmx b/engine/src/flutter/testing/fuchsia/meta/fuchsia_test.cmx index c1f1fa71c4..438d294b52 100644 --- a/engine/src/flutter/testing/fuchsia/meta/fuchsia_test.cmx +++ b/engine/src/flutter/testing/fuchsia/meta/fuchsia_test.cmx @@ -11,13 +11,14 @@ ], "services": [ "fuchsia.accessibility.semantics.SemanticsManager", - "fuchsia.process.Launcher", "fuchsia.deprecatedtimezone.Timezone", - "fuchsia.netstack.Netstack", - "fuchsia.sysmem.Allocator", - "fuchsia.vulkan.loader.Loader", "fuchsia.logger.LogSink", + "fuchsia.netstack.Netstack", + "fuchsia.process.Launcher", + "fuchsia.settings.Intl", + "fuchsia.sysmem.Allocator", "fuchsia.tracing.provider.Registry", + "fuchsia.vulkan.loader.Loader", "fuchsia.intl.PropertyProvider" ] }