Compute can_use_lcd_text using property trees.
[chromium-blink-merge.git] / remoting / host / gcd_rest_client_unittest.cc
blobeb42757fd547f8354b1b0233703666fc4c62c05a
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.
5 #include "remoting/host/gcd_rest_client.h"
7 #include "base/run_loop.h"
8 #include "base/test/simple_test_clock.h"
9 #include "base/values.h"
10 #include "net/url_request/test_url_fetcher_factory.h"
11 #include "remoting/host/fake_oauth_token_getter.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace remoting {
16 class GcdRestClientTest : public testing::Test {
17 public:
18 GcdRestClientTest()
19 : default_token_getter_(OAuthTokenGetter::SUCCESS,
20 "<fake_user_email>",
21 "<fake_access_token>") {}
23 void OnRequestComplete(GcdRestClient::Status status) {
24 ++counter_;
25 last_status_ = status;
26 if (delete_client_) {
27 client_.reset();
31 scoped_ptr<base::DictionaryValue> MakePatchDetails(int id) {
32 scoped_ptr<base::DictionaryValue> patch_details(new base::DictionaryValue);
33 patch_details->SetInteger("id", id);
34 return patch_details.Pass();
37 void CreateClient(OAuthTokenGetter* token_getter = nullptr) {
38 if (!token_getter) {
39 token_getter = &default_token_getter_;
41 client_.reset(new GcdRestClient("http://gcd_base_url", "<gcd_device_id>",
42 nullptr, token_getter));
43 client_->SetClockForTest(make_scoped_ptr(new base::SimpleTestClock));
46 protected:
47 net::TestURLFetcherFactory url_fetcher_factory_;
48 FakeOAuthTokenGetter default_token_getter_;
49 scoped_ptr<GcdRestClient> client_;
50 bool delete_client_ = false;
51 int counter_ = 0;
52 GcdRestClient::Status last_status_ = GcdRestClient::OTHER_ERROR;
54 private:
55 base::MessageLoop message_loop_;
58 TEST_F(GcdRestClientTest, NetworkErrorGettingToken) {
59 FakeOAuthTokenGetter token_getter(OAuthTokenGetter::NETWORK_ERROR, "", "");
60 CreateClient(&token_getter);
62 client_->PatchState(MakePatchDetails(0).Pass(),
63 base::Bind(&GcdRestClientTest::OnRequestComplete,
64 base::Unretained(this)));
65 base::RunLoop().RunUntilIdle();
67 EXPECT_EQ(1, counter_);
68 EXPECT_EQ(GcdRestClient::NETWORK_ERROR, last_status_);
71 TEST_F(GcdRestClientTest, AuthErrorGettingToken) {
72 FakeOAuthTokenGetter token_getter(OAuthTokenGetter::AUTH_ERROR, "", "");
73 CreateClient(&token_getter);
75 client_->PatchState(MakePatchDetails(0).Pass(),
76 base::Bind(&GcdRestClientTest::OnRequestComplete,
77 base::Unretained(this)));
78 base::RunLoop().RunUntilIdle();
80 EXPECT_EQ(1, counter_);
81 EXPECT_EQ(GcdRestClient::OTHER_ERROR, last_status_);
84 TEST_F(GcdRestClientTest, NetworkErrorOnPost) {
85 CreateClient();
87 client_->PatchState(MakePatchDetails(0).Pass(),
88 base::Bind(&GcdRestClientTest::OnRequestComplete,
89 base::Unretained(this)));
90 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
92 base::RunLoop().RunUntilIdle();
94 ASSERT_TRUE(fetcher);
95 fetcher->set_response_code(0);
96 fetcher->delegate()->OnURLFetchComplete(fetcher);
97 EXPECT_EQ(1, counter_);
98 EXPECT_EQ(GcdRestClient::NETWORK_ERROR, last_status_);
101 TEST_F(GcdRestClientTest, OtherErrorOnPost) {
102 CreateClient();
104 client_->PatchState(MakePatchDetails(0).Pass(),
105 base::Bind(&GcdRestClientTest::OnRequestComplete,
106 base::Unretained(this)));
107 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
109 base::RunLoop().RunUntilIdle();
111 ASSERT_TRUE(fetcher);
112 fetcher->set_response_code(500);
113 fetcher->delegate()->OnURLFetchComplete(fetcher);
114 EXPECT_EQ(1, counter_);
115 EXPECT_EQ(GcdRestClient::OTHER_ERROR, last_status_);
118 TEST_F(GcdRestClientTest, NoSuchHost) {
119 CreateClient();
121 client_->PatchState(MakePatchDetails(0).Pass(),
122 base::Bind(&GcdRestClientTest::OnRequestComplete,
123 base::Unretained(this)));
124 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
126 base::RunLoop().RunUntilIdle();
128 ASSERT_TRUE(fetcher);
129 fetcher->set_response_code(404);
130 fetcher->delegate()->OnURLFetchComplete(fetcher);
131 EXPECT_EQ(1, counter_);
132 EXPECT_EQ(GcdRestClient::NO_SUCH_HOST, last_status_);
135 TEST_F(GcdRestClientTest, Succeed) {
136 CreateClient();
138 client_->PatchState(MakePatchDetails(0).Pass(),
139 base::Bind(&GcdRestClientTest::OnRequestComplete,
140 base::Unretained(this)));
141 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
143 base::RunLoop().RunUntilIdle();
145 ASSERT_TRUE(fetcher);
146 EXPECT_EQ("http://gcd_base_url/devices/%3Cgcd_device_id%3E/patchState",
147 fetcher->GetOriginalURL().spec());
148 EXPECT_EQ(
149 "{\"patches\":[{\"patch\":{\"id\":0},\"timeMs\":0.0}],"
150 "\"requestTimeMs\":0.0}",
151 fetcher->upload_data());
152 EXPECT_EQ("application/json", fetcher->upload_content_type());
153 fetcher->set_response_code(200);
154 fetcher->delegate()->OnURLFetchComplete(fetcher);
155 EXPECT_EQ(1, counter_);
156 EXPECT_EQ(GcdRestClient::SUCCESS, last_status_);
159 TEST_F(GcdRestClientTest, SucceedTwice) {
160 CreateClient();
162 client_->PatchState(MakePatchDetails(0).Pass(),
163 base::Bind(&GcdRestClientTest::OnRequestComplete,
164 base::Unretained(this)));
165 net::TestURLFetcher* fetcher0 = url_fetcher_factory_.GetFetcherByID(0);
166 client_->PatchState(MakePatchDetails(1).Pass(),
167 base::Bind(&GcdRestClientTest::OnRequestComplete,
168 base::Unretained(this)));
169 net::TestURLFetcher* fetcher1 = url_fetcher_factory_.GetFetcherByID(0);
171 base::RunLoop().RunUntilIdle();
173 EXPECT_EQ(
174 "{\"patches\":[{\"patch\":{\"id\":0},\"timeMs\":0.0}],"
175 "\"requestTimeMs\":0.0}",
176 fetcher0->upload_data());
177 fetcher0->set_response_code(200);
178 fetcher0->delegate()->OnURLFetchComplete(fetcher0);
179 EXPECT_EQ(GcdRestClient::SUCCESS, last_status_);
180 EXPECT_EQ(1, counter_);
182 base::RunLoop().RunUntilIdle();
184 EXPECT_EQ(
185 "{\"patches\":[{\"patch\":{\"id\":1},\"timeMs\":0.0}],"
186 "\"requestTimeMs\":0.0}",
187 fetcher1->upload_data());
188 fetcher1->set_response_code(200);
189 fetcher1->delegate()->OnURLFetchComplete(fetcher1);
190 EXPECT_EQ(GcdRestClient::SUCCESS, last_status_);
191 EXPECT_EQ(2, counter_);
194 TEST_F(GcdRestClientTest, SucceedAndDelete) {
195 CreateClient();
197 client_->PatchState(MakePatchDetails(0).Pass(),
198 base::Bind(&GcdRestClientTest::OnRequestComplete,
199 base::Unretained(this)));
200 net::TestURLFetcher* fetcher0 = url_fetcher_factory_.GetFetcherByID(0);
201 client_->PatchState(MakePatchDetails(1).Pass(),
202 base::Bind(&GcdRestClientTest::OnRequestComplete,
203 base::Unretained(this)));
204 delete_client_ = true;
206 base::RunLoop().RunUntilIdle();
208 EXPECT_EQ(
209 "{\"patches\":[{\"patch\":{\"id\":0},\"timeMs\":0.0}],"
210 "\"requestTimeMs\":0.0}",
211 fetcher0->upload_data());
212 fetcher0->set_response_code(200);
213 fetcher0->delegate()->OnURLFetchComplete(fetcher0);
214 EXPECT_EQ(GcdRestClient::SUCCESS, last_status_);
215 EXPECT_EQ(1, counter_);
217 base::RunLoop().RunUntilIdle();
220 } // namespace remoting