BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / local_discovery / gcd_api_flow_unittest.cc
blob9e8da41095d7c0f83c2d51be8ab7bf7bf23d929e
1 // Copyright 2014 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 "chrome/browser/local_discovery/gcd_api_flow.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "base/values.h"
11 #include "chrome/browser/local_discovery/gcd_api_flow_impl.h"
12 #include "content/public/test/test_browser_thread.h"
13 #include "google_apis/gaia/fake_oauth2_token_service.h"
14 #include "google_apis/gaia/google_service_auth_error.h"
15 #include "net/base/host_port_pair.h"
16 #include "net/base/net_errors.h"
17 #include "net/http/http_request_headers.h"
18 #include "net/url_request/test_url_fetcher_factory.h"
19 #include "net/url_request/url_request_test_util.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using testing::_;
24 using testing::Return;
26 namespace local_discovery {
28 namespace {
30 const char kSampleConfirmResponse[] = "{}";
32 const char kFailedConfirmResponseBadJson[] = "[]";
34 const char kAccountId[] = "account_id";
36 class MockDelegate : public CloudPrintApiFlowRequest {
37 public:
38 MOCK_METHOD1(OnGCDAPIFlowError, void(GCDApiFlow::Status));
39 MOCK_METHOD1(OnGCDAPIFlowComplete, void(const base::DictionaryValue&));
41 MOCK_METHOD0(GetURL, GURL());
44 class GCDApiFlowTest : public testing::Test {
45 public:
46 GCDApiFlowTest()
47 : ui_thread_(content::BrowserThread::UI, &loop_),
48 request_context_(new net::TestURLRequestContextGetter(
49 base::ThreadTaskRunnerHandle::Get())),
50 account_id_(kAccountId) {}
52 ~GCDApiFlowTest() override {}
54 protected:
55 void SetUp() override {
56 token_service_.GetFakeOAuth2TokenServiceDelegate()->set_request_context(
57 request_context_.get());
58 token_service_.AddAccount(account_id_);
59 ui_thread_.Stop(); // HACK: Fake being on the UI thread
61 scoped_ptr<MockDelegate> delegate(new MockDelegate);
62 mock_delegate_ = delegate.get();
63 EXPECT_CALL(*mock_delegate_, GetURL())
64 .WillRepeatedly(Return(
65 GURL("https://www.google.com/cloudprint/confirm?token=SomeToken")));
66 gcd_flow_.reset(new GCDApiFlowImpl(
67 request_context_.get(), &token_service_, account_id_));
68 gcd_flow_->Start(delegate.Pass());
70 base::MessageLoopForUI loop_;
71 content::TestBrowserThread ui_thread_;
72 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
73 net::TestURLFetcherFactory fetcher_factory_;
74 FakeOAuth2TokenService token_service_;
75 std::string account_id_;
76 scoped_ptr<GCDApiFlowImpl> gcd_flow_;
77 MockDelegate* mock_delegate_;
80 TEST_F(GCDApiFlowTest, SuccessOAuth2) {
81 gcd_flow_->OnGetTokenSuccess(NULL, "SomeToken", base::Time());
82 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
84 EXPECT_EQ(GURL("https://www.google.com/cloudprint/confirm?token=SomeToken"),
85 fetcher->GetOriginalURL());
87 net::HttpRequestHeaders headers;
88 fetcher->GetExtraRequestHeaders(&headers);
89 std::string oauth_header;
90 std::string proxy;
91 EXPECT_TRUE(headers.GetHeader("Authorization", &oauth_header));
92 EXPECT_EQ("Bearer SomeToken", oauth_header);
93 EXPECT_TRUE(headers.GetHeader("X-Cloudprint-Proxy", &proxy));
94 EXPECT_EQ("Chrome", proxy);
96 fetcher->SetResponseString(kSampleConfirmResponse);
97 fetcher->set_status(
98 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
99 fetcher->set_response_code(200);
101 EXPECT_CALL(*mock_delegate_, OnGCDAPIFlowComplete(_));
103 fetcher->delegate()->OnURLFetchComplete(fetcher);
106 TEST_F(GCDApiFlowTest, BadToken) {
107 EXPECT_CALL(*mock_delegate_, OnGCDAPIFlowError(GCDApiFlow::ERROR_TOKEN));
108 gcd_flow_->OnGetTokenFailure(
109 NULL, GoogleServiceAuthError(GoogleServiceAuthError::USER_NOT_SIGNED_UP));
112 TEST_F(GCDApiFlowTest, BadJson) {
113 gcd_flow_->OnGetTokenSuccess(NULL, "SomeToken", base::Time());
114 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
116 EXPECT_EQ(GURL("https://www.google.com/cloudprint/confirm?token=SomeToken"),
117 fetcher->GetOriginalURL());
119 fetcher->SetResponseString(kFailedConfirmResponseBadJson);
120 fetcher->set_status(
121 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
122 fetcher->set_response_code(200);
124 EXPECT_CALL(*mock_delegate_,
125 OnGCDAPIFlowError(GCDApiFlow::ERROR_MALFORMED_RESPONSE));
127 fetcher->delegate()->OnURLFetchComplete(fetcher);
130 } // namespace
132 } // namespace local_discovery