From b0d95460e4de960a059228c1e80cf58158aaf69e Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Tue, 4 Feb 2025 16:08:24 -0800 Subject: [PATCH] Refactors platform_view_android_delegate test (#162696) Reduce magic number ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- .../platform_view_android_delegate.cc | 6 ------ .../platform_view_android_delegate.h | 5 +++++ .../platform_view_android_delegate_unittests.cc | 14 ++++++++++---- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/platform_view_android_delegate/platform_view_android_delegate.cc b/engine/src/flutter/shell/platform/android/platform_view_android_delegate/platform_view_android_delegate.cc index 1b8a3cdc5e6..4893eaa9cf5 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android_delegate/platform_view_android_delegate.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android_delegate/platform_view_android_delegate.cc @@ -44,12 +44,6 @@ PlatformViewAndroidDelegate::PlatformViewAndroidDelegate( void PlatformViewAndroidDelegate::UpdateSemantics( const flutter::SemanticsNodeUpdates& update, const flutter::CustomAccessibilityActionUpdates& actions) { - constexpr size_t kBytesPerNode = 49 * sizeof(int32_t); - constexpr size_t kBytesPerChild = sizeof(int32_t); - constexpr size_t kBytesPerCustomAction = sizeof(int32_t); - constexpr size_t kBytesPerAction = 4 * sizeof(int32_t); - constexpr size_t kBytesPerStringAttribute = 4 * sizeof(int32_t); - { size_t num_bytes = 0; for (const auto& value : update) { diff --git a/engine/src/flutter/shell/platform/android/platform_view_android_delegate/platform_view_android_delegate.h b/engine/src/flutter/shell/platform/android/platform_view_android_delegate/platform_view_android_delegate.h index 0a9d2a39f80..5e2b59f70e2 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android_delegate/platform_view_android_delegate.h +++ b/engine/src/flutter/shell/platform/android/platform_view_android_delegate/platform_view_android_delegate.h @@ -16,6 +16,11 @@ namespace flutter { class PlatformViewAndroidDelegate { public: + static constexpr size_t kBytesPerNode = 49 * sizeof(int32_t); + static constexpr size_t kBytesPerChild = sizeof(int32_t); + static constexpr size_t kBytesPerCustomAction = sizeof(int32_t); + static constexpr size_t kBytesPerAction = 4 * sizeof(int32_t); + static constexpr size_t kBytesPerStringAttribute = 4 * sizeof(int32_t); explicit PlatformViewAndroidDelegate( std::shared_ptr jni_facade); void UpdateSemantics( diff --git a/engine/src/flutter/shell/platform/android/platform_view_android_delegate/platform_view_android_delegate_unittests.cc b/engine/src/flutter/shell/platform/android/platform_view_android_delegate/platform_view_android_delegate_unittests.cc index 33039dcf976..687c7b354e0 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android_delegate/platform_view_android_delegate_unittests.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android_delegate/platform_view_android_delegate_unittests.cc @@ -23,7 +23,8 @@ TEST(PlatformViewShell, UpdateSemanticsDoesFlutterViewUpdateSemantics) { node0.tooltip = "tooltip"; update.insert(std::make_pair(0, node0)); - std::vector expected_buffer(196); + std::vector expected_buffer( + PlatformViewAndroidDelegate::kBytesPerNode); std::vector> expected_string_attribute_args(0); size_t position = 0; int32_t* buffer_int32 = reinterpret_cast(&expected_buffer[0]); @@ -87,7 +88,8 @@ TEST(PlatformViewShell, UpdateSemanticsDoesUpdatelinkUrl) { node0.linkUrl = "url"; update.insert(std::make_pair(0, node0)); - std::vector expected_buffer(196); + std::vector expected_buffer( + PlatformViewAndroidDelegate::kBytesPerNode); std::vector> expected_string_attribute_args(0); size_t position = 0; int32_t* buffer_int32 = reinterpret_cast(&expected_buffer[0]); @@ -165,7 +167,10 @@ TEST(PlatformViewShell, node0.hintAttributes.push_back(locale_attribute); update.insert(std::make_pair(0, node0)); - std::vector expected_buffer(228); + std::vector expected_buffer( + PlatformViewAndroidDelegate::kBytesPerNode + + // 1 label attribute + 1 hint attribute. + 2 * PlatformViewAndroidDelegate::kBytesPerStringAttribute); std::vector> expected_string_attribute_args; size_t position = 0; int32_t* buffer_int32 = reinterpret_cast(&expected_buffer[0]); @@ -241,7 +246,8 @@ TEST(PlatformViewShell, action0.hint = "hint"; actions.insert(std::make_pair(0, action0)); - std::vector expected_actions_buffer(16); + std::vector expected_actions_buffer( + PlatformViewAndroidDelegate::kBytesPerAction); int32_t* actions_buffer_int32 = reinterpret_cast(&expected_actions_buffer[0]); std::vector expected_action_strings;