|
| 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/testing/dart_isolate_runner.h" |
| 6 | +#include "flutter/testing/fixture_test.h" |
| 7 | + |
| 8 | +namespace flutter { |
| 9 | +namespace testing { |
| 10 | + |
| 11 | +class DartPersistentHandleTest : public FixtureTest { |
| 12 | + public: |
| 13 | + DartPersistentHandleTest() |
| 14 | + : settings_(CreateSettingsForFixture()), |
| 15 | + vm_(DartVMRef::Create(settings_)), |
| 16 | + thread_(CreateNewThread()), |
| 17 | + task_runners_(GetCurrentTestName(), |
| 18 | + thread_, |
| 19 | + thread_, |
| 20 | + thread_, |
| 21 | + thread_) {} |
| 22 | + |
| 23 | + ~DartPersistentHandleTest() = default; |
| 24 | + |
| 25 | + [[nodiscard]] bool RunWithEntrypoint(const std::string& entrypoint) { |
| 26 | + if (running_isolate_) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + auto isolate = RunDartCodeInIsolate(vm_, settings_, task_runners_, |
| 30 | + entrypoint, {}, GetFixturesPath()); |
| 31 | + if (!isolate || isolate->get()->GetPhase() != DartIsolate::Phase::Running) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + running_isolate_ = std::move(isolate); |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + protected: |
| 40 | + Settings settings_; |
| 41 | + DartVMRef vm_; |
| 42 | + std::unique_ptr<AutoIsolateShutdown> running_isolate_; |
| 43 | + fml::RefPtr<fml::TaskRunner> thread_; |
| 44 | + TaskRunners task_runners_; |
| 45 | + FML_DISALLOW_COPY_AND_ASSIGN(DartPersistentHandleTest); |
| 46 | +}; |
| 47 | + |
| 48 | +TEST_F(DartPersistentHandleTest, ClearAfterShutdown) { |
| 49 | + auto persistent_value = tonic::DartPersistentValue(); |
| 50 | + |
| 51 | + fml::AutoResetWaitableEvent event; |
| 52 | + AddNativeCallback("GiveObjectToNative", |
| 53 | + CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { |
| 54 | + auto handle = Dart_GetNativeArgument(args, 0); |
| 55 | + |
| 56 | + auto dart_state = tonic::DartState::Current(); |
| 57 | + ASSERT_TRUE(dart_state); |
| 58 | + ASSERT_TRUE(tonic::DartState::Current()); |
| 59 | + persistent_value.Set(dart_state, handle); |
| 60 | + |
| 61 | + event.Signal(); |
| 62 | + })); |
| 63 | + |
| 64 | + ASSERT_TRUE(RunWithEntrypoint("callGiveObjectToNative")); |
| 65 | + event.Wait(); |
| 66 | + |
| 67 | + running_isolate_->Shutdown(); |
| 68 | + |
| 69 | + fml::AutoResetWaitableEvent clear; |
| 70 | + task_runners_.GetUITaskRunner()->PostTask([&] { |
| 71 | + persistent_value.Clear(); |
| 72 | + clear.Signal(); |
| 73 | + }); |
| 74 | + clear.Wait(); |
| 75 | +} |
| 76 | +} // namespace testing |
| 77 | +} // namespace flutter |
0 commit comments