• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/SkMessageBus.h"
9 #include "tests/Test.h"
10 
11 namespace {
12 
13 struct TestMessage {
TestMessage__anon6c5927350111::TestMessage14     TestMessage(int i, float f) : x(i), y(f) {}
15 
16     int x;
17     float y;
18 };
19 
SkShouldPostMessageToBus(const TestMessage &,uint32_t)20 static inline bool SkShouldPostMessageToBus(const TestMessage&, uint32_t) {
21     return true;
22 }
23 
24 }
25 DECLARE_SKMESSAGEBUS_MESSAGE(TestMessage)
26 
DEF_TEST(MessageBus,r)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.
38     SkTArray<TestMessage> messages;
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 
63 namespace {
64 
65 struct AddressedMessage {
66     uint32_t fInboxID;
67 };
68 
SkShouldPostMessageToBus(const AddressedMessage & msg,uint32_t msgBusUniqueID)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;
75 }
76 
77 }
78 
79 DECLARE_SKMESSAGEBUS_MESSAGE(AddressedMessage)
80 
DEF_TEST(MessageBus_SkShouldPostMessageToBus,r)81 DEF_TEST(MessageBus_SkShouldPostMessageToBus, r) {
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 
104 // Multithreaded tests tbd.
105