Skip to content

Commit 8085624

Browse files
committed
[Linux] add MockSignalHandler for testing GObject signals
This is a simple helper class that allows settings GMock expectations on GObject signals: ```c flutter::testing::MockSignalHandler foo_changed(foo_object, "changed"); EXPECT_SIGNAL(foo_changed).Times(1); foo_set_something(foo_object, ...); ```
1 parent 230b49b commit 8085624

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

shell/platform/linux/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ executable("flutter_linux_unittests") {
208208
"testing/mock_epoxy.cc",
209209
"testing/mock_plugin_registrar.cc",
210210
"testing/mock_renderer.cc",
211+
"testing/mock_signal_handler.cc",
211212
"testing/mock_text_input_plugin.cc",
212213
"testing/mock_texture_registrar.cc",
213214
]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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/testing/mock_signal_handler.h"
6+
7+
#include <glib-object.h>
8+
9+
namespace flutter {
10+
namespace testing {
11+
12+
MockSignalHandler::MockSignalHandler(gpointer instance, const gchar* name) {
13+
id_ = g_signal_connect_data(instance, name, G_CALLBACK(OnSignal), this,
14+
nullptr, G_CONNECT_SWAPPED);
15+
g_object_add_weak_pointer(G_OBJECT(instance), &instance_);
16+
}
17+
18+
MockSignalHandler::~MockSignalHandler() {
19+
if (instance_) {
20+
g_signal_handler_disconnect(instance_, id_);
21+
g_object_remove_weak_pointer(G_OBJECT(instance_), &instance_);
22+
}
23+
}
24+
25+
void MockSignalHandler::OnSignal(MockSignalHandler* mock) {
26+
mock->Handler();
27+
}
28+
29+
} // namespace testing
30+
} // namespace flutter
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
#ifndef FLUTTER_SHELL_PLATFORM_LINUX_MOCK_SIGNAL_HANDLER_H_
6+
#define FLUTTER_SHELL_PLATFORM_LINUX_MOCK_SIGNAL_HANDLER_H_
7+
8+
#include <glib.h>
9+
10+
#include "gmock/gmock.h"
11+
12+
// Sets an expectation on the mock signal handler.
13+
//
14+
// For example:
15+
//
16+
// MockSignalHandler foo_changed(foo, "changed");
17+
// EXPECT_SIGNAL(foo_changed).Times(3);
18+
//
19+
#define EXPECT_SIGNAL(mock) EXPECT_CALL(mock, Handler())
20+
21+
namespace flutter {
22+
namespace testing {
23+
24+
// Allows setting expectations on signals. See EXPECT_SIGNAL() above.
25+
class MockSignalHandler {
26+
public:
27+
MockSignalHandler(gpointer instance, const gchar* name);
28+
~MockSignalHandler();
29+
30+
MOCK_METHOD0(Handler, void());
31+
32+
private:
33+
static void OnSignal(MockSignalHandler* mock);
34+
35+
gulong id_ = 0;
36+
gpointer instance_ = nullptr;
37+
};
38+
39+
} // namespace testing
40+
} // namespace flutter
41+
42+
#endif // FLUTTER_SHELL_PLATFORM_LINUX_MOCK_SIGNAL_HANDLER_H_

0 commit comments

Comments
 (0)