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