Chromium Code Reviews| Index: remoting/host/log_to_server_unittest.cc |
| diff --git a/remoting/host/log_to_server_unittest.cc b/remoting/host/log_to_server_unittest.cc |
| index c4431572a1ca64ac3bb6a05110404fb63ee98e4a..f09850f5a0a185c8d38f7f076289b84c6e522358 100644 |
| --- a/remoting/host/log_to_server_unittest.cc |
| +++ b/remoting/host/log_to_server_unittest.cc |
| @@ -11,6 +11,8 @@ |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| +using buzz::XmlElement; |
| +using buzz::QName; |
| using testing::_; |
| using testing::DeleteArg; |
| using testing::InSequence; |
| @@ -24,6 +26,96 @@ ACTION_P(QuitMainMessageLoop, message_loop) { |
| message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| } |
| +const char kJabberClientNamespace[] = "jabber:client"; |
| +const char kChromotingNamespace[] = "google:remoting"; |
| +const char kClientJid1[] = "client@domain.com/1234"; |
| +const char kClientJid2[] = "client@domain.com/5678"; |
| +const char kHostJid[] = "host@domain.com/1234"; |
| + |
| +bool IsLogEntryForConnection(XmlElement* node, const char* connection_type) { |
| + return (node->Name() == QName(kChromotingNamespace, "entry") && |
| + node->Attr(QName("", "event-name")) == "session-state" && |
| + node->Attr(QName("", "session-state")) == "connected" && |
| + node->Attr(QName("", "role")) == "host" && |
| + node->Attr(QName("", "mode")) == "me2me" && |
| + node->Attr(QName("", "connection-type")) == connection_type); |
| +} |
| + |
| +MATCHER_P(IsClientConnected, connection_type, "") { |
| + if (arg->Name() != QName(kJabberClientNamespace, "iq")) { |
| + return false; |
| + } |
| + buzz::XmlElement* log_stanza = arg->FirstChild()->AsElement(); |
| + if (log_stanza->Name() !=QName(kChromotingNamespace, "log")) { |
| + return false; |
| + } |
| + if (log_stanza->NextChild()) { |
| + return false; |
| + } |
| + buzz::XmlElement* log_entry = log_stanza->FirstChild()->AsElement(); |
| + if (!IsLogEntryForConnection(log_entry, connection_type)) { |
| + return false; |
| + } |
| + if (log_entry->NextChild()) { |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +MATCHER_P2(IsTwoClientsConnected, connection_type1, connection_type2, "") { |
|
Wez
2012/05/21 22:47:31
nit: AreTwoClientsConnected.
simonmorris
2012/05/24 22:15:27
The matcher is a function of a single parameter, s
|
| + if (arg->Name() != QName(kJabberClientNamespace, "iq")) { |
| + return false; |
| + } |
| + buzz::XmlElement* log_stanza = arg->FirstChild()->AsElement(); |
| + if (log_stanza->Name() !=QName(kChromotingNamespace, "log")) { |
| + return false; |
| + } |
| + if (log_stanza->NextChild()) { |
| + return false; |
| + } |
| + buzz::XmlElement* log_entry = log_stanza->FirstChild()->AsElement(); |
| + if (!IsLogEntryForConnection(log_entry, connection_type1)) { |
| + return false; |
| + } |
| + log_entry = log_entry->NextChild()->AsElement(); |
| + if (!IsLogEntryForConnection(log_entry, connection_type2)) { |
| + return false; |
| + } |
| + if (log_entry->NextChild()) { |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +bool IsLogEntryForDisconnection(XmlElement* node) { |
| + return (node->Name() == QName(kChromotingNamespace, "entry") && |
| + node->Attr(QName("", "event-name")) == "session-state" && |
| + node->Attr(QName("", "session-state")) == "closed" && |
| + node->Attr(QName("", "role")) == "host" && |
| + node->Attr(QName("", "mode")) == "me2me"); |
| +} |
| + |
| +MATCHER(IsClientDisconnected, "") { |
| + if (arg->Name() != QName(kJabberClientNamespace, "iq")) { |
| + return false; |
| + } |
| + buzz::XmlElement* log_stanza = arg->FirstChild()->AsElement(); |
| + if (log_stanza->Name() !=QName(kChromotingNamespace, "log")) { |
| + return false; |
| + } |
| + if (log_stanza->NextChild()) { |
| + return false; |
| + } |
| + buzz::XmlElement* log_entry = log_stanza->FirstChild()->AsElement(); |
| + if (!IsLogEntryForDisconnection(log_entry)) { |
| + return false; |
| + } |
| + if (log_entry->NextChild()) { |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| } // namespace |
| class LogToServerTest : public testing::Test { |
| @@ -48,10 +140,10 @@ TEST_F(LogToServerTest, SendNow) { |
| { |
| InSequence s; |
| EXPECT_CALL(signal_strategy_, GetLocalJid()) |
| - .WillRepeatedly(Return("host@domain.com/1234")); |
| + .WillRepeatedly(Return(kHostJid)); |
| EXPECT_CALL(signal_strategy_, AddListener(_)); |
| EXPECT_CALL(signal_strategy_, GetNextId()); |
| - EXPECT_CALL(signal_strategy_, SendStanzaPtr(_)) |
| + EXPECT_CALL(signal_strategy_, SendStanzaPtr(IsClientConnected("direct"))) |
| .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| EXPECT_CALL(signal_strategy_, RemoveListener(_)) |
| .WillOnce(QuitMainMessageLoop(&message_loop_)) |
| @@ -60,8 +152,8 @@ TEST_F(LogToServerTest, SendNow) { |
| log_to_server_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED); |
| protocol::TransportRoute route; |
| route.type = protocol::TransportRoute::DIRECT; |
| - log_to_server_->OnClientRouteChange("client@domain.com/5678", "video", route); |
| - log_to_server_->OnClientAuthenticated("client@domain.com/5678"); |
| + log_to_server_->OnClientRouteChange(kClientJid1, "video", route); |
| + log_to_server_->OnClientAuthenticated(kClientJid1); |
| log_to_server_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED); |
| message_loop_.Run(); |
| } |
| @@ -69,15 +161,15 @@ TEST_F(LogToServerTest, SendNow) { |
| TEST_F(LogToServerTest, SendLater) { |
| protocol::TransportRoute route; |
| route.type = protocol::TransportRoute::DIRECT; |
| - log_to_server_->OnClientRouteChange("client@domain.com/5678", "video", route); |
| - log_to_server_->OnClientAuthenticated("client@domain.com/5678"); |
| + log_to_server_->OnClientRouteChange(kClientJid1, "video", route); |
| + log_to_server_->OnClientAuthenticated(kClientJid1); |
| { |
| InSequence s; |
| EXPECT_CALL(signal_strategy_, GetLocalJid()) |
| - .WillRepeatedly(Return("host@domain.com/1234")); |
| + .WillRepeatedly(Return(kHostJid)); |
| EXPECT_CALL(signal_strategy_, AddListener(_)); |
| EXPECT_CALL(signal_strategy_, GetNextId()); |
| - EXPECT_CALL(signal_strategy_, SendStanzaPtr(_)) |
| + EXPECT_CALL(signal_strategy_, SendStanzaPtr(IsClientConnected("direct"))) |
| .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| EXPECT_CALL(signal_strategy_, RemoveListener(_)) |
| .WillOnce(QuitMainMessageLoop(&message_loop_)) |
| @@ -89,24 +181,61 @@ TEST_F(LogToServerTest, SendLater) { |
| } |
| TEST_F(LogToServerTest, SendTwoEntriesLater) { |
| - protocol::TransportRoute route; |
| - route.type = protocol::TransportRoute::DIRECT; |
| - log_to_server_->OnClientRouteChange("client@domain.com/5678", "video", route); |
| - log_to_server_->OnClientAuthenticated("client@domain.com/5678"); |
| - log_to_server_->OnClientAuthenticated("client2@domain.com/6789"); |
| + protocol::TransportRoute route1; |
| + route1.type = protocol::TransportRoute::DIRECT; |
| + log_to_server_->OnClientRouteChange(kClientJid1, "video", route1); |
| + log_to_server_->OnClientAuthenticated(kClientJid1); |
| + protocol::TransportRoute route2; |
| + route2.type = protocol::TransportRoute::STUN; |
| + log_to_server_->OnClientRouteChange(kClientJid2, "video", route2); |
| + log_to_server_->OnClientAuthenticated(kClientJid2); |
| + { |
| + InSequence s; |
| + EXPECT_CALL(signal_strategy_, GetLocalJid()) |
| + .WillRepeatedly(Return(kHostJid)); |
| + EXPECT_CALL(signal_strategy_, AddListener(_)); |
| + EXPECT_CALL(signal_strategy_, GetNextId()); |
| + EXPECT_CALL(signal_strategy_, SendStanzaPtr( |
| + IsTwoClientsConnected("direct", "stun"))) |
| + .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| + EXPECT_CALL(signal_strategy_, RemoveListener(_)) |
| + .WillOnce(QuitMainMessageLoop(&message_loop_)) |
| + .RetiresOnSaturation(); |
| + } |
| + log_to_server_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED); |
| + log_to_server_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED); |
| + message_loop_.Run(); |
| +} |
| + |
| +TEST_F(LogToServerTest, HandleRouteChangeInUnusualOrder) { |
| { |
| InSequence s; |
| EXPECT_CALL(signal_strategy_, GetLocalJid()) |
| - .WillRepeatedly(Return("host@domain.com/1234")); |
| + .WillRepeatedly(Return(kHostJid)); |
| EXPECT_CALL(signal_strategy_, AddListener(_)); |
| EXPECT_CALL(signal_strategy_, GetNextId()); |
| - EXPECT_CALL(signal_strategy_, SendStanzaPtr(_)) |
| + EXPECT_CALL(signal_strategy_, SendStanzaPtr(IsClientConnected("direct"))) |
| + .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| + EXPECT_CALL(signal_strategy_, GetNextId()); |
| + EXPECT_CALL(signal_strategy_, SendStanzaPtr(IsClientDisconnected())) |
| + .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| + EXPECT_CALL(signal_strategy_, GetNextId()); |
| + EXPECT_CALL(signal_strategy_, SendStanzaPtr(IsClientConnected("stun"))) |
| .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| EXPECT_CALL(signal_strategy_, RemoveListener(_)) |
| .WillOnce(QuitMainMessageLoop(&message_loop_)) |
| .RetiresOnSaturation(); |
| } |
| log_to_server_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED); |
| + protocol::TransportRoute route1; |
| + route1.type = protocol::TransportRoute::DIRECT; |
| + log_to_server_->OnClientRouteChange(kClientJid1, "video", route1); |
| + log_to_server_->OnClientAuthenticated(kClientJid1); |
| + protocol::TransportRoute route2; |
| + route2.type = protocol::TransportRoute::STUN; |
| + log_to_server_->OnClientRouteChange(kClientJid2, "video", route2); |
| + log_to_server_->OnClientDisconnected(kClientJid1); |
| + log_to_server_->OnClientAuthenticated(kClientJid2); |
| log_to_server_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED); |
| message_loop_.Run(); |
| } |