blob: e1f3062233421302aaf445efb1cb4c68d68c25eb [file] [log] [blame]
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Ben Wagner21bca282019-05-15 10:15:52 -04008#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "tests/Test.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000010
Brian Salomon238069b2018-07-11 15:58:57 -040011namespace {
Chris Dalton9a986cf2018-10-18 15:27:59 -060012
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000013struct TestMessage {
Brian Salomon238069b2018-07-11 15:58:57 -040014 TestMessage(int i, float f) : x(i), y(f) {}
15
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000016 int x;
17 float y;
18};
Chris Dalton9a986cf2018-10-18 15:27:59 -060019
20static inline bool SkShouldPostMessageToBus(const TestMessage&, uint32_t) {
21 return true;
22}
23
Brian Salomon238069b2018-07-11 15:58:57 -040024}
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000025DECLARE_SKMESSAGEBUS_MESSAGE(TestMessage)
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000026
27DEF_TEST(MessageBus, r) {
28 // Register two inboxes to receive all TestMessages.
29 SkMessageBus<TestMessage>::Inbox inbox1, inbox2;
30
31 // Send two messages.
32 const TestMessage m1 = { 5, 4.2f };
33 const TestMessage m2 = { 6, 4.3f };
34 SkMessageBus<TestMessage>::Post(m1);
35 SkMessageBus<TestMessage>::Post(m2);
36
37 // Make sure we got two.
bsalomon23e619c2015-02-06 11:54:28 -080038 SkTArray<TestMessage> messages;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000039 inbox1.poll(&messages);
40 REPORTER_ASSERT(r, 2 == messages.count());
41 REPORTER_ASSERT(r, 5 == messages[0].x);
42 REPORTER_ASSERT(r, 6 == messages[1].x);
43
44 // Send another; check we get just that one.
45 const TestMessage m3 = { 1, 0.3f };
46 SkMessageBus<TestMessage>::Post(m3);
47 inbox1.poll(&messages);
48 REPORTER_ASSERT(r, 1 == messages.count());
49 REPORTER_ASSERT(r, 1 == messages[0].x);
50
51 // Nothing was sent since the last read.
52 inbox1.poll(&messages);
53 REPORTER_ASSERT(r, 0 == messages.count());
54
55 // Over all this time, inbox2 should have piled up 3 messages.
56 inbox2.poll(&messages);
57 REPORTER_ASSERT(r, 3 == messages.count());
58 REPORTER_ASSERT(r, 5 == messages[0].x);
59 REPORTER_ASSERT(r, 6 == messages[1].x);
60 REPORTER_ASSERT(r, 1 == messages[2].x);
61}
62
Brian Salomon238069b2018-07-11 15:58:57 -040063namespace {
Chris Dalton9a986cf2018-10-18 15:27:59 -060064
Brian Salomon238069b2018-07-11 15:58:57 -040065struct AddressedMessage {
66 uint32_t fInboxID;
Brian Salomon238069b2018-07-11 15:58:57 -040067};
Chris Dalton9a986cf2018-10-18 15:27:59 -060068
69static inline bool SkShouldPostMessageToBus(const AddressedMessage& msg, uint32_t msgBusUniqueID) {
70 SkASSERT(msgBusUniqueID);
71 if (!msg.fInboxID) {
72 return true;
73 }
74 return msgBusUniqueID == msg.fInboxID;
Brian Salomon238069b2018-07-11 15:58:57 -040075}
Chris Dalton9a986cf2018-10-18 15:27:59 -060076
77}
78
Brian Salomon238069b2018-07-11 15:58:57 -040079DECLARE_SKMESSAGEBUS_MESSAGE(AddressedMessage)
80
Chris Dalton9a986cf2018-10-18 15:27:59 -060081DEF_TEST(MessageBus_SkShouldPostMessageToBus, r) {
Brian Salomon238069b2018-07-11 15:58:57 -040082 SkMessageBus<AddressedMessage>::Inbox inbox1(1), inbox2(2);
83
84 SkMessageBus<AddressedMessage>::Post({0}); // Should go to both
85 SkMessageBus<AddressedMessage>::Post({1}); // Should go to inbox1
86 SkMessageBus<AddressedMessage>::Post({2}); // Should go to inbox2
87 SkMessageBus<AddressedMessage>::Post({3}); // Should go nowhere
88
89 SkTArray<AddressedMessage> messages;
90 inbox1.poll(&messages);
91 REPORTER_ASSERT(r, messages.count() == 2);
92 if (messages.count() == 2) {
93 REPORTER_ASSERT(r, messages[0].fInboxID == 0);
94 REPORTER_ASSERT(r, messages[1].fInboxID == 1);
95 }
96 inbox2.poll(&messages);
97 REPORTER_ASSERT(r, messages.count() == 2);
98 if (messages.count() == 2) {
99 REPORTER_ASSERT(r, messages[0].fInboxID == 0);
100 REPORTER_ASSERT(r, messages[1].fInboxID == 2);
101 }
102}
103
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000104// Multithreaded tests tbd.