Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: shill_unittest.cc

Issue 6575006: Add initial sketch to shill repository (Closed) Base URL: ssh://git@gitrw.chromium.org/shill.git@master
Patch Set: Add device and service mockups Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« shill_event.h ('K') | « shill_main.cc ('k') | testrunner.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_(new EventQueue<int>(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 delete(int_callback_queue_);
40 g_source_remove(timer_source_);
41 }
42
43 void TimerFunction(int counter) {
44 printf("Callback func called #%d\n", counter);
45 if (counter > 1)
46 int_callback_queue_->AddEvent(counter);
47 }
48
49 void HandleInt(int arg) {
50 printf("MockEventDispatchTester handling int %d\n", arg);
51 // Depending on the course of events, we may be called either once or
52 // twice depending on timing. Only call the mock routine once.
53 if (!triggered_) {
54 CallbackComplete(arg);
55 triggered_ = true;
56 }
57 }
58
59 bool GetTrigger() { return triggered_; }
60 void ResetTrigger() { triggered_ = false; }
61 void RemoveCallback() { int_callback_queue_->RemoveCallback(int_callback_); }
62
63 MOCK_METHOD1(CallbackComplete, void(int));
64 private:
65 bool triggered_;
66 Callback<int> *int_callback_;
67 EventQueue<int>*int_callback_queue_;
68 int timer_source_;
69 static gboolean CallbackFunc(gpointer data) {
70 static int i = 0;
71 MockEventDispatchTester *dispatcher_test =
72 static_cast<MockEventDispatchTester *>(data);
73 dispatcher_test->TimerFunction(i++);
74 return true;
75 }
76 };
77
78 class ShillDaemonTest : public Test {
79 public:
80 ShillDaemonTest()
81 : daemon_(&config_, new DBusControl()),
82 dispatcher_(&daemon_.dispatcher_),
83 dispatcher_test_(dispatcher_) {}
84 virtual void SetUp() {
85 // Tests initialization done by the daemon's constructor
86 EXPECT_NE((Config *)NULL, daemon_.config_);
87 EXPECT_NE((ControlInterface *)NULL, daemon_.control_);
88 }
89 protected:
90 Config config_;
91 Daemon daemon_;
92 EventDispatcher *dispatcher_;
93 StrictMock<MockEventDispatchTester> dispatcher_test_;
94 };
95
96
97 TEST_F(ShillDaemonTest, EventDispatcher) {
98 EXPECT_CALL(dispatcher_test_, CallbackComplete(2));
99
100 // Crank the glib main loop a few times until the event handler triggers
101 for (int main_loop_count = 0;
102 main_loop_count < 6 && !dispatcher_test_.GetTrigger(); ++main_loop_count)
103 g_main_context_iteration(NULL, TRUE);
104
105 EXPECT_EQ(dispatcher_test_.GetTrigger(), true);
106
107 dispatcher_test_.ResetTrigger();
108 dispatcher_test_.RemoveCallback();
109
110 // Crank the glib main loop a few more times, ensuring there is no trigger
111 for (int main_loop_count = 0;
112 main_loop_count < 6 && !dispatcher_test_.GetTrigger(); ++main_loop_count)
113 g_main_context_iteration(NULL, TRUE);
114 }
115
116 }
OLDNEW
« shill_event.h ('K') | « shill_main.cc ('k') | testrunner.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698