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 public: |
| 30 virtual void OnMessage(int message_id) OVERRIDE { |
| 31 |
| 32 } |
| 33 |
| 34 protected: |
| 35 ThreadWrapperTest() { |
| 36 } |
| 37 |
| 38 talk_base::Thread* thread() { |
| 39 return talk_base::Thread::Current(); |
| 40 } |
| 41 |
| 42 virtual void SetUp() OVERRIDE { |
| 43 wrapper_ = new JingleThreadWrapper(&message_loop_); |
| 44 } |
| 45 |
| 46 // ThreadWrapper destroyes itself when |message_loop_| is destroyed. |
| 47 JingleThreadWrapper* wrapper_; |
| 48 MessageLoop message_loop_; |
| 49 MockMessageHandler handler1_; |
| 50 MockMessageHandler handler2_; |
| 51 }; |
| 52 |
| 53 TEST_F(ThreadWrapperTest, Create) { |
| 54 EXPECT_EQ(thread(), static_cast<talk_base::Thread*>(wrapper_)); |
| 55 } |
| 56 |
| 57 MATCHER_P3(MatchMessage, handler, message_id, data, "") { |
| 58 return arg->phandler == handler && |
| 59 arg->message_id == message_id && |
| 60 arg->pdata == data; |
| 61 } |
| 62 |
| 63 TEST_F(ThreadWrapperTest, Post) { |
| 64 talk_base::MessageData* data1_ = new talk_base::MessageData(); |
| 65 talk_base::MessageData* data2_ = new talk_base::MessageData(); |
| 66 talk_base::MessageData* data3_ = new talk_base::MessageData(); |
| 67 talk_base::MessageData* data4_ = new talk_base::MessageData(); |
| 68 |
| 69 thread()->Post(&handler1_, kTestMessage1, data1_); |
| 70 thread()->Post(&handler1_, kTestMessage2, data2_); |
| 71 thread()->Post(&handler2_, kTestMessage1, data3_); |
| 72 thread()->Post(&handler2_, kTestMessage1, data4_); |
| 73 |
| 74 InSequence in_seq; |
| 75 |
| 76 EXPECT_CALL(handler1_, OnMessage( |
| 77 MatchMessage(&handler1_, kTestMessage1, data1_))); |
| 78 EXPECT_CALL(handler1_, OnMessage( |
| 79 MatchMessage(&handler1_, kTestMessage2, data2_))); |
| 80 EXPECT_CALL(handler2_, OnMessage( |
| 81 MatchMessage(&handler2_, kTestMessage1, data3_))); |
| 82 EXPECT_CALL(handler2_, OnMessage( |
| 83 MatchMessage(&handler2_, kTestMessage1, data4_))); |
| 84 |
| 85 message_loop_.RunAllPending(); |
| 86 } |
| 87 |
| 88 TEST_F(ThreadWrapperTest, PostDelayed) { |
| 89 talk_base::MessageData* data1_ = new talk_base::MessageData(); |
| 90 talk_base::MessageData* data2_ = new talk_base::MessageData(); |
| 91 talk_base::MessageData* data3_ = new talk_base::MessageData(); |
| 92 talk_base::MessageData* data4_ = new talk_base::MessageData(); |
| 93 |
| 94 // Post messages in reverse order and verify that they are called in |
| 95 // the order of the delays. |
| 96 thread()->PostDelayed(kTestDelayMs4, &handler2_, kTestMessage1, data4_); |
| 97 thread()->PostDelayed(kTestDelayMs3, &handler2_, kTestMessage1, data3_); |
| 98 thread()->PostDelayed(kTestDelayMs2, &handler1_, kTestMessage2, data2_); |
| 99 thread()->PostDelayed(kTestDelayMs1, &handler1_, kTestMessage1, data1_); |
| 100 |
| 101 InSequence in_seq; |
| 102 |
| 103 EXPECT_CALL(handler1_, OnMessage( |
| 104 MatchMessage(&handler1_, kTestMessage1, data1_))); |
| 105 EXPECT_CALL(handler1_, OnMessage( |
| 106 MatchMessage(&handler1_, kTestMessage2, data2_))); |
| 107 EXPECT_CALL(handler2_, OnMessage( |
| 108 MatchMessage(&handler2_, kTestMessage1, data3_))); |
| 109 EXPECT_CALL(handler2_, OnMessage( |
| 110 MatchMessage(&handler2_, kTestMessage1, data4_))); |
| 111 |
| 112 message_loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(), |
| 113 kMaxTestDelay); |
| 114 message_loop_.Run(); |
| 115 } |
| 116 |
| 117 TEST_F(ThreadWrapperTest, Clear) { |
| 118 thread()->Post(&handler1_, kTestMessage1, NULL); |
| 119 thread()->Post(&handler1_, kTestMessage2, NULL); |
| 120 thread()->Post(&handler2_, kTestMessage1, NULL); |
| 121 thread()->Post(&handler2_, kTestMessage2, NULL); |
| 122 |
| 123 thread()->Clear(&handler1_, kTestMessage2); |
| 124 |
| 125 InSequence in_seq; |
| 126 |
| 127 talk_base::MessageData* null_data = NULL; |
| 128 EXPECT_CALL(handler1_, OnMessage( |
| 129 MatchMessage(&handler1_, kTestMessage1, null_data))); |
| 130 EXPECT_CALL(handler2_, OnMessage( |
| 131 MatchMessage(&handler2_, kTestMessage1, null_data))); |
| 132 EXPECT_CALL(handler2_, OnMessage( |
| 133 MatchMessage(&handler2_, kTestMessage2, null_data))); |
| 134 |
| 135 message_loop_.RunAllPending(); |
| 136 } |
| 137 |
| 138 TEST_F(ThreadWrapperTest, ClearDelayed) { |
| 139 // Post messages in reverse order and verify that they are called in |
| 140 // the order of the delays. |
| 141 thread()->PostDelayed(kTestDelayMs4, &handler2_, kTestMessage1, NULL); |
| 142 thread()->PostDelayed(kTestDelayMs3, &handler2_, kTestMessage1, NULL); |
| 143 thread()->PostDelayed(kTestDelayMs2, &handler1_, kTestMessage2, NULL); |
| 144 thread()->PostDelayed(kTestDelayMs1, &handler1_, kTestMessage1, NULL); |
| 145 |
| 146 thread()->Clear(&handler1_, kTestMessage2); |
| 147 |
| 148 InSequence in_seq; |
| 149 |
| 150 talk_base::MessageData* null_data = NULL; |
| 151 EXPECT_CALL(handler1_, OnMessage( |
| 152 MatchMessage(&handler1_, kTestMessage1, null_data))); |
| 153 EXPECT_CALL(handler2_, OnMessage( |
| 154 MatchMessage(&handler2_, kTestMessage1, null_data))); |
| 155 EXPECT_CALL(handler2_, OnMessage( |
| 156 MatchMessage(&handler2_, kTestMessage1, null_data))); |
| 157 |
| 158 message_loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(), |
| 159 kMaxTestDelay); |
| 160 message_loop_.Run(); |
| 161 } |
| 162 |
| 163 } // namespace jingle_glue |
OLD | NEW |