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

Side by Side Diff: remoting/host/register_support_host_request_unittest.cc

Issue 8432009: Refactor IqRequest. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: - Created 9 years, 1 month 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "remoting/host/register_support_host_request.h" 5 #include "remoting/host/register_support_host_request.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
11 #include "remoting/base/constants.h" 11 #include "remoting/base/constants.h"
12 #include "remoting/host/host_key_pair.h" 12 #include "remoting/host/host_key_pair.h"
13 #include "remoting/host/in_memory_host_config.h" 13 #include "remoting/host/in_memory_host_config.h"
14 #include "remoting/host/test_key_pair.h" 14 #include "remoting/host/test_key_pair.h"
15 #include "remoting/jingle_glue/iq_request.h" 15 #include "remoting/jingle_glue/iq_sender.h"
16 #include "remoting/jingle_glue/mock_objects.h" 16 #include "remoting/jingle_glue/mock_objects.h"
17 #include "testing/gmock/include/gmock/gmock.h" 17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" 19 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
20 #include "third_party/libjingle/source/talk/xmpp/constants.h" 20 #include "third_party/libjingle/source/talk/xmpp/constants.h"
21 21
22 using buzz::QName; 22 using buzz::QName;
23 using buzz::XmlElement; 23 using buzz::XmlElement;
24 24
25 using testing::_; 25 using testing::_;
26 using testing::Invoke; 26 using testing::Invoke;
27 using testing::NotNull; 27 using testing::NotNull;
28 using testing::Return; 28 using testing::Return;
29 using testing::SaveArg; 29 using testing::SaveArg;
30 30
31 namespace remoting { 31 namespace remoting {
32 32
33 namespace { 33 namespace {
34 const char kTestJid[] = "user@gmail.com/chromoting123"; 34 const char kTestJid[] = "user@gmail.com/chromoting123";
35 const int64 kTestTime = 123123123; 35 const int64 kTestTime = 123123123;
36 const char kSupportId[] = "AB4RF3"; 36 const char kSupportId[] = "AB4RF3";
37 const char kSupportIdLifetime[] = "300"; 37 const char kSupportIdLifetime[] = "300";
38 const char kStanzaId[] = "123";
38 39
39 class MockCallback { 40 class MockCallback {
40 public: 41 public:
41 MOCK_METHOD3(OnResponse, void(bool result, const std::string& support_id, 42 MOCK_METHOD3(OnResponse, void(bool result, const std::string& support_id,
42 const base::TimeDelta& lifetime)); 43 const base::TimeDelta& lifetime));
43 }; 44 };
44 45
45 } // namespace 46 } // namespace
46 47
47 class RegisterSupportHostRequestTest : public testing::Test { 48 class RegisterSupportHostRequestTest : public testing::Test {
48 public: 49 public:
49 protected: 50 protected:
50 virtual void SetUp() { 51 virtual void SetUp() {
51 config_ = new InMemoryHostConfig(); 52 config_ = new InMemoryHostConfig();
52 config_->SetString(kPrivateKeyConfigPath, kTestHostKeyPair); 53 config_->SetString(kPrivateKeyConfigPath, kTestHostKeyPair);
53 } 54 }
54 55
55 MockSignalStrategy signal_strategy_; 56 MockSignalStrategy signal_strategy_;
56 MessageLoop message_loop_; 57 MessageLoop message_loop_;
57 scoped_refptr<InMemoryHostConfig> config_; 58 scoped_refptr<InMemoryHostConfig> config_;
58 MockCallback callback_; 59 MockCallback callback_;
59 }; 60 };
60 61
61 TEST_F(RegisterSupportHostRequestTest, Send) { 62 TEST_F(RegisterSupportHostRequestTest, Send) {
62 // |iq_request| is freed by RegisterSupportHostRequest. 63 // |iq_request| is freed by RegisterSupportHostRequest.
63 int64 start_time = static_cast<int64>(base::Time::Now().ToDoubleT()); 64 int64 start_time = static_cast<int64>(base::Time::Now().ToDoubleT());
64 65
66 SignalStrategy::Listener* listener;
67 EXPECT_CALL(signal_strategy_, AddListener(NotNull()))
68 .WillOnce(SaveArg<0>(&listener));
69
65 scoped_ptr<RegisterSupportHostRequest> request( 70 scoped_ptr<RegisterSupportHostRequest> request(
66 new RegisterSupportHostRequest()); 71 new RegisterSupportHostRequest());
67 ASSERT_TRUE(request->Init( 72 ASSERT_TRUE(request->Init(
68 config_, base::Bind(&MockCallback::OnResponse, 73 config_, base::Bind(&MockCallback::OnResponse,
69 base::Unretained(&callback_)))); 74 base::Unretained(&callback_))));
70 75
71 MockIqRequest* iq_request = new MockIqRequest();
72 iq_request->Init();
73 EXPECT_CALL(*iq_request, set_callback(_)).Times(1);
74
75 EXPECT_CALL(signal_strategy_, CreateIqRequest())
76 .WillOnce(Return(iq_request));
77
78 XmlElement* sent_iq = NULL; 76 XmlElement* sent_iq = NULL;
79 EXPECT_CALL(*iq_request, SendIq(NotNull())) 77 EXPECT_CALL(signal_strategy_, GetNextId())
80 .WillOnce(SaveArg<0>(&sent_iq)); 78 .WillOnce(Return(kStanzaId));
79 EXPECT_CALL(signal_strategy_, SendStanza(NotNull()))
80 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true)));
81 81
82 request->OnSignallingConnected(&signal_strategy_, kTestJid); 82 request->OnSignallingConnected(&signal_strategy_, kTestJid);
83 message_loop_.RunAllPending(); 83 message_loop_.RunAllPending();
84 84
85 // Verify format of the query. 85 // Verify format of the query.
86 scoped_ptr<XmlElement> stanza(sent_iq); 86 scoped_ptr<XmlElement> stanza(sent_iq);
87 ASSERT_TRUE(stanza != NULL); 87 ASSERT_TRUE(stanza != NULL);
88 88
89 EXPECT_EQ(stanza->Attr(buzz::QName("", "to")), 89 EXPECT_EQ(stanza->Attr(buzz::QName("", "to")),
90 std::string(kChromotingBotJid)); 90 std::string(kChromotingBotJid));
(...skipping 18 matching lines...) Expand all
109 HostKeyPair key_pair; 109 HostKeyPair key_pair;
110 key_pair.LoadFromString(kTestHostKeyPair); 110 key_pair.LoadFromString(kTestHostKeyPair);
111 std::string expected_signature = 111 std::string expected_signature =
112 key_pair.GetSignature(std::string(kTestJid) + ' ' + time_str); 112 key_pair.GetSignature(std::string(kTestJid) + ' ' + time_str);
113 EXPECT_EQ(expected_signature, signature->BodyText()); 113 EXPECT_EQ(expected_signature, signature->BodyText());
114 114
115 // Generate response and verify that callback is called. 115 // Generate response and verify that callback is called.
116 EXPECT_CALL(callback_, OnResponse(true, kSupportId, 116 EXPECT_CALL(callback_, OnResponse(true, kSupportId,
117 base::TimeDelta::FromSeconds(300))); 117 base::TimeDelta::FromSeconds(300)));
118 118
119 scoped_ptr<XmlElement> response(new XmlElement(QName("", "iq"))); 119 scoped_ptr<XmlElement> response(new XmlElement(buzz::QN_IQ));
120 response->AddAttr(QName("", "type"), "result"); 120 response->AddAttr(QName("", "type"), "result");
121 response->AddAttr(QName("", "id"), kStanzaId);
121 122
122 XmlElement* result = new XmlElement( 123 XmlElement* result = new XmlElement(
123 QName(kChromotingXmlNamespace, "register-support-host-result")); 124 QName(kChromotingXmlNamespace, "register-support-host-result"));
124 response->AddElement(result); 125 response->AddElement(result);
125 126
126 XmlElement* support_id = new XmlElement( 127 XmlElement* support_id = new XmlElement(
127 QName(kChromotingXmlNamespace, "support-id")); 128 QName(kChromotingXmlNamespace, "support-id"));
128 support_id->AddText(kSupportId); 129 support_id->AddText(kSupportId);
129 result->AddElement(support_id); 130 result->AddElement(support_id);
130 131
131 XmlElement* support_id_lifetime = new XmlElement( 132 XmlElement* support_id_lifetime = new XmlElement(
132 QName(kChromotingXmlNamespace, "support-id-lifetime")); 133 QName(kChromotingXmlNamespace, "support-id-lifetime"));
133 support_id_lifetime->AddText(kSupportIdLifetime); 134 support_id_lifetime->AddText(kSupportIdLifetime);
134 result->AddElement(support_id_lifetime); 135 result->AddElement(support_id_lifetime);
135 136
136 iq_request->callback().Run(response.get()); 137 EXPECT_TRUE(listener->OnIncomingStanza(response.get()));
137 message_loop_.RunAllPending(); 138 message_loop_.RunAllPending();
139
140 EXPECT_CALL(signal_strategy_, RemoveListener(listener));
138 } 141 }
139 142
140 } // namespace remoting 143 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698