OLD | NEW |
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/observer_list.h" |
10 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
11 #include "remoting/base/constants.h" | 12 #include "remoting/base/constants.h" |
12 #include "remoting/host/host_key_pair.h" | 13 #include "remoting/host/host_key_pair.h" |
13 #include "remoting/host/in_memory_host_config.h" | 14 #include "remoting/host/in_memory_host_config.h" |
14 #include "remoting/host/test_key_pair.h" | 15 #include "remoting/host/test_key_pair.h" |
15 #include "remoting/jingle_glue/iq_sender.h" | 16 #include "remoting/jingle_glue/iq_sender.h" |
16 #include "remoting/jingle_glue/mock_objects.h" | 17 #include "remoting/jingle_glue/mock_objects.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 20 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
(...skipping 10 matching lines...) Expand all Loading... |
30 | 31 |
31 namespace remoting { | 32 namespace remoting { |
32 | 33 |
33 namespace { | 34 namespace { |
34 const char kTestJid[] = "user@gmail.com/chromoting123"; | 35 const char kTestJid[] = "user@gmail.com/chromoting123"; |
35 const int64 kTestTime = 123123123; | 36 const int64 kTestTime = 123123123; |
36 const char kSupportId[] = "AB4RF3"; | 37 const char kSupportId[] = "AB4RF3"; |
37 const char kSupportIdLifetime[] = "300"; | 38 const char kSupportIdLifetime[] = "300"; |
38 const char kStanzaId[] = "123"; | 39 const char kStanzaId[] = "123"; |
39 | 40 |
| 41 ACTION_P(AddListener, list) { |
| 42 list->AddObserver(arg0); |
| 43 } |
| 44 ACTION_P(RemoveListener, list) { |
| 45 list->RemoveObserver(arg0); |
| 46 } |
| 47 |
40 class MockCallback { | 48 class MockCallback { |
41 public: | 49 public: |
42 MOCK_METHOD3(OnResponse, void(bool result, const std::string& support_id, | 50 MOCK_METHOD3(OnResponse, void(bool result, const std::string& support_id, |
43 const base::TimeDelta& lifetime)); | 51 const base::TimeDelta& lifetime)); |
44 }; | 52 }; |
45 | 53 |
46 } // namespace | 54 } // namespace |
47 | 55 |
48 class RegisterSupportHostRequestTest : public testing::Test { | 56 class RegisterSupportHostRequestTest : public testing::Test { |
49 public: | 57 public: |
50 protected: | 58 protected: |
51 virtual void SetUp() { | 59 virtual void SetUp() { |
52 config_ = new InMemoryHostConfig(); | 60 config_ = new InMemoryHostConfig(); |
53 config_->SetString(kPrivateKeyConfigPath, kTestHostKeyPair); | 61 config_->SetString(kPrivateKeyConfigPath, kTestHostKeyPair); |
| 62 |
| 63 EXPECT_CALL(signal_strategy_, AddListener(NotNull())) |
| 64 .WillRepeatedly(AddListener(&signal_strategy_listeners_)); |
| 65 EXPECT_CALL(signal_strategy_, RemoveListener(NotNull())) |
| 66 .WillRepeatedly(RemoveListener(&signal_strategy_listeners_)); |
| 67 EXPECT_CALL(signal_strategy_, GetLocalJid()) |
| 68 .WillRepeatedly(Return(kTestJid)); |
54 } | 69 } |
55 | 70 |
| 71 MessageLoop message_loop_; |
56 MockSignalStrategy signal_strategy_; | 72 MockSignalStrategy signal_strategy_; |
57 MessageLoop message_loop_; | 73 ObserverList<SignalStrategy::Listener, true> signal_strategy_listeners_; |
58 scoped_refptr<InMemoryHostConfig> config_; | 74 scoped_refptr<InMemoryHostConfig> config_; |
59 MockCallback callback_; | 75 MockCallback callback_; |
60 }; | 76 }; |
61 | 77 |
| 78 |
62 TEST_F(RegisterSupportHostRequestTest, Send) { | 79 TEST_F(RegisterSupportHostRequestTest, Send) { |
63 // |iq_request| is freed by RegisterSupportHostRequest. | 80 // |iq_request| is freed by RegisterSupportHostRequest. |
64 int64 start_time = static_cast<int64>(base::Time::Now().ToDoubleT()); | 81 int64 start_time = static_cast<int64>(base::Time::Now().ToDoubleT()); |
65 | 82 |
66 SignalStrategy::Listener* listener; | |
67 EXPECT_CALL(signal_strategy_, AddListener(NotNull())) | |
68 .WillOnce(SaveArg<0>(&listener)); | |
69 | |
70 scoped_ptr<RegisterSupportHostRequest> request( | 83 scoped_ptr<RegisterSupportHostRequest> request( |
71 new RegisterSupportHostRequest()); | 84 new RegisterSupportHostRequest()); |
72 ASSERT_TRUE(request->Init( | 85 ASSERT_TRUE(request->Init( |
73 config_, base::Bind(&MockCallback::OnResponse, | 86 &signal_strategy_, config_, base::Bind(&MockCallback::OnResponse, |
74 base::Unretained(&callback_)))); | 87 base::Unretained(&callback_)))); |
75 | 88 |
76 XmlElement* sent_iq = NULL; | 89 XmlElement* sent_iq = NULL; |
77 EXPECT_CALL(signal_strategy_, GetNextId()) | 90 EXPECT_CALL(signal_strategy_, GetNextId()) |
78 .WillOnce(Return(kStanzaId)); | 91 .WillOnce(Return(kStanzaId)); |
79 EXPECT_CALL(signal_strategy_, GetLocalJid()) | |
80 .WillRepeatedly(Return(kTestJid)); | |
81 EXPECT_CALL(signal_strategy_, SendStanza(NotNull())) | 92 EXPECT_CALL(signal_strategy_, SendStanza(NotNull())) |
82 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true))); | 93 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true))); |
83 | 94 |
84 request->OnSignallingConnected(&signal_strategy_); | 95 request->OnSignalStrategyStateChange(SignalStrategy::CONNECTED); |
85 message_loop_.RunAllPending(); | 96 message_loop_.RunAllPending(); |
86 | 97 |
87 // Verify format of the query. | 98 // Verify format of the query. |
88 scoped_ptr<XmlElement> stanza(sent_iq); | 99 scoped_ptr<XmlElement> stanza(sent_iq); |
89 ASSERT_TRUE(stanza != NULL); | 100 ASSERT_TRUE(stanza != NULL); |
90 | 101 |
91 EXPECT_EQ(stanza->Attr(buzz::QName("", "to")), | 102 EXPECT_EQ(stanza->Attr(buzz::QName("", "to")), |
92 std::string(kChromotingBotJid)); | 103 std::string(kChromotingBotJid)); |
93 EXPECT_EQ(stanza->Attr(buzz::QName("", "type")), "set"); | 104 EXPECT_EQ(stanza->Attr(buzz::QName("", "type")), "set"); |
94 | 105 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 XmlElement* support_id = new XmlElement( | 140 XmlElement* support_id = new XmlElement( |
130 QName(kChromotingXmlNamespace, "support-id")); | 141 QName(kChromotingXmlNamespace, "support-id")); |
131 support_id->AddText(kSupportId); | 142 support_id->AddText(kSupportId); |
132 result->AddElement(support_id); | 143 result->AddElement(support_id); |
133 | 144 |
134 XmlElement* support_id_lifetime = new XmlElement( | 145 XmlElement* support_id_lifetime = new XmlElement( |
135 QName(kChromotingXmlNamespace, "support-id-lifetime")); | 146 QName(kChromotingXmlNamespace, "support-id-lifetime")); |
136 support_id_lifetime->AddText(kSupportIdLifetime); | 147 support_id_lifetime->AddText(kSupportIdLifetime); |
137 result->AddElement(support_id_lifetime); | 148 result->AddElement(support_id_lifetime); |
138 | 149 |
139 EXPECT_TRUE(listener->OnSignalStrategyIncomingStanza(response.get())); | 150 int consumed = 0; |
| 151 ObserverListBase<SignalStrategy::Listener>::Iterator it( |
| 152 signal_strategy_listeners_); |
| 153 SignalStrategy::Listener* listener; |
| 154 while ((listener = it.GetNext()) != NULL) { |
| 155 if (listener->OnSignalStrategyIncomingStanza(response.get())) |
| 156 consumed++; |
| 157 } |
| 158 EXPECT_EQ(1, consumed); |
| 159 |
140 message_loop_.RunAllPending(); | 160 message_loop_.RunAllPending(); |
141 | |
142 EXPECT_CALL(signal_strategy_, RemoveListener(listener)); | |
143 } | 161 } |
144 | 162 |
145 } // namespace remoting | 163 } // namespace remoting |
OLD | NEW |