|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
5 | | -#include "fml/task_runner.h" |
6 | 5 | #define FML_USED_ON_EMBEDDER |
7 | 6 |
|
8 | 7 | #include <string> |
|
18 | 17 | #include "flutter/fml/paths.h" |
19 | 18 | #include "flutter/fml/synchronization/count_down_latch.h" |
20 | 19 | #include "flutter/fml/synchronization/waitable_event.h" |
| 20 | +#include "flutter/fml/task_runner.h" |
21 | 21 | #include "flutter/fml/thread.h" |
| 22 | +#include "flutter/fml/time/time_delta.h" |
| 23 | +#include "flutter/fml/time/time_point.h" |
22 | 24 | #include "flutter/runtime/dart_vm.h" |
23 | 25 | #include "flutter/shell/platform/embedder/tests/embedder_assertions.h" |
24 | 26 | #include "flutter/shell/platform/embedder/tests/embedder_config_builder.h" |
|
29 | 31 | #include "third_party/skia/include/core/SkSurface.h" |
30 | 32 | #include "third_party/tonic/converter/dart_converter.h" |
31 | 33 |
|
| 34 | +namespace { |
| 35 | + |
| 36 | +static uint64_t NanosFromEpoch(int millis_from_now) { |
| 37 | + const auto now = fml::TimePoint::Now(); |
| 38 | + const auto delta = fml::TimeDelta::FromMilliseconds(millis_from_now); |
| 39 | + return (now + delta).ToEpochDelta().ToNanoseconds(); |
| 40 | +} |
| 41 | + |
| 42 | +} // namespace |
| 43 | + |
32 | 44 | namespace flutter { |
33 | 45 | namespace testing { |
34 | 46 |
|
@@ -1575,5 +1587,60 @@ TEST_F(EmbedderTest, BackToBackKeyEventResponsesCorrectlyInvoked) { |
1575 | 1587 | shutdown_latch.Wait(); |
1576 | 1588 | } |
1577 | 1589 |
|
| 1590 | +// This test schedules a frame for the future and asserts that vsync waiter |
| 1591 | +// posts the event at the right frame start time (which is in the future). |
| 1592 | +TEST_F(EmbedderTest, VsyncCallbackPostedIntoFuture) { |
| 1593 | + UniqueEngine engine; |
| 1594 | + fml::AutoResetWaitableEvent present_latch; |
| 1595 | + |
| 1596 | + // One of the threads that the callback (FlutterEngineOnVsync) will be posted |
| 1597 | + // to is the platform thread. So we cannot wait for assertions to complete on |
| 1598 | + // the platform thread. Create a new thread to manage the engine instance and |
| 1599 | + // wait for assertions on the test thread. |
| 1600 | + auto platform_task_runner = CreateNewThread("platform_thread"); |
| 1601 | + |
| 1602 | + platform_task_runner->PostTask([&]() { |
| 1603 | + auto& context = |
| 1604 | + GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); |
| 1605 | + |
| 1606 | + context.SetVsyncCallback([&](intptr_t baton) { |
| 1607 | + platform_task_runner->PostTask([baton = baton, engine = engine.get()]() { |
| 1608 | + FlutterEngineOnVsync(engine, baton, NanosFromEpoch(16), |
| 1609 | + NanosFromEpoch(32)); |
| 1610 | + }); |
| 1611 | + }); |
| 1612 | + context.AddNativeCallback( |
| 1613 | + "SignalNativeTest", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { |
| 1614 | + present_latch.Signal(); |
| 1615 | + })); |
| 1616 | + |
| 1617 | + EmbedderConfigBuilder builder(context); |
| 1618 | + builder.SetSoftwareRendererConfig(); |
| 1619 | + builder.SetupVsyncCallback(); |
| 1620 | + builder.SetDartEntrypoint("empty_scene"); |
| 1621 | + engine = builder.LaunchEngine(); |
| 1622 | + ASSERT_TRUE(engine.is_valid()); |
| 1623 | + |
| 1624 | + // Send a window metrics events so frames may be scheduled. |
| 1625 | + FlutterWindowMetricsEvent event = {}; |
| 1626 | + event.struct_size = sizeof(event); |
| 1627 | + event.width = 800; |
| 1628 | + event.height = 600; |
| 1629 | + event.pixel_ratio = 1.0; |
| 1630 | + |
| 1631 | + ASSERT_EQ(FlutterEngineSendWindowMetricsEvent(engine.get(), &event), |
| 1632 | + kSuccess); |
| 1633 | + }); |
| 1634 | + |
| 1635 | + present_latch.Wait(); |
| 1636 | + |
| 1637 | + fml::AutoResetWaitableEvent shutdown_latch; |
| 1638 | + platform_task_runner->PostTask([&]() { |
| 1639 | + engine.reset(); |
| 1640 | + shutdown_latch.Signal(); |
| 1641 | + }); |
| 1642 | + shutdown_latch.Wait(); |
| 1643 | +} |
| 1644 | + |
1578 | 1645 | } // namespace testing |
1579 | 1646 | } // namespace flutter |
0 commit comments