Flutter Engine
json_message_codec_unittests.cc
Go to the documentation of this file.
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/shell/platform/common/cpp/json_message_codec.h"
6 
7 #include <limits>
8 #include <map>
9 #include <vector>
10 
11 #include "gtest/gtest.h"
12 
13 namespace flutter {
14 
15 namespace {
16 
17 // Validates round-trip encoding and decoding of |value|.
18 static void CheckEncodeDecode(const rapidjson::Document& value) {
19  const JsonMessageCodec& codec = JsonMessageCodec::GetInstance();
20  auto encoded = codec.EncodeMessage(value);
21  ASSERT_TRUE(encoded);
22  auto decoded = codec.DecodeMessage(*encoded);
23  EXPECT_EQ(value, *decoded);
24 }
25 
26 } // namespace
27 
28 // Tests that a JSON document with various data types round-trips correctly.
29 TEST(JsonMessageCodec, EncodeDecode) {
30  rapidjson::Document array(rapidjson::kArrayType);
31  auto& allocator = array.GetAllocator();
32 
33  array.PushBack("string", allocator);
34 
35  rapidjson::Value map(rapidjson::kObjectType);
36  map.AddMember("a", -7, allocator);
37  map.AddMember("b", std::numeric_limits<int>::max(), allocator);
38  map.AddMember("c", 3.14159, allocator);
39  map.AddMember("d", true, allocator);
40  map.AddMember("e", rapidjson::Value(), allocator);
41  array.PushBack(map, allocator);
42 
43  CheckEncodeDecode(array);
44 }
45 
46 } // namespace flutter
static void CheckEncodeDecode(const EncodableValue &value, const std::vector< uint8_t > &expected_encoding, const StandardCodecSerializer *serializer=nullptr, std::function< bool(const EncodableValue &a, const EncodableValue &b)> custom_comparator=nullptr)
uint8_t value
static const JsonMessageCodec & GetInstance()
TEST(DartServiceIsolateTest, CanAddAndRemoveHandles)