blob: 145176ba1b1c0940c2a03ec6352c40772b66ec29 [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
8#include "SkMessageBus.h"
9#include "Test.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000010
Brian Salomon238069b2018-07-11 15:58:57 -040011namespace {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000012struct TestMessage {
Brian Salomon238069b2018-07-11 15:58:57 -040013 bool shouldSend(uint32_t inboxID) const { return true; }
14 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};
Brian Salomon238069b2018-07-11 15:58:57 -040019}
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000020DECLARE_SKMESSAGEBUS_MESSAGE(TestMessage)
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000021
22DEF_TEST(MessageBus, r) {
23 // Register two inboxes to receive all TestMessages.
24 SkMessageBus<TestMessage>::Inbox inbox1, inbox2;
25
26 // Send two messages.
27 const TestMessage m1 = { 5, 4.2f };
28 const TestMessage m2 = { 6, 4.3f };
29 SkMessageBus<TestMessage>::Post(m1);
30 SkMessageBus<TestMessage>::Post(m2);
31
32 // Make sure we got two.
bsalomon23e619c2015-02-06 11:54:28 -080033 SkTArray<TestMessage> messages;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000034 inbox1.poll(&messages);
35 REPORTER_ASSERT(r, 2 == messages.count());
36 REPORTER_ASSERT(r, 5 == messages[0].x);
37 REPORTER_ASSERT(r, 6 == messages[1].x);
38
39 // Send another; check we get just that one.
40 const TestMessage m3 = { 1, 0.3f };
41 SkMessageBus<TestMessage>::Post(m3);
42 inbox1.poll(&messages);
43 REPORTER_ASSERT(r, 1 == messages.count());
44 REPORTER_ASSERT(r, 1 == messages[0].x);
45
46 // Nothing was sent since the last read.
47 inbox1.poll(&messages);
48 REPORTER_ASSERT(r, 0 == messages.count());
49
50 // Over all this time, inbox2 should have piled up 3 messages.
51 inbox2.poll(&messages);
52 REPORTER_ASSERT(r, 3 == messages.count());
53 REPORTER_ASSERT(r, 5 == messages[0].x);
54 REPORTER_ASSERT(r, 6 == messages[1].x);
55 REPORTER_ASSERT(r, 1 == messages[2].x);
56}
57
Brian Salomon238069b2018-07-11 15:58:57 -040058namespace {
59struct AddressedMessage {
60 uint32_t fInboxID;
61
62 bool shouldSend(uint32_t inboxID) const {
63 SkASSERT(inboxID);
64 if (!fInboxID) {
65 return true;
66 }
67 return inboxID == fInboxID;
68 }
69};
70}
71DECLARE_SKMESSAGEBUS_MESSAGE(AddressedMessage)
72
73DEF_TEST(MessageBus_shouldSend, r) {
74 SkMessageBus<AddressedMessage>::Inbox inbox1(1), inbox2(2);
75
76 SkMessageBus<AddressedMessage>::Post({0}); // Should go to both
77 SkMessageBus<AddressedMessage>::Post({1}); // Should go to inbox1
78 SkMessageBus<AddressedMessage>::Post({2}); // Should go to inbox2
79 SkMessageBus<AddressedMessage>::Post({3}); // Should go nowhere
80
81 SkTArray<AddressedMessage> messages;
82 inbox1.poll(&messages);
83 REPORTER_ASSERT(r, messages.count() == 2);
84 if (messages.count() == 2) {
85 REPORTER_ASSERT(r, messages[0].fInboxID == 0);
86 REPORTER_ASSERT(r, messages[1].fInboxID == 1);
87 }
88 inbox2.poll(&messages);
89 REPORTER_ASSERT(r, messages.count() == 2);
90 if (messages.count() == 2) {
91 REPORTER_ASSERT(r, messages[0].fInboxID == 0);
92 REPORTER_ASSERT(r, messages[1].fInboxID == 2);
93 }
94}
95
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000096// Multithreaded tests tbd.