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/client_wrapper/include/flutter/standard_method_codec.h"
6
7 #include "flutter/shell/platform/common/cpp/client_wrapper/testing/encodable_value_utils.h"
8 #include "gtest/gtest.h"
9
10 namespace flutter {
11
12 namespace {
13
14 // Returns true if the given method calls have the same method name, and their
15 // arguments have equivalent values.
MethodCallsAreEqual(const MethodCall<EncodableValue> & a,const MethodCall<EncodableValue> & b)16 bool MethodCallsAreEqual(const MethodCall<EncodableValue>& a,
17 const MethodCall<EncodableValue>& b) {
18 if (a.method_name() != b.method_name()) {
19 return false;
20 }
21 // Treat nullptr and Null as equivalent.
22 if ((!a.arguments() || a.arguments()->IsNull()) &&
23 (!b.arguments() || b.arguments()->IsNull())) {
24 return true;
25 }
26 return testing::EncodableValuesAreEqual(*a.arguments(), *b.arguments());
27 }
28
29 } // namespace
30
TEST(StandardMethodCodec,HandlesMethodCallsWithNullArguments)31 TEST(StandardMethodCodec, HandlesMethodCallsWithNullArguments) {
32 const StandardMethodCodec& codec = StandardMethodCodec::GetInstance();
33 MethodCall<EncodableValue> call("hello", nullptr);
34 auto encoded = codec.EncodeMethodCall(call);
35 ASSERT_NE(encoded.get(), nullptr);
36 std::unique_ptr<MethodCall<EncodableValue>> decoded =
37 codec.DecodeMethodCall(*encoded);
38 ASSERT_NE(decoded.get(), nullptr);
39 EXPECT_TRUE(MethodCallsAreEqual(call, *decoded));
40 }
41
TEST(StandardMethodCodec,HandlesMethodCallsWithArgument)42 TEST(StandardMethodCodec, HandlesMethodCallsWithArgument) {
43 const StandardMethodCodec& codec = StandardMethodCodec::GetInstance();
44 MethodCall<EncodableValue> call(
45 "hello", std::make_unique<EncodableValue>(EncodableList{
46 EncodableValue(42),
47 EncodableValue("world"),
48 }));
49 auto encoded = codec.EncodeMethodCall(call);
50 ASSERT_NE(encoded.get(), nullptr);
51 std::unique_ptr<MethodCall<EncodableValue>> decoded =
52 codec.DecodeMethodCall(*encoded);
53 ASSERT_NE(decoded.get(), nullptr);
54 EXPECT_TRUE(MethodCallsAreEqual(call, *decoded));
55 }
56
TEST(StandardMethodCodec,HandlesSuccessEnvelopesWithNullResult)57 TEST(StandardMethodCodec, HandlesSuccessEnvelopesWithNullResult) {
58 const StandardMethodCodec& codec = StandardMethodCodec::GetInstance();
59 auto encoded = codec.EncodeSuccessEnvelope();
60 ASSERT_NE(encoded.get(), nullptr);
61 std::vector<uint8_t> bytes = {0x00, 0x00};
62 EXPECT_EQ(*encoded, bytes);
63 // TODO: Add round-trip check once decoding replies is implemented.
64 }
65
TEST(StandardMethodCodec,HandlesSuccessEnvelopesWithResult)66 TEST(StandardMethodCodec, HandlesSuccessEnvelopesWithResult) {
67 const StandardMethodCodec& codec = StandardMethodCodec::GetInstance();
68 EncodableValue result(42);
69 auto encoded = codec.EncodeSuccessEnvelope(&result);
70 ASSERT_NE(encoded.get(), nullptr);
71 std::vector<uint8_t> bytes = {0x00, 0x03, 0x2a, 0x00, 0x00, 0x00};
72 EXPECT_EQ(*encoded, bytes);
73 // TODO: Add round-trip check once decoding replies is implemented.
74 }
75
TEST(StandardMethodCodec,HandlesErrorEnvelopesWithNulls)76 TEST(StandardMethodCodec, HandlesErrorEnvelopesWithNulls) {
77 const StandardMethodCodec& codec = StandardMethodCodec::GetInstance();
78 auto encoded = codec.EncodeErrorEnvelope("errorCode");
79 ASSERT_NE(encoded.get(), nullptr);
80 std::vector<uint8_t> bytes = {0x01, 0x07, 0x09, 0x65, 0x72, 0x72, 0x6f,
81 0x72, 0x43, 0x6f, 0x64, 0x65, 0x00, 0x00};
82 EXPECT_EQ(*encoded, bytes);
83 // TODO: Add round-trip check once decoding replies is implemented.
84 }
85
TEST(StandardMethodCodec,HandlesErrorEnvelopesWithDetails)86 TEST(StandardMethodCodec, HandlesErrorEnvelopesWithDetails) {
87 const StandardMethodCodec& codec = StandardMethodCodec::GetInstance();
88 EncodableValue details(EncodableList{
89 EncodableValue("a"),
90 EncodableValue(42),
91 });
92 auto encoded =
93 codec.EncodeErrorEnvelope("errorCode", "something failed", &details);
94 ASSERT_NE(encoded.get(), nullptr);
95 std::vector<uint8_t> bytes = {
96 0x01, 0x07, 0x09, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x43, 0x6f,
97 0x64, 0x65, 0x07, 0x10, 0x73, 0x6f, 0x6d, 0x65, 0x74, 0x68,
98 0x69, 0x6e, 0x67, 0x20, 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64,
99 0x0c, 0x02, 0x07, 0x01, 0x61, 0x03, 0x2a, 0x00, 0x00, 0x00,
100 };
101 EXPECT_EQ(*encoded, bytes);
102 // TODO: Add round-trip check once decoding replies is implemented.
103 }
104
105 } // namespace flutter
106