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.
5 #include "remoting/client/client_status_logger.h"
7 #include "base/message_loop/message_loop.h"
8 #include "remoting/client/chromoting_stats.h"
9 #include "remoting/signaling/mock_signal_strategy.h"
10 #include "remoting/signaling/server_log_entry_unittest.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
15 using buzz::XmlElement
;
17 using remoting::protocol::ConnectionToHost
;
19 using testing::DeleteArg
;
20 using testing::InSequence
;
21 using testing::Return
;
27 ACTION_P(QuitMainMessageLoop
, message_loop
) {
28 message_loop
->PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
31 const char kTestBotJid
[] = "remotingunittest@bot.talk.google.com";
32 const char kClientJid
[] = "host@domain.com/1234";
34 MATCHER_P2(IsStateChange
, new_state
, error
, "") {
35 XmlElement
* entry
= GetSingleLogEntryFromStanza(arg
);
40 bool is_state_change
= (
41 entry
->Attr(QName(std::string(), "event-name")) == "session-state" &&
42 entry
->Attr(QName(std::string(), "session-state")) == new_state
&&
43 entry
->Attr(QName(std::string(), "role")) == "client" &&
44 entry
->Attr(QName(std::string(), "mode")) == "me2me");
45 if (!std::string(error
).empty()) {
46 is_state_change
= is_state_change
&&
47 entry
->Attr(QName(std::string(), "connection-error")) == error
;
49 return is_state_change
;
52 MATCHER(IsStatisticsLog
, "") {
53 XmlElement
* entry
= GetSingleLogEntryFromStanza(arg
);
58 return entry
->Attr(QName(std::string(), "event-name")) ==
59 "connection-statistics";
64 class ClientStatusLoggerTest
: public testing::Test
{
66 ClientStatusLoggerTest() {}
67 void SetUp() override
{
68 EXPECT_CALL(signal_strategy_
, AddListener(_
));
69 EXPECT_CALL(signal_strategy_
, RemoveListener(_
));
70 client_status_logger_
.reset(
71 new ClientStatusLogger(ServerLogEntry::ME2ME
,
77 base::MessageLoop message_loop_
;
78 MockSignalStrategy signal_strategy_
;
79 scoped_ptr
<ClientStatusLogger
> client_status_logger_
;
82 TEST_F(ClientStatusLoggerTest
, LogStateChange
) {
85 EXPECT_CALL(signal_strategy_
, GetLocalJid())
86 .WillRepeatedly(Return(kClientJid
));
87 EXPECT_CALL(signal_strategy_
, AddListener(_
));
88 EXPECT_CALL(signal_strategy_
, GetNextId());
89 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(
90 IsStateChange("connected", std::string())))
91 .WillOnce(DoAll(DeleteArg
<0>(), Return(true)));
92 EXPECT_CALL(signal_strategy_
, RemoveListener(_
))
93 .WillOnce(QuitMainMessageLoop(&message_loop_
))
94 .RetiresOnSaturation();
96 client_status_logger_
->LogSessionStateChange(ConnectionToHost::CONNECTED
,
99 // Setting the state to CONNECTED causes the log to be sent. Setting the
100 // state to DISCONNECTED causes |signal_strategy_| to be cleaned up,
101 // which removes the listener and terminates the test.
102 client_status_logger_
->SetSignalingStateForTest(SignalStrategy::CONNECTED
);
103 client_status_logger_
->SetSignalingStateForTest(SignalStrategy::DISCONNECTED
);
107 TEST_F(ClientStatusLoggerTest
, LogStateChangeError
) {
110 EXPECT_CALL(signal_strategy_
, GetLocalJid())
111 .WillRepeatedly(Return(kClientJid
));
112 EXPECT_CALL(signal_strategy_
, AddListener(_
));
113 EXPECT_CALL(signal_strategy_
, GetNextId());
114 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(
115 IsStateChange("connection-failed", "host-is-offline")))
116 .WillOnce(DoAll(DeleteArg
<0>(), Return(true)));
117 EXPECT_CALL(signal_strategy_
, RemoveListener(_
))
118 .WillOnce(QuitMainMessageLoop(&message_loop_
))
119 .RetiresOnSaturation();
121 client_status_logger_
->LogSessionStateChange(ConnectionToHost::FAILED
,
122 protocol::PEER_IS_OFFLINE
);
124 client_status_logger_
->SetSignalingStateForTest(SignalStrategy::CONNECTED
);
125 client_status_logger_
->SetSignalingStateForTest(SignalStrategy::DISCONNECTED
);
129 TEST_F(ClientStatusLoggerTest
, LogStatistics
) {
132 EXPECT_CALL(signal_strategy_
, GetLocalJid())
133 .WillRepeatedly(Return(kClientJid
));
134 EXPECT_CALL(signal_strategy_
, AddListener(_
));
135 EXPECT_CALL(signal_strategy_
, GetNextId());
136 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(
138 .WillOnce(DoAll(DeleteArg
<0>(), Return(true)));
139 EXPECT_CALL(signal_strategy_
, RemoveListener(_
))
140 .WillOnce(QuitMainMessageLoop(&message_loop_
))
141 .RetiresOnSaturation();
144 ChromotingStats stats
;
145 client_status_logger_
->LogStatistics(&stats
);
147 client_status_logger_
->SetSignalingStateForTest(SignalStrategy::CONNECTED
);
148 client_status_logger_
->SetSignalingStateForTest(SignalStrategy::DISCONNECTED
);
152 } // namespace remoting