Skip to content

Commit

Permalink
[shell] Adds a shell test for timezone fetches (flutter#19108)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
filmil authored Jun 18, 2020
1 parent 5157a6b commit 1551b74
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
12 changes: 12 additions & 0 deletions shell/common/fixtures/shell_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,15 @@ void canDecompressImageFromAsset() {
}

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]);
}
49 changes: 49 additions & 0 deletions shell/common/shell_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#define FML_USED_ON_EMBEDDER

#include <time.h>
#include <algorithm>
#include <functional>
#include <future>
Expand Down Expand Up @@ -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<std::string>::FromDart(
Dart_GetNativeArgument(args, 0));
latch.Signal();
}));

auto settings = CreateSettingsForFixture();
auto configuration = RunConfiguration::InferFromSettings(settings);
configuration.SetEntrypoint("localtimesMatch");
std::unique_ptr<Shell> 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(&timestamp);
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) {
Expand Down
3 changes: 2 additions & 1 deletion testing/fuchsia/meta/fuchsia_test.cmx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}

0 comments on commit 1551b74

Please sign in to comment.