• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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// TODO(flutter_web): the Web-only API below need to be cleaned up.
6
7part of ui;
8
9/// Used to track when the platform is initialized. This ensures the test fonts
10/// are available.
11Future<void> _testPlatformInitializedFuture;
12
13/// If the platform is already initialized (by a previous test), then run the test
14/// body immediately. Otherwise, initialize the platform then run the test.
15Future<dynamic> ensureTestPlatformInitializedThenRunTest(
16    dynamic Function() body) {
17  if (_testPlatformInitializedFuture == null) {
18    debugEmulateFlutterTesterEnvironment = true;
19
20    // Initializing the platform will ensure that the test font is loaded.
21    _testPlatformInitializedFuture = webOnlyInitializePlatform(
22        assetManager: engine.WebOnlyMockAssetManager());
23  }
24  return _testPlatformInitializedFuture.then<dynamic>((_) => body());
25}
26
27/// This setter is used by [WebNavigatorObserver] to update the url to
28/// reflect the [Navigator]'s current route name.
29set webOnlyRouteName(String routeName) {
30  engine.window.webOnlyRouteName = routeName;
31}
32
33/// Used to track when the platform is initialized. This ensures the test fonts
34/// are available.
35Future<void> _platformInitializedFuture;
36
37/// Initializes domRenderer with specific devicePixelRation and physicalSize.
38Future<void> webOnlyInitializeTestDomRenderer({double devicePixelRatio = 3.0}) {
39  // Force-initialize DomRenderer so it doesn't overwrite test pixel ratio.
40  engine.domRenderer;
41
42  // The following parameters are hard-coded in Flutter's test embedder. Since
43  // we don't have an embedder yet this is the lowest-most layer we can put
44  // this stuff in.
45  engine.window.debugOverrideDevicePixelRatio(devicePixelRatio);
46  engine.window.webOnlyDebugPhysicalSizeOverride =
47      Size(800 * devicePixelRatio, 600 * devicePixelRatio);
48  webOnlyScheduleFrameCallback = () {};
49  debugEmulateFlutterTesterEnvironment = true;
50
51  if (_platformInitializedFuture != null) {
52    return _platformInitializedFuture;
53  }
54
55  // Only load the Ahem font once and await the same future in all tests.
56  return _platformInitializedFuture =
57      webOnlyInitializePlatform(assetManager: engine.WebOnlyMockAssetManager())
58          .timeout(const Duration(seconds: 2), onTimeout: () async {
59    throw Exception('Timed out loading Ahem font.');
60  });
61}
62