blob: 163addf8f05cb9e34a8a4f4378cd0b4348fa5abf [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
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000011struct TestMessage {
12 int x;
13 float y;
14};
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000015DECLARE_SKMESSAGEBUS_MESSAGE(TestMessage)
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000016
17DEF_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.
bsalomon23e619c2015-02-06 11:54:28 -080028 SkTArray<TestMessage> messages;
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000029 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.