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