“6f09801ef97d58ac9a2cc4962edbb812287a9df5”上不存在“projects/hinanmu/imports.yml”
shell_unittests.cc 9.6 KB
Newer Older
M
Michael Goderbauer 已提交
1
// Copyright 2013 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#define FML_USED_ON_EMBEDDER

#include <functional>
#include <future>
#include <memory>

11
#include "flutter/fml/make_copyable.h"
12
#include "flutter/fml/message_loop.h"
13
#include "flutter/fml/synchronization/waitable_event.h"
14 15 16
#include "flutter/shell/common/platform_view.h"
#include "flutter/shell/common/rasterizer.h"
#include "flutter/shell/common/shell.h"
17
#include "flutter/shell/common/shell_test.h"
18
#include "flutter/shell/common/thread_host.h"
19
#include "flutter/shell/gpu/gpu_surface_software.h"
20
#include "flutter/testing/testing.h"
21 22
#include "gtest/gtest.h"

23
namespace flutter {
24
namespace testing {
25

26 27 28
class TestPlatformView : public PlatformView,
                         public GPUSurfaceSoftwareDelegate {
 public:
29
  TestPlatformView(PlatformView::Delegate& delegate, TaskRunners task_runners)
30 31 32 33 34 35 36 37 38 39
      : PlatformView(delegate, std::move(task_runners)) {}

 private:
  // |PlatformView|
  std::unique_ptr<Surface> CreateRenderingSurface() override {
    return std::make_unique<GPUSurfaceSoftware>(this);
  }

  // |GPUSurfaceSoftwareDelegate|
  virtual sk_sp<SkSurface> AcquireBackingStore(const SkISize& size) override {
40 41 42
    SkImageInfo image_info = SkImageInfo::MakeN32Premul(
        size.width(), size.height(), SkColorSpace::MakeSRGB());
    return SkSurface::MakeRaster(image_info);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  }

  // |GPUSurfaceSoftwareDelegate|
  virtual bool PresentBackingStore(sk_sp<SkSurface> backing_store) override {
    return true;
  }

  FML_DISALLOW_COPY_AND_ASSIGN(TestPlatformView);
};

static bool ValidateShell(Shell* shell) {
  if (!shell) {
    return false;
  }

  if (!shell->IsSetup()) {
    return false;
  }

  {
    fml::AutoResetWaitableEvent latch;
    fml::TaskRunner::RunNowOrPostTask(
        shell->GetTaskRunners().GetPlatformTaskRunner(), [shell, &latch]() {
          shell->GetPlatformView()->NotifyCreated();
          latch.Signal();
        });
    latch.Wait();
  }

  {
    fml::AutoResetWaitableEvent latch;
    fml::TaskRunner::RunNowOrPostTask(
        shell->GetTaskRunners().GetPlatformTaskRunner(), [shell, &latch]() {
          shell->GetPlatformView()->NotifyDestroyed();
          latch.Signal();
        });
    latch.Wait();
  }

  return true;
}

85
TEST_F(ShellTest, InitializeWithInvalidThreads) {
86
  ASSERT_FALSE(DartVMRef::IsInstanceRunning());
87 88
  Settings settings = CreateSettingsForFixture();
  TaskRunners task_runners("test", nullptr, nullptr, nullptr, nullptr);
89 90 91
  auto shell = Shell::Create(
      std::move(task_runners), settings,
      [](Shell& shell) {
92 93
        return std::make_unique<TestPlatformView>(shell,
                                                  shell.GetTaskRunners());
94 95 96 97 98
      },
      [](Shell& shell) {
        return std::make_unique<Rasterizer>(shell.GetTaskRunners());
      });
  ASSERT_FALSE(shell);
99
  ASSERT_FALSE(DartVMRef::IsInstanceRunning());
100 101
}

102
TEST_F(ShellTest, InitializeWithDifferentThreads) {
103
  ASSERT_FALSE(DartVMRef::IsInstanceRunning());
104
  Settings settings = CreateSettingsForFixture();
105 106 107 108
  ThreadHost thread_host(
      "io.flutter.test." + ::testing::GetCurrentTestName() + ".",
      ThreadHost::Type::Platform | ThreadHost::Type::GPU |
          ThreadHost::Type::IO | ThreadHost::Type::UI);
109 110 111 112
  TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(),
                           thread_host.gpu_thread->GetTaskRunner(),
                           thread_host.ui_thread->GetTaskRunner(),
                           thread_host.io_thread->GetTaskRunner());
113 114 115
  auto shell = Shell::Create(
      std::move(task_runners), settings,
      [](Shell& shell) {
116 117
        return std::make_unique<TestPlatformView>(shell,
                                                  shell.GetTaskRunners());
118 119 120 121
      },
      [](Shell& shell) {
        return std::make_unique<Rasterizer>(shell.GetTaskRunners());
      });
122
  ASSERT_TRUE(ValidateShell(shell.get()));
123 124 125
  ASSERT_TRUE(DartVMRef::IsInstanceRunning());
  shell.reset();
  ASSERT_FALSE(DartVMRef::IsInstanceRunning());
126 127
}

128
TEST_F(ShellTest, InitializeWithSingleThread) {
129
  ASSERT_FALSE(DartVMRef::IsInstanceRunning());
130
  Settings settings = CreateSettingsForFixture();
131 132 133
  ThreadHost thread_host(
      "io.flutter.test." + ::testing::GetCurrentTestName() + ".",
      ThreadHost::Type::Platform);
134
  auto task_runner = thread_host.platform_thread->GetTaskRunner();
135 136
  TaskRunners task_runners("test", task_runner, task_runner, task_runner,
                           task_runner);
137 138 139
  auto shell = Shell::Create(
      std::move(task_runners), settings,
      [](Shell& shell) {
140 141
        return std::make_unique<TestPlatformView>(shell,
                                                  shell.GetTaskRunners());
142 143 144 145
      },
      [](Shell& shell) {
        return std::make_unique<Rasterizer>(shell.GetTaskRunners());
      });
146
  ASSERT_TRUE(DartVMRef::IsInstanceRunning());
147
  ASSERT_TRUE(ValidateShell(shell.get()));
148 149
  shell.reset();
  ASSERT_FALSE(DartVMRef::IsInstanceRunning());
150 151
}

152
TEST_F(ShellTest, InitializeWithSingleThreadWhichIsTheCallingThread) {
153
  ASSERT_FALSE(DartVMRef::IsInstanceRunning());
154
  Settings settings = CreateSettingsForFixture();
155 156
  fml::MessageLoop::EnsureInitializedForCurrentThread();
  auto task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
157 158
  TaskRunners task_runners("test", task_runner, task_runner, task_runner,
                           task_runner);
159 160 161
  auto shell = Shell::Create(
      std::move(task_runners), settings,
      [](Shell& shell) {
162 163
        return std::make_unique<TestPlatformView>(shell,
                                                  shell.GetTaskRunners());
164 165 166 167
      },
      [](Shell& shell) {
        return std::make_unique<Rasterizer>(shell.GetTaskRunners());
      });
168
  ASSERT_TRUE(ValidateShell(shell.get()));
169 170 171
  ASSERT_TRUE(DartVMRef::IsInstanceRunning());
  shell.reset();
  ASSERT_FALSE(DartVMRef::IsInstanceRunning());
172 173
}

174 175
TEST_F(ShellTest,
       InitializeWithMultipleThreadButCallingThreadAsPlatformThread) {
176
  ASSERT_FALSE(DartVMRef::IsInstanceRunning());
177
  Settings settings = CreateSettingsForFixture();
178
  ThreadHost thread_host(
179
      "io.flutter.test." + ::testing::GetCurrentTestName() + ".",
180 181
      ThreadHost::Type::GPU | ThreadHost::Type::IO | ThreadHost::Type::UI);
  fml::MessageLoop::EnsureInitializedForCurrentThread();
182 183 184 185 186
  TaskRunners task_runners("test",
                           fml::MessageLoop::GetCurrent().GetTaskRunner(),
                           thread_host.gpu_thread->GetTaskRunner(),
                           thread_host.ui_thread->GetTaskRunner(),
                           thread_host.io_thread->GetTaskRunner());
187 188 189
  auto shell = Shell::Create(
      std::move(task_runners), settings,
      [](Shell& shell) {
190 191 192 193 194 195 196
        return std::make_unique<TestPlatformView>(shell,
                                                  shell.GetTaskRunners());
      },
      [](Shell& shell) {
        return std::make_unique<Rasterizer>(shell.GetTaskRunners());
      });
  ASSERT_TRUE(ValidateShell(shell.get()));
197 198 199
  ASSERT_TRUE(DartVMRef::IsInstanceRunning());
  shell.reset();
  ASSERT_FALSE(DartVMRef::IsInstanceRunning());
200 201
}

202
TEST_F(ShellTest, InitializeWithGPUAndPlatformThreadsTheSame) {
203
  ASSERT_FALSE(DartVMRef::IsInstanceRunning());
204
  Settings settings = CreateSettingsForFixture();
205
  ThreadHost thread_host(
206
      "io.flutter.test." + ::testing::GetCurrentTestName() + ".",
207
      ThreadHost::Type::Platform | ThreadHost::Type::IO | ThreadHost::Type::UI);
208
  TaskRunners task_runners(
209 210 211 212 213 214 215 216 217 218 219
      "test",
      thread_host.platform_thread->GetTaskRunner(),  // platform
      thread_host.platform_thread->GetTaskRunner(),  // gpu
      thread_host.ui_thread->GetTaskRunner(),        // ui
      thread_host.io_thread->GetTaskRunner()         // io
  );
  auto shell = Shell::Create(
      std::move(task_runners), settings,
      [](Shell& shell) {
        return std::make_unique<TestPlatformView>(shell,
                                                  shell.GetTaskRunners());
220 221 222 223
      },
      [](Shell& shell) {
        return std::make_unique<Rasterizer>(shell.GetTaskRunners());
      });
224
  ASSERT_TRUE(DartVMRef::IsInstanceRunning());
225
  ASSERT_TRUE(ValidateShell(shell.get()));
226 227
  shell.reset();
  ASSERT_FALSE(DartVMRef::IsInstanceRunning());
228 229
}

230
TEST_F(ShellTest, FixturesAreFunctional) {
231
  ASSERT_FALSE(DartVMRef::IsInstanceRunning());
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
  const auto settings = CreateSettingsForFixture();
  auto shell = Shell::Create(
      GetTaskRunnersForFixture(), settings,
      [](Shell& shell) {
        return std::make_unique<TestPlatformView>(shell,
                                                  shell.GetTaskRunners());
      },
      [](Shell& shell) {
        return std::make_unique<Rasterizer>(shell.GetTaskRunners());
      });
  ASSERT_TRUE(ValidateShell(shell.get()));

  auto configuration = RunConfiguration::InferFromSettings(settings);
  ASSERT_TRUE(configuration.IsValid());
  configuration.SetEntrypoint("fixturesAreFunctionalMain");

  fml::AutoResetWaitableEvent main_latch;
  AddNativeCallback(
      "SayHiFromFixturesAreFunctionalMain",
      CREATE_NATIVE_ENTRY([&main_latch](auto args) { main_latch.Signal(); }));

  fml::AutoResetWaitableEvent latch;
  fml::TaskRunner::RunNowOrPostTask(
      shell->GetTaskRunners().GetUITaskRunner(),
      fml::MakeCopyable([&latch, config = std::move(configuration),
                         engine = shell->GetEngine()]() mutable {
        ASSERT_TRUE(engine);
        ASSERT_EQ(engine->Run(std::move(config)), Engine::RunStatus::Success);
        latch.Signal();
      }));

  latch.Wait();
  main_latch.Wait();
265 266 267
  ASSERT_TRUE(DartVMRef::IsInstanceRunning());
  shell.reset();
  ASSERT_FALSE(DartVMRef::IsInstanceRunning());
268 269 270
}

}  // namespace testing
271
}  // namespace flutter