| Flutter Linux Embedder |

fl_platform_plugin_test.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/linux/fl_platform_plugin.h"
6 #include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
7 #include "flutter/shell/platform/linux/fl_method_codec_private.h"
8 #include "flutter/shell/platform/linux/public/flutter_linux/fl_json_method_codec.h"
9 #include "flutter/shell/platform/linux/public/flutter_linux/fl_method_codec.h"
10 #include "flutter/shell/platform/linux/testing/fl_test.h"
11 #include "flutter/shell/platform/linux/testing/mock_binary_messenger.h"
12 #include "flutter/testing/testing.h"
13
14 #include "gmock/gmock.h"
15 #include "gtest/gtest.h"
16
17 MATCHER_P(SuccessResponse, result, "") {
18 g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
19 g_autoptr(FlMethodResponse) response =
20 fl_method_codec_decode_response(FL_METHOD_CODEC(codec), arg, nullptr);
21 if (fl_value_equal(fl_method_response_get_result(response, nullptr),
22 result)) {
23 return true;
24 }
25 *result_listener << ::testing::PrintToString(response);
26 return false;
27 }
28
29 class MethodCallMatcher {
30 public:
31 using is_gtest_matcher = void;
32
33 explicit MethodCallMatcher(::testing::Matcher<std::string> name,
34 ::testing::Matcher<FlValue*> args)
35 : name_(std::move(name)), args_(std::move(args)) {}
36
37 bool MatchAndExplain(GBytes* method_call,
38 ::testing::MatchResultListener* result_listener) const {
39 g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
40 g_autoptr(GError) error = nullptr;
41 g_autofree gchar* name = nullptr;
42 g_autoptr(FlValue) args = nullptr;
43 gboolean result = fl_method_codec_decode_method_call(
44 FL_METHOD_CODEC(codec), method_call, &name, &args, &error);
45 if (!result) {
46 *result_listener << ::testing::PrintToString(error->message);
47 return false;
48 }
49 if (!name_.MatchAndExplain(name, result_listener)) {
50 *result_listener << " where the name doesn't match: \"" << name << "\"";
51 return false;
52 }
53 if (!args_.MatchAndExplain(args, result_listener)) {
54 *result_listener << " where the args don't match: "
55 << ::testing::PrintToString(args);
56 return false;
57 }
58 return true;
59 }
60
61 void DescribeTo(std::ostream* os) const {
62 *os << "method name ";
63 name_.DescribeTo(os);
64 *os << " and args ";
65 args_.DescribeTo(os);
66 }
67
68 void DescribeNegationTo(std::ostream* os) const {
69 *os << "method name ";
70 name_.DescribeNegationTo(os);
71 *os << " or args ";
72 args_.DescribeNegationTo(os);
73 }
74
75 private:
76 ::testing::Matcher<std::string> name_;
77 ::testing::Matcher<FlValue*> args_;
78 };
79
80 static ::testing::Matcher<GBytes*> MethodCall(
81 const std::string& name,
82 ::testing::Matcher<FlValue*> args) {
83 return MethodCallMatcher(::testing::StrEq(name), std::move(args));
84 }
85
86 MATCHER_P(FlValueEq, value, "equal to " + ::testing::PrintToString(value)) {
87 return fl_value_equal(arg, value);
88 }
89
90 TEST(FlPlatformPluginTest, PlaySound) {
91 ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
92
93 g_autoptr(FlPlatformPlugin) plugin = fl_platform_plugin_new(messenger);
94 EXPECT_NE(plugin, nullptr);
95
96 g_autoptr(FlValue) args = fl_value_new_string("SystemSoundType.alert");
97 g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
98 g_autoptr(GBytes) message = fl_method_codec_encode_method_call(
99 FL_METHOD_CODEC(codec), "SystemSound.play", args, nullptr);
100
101 g_autoptr(FlValue) null = fl_value_new_null();
102 EXPECT_CALL(messenger, fl_binary_messenger_send_response(
103 ::testing::Eq<FlBinaryMessenger*>(messenger),
104 ::testing::_, SuccessResponse(null), ::testing::_))
105 .WillOnce(::testing::Return(true));
106
107 messenger.ReceiveMessage("flutter/platform", message);
108 }
109
110 TEST(FlPlatformPluginTest, ExitApplication) {
111 ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
112
113 g_autoptr(FlPlatformPlugin) plugin = fl_platform_plugin_new(messenger);
114 EXPECT_NE(plugin, nullptr);
115 g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
116
117 g_autoptr(FlValue) null = fl_value_new_null();
118 ON_CALL(messenger, fl_binary_messenger_send_response(
119 ::testing::Eq<FlBinaryMessenger*>(messenger),
120 ::testing::_, SuccessResponse(null), ::testing::_))
121 .WillByDefault(testing::Return(TRUE));
122
123 // Indicate that the binding is initialized.
124 g_autoptr(GError) error = nullptr;
125 g_autoptr(GBytes) init_message = fl_method_codec_encode_method_call(
126 FL_METHOD_CODEC(codec), "System.initializationComplete", nullptr, &error);
127 messenger.ReceiveMessage("flutter/platform", init_message);
128
129 g_autoptr(FlValue) request_args = fl_value_new_map();
130 fl_value_set_string_take(request_args, "type",
131 fl_value_new_string("cancelable"));
132 EXPECT_CALL(messenger,
133 fl_binary_messenger_send_on_channel(
134 ::testing::Eq<FlBinaryMessenger*>(messenger),
135 ::testing::StrEq("flutter/platform"),
136 MethodCall("System.requestAppExit", FlValueEq(request_args)),
137 ::testing::_, ::testing::_, ::testing::_));
138
139 g_autoptr(FlValue) args = fl_value_new_map();
140 fl_value_set_string_take(args, "type", fl_value_new_string("cancelable"));
141 g_autoptr(GBytes) message = fl_method_codec_encode_method_call(
142 FL_METHOD_CODEC(codec), "System.exitApplication", args, nullptr);
143 messenger.ReceiveMessage("flutter/platform", message);
144 }
fl_json_method_codec_new
G_MODULE_EXPORT FlJsonMethodCodec * fl_json_method_codec_new()
Definition: fl_json_method_codec.cc:205
fl_method_codec_encode_method_call
GBytes * fl_method_codec_encode_method_call(FlMethodCodec *self, const gchar *name, FlValue *args, GError **error)
Definition: fl_method_codec.cc:16
MethodCallMatcher::MethodCallMatcher
MethodCallMatcher(::testing::Matcher< std::string > name, ::testing::Matcher< FlValue * > args)
Definition: fl_platform_plugin_test.cc:33
MethodCallMatcher::DescribeNegationTo
void DescribeNegationTo(std::ostream *os) const
Definition: fl_platform_plugin_test.cc:68
fl_value_set_string_take
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:610
MethodCall
static ::testing::Matcher< GBytes * > MethodCall(const std::string &name, ::testing::Matcher< FlValue * > args)
Definition: fl_platform_plugin_test.cc:80
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
fl_platform_plugin_new
FlPlatformPlugin * fl_platform_plugin_new(FlBinaryMessenger *messenger)
Definition: fl_platform_plugin.cc:391
fl_value_new_null
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:240
MATCHER_P
MATCHER_P(SuccessResponse, result, "")
Definition: fl_platform_plugin_test.cc:17
fl_method_response_get_result
G_MODULE_EXPORT FlValue * fl_method_response_get_result(FlMethodResponse *self, GError **error)
Definition: fl_method_response.cc:82
TEST
TEST(FlPlatformPluginTest, PlaySound)
Definition: fl_platform_plugin_test.cc:90
fl_value_new_map
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:355
MethodCallMatcher::DescribeTo
void DescribeTo(std::ostream *os) const
Definition: fl_platform_plugin_test.cc:61
method_call
G_BEGIN_DECLS G_MODULE_EXPORT FlMethodCall * method_call
Definition: fl_method_channel.h:120
fl_method_codec_decode_method_call
gboolean fl_method_codec_decode_method_call(FlMethodCodec *self, GBytes *message, gchar **name, FlValue **args, GError **error)
Definition: fl_method_codec.cc:27
MethodCallMatcher::is_gtest_matcher
void is_gtest_matcher
Definition: fl_platform_plugin_test.cc:31
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_method_codec_decode_response
FlMethodResponse * fl_method_codec_decode_response(FlMethodCodec *self, GBytes *message, GError **error)
Definition: fl_method_codec.cc:62
fl_method_codec.h
MethodCallMatcher::MatchAndExplain
bool MatchAndExplain(GBytes *method_call, ::testing::MatchResultListener *result_listener) const
Definition: fl_platform_plugin_test.cc:37
fl_value_equal
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:433
fl_binary_messenger_private.h
result
GAsyncResult * result
Definition: fl_text_input_plugin.cc:106
MethodCallMatcher
Definition: fl_platform_plugin_test.cc:29
args
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
Definition: fl_event_channel.h:89
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
fl_platform_plugin.h
fl_binary_messenger_send_response
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
Definition: fl_binary_messenger.cc:438
fl_method_codec_private.h
value
uint8_t value
Definition: fl_standard_message_codec.cc:41
fl_binary_messenger_send_on_channel
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel(FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_binary_messenger.cc:451
fl_json_method_codec.h
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:265