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

Side by Side Diff: remoting/client/log_to_server_client_unittest.cc

Issue 320403002: Pull out common code from client and host versions of LogToServer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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/message_loop.h"
6 #include "base/message_loop/message_loop_proxy.h"
7 #include "remoting/client/chromoting_stats.h"
8 #include "remoting/client/log_to_server_client.h"
9 #include "remoting/jingle_glue/mock_objects.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
13
14 using buzz::XmlElement;
15 using buzz::QName;
16 using remoting::protocol::ConnectionToHost;
17 using testing::_;
18 using testing::DeleteArg;
19 using testing::InSequence;
20 using testing::Return;
21
22 namespace remoting {
23
24 namespace {
25
26 ACTION_P(QuitMainMessageLoop, message_loop) {
27 message_loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
28 }
29
30 const char kJabberClientNamespace[] = "jabber:client";
31 const char kChromotingNamespace[] = "google:remoting";
32 const char kTestBotJid[] = "remotingunittest@bot.talk.google.com";
33 const char kClientJid[] = "host@domain.com/1234";
34
35 // Verifies that |stanza| contains a log entry and returns it. Otherwise
36 // returns NULL and records a test failure.
37 XmlElement* GetLogElement(XmlElement* stanza) {
38 if (stanza->Name() != QName(kJabberClientNamespace, "iq")) {
39 ADD_FAILURE() << "Expected element 'iq'";
40 return NULL;
41 }
42 XmlElement* log_element = stanza->FirstChild()->AsElement();
43 if (log_element->Name() != QName(kChromotingNamespace, "log")) {
44 ADD_FAILURE() << "Expected element 'log'";
45 return NULL;
46 }
47 if (log_element->NextChild()) {
48 ADD_FAILURE() << "Expected only 1 child of 'iq'";
49 return NULL;
50 }
51 return log_element;
52 }
53
54 // Verifies that |stanza| contains only 1 log entry, and returns it.
55 // Otherwise returns NULL and records a test failure.
56 XmlElement* GetSingleLogEntry(XmlElement* stanza) {
57 XmlElement* log_element = GetLogElement(stanza);
58 if (!log_element) {
59 // Test failure already recorded, so just return NULL here.
60 return NULL;
61 }
62 XmlElement* entry = log_element->FirstChild()->AsElement();
63 if (entry->Name() != QName(kChromotingNamespace, "entry")) {
64 ADD_FAILURE() << "Expected element 'entry'";
65 return NULL;
66 }
67 if (entry->NextChild()) {
68 ADD_FAILURE() << "Expected only 1 child of 'log'";
69 return NULL;
70 }
71 return entry;
72 }
73
74 MATCHER_P2(IsStateChange, new_state, error, "") {
75 XmlElement* entry = GetSingleLogEntry(arg);
76 if (!entry) {
77 return false;
78 }
79
80 bool is_state_change = (
81 entry->Attr(QName(std::string(), "event-name")) == "session-state" &&
82 entry->Attr(QName(std::string(), "session-state")) == new_state &&
83 entry->Attr(QName(std::string(), "role")) == "client" &&
84 entry->Attr(QName(std::string(), "mode")) == "me2me");
85 if (!std::string(error).empty()) {
86 is_state_change = is_state_change &&
87 entry->Attr(QName(std::string(), "connection-error")) == error;
88 }
89 return is_state_change;
90 }
91
92 MATCHER(IsStatisticsLog, "") {
93 XmlElement* entry = GetSingleLogEntry(arg);
94 if (!entry) {
95 return false;
96 }
97
98 return entry->Attr(QName(std::string(), "event-name")) ==
99 "connection-statistics";
100 }
101
102 } // namespace
103
104 class LogToServerClientTest : public testing::Test {
105 public:
106 LogToServerClientTest() {}
107 virtual void SetUp() OVERRIDE {
108 EXPECT_CALL(signal_strategy_, AddListener(_));
109 EXPECT_CALL(signal_strategy_, RemoveListener(_));
110 message_loop_proxy_ = base::MessageLoopProxy::current();
111 log_to_server_.reset(
112 new LogToServerClient(ServerLogEntry::ME2ME,
113 &signal_strategy_,
114 kTestBotJid));
115 }
116
117 protected:
118 base::MessageLoop message_loop_;
119 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
120 MockSignalStrategy signal_strategy_;
121 scoped_ptr<LogToServerClient> log_to_server_;
122 };
123
124 TEST_F(LogToServerClientTest, LogStateChange) {
125 {
126 InSequence s;
127 EXPECT_CALL(signal_strategy_, GetLocalJid())
128 .WillRepeatedly(Return(kClientJid));
129 EXPECT_CALL(signal_strategy_, AddListener(_));
130 EXPECT_CALL(signal_strategy_, GetNextId());
131 EXPECT_CALL(signal_strategy_, SendStanzaPtr(
132 IsStateChange("connected", std::string())))
133 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
134 EXPECT_CALL(signal_strategy_, RemoveListener(_))
135 .WillOnce(QuitMainMessageLoop(&message_loop_))
136 .RetiresOnSaturation();
137 }
138 log_to_server_->LogSessionStateChange(ConnectionToHost::CONNECTED,
139 protocol::OK);
140
141 // Setting the state to CONNECTED causes the log to be sent. Setting the
142 // state to DISCONNECTED causes |signal_strategy_| to be cleaned up,
143 // which removes the listener and terminates the test.
144 log_to_server_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED);
145 log_to_server_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED);
146 message_loop_.Run();
147 }
148
149 TEST_F(LogToServerClientTest, LogStateChangeError) {
150 {
151 InSequence s;
152 EXPECT_CALL(signal_strategy_, GetLocalJid())
153 .WillRepeatedly(Return(kClientJid));
154 EXPECT_CALL(signal_strategy_, AddListener(_));
155 EXPECT_CALL(signal_strategy_, GetNextId());
156 EXPECT_CALL(signal_strategy_, SendStanzaPtr(
157 IsStateChange("connection-failed", "host-is-offline")))
158 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
159 EXPECT_CALL(signal_strategy_, RemoveListener(_))
160 .WillOnce(QuitMainMessageLoop(&message_loop_))
161 .RetiresOnSaturation();
162 }
163 log_to_server_->LogSessionStateChange(ConnectionToHost::FAILED,
164 protocol::PEER_IS_OFFLINE);
165
166 log_to_server_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED);
167 log_to_server_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED);
168 message_loop_.Run();
169 }
170
171 TEST_F(LogToServerClientTest, LogStatistics) {
172 {
173 InSequence s;
174 EXPECT_CALL(signal_strategy_, GetLocalJid())
175 .WillRepeatedly(Return(kClientJid));
176 EXPECT_CALL(signal_strategy_, AddListener(_));
177 EXPECT_CALL(signal_strategy_, GetNextId());
178 EXPECT_CALL(signal_strategy_, SendStanzaPtr(
179 IsStatisticsLog()))
180 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
181 EXPECT_CALL(signal_strategy_, RemoveListener(_))
182 .WillOnce(QuitMainMessageLoop(&message_loop_))
183 .RetiresOnSaturation();
184 }
185
186 ChromotingStats stats;
187 log_to_server_->LogStatistics(&stats);
188
189 log_to_server_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED);
190 log_to_server_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED);
191 message_loop_.Run();
192 }
193
194 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698