Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 8fd30ea

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 GObject* foo_object = ...; flutter::testing::MockSignalHandler foo_changed(foo_object, "changed"); EXPECT_SIGNAL(foo_changed).Times(1); foo_set_something(foo_object, ...); ```
1 parent c6dc1b3 commit 8fd30ea

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

shell/platform/linux/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ executable("flutter_linux_unittests") {
209209
"testing/mock_epoxy.cc",
210210
"testing/mock_plugin_registrar.cc",
211211
"testing/mock_renderer.cc",
212+
"testing/mock_signal_handler.cc",
212213
"testing/mock_text_input_plugin.cc",
213214
"testing/mock_texture_registrar.cc",
214215
]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
SignalHandler::SignalHandler(gpointer instance,
13+
const gchar* name,
14+
GCallback callback) {
15+
id_ = g_signal_connect_data(instance, name, callback, this, nullptr,
16+
G_CONNECT_SWAPPED);
17+
g_object_add_weak_pointer(G_OBJECT(instance), &instance_);
18+
}
19+
20+
SignalHandler::~SignalHandler() {
21+
if (instance_) {
22+
g_signal_handler_disconnect(instance_, id_);
23+
g_object_remove_weak_pointer(G_OBJECT(instance_), &instance_);
24+
}
25+
}
26+
27+
} // namespace testing
28+
} // namespace flutter
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
// Expects a signal that has no arguments.
13+
//
14+
// MockSignalHandler timeout(timer, "timeout");
15+
// EXPECT_SIGNAL(timeout).Times(3);
16+
//
17+
#define EXPECT_SIGNAL(mock) EXPECT_CALL(mock, Handler())
18+
19+
// Expects a signal that has 1 argument.
20+
//
21+
// MockSignalHandler1<int> name_changed(object, "name-changed");
22+
// EXPECT_SIGNAL(name_changed, testing::StrEq("example"));
23+
//
24+
#define EXPECT_SIGNAL1(mock, a1) EXPECT_CALL(mock, Handler1(a1))
25+
26+
// Expects a signal that has 2 arguments.
27+
//
28+
// MockSignalHandler2<int, GObject*> child_added(parent, "children::add");
29+
// EXPECT_SIGNAL2(child_added, testing::Eq(1), testing::A<GObject*>());
30+
//
31+
#define EXPECT_SIGNAL2(mock, a1, a2) EXPECT_CALL(mock, Handler2(a1, a2))
32+
33+
namespace flutter {
34+
namespace testing {
35+
36+
class SignalHandler {
37+
public:
38+
SignalHandler(gpointer instance, const gchar* name, GCallback callback);
39+
virtual ~SignalHandler();
40+
41+
private:
42+
gulong id_ = 0;
43+
gpointer instance_ = nullptr;
44+
};
45+
46+
// A mock signal handler that has no arguments. Used with EXPECT_SIGNAL().
47+
class MockSignalHandler : public SignalHandler {
48+
public:
49+
MockSignalHandler(gpointer instance, const gchar* name)
50+
: SignalHandler(instance, name, G_CALLBACK(OnSignal)) {}
51+
52+
MOCK_METHOD0(Handler, void());
53+
54+
private:
55+
static void OnSignal(MockSignalHandler* mock) { mock->Handler(); }
56+
};
57+
58+
// A mock signal handler that has 1 argument. Used with EXPECT_SIGNAL1().
59+
template <typename A1>
60+
class MockSignalHandler1 : public SignalHandler {
61+
public:
62+
MockSignalHandler1(gpointer instance, const gchar* name)
63+
: SignalHandler(instance, name, G_CALLBACK(OnSignal1)) {}
64+
65+
MOCK_METHOD1(Handler1, void(A1 a1));
66+
67+
private:
68+
static void OnSignal1(MockSignalHandler1* mock, A1 a1) { mock->Handler1(a1); }
69+
};
70+
71+
// A mock signal handler that has 2 arguments. Used with EXPECT_SIGNAL2().
72+
template <typename A1, typename A2>
73+
class MockSignalHandler2 : public SignalHandler {
74+
public:
75+
MockSignalHandler2(gpointer instance, const gchar* name)
76+
: SignalHandler(instance, name, G_CALLBACK(OnSignal2)) {}
77+
78+
MOCK_METHOD2(Handler2, void(A1 a1, A2 a2));
79+
80+
private:
81+
static void OnSignal2(MockSignalHandler2* mock, A1 a1, A2 a2) {
82+
mock->Handler2(a1, a2);
83+
}
84+
};
85+
86+
} // namespace testing
87+
} // namespace flutter
88+
89+
#endif // FLUTTER_SHELL_PLATFORM_LINUX_MOCK_SIGNAL_HANDLER_H_

0 commit comments

Comments
 (0)