Flutter Engine
image_encoding_unittests.cc
Go to the documentation of this file.
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 #include "flutter/lib/ui/painting/image_encoding.h"
6 
7 #include "flutter/common/task_runners.h"
8 #include "flutter/fml/synchronization/waitable_event.h"
9 #include "flutter/runtime/dart_vm.h"
10 #include "flutter/shell/common/shell_test.h"
11 #include "flutter/shell/common/thread_host.h"
12 #include "flutter/testing/testing.h"
13 
14 namespace flutter {
15 namespace testing {
16 
17 namespace {
18 fml::AutoResetWaitableEvent message_latch;
19 };
20 
21 TEST_F(ShellTest, EncodeImageGivesExternalTypedData) {
22  auto nativeEncodeImage = [&](Dart_NativeArguments args) {
23  auto image_handle = Dart_GetNativeArgument(args, 0);
24  image_handle =
25  Dart_GetField(image_handle, Dart_NewStringFromCString("_image"));
26  ASSERT_FALSE(Dart_IsError(image_handle)) << Dart_GetError(image_handle);
27  ASSERT_FALSE(Dart_IsNull(image_handle));
28  auto format_handle = Dart_GetNativeArgument(args, 1);
29  auto callback_handle = Dart_GetNativeArgument(args, 2);
30 
31  intptr_t peer = 0;
32  Dart_Handle result = Dart_GetNativeInstanceField(
33  image_handle, tonic::DartWrappable::kPeerIndex, &peer);
34  ASSERT_FALSE(Dart_IsError(result));
35  CanvasImage* canvas_image = reinterpret_cast<CanvasImage*>(peer);
36 
37  int64_t format = -1;
38  result = Dart_IntegerToInt64(format_handle, &format);
39  ASSERT_FALSE(Dart_IsError(result));
40 
41  result = EncodeImage(canvas_image, format, callback_handle);
42  ASSERT_TRUE(Dart_IsNull(result));
43  };
44 
45  auto nativeValidateExternal = [&](Dart_NativeArguments args) {
46  auto handle = Dart_GetNativeArgument(args, 0);
47 
48  auto typed_data_type = Dart_GetTypeOfExternalTypedData(handle);
49  EXPECT_EQ(typed_data_type, Dart_TypedData_kUint8);
50 
51  message_latch.Signal();
52  };
53 
54  Settings settings = CreateSettingsForFixture();
55  TaskRunners task_runners("test", // label
56  GetCurrentTaskRunner(), // platform
57  CreateNewThread(), // raster
58  CreateNewThread(), // ui
59  CreateNewThread() // io
60  );
61 
62  AddNativeCallback("EncodeImage", CREATE_NATIVE_ENTRY(nativeEncodeImage));
63  AddNativeCallback("ValidateExternal",
64  CREATE_NATIVE_ENTRY(nativeValidateExternal));
65 
66  std::unique_ptr<Shell> shell =
67  CreateShell(std::move(settings), std::move(task_runners));
68 
69  ASSERT_TRUE(shell->IsSetup());
70  auto configuration = RunConfiguration::InferFromSettings(settings);
71  configuration.SetEntrypoint("encodeImageProducesExternalUint8List");
72 
73  shell->RunEngine(std::move(configuration), [&](auto result) {
74  ASSERT_EQ(result, Engine::RunStatus::Success);
75  });
76 
77  message_latch.Wait();
78  DestroyShell(std::move(shell), std::move(task_runners));
79 }
80 
81 } // namespace testing
82 } // namespace flutter
static RunConfiguration InferFromSettings(const Settings &settings, fml::RefPtr< fml::TaskRunner > io_worker=nullptr)
Attempts to infer a run configuration from the settings object. This tries to create a run configurat...
#define CREATE_NATIVE_ENTRY(native_entry)
TEST_F(BackdropFilterLayerTest, PaintingEmptyLayerDies)
Dart_Handle EncodeImage(CanvasImage *canvas_image, int format, Dart_Handle callback_handle)