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 | |
| 8 | #include "SkMessageBus.h" |
| 9 | #include "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 { |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 12 | struct TestMessage { |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame^] | 13 | bool shouldSend(uint32_t inboxID) const { return true; } |
| 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 | }; |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame^] | 19 | } |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 20 | DECLARE_SKMESSAGEBUS_MESSAGE(TestMessage) |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 21 | |
| 22 | DEF_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. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 33 | SkTArray<TestMessage> messages; |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 34 | 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 Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame^] | 58 | namespace { |
| 59 | struct 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 | } |
| 71 | DECLARE_SKMESSAGEBUS_MESSAGE(AddressedMessage) |
| 72 | |
| 73 | DEF_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.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 96 | // Multithreaded tests tbd. |