Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 992689c

Browse files
authored
[fuchsia] Adds a test for timezone change (#21934)
1 parent 0449812 commit 992689c

File tree

4 files changed

+294
-10
lines changed

4 files changed

+294
-10
lines changed

shell/common/BUILD.gn

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,5 +255,16 @@ if (enable_unittests) {
255255
"//flutter/shell/version",
256256
"//third_party/googletest:gmock",
257257
]
258+
259+
if (is_fuchsia) {
260+
sources += [ "shell_fuchsia_unittests.cc" ]
261+
262+
deps += [
263+
"$fuchsia_sdk_root/fidl:fuchsia.intl",
264+
"$fuchsia_sdk_root/fidl:fuchsia.settings",
265+
"$fuchsia_sdk_root/pkg:fidl_cpp",
266+
"$fuchsia_sdk_root/pkg:sys_cpp",
267+
]
268+
}
258269
}
259270
}

shell/common/fixtures/shell_test.dart

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,29 @@ List<int> getFixtureImage() native 'GetFixtureImage';
164164

165165
void notifyLocalTime(String string) native 'NotifyLocalTime';
166166

167+
bool waitFixture() native 'WaitFixture';
168+
169+
// Return local date-time as a string, to an hour resolution. So, "2020-07-23
170+
// 14:03:22" will become "2020-07-23 14".
171+
String localTimeAsString() {
172+
final now = DateTime.now().toLocal();
173+
// This is: "$y-$m-$d $h:$min:$sec.$ms$us";
174+
final timeStr = now.toString();
175+
// Forward only "$y-$m-$d $h" for timestamp comparison. Not using DateTime
176+
// formatting since package:intl is not available.
177+
return timeStr.split(":")[0];
178+
}
179+
167180
@pragma('vm:entry-point')
168181
void localtimesMatch() {
169-
final now = DateTime.now().toLocal();
170-
// This is: "$y-$m-$d $h:$min:$sec.$ms$us";
171-
final timeStr = now.toString();
172-
// Forward only "$y-$m-$d $h" for timestamp comparison. Not using DateTime
173-
// formatting since package:intl is not available.
174-
notifyLocalTime(timeStr.split(":")[0]);
182+
notifyLocalTime(localTimeAsString());
183+
}
184+
185+
@pragma('vm:entry-point')
186+
void timezonesChange() {
187+
do {
188+
notifyLocalTime(localTimeAsString());
189+
} while (waitFixture());
175190
}
176191

177192
void notifyCanAccessResource(bool success) native 'NotifyCanAccessResource';
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// A fuchsia-specific shell test.
6+
//
7+
// This test is only supposed to be ran on Fuchsia OS, as it exercises
8+
// Fuchsia-specific functionality for which no equivalent exists elsewhere.
9+
10+
#define FML_USED_ON_EMBEDDER
11+
12+
#include <time.h>
13+
#include <unistd.h>
14+
15+
#include <memory>
16+
17+
#include <fuchsia/intl/cpp/fidl.h>
18+
#include <fuchsia/settings/cpp/fidl.h>
19+
#include <lib/sys/cpp/component_context.h>
20+
21+
#include "flutter/fml/dart/dart_converter.h"
22+
#include "flutter/fml/logging.h"
23+
#include "flutter/fml/synchronization/count_down_latch.h"
24+
#include "flutter/runtime/dart_vm.h"
25+
#include "flutter/shell/common/shell_test.h"
26+
27+
namespace flutter {
28+
namespace testing {
29+
30+
using fuchsia::intl::TimeZoneId;
31+
using fuchsia::settings::Intl_Set_Result;
32+
using fuchsia::settings::IntlSettings;
33+
34+
class FuchsiaShellTest : public ShellTest {
35+
protected:
36+
FuchsiaShellTest()
37+
: ctx_(sys::ComponentContext::CreateAndServeOutgoingDirectory()),
38+
intl_() {
39+
ctx_->svc()->Connect(intl_.NewRequest());
40+
}
41+
42+
~FuchsiaShellTest() {
43+
// Restore the time zone that matche that of the test harness. This is
44+
// the default.
45+
const std::string local_timezone = GetLocalTimezone();
46+
SetTimezone(local_timezone);
47+
AssertTimezone(local_timezone, GetSettings());
48+
}
49+
50+
// Gets the international settings from this Fuchsia realm.
51+
IntlSettings GetSettings() {
52+
IntlSettings settings;
53+
zx_status_t status = intl_->Watch2(&settings);
54+
EXPECT_EQ(status, ZX_OK);
55+
return settings;
56+
}
57+
58+
// Sets the timezone of this Fuchsia realm to `timezone_name`.
59+
void SetTimezone(const std::string& timezone_name) {
60+
fuchsia::settings::IntlSettings settings;
61+
settings.set_time_zone_id(TimeZoneId{.id = timezone_name});
62+
Intl_Set_Result result;
63+
zx_status_t status = intl_->Set(std::move(settings), &result);
64+
ASSERT_EQ(status, ZX_OK);
65+
}
66+
67+
std::string GetLocalTimezone() {
68+
const time_t timestamp = time(nullptr);
69+
const struct tm* local_time = localtime(&timestamp);
70+
EXPECT_NE(local_time, nullptr)
71+
<< "Could not get local time: errno=" << errno << ": "
72+
<< strerror(errno);
73+
return std::string(local_time->tm_zone);
74+
}
75+
76+
std::string GetLocalTime() {
77+
const time_t timestamp = time(nullptr);
78+
const struct tm* local_time = localtime(&timestamp);
79+
EXPECT_NE(local_time, nullptr)
80+
<< "Could not get local time: errno=" << errno << ": "
81+
<< strerror(errno);
82+
char buffer[sizeof("2020-08-26 14")];
83+
const size_t written =
84+
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H", local_time);
85+
EXPECT_LT(0UL, written);
86+
return std::string(buffer);
87+
}
88+
89+
// Checks that the timezone name in the `settings` matches what is `expected`.
90+
void AssertTimezone(const std::string& expected,
91+
const IntlSettings& settings) {
92+
ASSERT_EQ(expected, settings.time_zone_id().id);
93+
}
94+
95+
std::unique_ptr<sys::ComponentContext> ctx_;
96+
fuchsia::settings::IntlSyncPtr intl_;
97+
98+
fuchsia::settings::IntlSettings save_settings_;
99+
};
100+
101+
static bool ValidateShell(Shell* shell) {
102+
if (!shell) {
103+
return false;
104+
}
105+
106+
if (!shell->IsSetup()) {
107+
return false;
108+
}
109+
110+
ShellTest::PlatformViewNotifyCreated(shell);
111+
112+
{
113+
fml::AutoResetWaitableEvent latch;
114+
fml::TaskRunner::RunNowOrPostTask(
115+
shell->GetTaskRunners().GetPlatformTaskRunner(), [shell, &latch]() {
116+
shell->GetPlatformView()->NotifyDestroyed();
117+
latch.Signal();
118+
});
119+
latch.Wait();
120+
}
121+
122+
return true;
123+
}
124+
125+
// Runs the function `f` in lock-step with a Dart isolate until the function
126+
// returns `true`, or until a certain fixed number of retries is exhausted.
127+
// Events `tick` and `tock` are used to synchronize the lock-stepping. The
128+
// event 'tick' is a signal to the dart isolate to advance a single iteration
129+
// step. 'tock' is used by the dart isolate to signal that it has completed
130+
// its step.
131+
static void RunCoroutineWithRetry(int retries,
132+
fml::AutoResetWaitableEvent* tick,
133+
fml::AutoResetWaitableEvent* tock,
134+
std::function<bool()> f) {
135+
for (; retries > 0; retries--) {
136+
// Do a single coroutine step.
137+
tick->Signal();
138+
tock->Wait();
139+
if (f()) {
140+
break;
141+
}
142+
FML_LOG(INFO) << "Retries left: " << retries;
143+
sleep(1);
144+
}
145+
}
146+
147+
// Verifies that changing the Fuchsia settings timezone through the FIDL
148+
// settings interface results in a change of the reported local time in the
149+
// isolate.
150+
//
151+
// The test is as follows:
152+
//
153+
// - Set an initial timezone, then get a timestamp from the isolate rounded down
154+
// to the nearest hour. The assumption is as long as this test doesn't run
155+
// very near the whole hour, which should be very unlikely, the nearest hour
156+
// will vary depending on the time zone.
157+
// - Set a different timezone. Get a timestamp from the isolate again and
158+
// confirm that this time around the timestamps are different.
159+
// - Set the initial timezone again, and get the timestamp. This time, the
160+
// timestamp rounded down to whole hour should match the timestamp we got
161+
// in the initial step.
162+
TEST_F(FuchsiaShellTest, LocaltimesVaryOnTimezoneChanges) {
163+
// See fixtures/shell_test.dart, the callback NotifyLocalTime is declared
164+
// there.
165+
fml::AutoResetWaitableEvent latch;
166+
std::string dart_isolate_time_str;
167+
AddNativeCallback("NotifyLocalTime", CREATE_NATIVE_ENTRY([&](auto args) {
168+
dart_isolate_time_str =
169+
tonic::DartConverter<std::string>::FromDart(
170+
Dart_GetNativeArgument(args, 0));
171+
latch.Signal();
172+
}));
173+
174+
// As long as this is set, the isolate will keep rerunning its only task.
175+
bool continue_fixture = true;
176+
fml::AutoResetWaitableEvent fixture_latch;
177+
AddNativeCallback("WaitFixture", CREATE_NATIVE_ENTRY([&](auto args) {
178+
// Wait for the test fixture to advance.
179+
fixture_latch.Wait();
180+
tonic::DartConverter<bool>::SetReturnValue(
181+
args, continue_fixture);
182+
}));
183+
184+
auto settings = CreateSettingsForFixture();
185+
auto configuration = RunConfiguration::InferFromSettings(settings);
186+
configuration.SetEntrypoint("timezonesChange");
187+
188+
std::unique_ptr<Shell> shell = CreateShell(settings);
189+
ASSERT_NE(shell.get(), nullptr);
190+
ASSERT_TRUE(ValidateShell(shell.get()));
191+
RunEngine(shell.get(), std::move(configuration));
192+
latch.Wait(); // After this point, the fixture is at waitFixture().
193+
194+
// Start with the local timezone, ensure that the isolate and the test
195+
// fixture are the same.
196+
SetTimezone(GetLocalTimezone());
197+
AssertTimezone(GetLocalTimezone(), GetSettings());
198+
std::string expected = GetLocalTime();
199+
std::string actual = "undefined";
200+
RunCoroutineWithRetry(10, &fixture_latch, &latch, [&]() {
201+
actual = dart_isolate_time_str;
202+
FML_LOG(INFO) << "reference: " << expected << ", actual: " << actual;
203+
return expected == actual;
204+
});
205+
ASSERT_EQ(expected, actual)
206+
<< "The Dart isolate was expected to show the same time as the test "
207+
<< "fixture eventually, but that didn't happen after multiple retries.";
208+
209+
// Set a new timezone, which is hopefully different from the local one.
210+
SetTimezone("America/New_York");
211+
AssertTimezone("America/New_York", GetSettings());
212+
RunCoroutineWithRetry(10, &fixture_latch, &latch, [&]() {
213+
actual = dart_isolate_time_str;
214+
FML_LOG(INFO) << "reference: " << expected << ", actual: " << actual;
215+
return expected != actual;
216+
});
217+
ASSERT_NE(expected, actual)
218+
<< "The Dart isolate was expected to show a time different from the test "
219+
<< "fixture eventually, but that didn't happen after multiple retries.";
220+
221+
// Set a new isolate timezone, and check that the reported time is eventually
222+
// different from what it used to be prior to the change.
223+
SetTimezone("Europe/Amsterdam");
224+
AssertTimezone("Europe/Amsterdam", GetSettings());
225+
RunCoroutineWithRetry(10, &fixture_latch, &latch, [&]() {
226+
actual = dart_isolate_time_str;
227+
FML_LOG(INFO) << "reference: " << expected << ", actual: " << actual;
228+
return expected != actual;
229+
});
230+
ASSERT_NE(expected, actual)
231+
<< "The Dart isolate was expected to show a time different from the "
232+
<< "prior timezone eventually, but that didn't happen after multiple "
233+
<< "retries.";
234+
235+
// Let's try to bring the timezone back to the old one.
236+
expected = actual;
237+
SetTimezone("America/New_York");
238+
AssertTimezone("America/New_York", GetSettings());
239+
RunCoroutineWithRetry(10, &fixture_latch, &latch, [&]() {
240+
actual = dart_isolate_time_str;
241+
FML_LOG(INFO) << "reference: " << expected << ", actual: " << actual;
242+
return expected != actual;
243+
});
244+
ASSERT_NE(expected, actual)
245+
<< "The Dart isolate was expected to show a time different from the "
246+
<< "prior timezone eventually, but that didn't happen after multiple "
247+
<< "retries.";
248+
249+
// Tell the isolate to exit its loop.
250+
ASSERT_FALSE(fixture_latch.IsSignaledForTest());
251+
continue_fixture = false;
252+
fixture_latch.Signal();
253+
DestroyShell(std::move(shell));
254+
}
255+
256+
} // namespace testing
257+
} // namespace flutter

testing/fuchsia/meta/fuchsia_test.cmx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
],
1212
"services": [
1313
"fuchsia.accessibility.semantics.SemanticsManager",
14-
"fuchsia.process.Launcher",
1514
"fuchsia.deprecatedtimezone.Timezone",
15+
"fuchsia.intl.PropertyProvider",
16+
"fuchsia.logger.LogSink",
1617
"fuchsia.netstack.Netstack",
18+
"fuchsia.process.Launcher",
19+
"fuchsia.settings.Intl",
1720
"fuchsia.sysmem.Allocator",
18-
"fuchsia.vulkan.loader.Loader",
19-
"fuchsia.logger.LogSink",
2021
"fuchsia.tracing.provider.Registry",
21-
"fuchsia.intl.PropertyProvider"
22+
"fuchsia.vulkan.loader.Loader"
2223
]
2324
}
2425
}

0 commit comments

Comments
 (0)