OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/message_loop.h" |
| 6 #include "jingle/glue/thread_wrapper.h" |
| 7 #include "testing/gmock/include/gmock/gmock.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 using ::testing::InSequence; |
| 11 |
| 12 namespace jingle_glue { |
| 13 |
| 14 static const uint32 kTestMessage1 = 1; |
| 15 static const uint32 kTestMessage2 = 2; |
| 16 |
| 17 static const int kTestDelayMs1 = 10; |
| 18 static const int kTestDelayMs2 = 20; |
| 19 static const int kTestDelayMs3 = 30; |
| 20 static const int kTestDelayMs4 = 40; |
| 21 static const int kMaxTestDelay = 40; |
| 22 |
| 23 class MockMessageHandler : public talk_base::MessageHandler { |
| 24 public: |
| 25 MOCK_METHOD1(OnMessage, void(talk_base::Message* msg)); |
| 26 }; |
| 27 |
| 28 class ThreadWrapperTest : public testing::Test { |
| 29 protected: |
| 30 ThreadWrapperTest() { |
| 31 } |
| 32 |
| 33 talk_base::Thread* thread() { |
| 34 return talk_base::Thread::Current(); |
| 35 } |
| 36 |
| 37 virtual void SetUp() OVERRIDE { |
| 38 wrapper_ = new JingleThreadWrapper(&message_loop_); |
| 39 } |
| 40 |
| 41 // ThreadWrapper destroyes itself when |message_loop_| is destroyed. |
| 42 JingleThreadWrapper* wrapper_; |
| 43 MessageLoop message_loop_; |
| 44 MockMessageHandler handler1_; |
| 45 MockMessageHandler handler2_; |
| 46 }; |
| 47 |
| 48 TEST_F(ThreadWrapperTest, Create) { |
| 49 EXPECT_EQ(thread(), static_cast<talk_base::Thread*>(wrapper_)); |
| 50 } |
| 51 |
| 52 MATCHER_P3(MatchMessage, handler, message_id, data, "") { |
| 53 return arg->phandler == handler && |
| 54 arg->message_id == message_id && |
| 55 arg->pdata == data; |
| 56 } |
| 57 |
| 58 TEST_F(ThreadWrapperTest, Post) { |
| 59 talk_base::MessageData* data1_ = new talk_base::MessageData(); |
| 60 talk_base::MessageData* data2_ = new talk_base::MessageData(); |
| 61 talk_base::MessageData* data3_ = new talk_base::MessageData(); |
| 62 talk_base::MessageData* data4_ = new talk_base::MessageData(); |
| 63 |
| 64 thread()->Post(&handler1_, kTestMessage1, data1_); |
| 65 thread()->Post(&handler1_, kTestMessage2, data2_); |
| 66 thread()->Post(&handler2_, kTestMessage1, data3_); |
| 67 thread()->Post(&handler2_, kTestMessage1, data4_); |
| 68 |
| 69 InSequence in_seq; |
| 70 |
| 71 EXPECT_CALL(handler1_, OnMessage( |
| 72 MatchMessage(&handler1_, kTestMessage1, data1_))); |
| 73 EXPECT_CALL(handler1_, OnMessage( |
| 74 MatchMessage(&handler1_, kTestMessage2, data2_))); |
| 75 EXPECT_CALL(handler2_, OnMessage( |
| 76 MatchMessage(&handler2_, kTestMessage1, data3_))); |
| 77 EXPECT_CALL(handler2_, OnMessage( |
| 78 MatchMessage(&handler2_, kTestMessage1, data4_))); |
| 79 |
| 80 message_loop_.RunAllPending(); |
| 81 } |
| 82 |
| 83 TEST_F(ThreadWrapperTest, PostDelayed) { |
| 84 talk_base::MessageData* data1_ = new talk_base::MessageData(); |
| 85 talk_base::MessageData* data2_ = new talk_base::MessageData(); |
| 86 talk_base::MessageData* data3_ = new talk_base::MessageData(); |
| 87 talk_base::MessageData* data4_ = new talk_base::MessageData(); |
| 88 |
| 89 // Post messages in reverse order and verify that they are called in |
| 90 // the order of the delays. |
| 91 thread()->PostDelayed(kTestDelayMs4, &handler2_, kTestMessage1, data4_); |
| 92 thread()->PostDelayed(kTestDelayMs3, &handler2_, kTestMessage1, data3_); |
| 93 thread()->PostDelayed(kTestDelayMs2, &handler1_, kTestMessage2, data2_); |
| 94 thread()->PostDelayed(kTestDelayMs1, &handler1_, kTestMessage1, data1_); |
| 95 |
| 96 InSequence in_seq; |
| 97 |
| 98 EXPECT_CALL(handler1_, OnMessage( |
| 99 MatchMessage(&handler1_, kTestMessage1, data1_))); |
| 100 EXPECT_CALL(handler1_, OnMessage( |
| 101 MatchMessage(&handler1_, kTestMessage2, data2_))); |
| 102 EXPECT_CALL(handler2_, OnMessage( |
| 103 MatchMessage(&handler2_, kTestMessage1, data3_))); |
| 104 EXPECT_CALL(handler2_, OnMessage( |
| 105 MatchMessage(&handler2_, kTestMessage1, data4_))); |
| 106 |
| 107 message_loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(), |
| 108 kMaxTestDelay); |
| 109 message_loop_.Run(); |
| 110 } |
| 111 |
| 112 TEST_F(ThreadWrapperTest, Clear) { |
| 113 thread()->Post(&handler1_, kTestMessage1, NULL); |
| 114 thread()->Post(&handler1_, kTestMessage2, NULL); |
| 115 thread()->Post(&handler2_, kTestMessage1, NULL); |
| 116 thread()->Post(&handler2_, kTestMessage2, NULL); |
| 117 |
| 118 thread()->Clear(&handler1_, kTestMessage2); |
| 119 |
| 120 InSequence in_seq; |
| 121 |
| 122 talk_base::MessageData* null_data = NULL; |
| 123 EXPECT_CALL(handler1_, OnMessage( |
| 124 MatchMessage(&handler1_, kTestMessage1, null_data))); |
| 125 EXPECT_CALL(handler2_, OnMessage( |
| 126 MatchMessage(&handler2_, kTestMessage1, null_data))); |
| 127 EXPECT_CALL(handler2_, OnMessage( |
| 128 MatchMessage(&handler2_, kTestMessage2, null_data))); |
| 129 |
| 130 message_loop_.RunAllPending(); |
| 131 } |
| 132 |
| 133 TEST_F(ThreadWrapperTest, ClearDelayed) { |
| 134 // Post messages in reverse order and verify that they are called in |
| 135 // the order of the delays. |
| 136 thread()->PostDelayed(kTestDelayMs4, &handler2_, kTestMessage1, NULL); |
| 137 thread()->PostDelayed(kTestDelayMs3, &handler2_, kTestMessage1, NULL); |
| 138 thread()->PostDelayed(kTestDelayMs2, &handler1_, kTestMessage2, NULL); |
| 139 thread()->PostDelayed(kTestDelayMs1, &handler1_, kTestMessage1, NULL); |
| 140 |
| 141 thread()->Clear(&handler1_, kTestMessage2); |
| 142 |
| 143 InSequence in_seq; |
| 144 |
| 145 talk_base::MessageData* null_data = NULL; |
| 146 EXPECT_CALL(handler1_, OnMessage( |
| 147 MatchMessage(&handler1_, kTestMessage1, null_data))); |
| 148 EXPECT_CALL(handler2_, OnMessage( |
| 149 MatchMessage(&handler2_, kTestMessage1, null_data))); |
| 150 EXPECT_CALL(handler2_, OnMessage( |
| 151 MatchMessage(&handler2_, kTestMessage1, null_data))); |
| 152 |
| 153 message_loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(), |
| 154 kMaxTestDelay); |
| 155 message_loop_.Run(); |
| 156 } |
| 157 |
| 158 } // namespace jingle_glue |
OLD | NEW |