embedder_unittests.cc 4.5 KB
Newer Older
M
Michael Goderbauer 已提交
1
// Copyright 2013 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include <string>
6
#include "embedder.h"
7 8
#include "flutter/fml/file.h"
#include "flutter/fml/mapping.h"
9 10
#include "flutter/shell/platform/embedder/tests/embedder_config_builder.h"
#include "flutter/shell/platform/embedder/tests/embedder_test.h"
11
#include "flutter/testing/testing.h"
12

13 14
namespace shell {
namespace testing {
15

16 17 18 19 20 21 22
static void MapAOTAsset(
    std::vector<std::unique_ptr<fml::FileMapping>>& aot_mappings,
    const fml::UniqueFD& fixtures_dir,
    const char* path,
    bool executable,
    const uint8_t** data,
    size_t* size) {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  fml::UniqueFD file =
      fml::OpenFile(fixtures_dir, path, false, fml::FilePermission::kRead);
  std::unique_ptr<fml::FileMapping> mapping;
  if (executable) {
    mapping = std::make_unique<fml::FileMapping>(
        file, std::initializer_list<fml::FileMapping::Protection>{
                  fml::FileMapping::Protection::kRead,
                  fml::FileMapping::Protection::kExecute});
  } else {
    mapping = std::make_unique<fml::FileMapping>(
        file, std::initializer_list<fml::FileMapping::Protection>{
                  fml::FileMapping::Protection::kRead});
  }
  *data = mapping->GetMapping();
  *size = mapping->GetSize();
  aot_mappings.emplace_back(std::move(mapping));
}

41 42 43 44
TEST(EmbedderTest, MustNotRunWithInvalidArgs) {
  FlutterEngine engine = nullptr;
  FlutterRendererConfig config = {};
  FlutterProjectArgs args = {};
45 46 47
  FlutterEngineResult result = FlutterEngineRun(FLUTTER_ENGINE_VERSION + 1,
                                                &config, &args, NULL, &engine);
  ASSERT_NE(result, FlutterEngineResult::kSuccess);
48 49 50
}

TEST(EmbedderTest, CanLaunchAndShutdownWithValidProjectArgs) {
51 52 53 54 55
  FlutterSoftwareRendererConfig renderer;
  renderer.struct_size = sizeof(FlutterSoftwareRendererConfig);
  renderer.surface_present_callback = [](void*, const void*, size_t, size_t) {
    return false;
  };
56

57
  FlutterRendererConfig config = {};
58 59
  config.type = FlutterRendererType::kSoftware;
  config.software = renderer;
60 61 62

  FlutterProjectArgs args = {};
  args.struct_size = sizeof(FlutterProjectArgs);
63
  args.assets_path = ::testing::GetFixturesPath();
64 65 66 67
  args.root_isolate_create_callback = [](void* data) {
    std::string str_data = reinterpret_cast<char*>(data);
    ASSERT_EQ(str_data, "Data");
  };
68

69
  fml::UniqueFD fixtures_dir = fml::OpenDirectory(
70
      ::testing::GetFixturesPath(), false, fml::FilePermission::kRead);
71 72 73 74 75 76 77 78 79 80 81 82 83 84
  std::vector<std::unique_ptr<fml::FileMapping>> aot_mappings;
  if (fml::FileExists(fixtures_dir, "vm_snapshot_data")) {
    MapAOTAsset(aot_mappings, fixtures_dir, "vm_snapshot_data", false,
                &args.vm_snapshot_data, &args.vm_snapshot_data_size);
    MapAOTAsset(aot_mappings, fixtures_dir, "vm_snapshot_instr", true,
                &args.vm_snapshot_instructions,
                &args.vm_snapshot_instructions_size);
    MapAOTAsset(aot_mappings, fixtures_dir, "isolate_snapshot_data", false,
                &args.isolate_snapshot_data, &args.isolate_snapshot_data_size);
    MapAOTAsset(aot_mappings, fixtures_dir, "isolate_snapshot_instr", true,
                &args.isolate_snapshot_instructions,
                &args.isolate_snapshot_instructions_size);
  }

85 86
  std::string str_data = "Data";
  void* user_data = const_cast<char*>(str_data.c_str());
87
  FlutterEngine engine = nullptr;
88
  FlutterEngineResult result = FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config,
89
                                                &args, user_data, &engine);
90
  ASSERT_EQ(result, FlutterEngineResult::kSuccess);
91 92

  result = FlutterEngineShutdown(engine);
93
  ASSERT_EQ(result, FlutterEngineResult::kSuccess);
94
}
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

using EmbedderFixture = testing::EmbedderTest;

TEST_F(EmbedderFixture, CanLaunchAndShutdownWithFixture) {
  EmbedderConfigBuilder builder;

  builder.SetSoftwareRendererConfig();
  builder.SetAssetsPathFromFixture(this);
  builder.SetSnapshotsFromFixture(this);

  auto engine = builder.LaunchEngine();
  ASSERT_TRUE(engine.is_valid());
}

TEST_F(EmbedderFixture, CanLaunchAndShutdownWithFixtureMultipleTimes) {
  EmbedderConfigBuilder builder;

  builder.SetSoftwareRendererConfig();
  builder.SetAssetsPathFromFixture(this);
  builder.SetSnapshotsFromFixture(this);
  for (size_t i = 0; i < 100; ++i) {
    auto engine = builder.LaunchEngine();
    ASSERT_TRUE(engine.is_valid());
    FML_LOG(INFO) << "Engine launch count: " << i + 1;
  }
}

}  // namespace testing
}  // namespace shell