commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 1 | /* |
| 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 Wagner | 21bca28 | 2019-05-15 10:15:52 -0400 | [diff] [blame] | 8 | #include "src/core/SkMessageBus.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "tests/Test.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 10 | |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 11 | namespace { |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 12 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 13 | struct TestMessage { |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 14 | TestMessage(int i, float f) : x(i), y(f) {} |
| 15 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 16 | int x; |
| 17 | float y; |
| 18 | }; |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 19 | |
| 20 | static inline bool SkShouldPostMessageToBus(const TestMessage&, uint32_t) { |
| 21 | return true; |
| 22 | } |
| 23 | |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 24 | } |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 25 | DECLARE_SKMESSAGEBUS_MESSAGE(TestMessage) |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 26 | |
| 27 | DEF_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. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 38 | SkTArray<TestMessage> messages; |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 39 | 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 Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 63 | namespace { |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 64 | |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 65 | struct AddressedMessage { |
| 66 | uint32_t fInboxID; |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 67 | }; |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 68 | |
| 69 | static 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 Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 75 | } |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 76 | |
| 77 | } |
| 78 | |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 79 | DECLARE_SKMESSAGEBUS_MESSAGE(AddressedMessage) |
| 80 | |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 81 | DEF_TEST(MessageBus_SkShouldPostMessageToBus, r) { |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 82 | 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.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 104 | // Multithreaded tests tbd. |