OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 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 <set> |
| 6 #include <string> |
| 7 |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/message_loop_proxy.h" |
| 11 #include "base/time.h" |
| 12 #include "chrome/browser/chrome_to_mobile/common/cloud_print_request.h" |
| 13 #include "chrome/browser/chrome_to_mobile/common/cloud_print_request_impl.h" |
| 14 #include "chrome/browser/signin/oauth2_token_service.h" |
| 15 #include "content/public/test/test_browser_thread.h" |
| 16 #include "googleurl/src/gurl.h" |
| 17 #include "google_apis/gaia/gaia_constants.h" |
| 18 #include "google_apis/gaia/google_service_auth_error.h" |
| 19 #include "google_apis/gaia/oauth2_access_token_consumer.h" |
| 20 #include "net/http/http_response_headers.h" |
| 21 #include "net/http/http_status_code.h" |
| 22 #include "net/url_request/test_url_fetcher_factory.h" |
| 23 #include "net/url_request/url_request_test_util.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 |
| 26 using namespace chrome_to_mobile; |
| 27 |
| 28 namespace { |
| 29 |
| 30 class MockOAuth2TokenService : public OAuth2TokenService { |
| 31 public: |
| 32 explicit MockOAuth2TokenService(net::URLRequestContextGetter* getter); |
| 33 virtual ~MockOAuth2TokenService(); |
| 34 |
| 35 virtual Request* StartRequest(const std::vector<std::string>& scopes, |
| 36 OAuth2AccessTokenConsumer* consumer) OVERRIDE; |
| 37 // Helper to set if oauth2 token fetch should be successful. |
| 38 void SetSucceed(bool succeed); |
| 39 |
| 40 private: |
| 41 bool succeed_; |
| 42 }; |
| 43 |
| 44 MockOAuth2TokenService::MockOAuth2TokenService( |
| 45 net::URLRequestContextGetter* getter) : succeed_(true) { |
| 46 } |
| 47 |
| 48 MockOAuth2TokenService::~MockOAuth2TokenService() { |
| 49 } |
| 50 |
| 51 OAuth2TokenService::Request* MockOAuth2TokenService::StartRequest( |
| 52 const std::vector<std::string>& scopes, |
| 53 OAuth2AccessTokenConsumer* consumer) { |
| 54 if (succeed_) { |
| 55 base::Time expiration_time; |
| 56 consumer->OnGetTokenSuccess(std::string("acces token"), expiration_time); |
| 57 } else { |
| 58 consumer->OnGetTokenFailure(GoogleServiceAuthError( |
| 59 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); |
| 60 } |
| 61 return NULL; |
| 62 } |
| 63 |
| 64 void MockOAuth2TokenService::SetSucceed(bool succeed) { |
| 65 succeed_ = succeed; |
| 66 } |
| 67 |
| 68 class MockCloudPrintRequestDelegate : public CloudPrintRequest::Delegate { |
| 69 public: |
| 70 MockCloudPrintRequestDelegate(); |
| 71 virtual ~MockCloudPrintRequestDelegate(); |
| 72 |
| 73 virtual void OnRequestComplete(CloudPrintRequest* source) OVERRIDE; |
| 74 |
| 75 const std::set<CloudPrintRequest*>* GetCompletedRequests() const; |
| 76 |
| 77 private: |
| 78 std::set<CloudPrintRequest*> requests_; |
| 79 }; |
| 80 |
| 81 MockCloudPrintRequestDelegate::MockCloudPrintRequestDelegate() { |
| 82 } |
| 83 |
| 84 MockCloudPrintRequestDelegate::~MockCloudPrintRequestDelegate() { |
| 85 } |
| 86 |
| 87 void MockCloudPrintRequestDelegate::OnRequestComplete( |
| 88 CloudPrintRequest* source) { |
| 89 requests_.insert(source); |
| 90 } |
| 91 |
| 92 const std::set<CloudPrintRequest*>* MockCloudPrintRequestDelegate:: |
| 93 GetCompletedRequests() const { |
| 94 return &requests_; |
| 95 } |
| 96 |
| 97 } // namespace |
| 98 |
| 99 class CloudPrintRequestImplTest : public testing::Test { |
| 100 public: |
| 101 CloudPrintRequestImplTest(); |
| 102 virtual ~CloudPrintRequestImplTest(); |
| 103 |
| 104 virtual void SetUp() OVERRIDE; |
| 105 |
| 106 protected: |
| 107 MessageLoop test_loop_; |
| 108 scoped_ptr<content::TestBrowserThread> test_thread_; |
| 109 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 110 scoped_ptr<MockOAuth2TokenService> mock_oauth2_token_service_; |
| 111 scoped_ptr<MockCloudPrintRequestDelegate> delegate_; |
| 112 net::TestURLFetcherFactory factory_; |
| 113 scoped_ptr<CloudPrintRequestImpl> request_; |
| 114 }; |
| 115 |
| 116 CloudPrintRequestImplTest::CloudPrintRequestImplTest() { |
| 117 } |
| 118 |
| 119 CloudPrintRequestImplTest::~CloudPrintRequestImplTest() { |
| 120 } |
| 121 |
| 122 void CloudPrintRequestImplTest::SetUp() { |
| 123 testing::Test::SetUp(); |
| 124 |
| 125 test_thread_.reset(new content::TestBrowserThread(content::BrowserThread::UI, |
| 126 &test_loop_)); |
| 127 request_context_getter_ = new TestURLRequestContextGetter( |
| 128 test_loop_.message_loop_proxy()); |
| 129 mock_oauth2_token_service_.reset(new MockOAuth2TokenService( |
| 130 request_context_getter_)); |
| 131 delegate_.reset(new MockCloudPrintRequestDelegate()); |
| 132 |
| 133 CloudPrintRequest::Settings settings; |
| 134 settings.cloud_print_client_id = std::string("client id"); |
| 135 settings.request_context_getter = request_context_getter_; |
| 136 settings.oauth2_token_service = mock_oauth2_token_service_.get(); |
| 137 request_.reset(new CloudPrintRequestImpl(GURL("http://www.test.test.test"), |
| 138 std::string(""), |
| 139 net::URLFetcher::GET, |
| 140 std::string(""), |
| 141 std::string(""), |
| 142 settings, |
| 143 delegate_.get())); |
| 144 } |
| 145 |
| 146 TEST_F(CloudPrintRequestImplTest, |
| 147 Success) { |
| 148 request_->Start(); |
| 149 EXPECT_EQ(0U, delegate_->GetCompletedRequests()->size()); |
| 150 |
| 151 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); |
| 152 EXPECT_TRUE(fetcher); |
| 153 fetcher->set_response_code(net::HTTP_OK); |
| 154 fetcher->SetResponseString("test test test"); |
| 155 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 156 |
| 157 bool succeed; |
| 158 EXPECT_EQ(1U, delegate_->GetCompletedRequests()->size()); |
| 159 EXPECT_TRUE(delegate_->GetCompletedRequests()->find(request_.get()) != |
| 160 delegate_->GetCompletedRequests()->end()); |
| 161 EXPECT_FALSE(request_->HasOAuth2AccessTokenFailure()); |
| 162 EXPECT_FALSE(request_->HasCloudPrintAuthError()); |
| 163 EXPECT_STREQ("test test test", request_->GetResponseData(&succeed).c_str()); |
| 164 EXPECT_TRUE(succeed); |
| 165 } |
| 166 |
| 167 TEST_F(CloudPrintRequestImplTest, |
| 168 OAuth2Error) { |
| 169 mock_oauth2_token_service_->SetSucceed(false); |
| 170 request_->Start(); |
| 171 |
| 172 EXPECT_EQ(1U, delegate_->GetCompletedRequests()->size()); |
| 173 |
| 174 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); |
| 175 EXPECT_FALSE(fetcher); |
| 176 |
| 177 bool succeed; |
| 178 EXPECT_EQ(1U, delegate_->GetCompletedRequests()->size()); |
| 179 EXPECT_TRUE(delegate_->GetCompletedRequests()->find(request_.get()) != |
| 180 delegate_->GetCompletedRequests()->end()); |
| 181 EXPECT_TRUE(request_->HasOAuth2AccessTokenFailure()); |
| 182 EXPECT_FALSE(request_->HasCloudPrintAuthError()); |
| 183 EXPECT_STREQ("", request_->GetResponseData(&succeed).c_str()); |
| 184 EXPECT_FALSE(succeed); |
| 185 } |
| 186 |
| 187 TEST_F(CloudPrintRequestImplTest, |
| 188 CloudPrintAuthError) { |
| 189 request_->Start(); |
| 190 |
| 191 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); |
| 192 EXPECT_TRUE(fetcher); |
| 193 fetcher->set_response_code(net::HTTP_FORBIDDEN); |
| 194 fetcher->SetResponseString(""); |
| 195 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 196 |
| 197 bool succeed; |
| 198 EXPECT_EQ(1U, delegate_->GetCompletedRequests()->size()); |
| 199 EXPECT_TRUE(delegate_->GetCompletedRequests()->find(request_.get()) != |
| 200 delegate_->GetCompletedRequests()->end()); |
| 201 EXPECT_FALSE(request_->HasOAuth2AccessTokenFailure()); |
| 202 EXPECT_TRUE(request_->HasCloudPrintAuthError()); |
| 203 EXPECT_STREQ("", request_->GetResponseData(&succeed).c_str()); |
| 204 EXPECT_FALSE(succeed); |
| 205 } |
OLD | NEW |