OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium OS 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 <gtest/gtest.h> |
| 6 #include <gmock/gmock.h> |
| 7 #include <stdint.h> |
| 8 #include <glib.h> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "shill/shill_daemon.h" |
| 12 #include "shill/dbus_control.h" |
| 13 |
| 14 namespace shill { |
| 15 using ::testing::Test; |
| 16 using ::testing::_; |
| 17 using ::testing::DoAll; |
| 18 using ::testing::InSequence; |
| 19 using ::testing::NotNull; |
| 20 using ::testing::Return; |
| 21 using ::testing::SetArgumentPointee; |
| 22 using ::testing::StrictMock; |
| 23 |
| 24 class MockEventDispatchTester { |
| 25 public: |
| 26 explicit MockEventDispatchTester(EventDispatcher *dispatcher) |
| 27 : triggered_(false), |
| 28 int_callback_(new ClassCallback<MockEventDispatchTester, int>(this, |
| 29 &MockEventDispatchTester::HandleInt)), |
| 30 int_callback_queue_(dispatcher) { |
| 31 int_callback_queue_.AddCallback(int_callback_); |
| 32 timer_source_ = g_timeout_add_seconds(1, |
| 33 &MockEventDispatchTester::CallbackFunc, this); |
| 34 } |
| 35 |
| 36 ~MockEventDispatchTester() { |
| 37 RemoveCallback(); |
| 38 delete(int_callback_); |
| 39 g_source_remove(timer_source_); |
| 40 } |
| 41 |
| 42 void TimerFunction(int counter) { |
| 43 printf("Callback func called #%d\n", counter); |
| 44 if (counter > 1) |
| 45 int_callback_queue_.AddEvent(counter); |
| 46 } |
| 47 |
| 48 void HandleInt(int arg) { |
| 49 printf("MockEventDispatchTester handling int %d\n", arg); |
| 50 // Depending on the course of events, we may be called either once or |
| 51 // twice depending on timing. Only call the mock routine once. |
| 52 if (!triggered_) { |
| 53 CallbackComplete(arg); |
| 54 triggered_ = true; |
| 55 } |
| 56 } |
| 57 |
| 58 bool GetTrigger() { return triggered_; } |
| 59 void ResetTrigger() { triggered_ = false; } |
| 60 void RemoveCallback() { int_callback_queue_.RemoveCallback(int_callback_); } |
| 61 |
| 62 MOCK_METHOD1(CallbackComplete, void(int)); |
| 63 private: |
| 64 bool triggered_; |
| 65 Callback<int> *int_callback_; |
| 66 EventQueue<int>int_callback_queue_; |
| 67 int timer_source_; |
| 68 static gboolean CallbackFunc(gpointer data) { |
| 69 static int i = 0; |
| 70 MockEventDispatchTester *dispatcher_test = |
| 71 static_cast<MockEventDispatchTester *>(data); |
| 72 dispatcher_test->TimerFunction(i++); |
| 73 return true; |
| 74 } |
| 75 }; |
| 76 |
| 77 class ShillDaemonTest : public Test { |
| 78 public: |
| 79 ShillDaemonTest() |
| 80 : daemon_(&config_, new DBusControl()), |
| 81 dispatcher_(&daemon_.dispatcher_), |
| 82 dispatcher_test_(dispatcher_) {} |
| 83 virtual void SetUp() { |
| 84 // Tests initialization done by the daemon's constructor |
| 85 EXPECT_NE((Config *)NULL, daemon_.config_); |
| 86 EXPECT_NE((ControlInterface *)NULL, daemon_.control_); |
| 87 } |
| 88 protected: |
| 89 Config config_; |
| 90 Daemon daemon_; |
| 91 EventDispatcher *dispatcher_; |
| 92 StrictMock<MockEventDispatchTester> dispatcher_test_; |
| 93 }; |
| 94 |
| 95 |
| 96 TEST_F(ShillDaemonTest, EventDispatcher) { |
| 97 EXPECT_CALL(dispatcher_test_, CallbackComplete(2)); |
| 98 |
| 99 // Crank the glib main loop a few times until the event handler triggers |
| 100 for (int main_loop_count = 0; |
| 101 main_loop_count < 6 && !dispatcher_test_.GetTrigger(); ++main_loop_count) |
| 102 g_main_context_iteration(NULL, TRUE); |
| 103 |
| 104 EXPECT_EQ(dispatcher_test_.GetTrigger(), true); |
| 105 |
| 106 dispatcher_test_.ResetTrigger(); |
| 107 dispatcher_test_.RemoveCallback(); |
| 108 |
| 109 // Crank the glib main loop a few more times, ensuring there is no trigger |
| 110 for (int main_loop_count = 0; |
| 111 main_loop_count < 6 && !dispatcher_test_.GetTrigger(); ++main_loop_count) |
| 112 g_main_context_iteration(NULL, TRUE); |
| 113 } |
| 114 |
| 115 } |
OLD | NEW |