OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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/test/chromoting_host_list_fetcher.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/run_loop.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "net/url_request/test_url_fetcher_factory.h" | |
12 #include "remoting/test/chromoting_host_info.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 | |
joedow
2015/07/06 22:19:13
remove extra newlines
tonychun
2015/07/08 03:12:15
Done.
| |
16 | |
17 namespace { | |
18 // Used as a ChromotingHostListCallback for testing. | |
19 void OnHostlistRetrieved( | |
20 base::Closure done_closure, | |
21 std::vector<remoting::test::ChromotingHostInfo>* hostlist, | |
22 std::vector<remoting::test::ChromotingHostInfo>* retrieved_hostlist) { | |
joedow
2015/07/06 22:19:14
retrieved_hostlist should be const ref
tonychun
2015/07/08 03:12:15
Done.
| |
23 *hostlist = *retrieved_hostlist; | |
24 | |
25 done_closure.Run(); | |
26 } | |
27 | |
28 const char kAccessTokenValue[] = "test_access_token_value"; | |
29 const char kChromotingHostListReadyResponse[] = | |
30 "{" | |
31 " \"data\":{" | |
32 " \"kind\":\"chromoting#hostList\"," | |
33 " \"items\":[" | |
34 " {" | |
35 " \"tokenUrlPatterns\":[" | |
36 " \"tokenUrlPattern_1A\"," | |
37 " \"tokenUrlPattern_1B\"," | |
38 " \"tokenUrlPattern_1C\"" | |
39 " ]," | |
40 " \"kind\":\"chromoting#host\"," | |
41 " \"hostId\":\"test_host_id_1\"," | |
42 " \"hostName\":\"test_host_name_1\"," | |
43 " \"publicKey\":\"test_public_key_1\"," | |
44 " \"jabberId\":\"test_jabber_id_1\"," | |
45 " \"createdTime\":\"test_created_time_1\"," | |
46 " \"updatedTime\":\"test_updated_time_1\"," | |
47 " \"status\":\"ONLINE\"," | |
48 " \"hostOfflineReason\":\"\"," | |
49 " \"hostVersion\":\"test_host_version_1\"" | |
50 " }," | |
51 " {" | |
52 " \"kind\":\"chromoting#host\"," | |
53 " \"hostId\":\"test_host_id_2\"," | |
54 " \"hostName\":\"test_host_name_2\"," | |
55 " \"publicKey\":\"test_public_key_2\"," | |
56 " \"jabberId\":\"test_jabber_id_2\"," | |
57 " \"createdTime\":\"test_created_time_2\"," | |
58 " \"updatedTime\":\"test_updated_time_2\"," | |
59 " \"status\":\"OFFLINE\"," | |
60 " \"hostOfflineReason\":\"test_host_offline_reason_2\"," | |
61 " \"hostVersion\":\"test_host_version_2\"" | |
62 " }" | |
63 " ]" | |
64 " }" | |
65 "}"; | |
joedow
2015/07/06 22:19:13
kChromotingHostListReadyResponse is a good start,
tonychun
2015/07/08 03:12:15
Done.
| |
66 | |
67 const char kChromotingHostListEmptyResponse[] = "{}"; | |
68 } // namespace | |
69 | |
70 namespace remoting { | |
71 namespace test { | |
72 | |
73 // Provides base functionality for the ChromotingHostListFetcher Tests below. | |
74 // The FakeURLFetcherFactory allows us to override the response data and payload | |
75 // for specified URLs. We use this to stub out network calls made by the | |
76 // ChromotingHostListFetcher. This fixture also creates an IO MessageLoop, if | |
joedow
2015/07/06 22:19:14
nit: it looks like it always creates a MessageLoop
tonychun
2015/07/08 03:12:15
Done.
| |
77 // necessary, for use by the ChromotingHostListFetcher. | |
78 class ChromotingHostListFetcherTest : public ::testing::Test { | |
79 public: | |
80 ChromotingHostListFetcherTest() : url_fetcher_factory_(nullptr) {} | |
81 ~ChromotingHostListFetcherTest() override {} | |
82 | |
83 protected: | |
84 // testing::Test interface. | |
85 void SetUp() override; | |
86 | |
87 // Sets the HTTP status and data returned for a specified URL. | |
88 void SetFakeResponse(const GURL& url, | |
89 const std::string& data, | |
90 net::HttpStatusCode code, | |
91 net::URLRequestStatus::Status status); | |
92 | |
93 private: | |
94 net::FakeURLFetcherFactory url_fetcher_factory_; | |
95 scoped_ptr<base::MessageLoopForIO> message_loop_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(ChromotingHostListFetcherTest); | |
98 }; | |
99 | |
100 void ChromotingHostListFetcherTest::SetUp() { | |
101 DCHECK(!message_loop_); | |
102 message_loop_.reset(new base::MessageLoopForIO); | |
103 | |
104 SetFakeResponse(GURL(kChromotingHostListProdRequestUrl), | |
105 kChromotingHostListEmptyResponse, net::HTTP_NOT_FOUND, | |
106 net::URLRequestStatus::FAILED); | |
107 } | |
108 | |
109 void ChromotingHostListFetcherTest::SetFakeResponse( | |
110 const GURL& url, | |
111 const std::string& data, | |
112 net::HttpStatusCode code, | |
113 net::URLRequestStatus::Status status) { | |
114 url_fetcher_factory_.SetFakeResponse(url, data, code, status); | |
115 } | |
116 | |
117 TEST_F(ChromotingHostListFetcherTest, RetrieveHostListFromProd) { | |
118 SetFakeResponse(GURL(kChromotingHostListProdRequestUrl), | |
119 kChromotingHostListReadyResponse, net::HTTP_OK, | |
120 net::URLRequestStatus::SUCCESS); | |
121 | |
122 std::vector<ChromotingHostInfo> hostlist; | |
123 | |
124 base::RunLoop run_loop; | |
125 HostlistCallback hostlist_fetch_callback = base::Bind( | |
126 &OnHostlistRetrieved, | |
127 run_loop.QuitClosure(), | |
128 &hostlist); | |
129 | |
130 ChromotingHostListFetcher chromoting_host_list_fetcher; | |
131 bool request_started = chromoting_host_list_fetcher.RetrieveHostlist( | |
132 kAccessTokenValue, hostlist_fetch_callback); | |
133 | |
134 run_loop.Run(); | |
135 | |
136 EXPECT_TRUE(request_started); | |
137 const unsigned int expectedHLSize = 2; | |
joedow
2015/07/06 22:19:14
no camel case, this var should be called expected_
tonychun
2015/07/08 03:12:15
Done.
| |
138 EXPECT_EQ(hostlist.size(), expectedHLSize); | |
139 | |
140 ChromotingHostInfo onlineChromotingHostInfo = hostlist.front(); | |
joedow
2015/07/06 22:19:13
Also, no camel case. online_chromoting_host_info.
joedow
2015/07/06 22:19:14
If you know the size, I would just use array index
tonychun
2015/07/08 03:12:15
Done.
tonychun
2015/07/08 03:12:15
Done.
| |
141 const unsigned int expectedTUPSize = 3; | |
joedow
2015/07/06 22:19:14
expected_pattern_num?
tonychun
2015/07/08 03:12:15
Done.
| |
142 EXPECT_EQ(onlineChromotingHostInfo.tokenUrlPatterns.size(), expectedTUPSize); | |
143 EXPECT_TRUE(!onlineChromotingHostInfo.host_id.empty()); | |
joedow
2015/07/06 22:19:14
I think it's more readable when you think the stri
tonychun
2015/07/08 03:12:15
Done.
| |
144 EXPECT_TRUE(!onlineChromotingHostInfo.host_jid.empty()); | |
145 EXPECT_TRUE(!onlineChromotingHostInfo.host_name.empty()); | |
146 EXPECT_EQ(onlineChromotingHostInfo.status, | |
147 ChromotingHostStatus::kChromotingHostStatusOnline); | |
148 EXPECT_TRUE(onlineChromotingHostInfo.offline_reason.empty()); | |
149 EXPECT_TRUE(!onlineChromotingHostInfo.public_key.empty()); | |
150 | |
151 ChromotingHostInfo offlineChromotingHostInfo = hostlist.back(); | |
joedow
2015/07/06 22:19:14
offline_chromoting_host_info
tonychun
2015/07/08 03:12:15
Done.
| |
152 EXPECT_TRUE(offlineChromotingHostInfo.tokenUrlPatterns.empty()); | |
153 EXPECT_TRUE(!offlineChromotingHostInfo.host_id.empty()); | |
154 EXPECT_TRUE(!offlineChromotingHostInfo.host_jid.empty()); | |
155 EXPECT_TRUE(!offlineChromotingHostInfo.host_name.empty()); | |
156 EXPECT_EQ(offlineChromotingHostInfo.status, | |
157 ChromotingHostStatus::kChromotingHostStatusOffline); | |
158 EXPECT_TRUE(!offlineChromotingHostInfo.offline_reason.empty()); | |
159 EXPECT_TRUE(!offlineChromotingHostInfo.public_key.empty()); | |
160 } | |
161 | |
162 TEST_F(ChromotingHostListFetcherTest, RetrieveHostListInvalidEnvironment) { | |
joedow
2015/07/06 22:19:14
The test names is "...InvalidEnvironment" but it l
tonychun
2015/07/08 03:12:15
Done.
| |
163 base::RunLoop run_loop; | |
164 | |
165 std::vector<ChromotingHostInfo> hostlist; | |
166 | |
167 HostlistCallback hostlist_fetch_callback = base::Bind( | |
168 &OnHostlistRetrieved, | |
169 run_loop.QuitClosure(), | |
170 &hostlist); | |
171 | |
172 ChromotingHostListFetcher chromoting_host_list_fetcher; | |
173 bool request_started = chromoting_host_list_fetcher.RetrieveHostlist( | |
174 kAccessTokenValue, hostlist_fetch_callback); | |
175 | |
176 EXPECT_TRUE(request_started); | |
177 // If there was a network error retrieving the host list, then the host list | |
178 // should be empty. | |
179 EXPECT_TRUE(hostlist.empty()); | |
180 } | |
181 | |
joedow
2015/07/06 22:19:14
You should also add a test which makes two request
tonychun
2015/07/08 03:12:15
Done.
| |
182 TEST_F(ChromotingHostListFetcherTest, RetrieveHostListEmptyResponse) { | |
183 SetFakeResponse(GURL(kChromotingHostListProdRequestUrl), | |
184 kChromotingHostListEmptyResponse, net::HTTP_OK, | |
185 net::URLRequestStatus::SUCCESS); | |
186 | |
187 base::RunLoop run_loop; | |
188 | |
189 std::vector<ChromotingHostInfo> hostlist; | |
190 | |
191 HostlistCallback hostlist_fetch_callback = base::Bind( | |
192 &OnHostlistRetrieved, | |
193 run_loop.QuitClosure(), | |
194 &hostlist); | |
195 | |
196 ChromotingHostListFetcher chromoting_host_list_fetcher; | |
197 bool request_started = chromoting_host_list_fetcher.RetrieveHostlist( | |
198 kAccessTokenValue, hostlist_fetch_callback); | |
199 | |
200 EXPECT_TRUE(request_started); | |
201 // If we received an empty response, then none of the connection details | |
202 // should be populated. | |
joedow
2015/07/06 22:19:14
"connection details"? Copy/paste error?
tonychun
2015/07/08 03:12:15
Done.
| |
203 EXPECT_TRUE(hostlist.empty()); | |
204 } | |
205 | |
206 } // namespace test | |
207 } // namespace remoting | |
OLD | NEW |