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

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

Powered by Google App Engine
This is Rietveld 408576698