Skip to content

Commit 84aa365

Browse files
Fix the legacy EncodableValue codepaths (flutter#20501)
A recent refactoring broke the USE_LEGACY_ENCODABLE_VALUE codepath in standard_codec.cc, which went unnoticed since it wasn't being compiled. This fixes the breakage, and also adds a temporary minimal unit test target that ensures that all the USE_LEGACY_ENCODABLE_VALUE paths are being compiled.
1 parent d4f6100 commit 84aa365

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

shell/platform/common/cpp/client_wrapper/BUILD.gn

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ source_set("client_wrapper") {
1919
[ "//flutter/shell/platform/common/cpp:relative_flutter_library_headers" ]
2020
}
2121

22+
# Temporary test for the legacy EncodableValue implementation. Remove once the
23+
# legacy version is removed.
24+
source_set("client_wrapper_legacy_encodable_value") {
25+
sources = core_cpp_client_wrapper_sources
26+
public = core_cpp_client_wrapper_includes
27+
28+
deps = [ "//flutter/shell/platform/common/cpp:common_cpp_library_headers" ]
29+
30+
configs +=
31+
[ "//flutter/shell/platform/common/cpp:desktop_library_implementation" ]
32+
33+
defines = [ "USE_LEGACY_ENCODABLE_VALUE" ]
34+
35+
public_configs =
36+
[ "//flutter/shell/platform/common/cpp:relative_flutter_library_headers" ]
37+
}
38+
2239
source_set("client_wrapper_library_stubs") {
2340
sources = [
2441
"testing/stub_flutter_api.cc",
@@ -56,6 +73,9 @@ executable("client_wrapper_unittests") {
5673
":client_wrapper",
5774
":client_wrapper_fixtures",
5875
":client_wrapper_library_stubs",
76+
77+
# Build the legacy version as well as a sanity check.
78+
":client_wrapper_unittests_legacy_encodable_value",
5979
"//flutter/testing",
6080

6181
# TODO(chunhtai): Consider refactoring flutter_root/testing so that there's a testing
@@ -66,3 +86,31 @@ executable("client_wrapper_unittests") {
6686

6787
defines = [ "FLUTTER_DESKTOP_LIBRARY" ]
6888
}
89+
90+
# Ensures that the legacy EncodableValue codepath still compiles.
91+
executable("client_wrapper_unittests_legacy_encodable_value") {
92+
testonly = true
93+
94+
sources = [
95+
"encodable_value_unittests.cc",
96+
"standard_message_codec_unittests.cc",
97+
"testing/test_codec_extensions.h",
98+
]
99+
100+
deps = [
101+
":client_wrapper_fixtures",
102+
":client_wrapper_legacy_encodable_value",
103+
":client_wrapper_library_stubs",
104+
"//flutter/testing",
105+
106+
# TODO(chunhtai): Consider refactoring flutter_root/testing so that there's a testing
107+
# target that doesn't require a Dart runtime to be linked in.
108+
# https://github.com/flutter/flutter/issues/41414.
109+
"//third_party/dart/runtime:libdart_jit",
110+
]
111+
112+
defines = [
113+
"FLUTTER_DESKTOP_LIBRARY",
114+
"USE_LEGACY_ENCODABLE_VALUE",
115+
]
116+
}

shell/platform/common/cpp/client_wrapper/encodable_value_unittests.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ TEST(EncodableValueTest, Null) {
1515
value.IsNull();
1616
}
1717

18+
#ifndef USE_LEGACY_ENCODABLE_VALUE
19+
1820
TEST(EncodableValueTest, Bool) {
1921
EncodableValue value(false);
2022

@@ -280,4 +282,6 @@ TEST(EncodableValueTest, DeepCopy) {
280282
EXPECT_EQ(std::get<std::string>(innermost_map[EncodableValue("a")]), "b");
281283
}
282284

285+
#endif // !LEGACY_ENCODABLE_VALUE
286+
283287
} // namespace flutter

shell/platform/common/cpp/client_wrapper/standard_codec.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@ void StandardCodecSerializer::WriteValue(const EncodableValue& value,
132132
// Null and bool are encoded directly in the type.
133133
break;
134134
case EncodableValue::Type::kInt:
135-
stream->WriteInt32(std::get<int32_t>(value));
135+
stream->WriteInt32(value.IntValue());
136136
break;
137-
case case EncodableValue::Type::kLong:
138-
stream->WriteInt64(std::get<int64_t>(value));
137+
case EncodableValue::Type::kLong:
138+
stream->WriteInt64(value.LongValue());
139139
break;
140140
case EncodableValue::Type::kDouble:
141141
stream->WriteAlignment(8);
142-
stream->WriteDouble(std::get<double>(value));
142+
stream->WriteDouble(value.DoubleValue());
143143
break;
144144
case EncodableValue::Type::kString: {
145145
const auto& string_value = value.StringValue();

shell/platform/common/cpp/client_wrapper/standard_message_codec_unittests.cc

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
#include <vector>
77

88
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_message_codec.h"
9-
#include "flutter/shell/platform/common/cpp/client_wrapper/testing/test_codec_extensions.h"
109
#include "gtest/gtest.h"
1110

11+
#ifndef USE_LEGACY_ENCODABLE_VALUE
12+
#include "flutter/shell/platform/common/cpp/client_wrapper/testing/test_codec_extensions.h"
13+
#endif
14+
1215
namespace flutter {
1316

1417
// Validates round-trip encoding and decoding of |value|, and checks that the
@@ -30,11 +33,21 @@ static void CheckEncodeDecode(
3033
EXPECT_EQ(*encoded, expected_encoding);
3134

3235
auto decoded = codec.DecodeMessage(*encoded);
36+
#ifdef USE_LEGACY_ENCODABLE_VALUE
37+
// Full equality isn't implemented for the legacy path; just do a sanity test
38+
// of basic types.
39+
if (value.IsNull() || value.IsBool() || value.IsInt() || value.IsLong() ||
40+
value.IsDouble() || value.IsString()) {
41+
EXPECT_FALSE(value < *decoded);
42+
EXPECT_FALSE(*decoded < value);
43+
}
44+
#else
3345
if (custom_comparator) {
3446
EXPECT_TRUE(custom_comparator(value, *decoded));
3547
} else {
3648
EXPECT_EQ(value, *decoded);
3749
}
50+
#endif
3851
}
3952

4053
// Validates round-trip encoding and decoding of |value|, and checks that the
@@ -46,7 +59,11 @@ static void CheckEncodeDecodeWithEncodePrefix(
4659
const EncodableValue& value,
4760
const std::vector<uint8_t>& expected_encoding_prefix,
4861
size_t expected_encoding_length) {
62+
#ifdef USE_LEGACY_ENCODABLE_VALUE
63+
EXPECT_TRUE(value.IsMap());
64+
#else
4965
EXPECT_TRUE(std::holds_alternative<EncodableMap>(value));
66+
#endif
5067
const StandardMessageCodec& codec = StandardMessageCodec::GetInstance();
5168
auto encoded = codec.EncodeMessage(value);
5269
ASSERT_TRUE(encoded);
@@ -58,7 +75,12 @@ static void CheckEncodeDecodeWithEncodePrefix(
5875
expected_encoding_prefix.begin(), expected_encoding_prefix.end()));
5976

6077
auto decoded = codec.DecodeMessage(*encoded);
78+
79+
#ifdef USE_LEGACY_ENCODABLE_VALUE
80+
EXPECT_NE(decoded, nullptr);
81+
#else
6182
EXPECT_EQ(value, *decoded);
83+
#endif
6284
}
6385

6486
TEST(StandardMessageCodec, CanEncodeAndDecodeNull) {
@@ -182,6 +204,8 @@ TEST(StandardMessageCodec, CanEncodeAndDecodeFloat64Array) {
182204
CheckEncodeDecode(value, bytes);
183205
}
184206

207+
#ifndef USE_LEGACY_ENCODABLE_VALUE
208+
185209
TEST(StandardMessageCodec, CanEncodeAndDecodeSimpleCustomType) {
186210
std::vector<uint8_t> bytes = {0x80, 0x09, 0x00, 0x00, 0x00,
187211
0x10, 0x00, 0x00, 0x00};
@@ -217,4 +241,6 @@ TEST(StandardMessageCodec, CanEncodeAndDecodeVariableLengthCustomType) {
217241
some_data_comparator);
218242
}
219243

244+
#endif // !USE_LEGACY_ENCODABLE_VALUE
245+
220246
} // namespace flutter

0 commit comments

Comments
 (0)