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"
10
11 struct TestMessage {
12 int x;
13 float y;
14 };
15 DECLARE_SKMESSAGEBUS_MESSAGE(TestMessage)
16
DEF_TEST(MessageBus,r)17 DEF_TEST(MessageBus, r) {
18 // Register two inboxes to receive all TestMessages.
19 SkMessageBus<TestMessage>::Inbox inbox1, inbox2;
20
21 // Send two messages.
22 const TestMessage m1 = { 5, 4.2f };
23 const TestMessage m2 = { 6, 4.3f };
24 SkMessageBus<TestMessage>::Post(m1);
25 SkMessageBus<TestMessage>::Post(m2);
26
27 // Make sure we got two.
28 SkTArray<TestMessage> messages;
29 inbox1.poll(&messages);
30 REPORTER_ASSERT(r, 2 == messages.count());
31 REPORTER_ASSERT(r, 5 == messages[0].x);
32 REPORTER_ASSERT(r, 6 == messages[1].x);
33
34 // Send another; check we get just that one.
35 const TestMessage m3 = { 1, 0.3f };
36 SkMessageBus<TestMessage>::Post(m3);
37 inbox1.poll(&messages);
38 REPORTER_ASSERT(r, 1 == messages.count());
39 REPORTER_ASSERT(r, 1 == messages[0].x);
40
41 // Nothing was sent since the last read.
42 inbox1.poll(&messages);
43 REPORTER_ASSERT(r, 0 == messages.count());
44
45 // Over all this time, inbox2 should have piled up 3 messages.
46 inbox2.poll(&messages);
47 REPORTER_ASSERT(r, 3 == messages.count());
48 REPORTER_ASSERT(r, 5 == messages[0].x);
49 REPORTER_ASSERT(r, 6 == messages[1].x);
50 REPORTER_ASSERT(r, 1 == messages[2].x);
51 }
52
53 // Multithreaded tests tbd.
54