BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / remoting / host / gcd_rest_client_unittest.cc
blob9e6195092d3c5be024d41f59444d1eff970de32d
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::Result result) {
24 ++counter_;
25 last_result_ = result;
28 scoped_ptr<base::DictionaryValue> MakePatchDetails(int id) {
29 scoped_ptr<base::DictionaryValue> patch_details(new base::DictionaryValue);
30 patch_details->SetInteger("id", id);
31 return patch_details.Pass();
34 void CreateClient(OAuthTokenGetter* token_getter = nullptr) {
35 if (!token_getter) {
36 token_getter = &default_token_getter_;
38 client_.reset(new GcdRestClient("http://gcd_base_url", "<gcd_device_id>",
39 nullptr, token_getter));
40 client_->SetClockForTest(make_scoped_ptr(new base::SimpleTestClock));
43 protected:
44 net::TestURLFetcherFactory url_fetcher_factory_;
45 FakeOAuthTokenGetter default_token_getter_;
46 scoped_ptr<GcdRestClient> client_;
47 int counter_ = 0;
48 GcdRestClient::Result last_result_ = GcdRestClient::OTHER_ERROR;
50 private:
51 base::MessageLoop message_loop_;
54 TEST_F(GcdRestClientTest, NetworkErrorGettingToken) {
55 FakeOAuthTokenGetter token_getter(OAuthTokenGetter::NETWORK_ERROR, "", "");
56 CreateClient(&token_getter);
58 client_->PatchState(MakePatchDetails(0).Pass(),
59 base::Bind(&GcdRestClientTest::OnRequestComplete,
60 base::Unretained(this)));
61 base::RunLoop().RunUntilIdle();
63 EXPECT_EQ(1, counter_);
64 EXPECT_EQ(GcdRestClient::NETWORK_ERROR, last_result_);
67 TEST_F(GcdRestClientTest, AuthErrorGettingToken) {
68 FakeOAuthTokenGetter token_getter(OAuthTokenGetter::AUTH_ERROR, "", "");
69 CreateClient(&token_getter);
71 client_->PatchState(MakePatchDetails(0).Pass(),
72 base::Bind(&GcdRestClientTest::OnRequestComplete,
73 base::Unretained(this)));
74 base::RunLoop().RunUntilIdle();
76 EXPECT_EQ(1, counter_);
77 EXPECT_EQ(GcdRestClient::OTHER_ERROR, last_result_);
80 TEST_F(GcdRestClientTest, NetworkErrorOnPost) {
81 CreateClient();
83 client_->PatchState(MakePatchDetails(0).Pass(),
84 base::Bind(&GcdRestClientTest::OnRequestComplete,
85 base::Unretained(this)));
86 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
88 base::RunLoop().RunUntilIdle();
90 ASSERT_TRUE(fetcher);
91 fetcher->set_response_code(0);
92 fetcher->delegate()->OnURLFetchComplete(fetcher);
93 EXPECT_EQ(1, counter_);
94 EXPECT_EQ(GcdRestClient::NETWORK_ERROR, last_result_);
97 TEST_F(GcdRestClientTest, OtherErrorOnPost) {
98 CreateClient();
100 client_->PatchState(MakePatchDetails(0).Pass(),
101 base::Bind(&GcdRestClientTest::OnRequestComplete,
102 base::Unretained(this)));
103 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
105 base::RunLoop().RunUntilIdle();
107 ASSERT_TRUE(fetcher);
108 fetcher->set_response_code(500);
109 fetcher->delegate()->OnURLFetchComplete(fetcher);
110 EXPECT_EQ(1, counter_);
111 EXPECT_EQ(GcdRestClient::OTHER_ERROR, last_result_);
114 TEST_F(GcdRestClientTest, NoSuchHost) {
115 CreateClient();
117 client_->PatchState(MakePatchDetails(0).Pass(),
118 base::Bind(&GcdRestClientTest::OnRequestComplete,
119 base::Unretained(this)));
120 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
122 base::RunLoop().RunUntilIdle();
124 ASSERT_TRUE(fetcher);
125 fetcher->set_response_code(404);
126 fetcher->delegate()->OnURLFetchComplete(fetcher);
127 EXPECT_EQ(1, counter_);
128 EXPECT_EQ(GcdRestClient::NO_SUCH_HOST, last_result_);
131 TEST_F(GcdRestClientTest, Succeed) {
132 CreateClient();
134 client_->PatchState(MakePatchDetails(0).Pass(),
135 base::Bind(&GcdRestClientTest::OnRequestComplete,
136 base::Unretained(this)));
137 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
139 base::RunLoop().RunUntilIdle();
141 ASSERT_TRUE(fetcher);
142 EXPECT_EQ("http://gcd_base_url/devices/%3Cgcd_device_id%3E/patchState",
143 fetcher->GetOriginalURL().spec());
144 EXPECT_EQ(
145 "{\"patches\":[{\"patch\":{\"id\":0},\"timeMs\":0.0}],"
146 "\"requestTimeMs\":0.0}",
147 fetcher->upload_data());
148 EXPECT_EQ("application/json", fetcher->upload_content_type());
149 fetcher->set_response_code(200);
150 fetcher->delegate()->OnURLFetchComplete(fetcher);
151 EXPECT_EQ(1, counter_);
152 EXPECT_EQ(GcdRestClient::SUCCESS, last_result_);
155 } // namespace remoting