Skip to content

Commit 2d82dca

Browse files
jpnurmijustinmc
authored andcommitted
[Linux] add MockSignalHandler for testing GObject signals (flutter#32650)
1 parent 48c4584 commit 2d82dca

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

0 commit comments

Comments
 (0)