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