| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/printing/print_dialog_cloud.h" | 5 #include "chrome/browser/printing/print_dialog_cloud.h" |
| 6 #include "chrome/browser/printing/print_dialog_cloud_internal.h" | 6 #include "chrome/browser/printing/print_dialog_cloud_internal.h" |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" | 21 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" |
| 22 #include "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
| 23 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 24 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
| 25 #include "content/public/browser/browser_thread.h" |
| 25 #include "content/public/browser/notification_details.h" | 26 #include "content/public/browser/notification_details.h" |
| 26 #include "content/public/browser/notification_source.h" | 27 #include "content/public/browser/notification_source.h" |
| 27 #include "content/public/browser/notification_types.h" | 28 #include "content/public/browser/notification_types.h" |
| 28 #include "content/public/test/test_browser_thread.h" | 29 #include "content/public/test/test_browser_thread_bundle.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 32 |
| 32 using content::BrowserThread; | 33 using content::BrowserThread; |
| 33 using content::WebContents; | 34 using content::WebContents; |
| 34 using content::WebUIMessageHandler; | 35 using content::WebUIMessageHandler; |
| 35 using testing::A; | 36 using testing::A; |
| 36 using testing::AtLeast; | 37 using testing::AtLeast; |
| 37 using testing::Eq; | 38 using testing::Eq; |
| 38 using testing::HasSubstr; | 39 using testing::HasSubstr; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 MockCloudPrintDataSenderHelper() : CloudPrintDataSenderHelper(NULL) {} | 142 MockCloudPrintDataSenderHelper() : CloudPrintDataSenderHelper(NULL) {} |
| 142 MOCK_METHOD1(CallJavascriptFunction, void(const std::wstring&)); | 143 MOCK_METHOD1(CallJavascriptFunction, void(const std::wstring&)); |
| 143 MOCK_METHOD2(CallJavascriptFunction, void(const std::wstring&, | 144 MOCK_METHOD2(CallJavascriptFunction, void(const std::wstring&, |
| 144 const Value& arg1)); | 145 const Value& arg1)); |
| 145 MOCK_METHOD3(CallJavascriptFunction, void(const std::wstring&, | 146 MOCK_METHOD3(CallJavascriptFunction, void(const std::wstring&, |
| 146 const Value& arg1, | 147 const Value& arg1, |
| 147 const Value& arg2)); | 148 const Value& arg2)); |
| 148 }; | 149 }; |
| 149 | 150 |
| 150 class CloudPrintURLTest : public testing::Test { | 151 class CloudPrintURLTest : public testing::Test { |
| 151 public: | |
| 152 CloudPrintURLTest() {} | |
| 153 | |
| 154 protected: | 152 protected: |
| 155 virtual void SetUp() { | 153 content::TestBrowserThreadBundle thread_bundle_; |
| 156 profile_.reset(new TestingProfile()); | 154 TestingProfile profile_; |
| 157 } | |
| 158 | |
| 159 scoped_ptr<Profile> profile_; | |
| 160 }; | 155 }; |
| 161 | 156 |
| 162 TEST_F(CloudPrintURLTest, CheckDefaultURLs) { | 157 TEST_F(CloudPrintURLTest, CheckDefaultURLs) { |
| 163 std::string service_url = | 158 std::string service_url = |
| 164 CloudPrintURL(profile_.get()). | 159 CloudPrintURL(&profile_). |
| 165 GetCloudPrintServiceURL().spec(); | 160 GetCloudPrintServiceURL().spec(); |
| 166 EXPECT_THAT(service_url, HasSubstr("www.google.com")); | 161 EXPECT_THAT(service_url, HasSubstr("www.google.com")); |
| 167 EXPECT_THAT(service_url, HasSubstr("cloudprint")); | 162 EXPECT_THAT(service_url, HasSubstr("cloudprint")); |
| 168 | 163 |
| 169 std::string dialog_url = | 164 std::string dialog_url = |
| 170 CloudPrintURL(profile_.get()). | 165 CloudPrintURL(&profile_). |
| 171 GetCloudPrintServiceDialogURL().spec(); | 166 GetCloudPrintServiceDialogURL().spec(); |
| 172 EXPECT_THAT(dialog_url, HasSubstr("www.google.com")); | 167 EXPECT_THAT(dialog_url, HasSubstr("www.google.com")); |
| 173 EXPECT_THAT(dialog_url, HasSubstr("/cloudprint/")); | 168 EXPECT_THAT(dialog_url, HasSubstr("/cloudprint/")); |
| 174 EXPECT_THAT(dialog_url, HasSubstr("/client/")); | 169 EXPECT_THAT(dialog_url, HasSubstr("/client/")); |
| 175 EXPECT_THAT(dialog_url, Not(HasSubstr("cloudprint/cloudprint"))); | 170 EXPECT_THAT(dialog_url, Not(HasSubstr("cloudprint/cloudprint"))); |
| 176 EXPECT_THAT(dialog_url, HasSubstr("/dialog.html")); | 171 EXPECT_THAT(dialog_url, HasSubstr("/dialog.html")); |
| 177 | 172 |
| 178 // Repeat to make sure there isn't a transient glitch. | 173 // Repeat to make sure there isn't a transient glitch. |
| 179 dialog_url = | 174 dialog_url = |
| 180 CloudPrintURL(profile_.get()). | 175 CloudPrintURL(&profile_). |
| 181 GetCloudPrintServiceDialogURL().spec(); | 176 GetCloudPrintServiceDialogURL().spec(); |
| 182 EXPECT_THAT(dialog_url, HasSubstr("www.google.com")); | 177 EXPECT_THAT(dialog_url, HasSubstr("www.google.com")); |
| 183 EXPECT_THAT(dialog_url, HasSubstr("/cloudprint/")); | 178 EXPECT_THAT(dialog_url, HasSubstr("/cloudprint/")); |
| 184 EXPECT_THAT(dialog_url, HasSubstr("/client/")); | 179 EXPECT_THAT(dialog_url, HasSubstr("/client/")); |
| 185 EXPECT_THAT(dialog_url, Not(HasSubstr("cloudprint/cloudprint"))); | 180 EXPECT_THAT(dialog_url, Not(HasSubstr("cloudprint/cloudprint"))); |
| 186 EXPECT_THAT(dialog_url, HasSubstr("/dialog.html")); | 181 EXPECT_THAT(dialog_url, HasSubstr("/dialog.html")); |
| 187 | 182 |
| 188 std::string manage_url = | 183 std::string manage_url = |
| 189 CloudPrintURL(profile_.get()). | 184 CloudPrintURL(&profile_). |
| 190 GetCloudPrintServiceManageURL().spec(); | 185 GetCloudPrintServiceManageURL().spec(); |
| 191 EXPECT_THAT(manage_url, HasSubstr("www.google.com")); | 186 EXPECT_THAT(manage_url, HasSubstr("www.google.com")); |
| 192 EXPECT_THAT(manage_url, HasSubstr("/cloudprint/")); | 187 EXPECT_THAT(manage_url, HasSubstr("/cloudprint/")); |
| 193 EXPECT_THAT(manage_url, Not(HasSubstr("/client/"))); | 188 EXPECT_THAT(manage_url, Not(HasSubstr("/client/"))); |
| 194 EXPECT_THAT(manage_url, Not(HasSubstr("cloudprint/cloudprint"))); | 189 EXPECT_THAT(manage_url, Not(HasSubstr("cloudprint/cloudprint"))); |
| 195 EXPECT_THAT(manage_url, HasSubstr("/manage")); | 190 EXPECT_THAT(manage_url, HasSubstr("/manage")); |
| 196 | 191 |
| 197 GURL learn_more_url = CloudPrintURL::GetCloudPrintLearnMoreURL(); | 192 GURL learn_more_url = CloudPrintURL::GetCloudPrintLearnMoreURL(); |
| 198 std::string learn_more_path = learn_more_url.spec(); | 193 std::string learn_more_path = learn_more_url.spec(); |
| 199 EXPECT_THAT(learn_more_path, HasSubstr("www.google.com")); | 194 EXPECT_THAT(learn_more_path, HasSubstr("www.google.com")); |
| 200 EXPECT_THAT(learn_more_path, HasSubstr("/support/")); | 195 EXPECT_THAT(learn_more_path, HasSubstr("/support/")); |
| 201 EXPECT_THAT(learn_more_path, HasSubstr("/cloudprint")); | 196 EXPECT_THAT(learn_more_path, HasSubstr("/cloudprint")); |
| 202 EXPECT_TRUE(learn_more_url.has_path()); | 197 EXPECT_TRUE(learn_more_url.has_path()); |
| 203 EXPECT_FALSE(learn_more_url.has_query()); | 198 EXPECT_FALSE(learn_more_url.has_query()); |
| 204 | 199 |
| 205 GURL test_page_url = CloudPrintURL::GetCloudPrintTestPageURL(); | 200 GURL test_page_url = CloudPrintURL::GetCloudPrintTestPageURL(); |
| 206 std::string test_page_path = test_page_url.spec(); | 201 std::string test_page_path = test_page_url.spec(); |
| 207 EXPECT_THAT(test_page_path, HasSubstr("www.google.com")); | 202 EXPECT_THAT(test_page_path, HasSubstr("www.google.com")); |
| 208 EXPECT_THAT(test_page_path, HasSubstr("/landing/")); | 203 EXPECT_THAT(test_page_path, HasSubstr("/landing/")); |
| 209 EXPECT_THAT(test_page_path, HasSubstr("/cloudprint/")); | 204 EXPECT_THAT(test_page_path, HasSubstr("/cloudprint/")); |
| 210 EXPECT_TRUE(test_page_url.has_path()); | 205 EXPECT_TRUE(test_page_url.has_path()); |
| 211 EXPECT_TRUE(test_page_url.has_query()); | 206 EXPECT_TRUE(test_page_url.has_query()); |
| 212 } | 207 } |
| 213 | 208 |
| 214 // Testing for CloudPrintDataSender needs a mock WebUI. | 209 // Testing for CloudPrintDataSender needs a mock WebUI. |
| 215 class CloudPrintDataSenderTest : public testing::Test { | 210 class CloudPrintDataSenderTest : public testing::Test { |
| 216 public: | |
| 217 CloudPrintDataSenderTest() | |
| 218 : file_thread_(BrowserThread::FILE, &message_loop_), | |
| 219 io_thread_(BrowserThread::IO, &message_loop_) {} | |
| 220 | |
| 221 protected: | 211 protected: |
| 222 virtual void SetUp() { | 212 virtual void SetUp() { |
| 223 mock_helper_.reset(new MockCloudPrintDataSenderHelper); | 213 mock_helper_.reset(new MockCloudPrintDataSenderHelper); |
| 224 } | 214 } |
| 225 | 215 |
| 226 scoped_refptr<CloudPrintDataSender> CreateSender( | 216 scoped_refptr<CloudPrintDataSender> CreateSender( |
| 227 const base::RefCountedString* data) { | 217 const base::RefCountedString* data) { |
| 228 return new CloudPrintDataSender(mock_helper_.get(), | 218 return new CloudPrintDataSender(mock_helper_.get(), |
| 229 ASCIIToUTF16(kMockJobTitle), | 219 ASCIIToUTF16(kMockJobTitle), |
| 230 ASCIIToUTF16(kMockPrintTicket), | 220 ASCIIToUTF16(kMockPrintTicket), |
| 231 std::string("application/pdf"), | 221 std::string("application/pdf"), |
| 232 data); | 222 data); |
| 233 } | 223 } |
| 234 | 224 |
| 235 scoped_refptr<CloudPrintDataSender> print_data_sender_; | 225 scoped_refptr<CloudPrintDataSender> print_data_sender_; |
| 236 scoped_ptr<MockCloudPrintDataSenderHelper> mock_helper_; | 226 scoped_ptr<MockCloudPrintDataSenderHelper> mock_helper_; |
| 237 | 227 |
| 238 base::MessageLoop message_loop_; | 228 content::TestBrowserThreadBundle thread_bundle_; |
| 239 content::TestBrowserThread file_thread_; | |
| 240 content::TestBrowserThread io_thread_; | |
| 241 }; | 229 }; |
| 242 | 230 |
| 243 TEST_F(CloudPrintDataSenderTest, CanSend) { | 231 TEST_F(CloudPrintDataSenderTest, CanSend) { |
| 244 StringValue mock_job_title(kMockJobTitle); | 232 StringValue mock_job_title(kMockJobTitle); |
| 245 EXPECT_CALL(*mock_helper_, | 233 EXPECT_CALL(*mock_helper_, |
| 246 CallJavascriptFunction(_, _, StringValueEq(&mock_job_title))). | 234 CallJavascriptFunction(_, _, StringValueEq(&mock_job_title))). |
| 247 WillOnce(Return()); | 235 WillOnce(Return()); |
| 248 | 236 |
| 249 std::string data("test_data"); | 237 std::string data("test_data"); |
| 250 scoped_refptr<CloudPrintDataSender> print_data_sender( | 238 scoped_refptr<CloudPrintDataSender> print_data_sender( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 // CloudPrintWebDialogDelegate, mock CloudPrintDataSender, and a mock | 272 // CloudPrintWebDialogDelegate, mock CloudPrintDataSender, and a mock |
| 285 // WebUI. | 273 // WebUI. |
| 286 | 274 |
| 287 // Testing for CloudPrintWebDialogDelegate needs a mock | 275 // Testing for CloudPrintWebDialogDelegate needs a mock |
| 288 // CloudPrintFlowHandler. | 276 // CloudPrintFlowHandler. |
| 289 | 277 |
| 290 using internal_cloud_print_helpers::MockCloudPrintFlowHandler; | 278 using internal_cloud_print_helpers::MockCloudPrintFlowHandler; |
| 291 using internal_cloud_print_helpers::CloudPrintWebDialogDelegate; | 279 using internal_cloud_print_helpers::CloudPrintWebDialogDelegate; |
| 292 | 280 |
| 293 class CloudPrintWebDialogDelegateTest : public testing::Test { | 281 class CloudPrintWebDialogDelegateTest : public testing::Test { |
| 294 public: | |
| 295 CloudPrintWebDialogDelegateTest() | |
| 296 : ui_thread_(BrowserThread::UI, &message_loop_) {} | |
| 297 | |
| 298 protected: | 282 protected: |
| 299 virtual void SetUp() { | 283 virtual void SetUp() { |
| 300 string16 mock_title; | 284 string16 mock_title; |
| 301 string16 mock_print_ticket; | 285 string16 mock_print_ticket; |
| 302 std::string mock_file_type; | 286 std::string mock_file_type; |
| 303 MockCloudPrintFlowHandler* handler = | 287 MockCloudPrintFlowHandler* handler = |
| 304 new MockCloudPrintFlowHandler(mock_print_ticket, mock_title, | 288 new MockCloudPrintFlowHandler(mock_print_ticket, mock_title, |
| 305 mock_file_type, false, base::Closure()); | 289 mock_file_type, false, base::Closure()); |
| 306 mock_flow_handler_ = handler->AsWeakPtr(); | 290 mock_flow_handler_ = handler->AsWeakPtr(); |
| 307 EXPECT_CALL(*mock_flow_handler_.get(), SetDialogDelegate(_)); | 291 EXPECT_CALL(*mock_flow_handler_.get(), SetDialogDelegate(_)); |
| 308 EXPECT_CALL(*mock_flow_handler_.get(), SetDialogDelegate(NULL)); | 292 EXPECT_CALL(*mock_flow_handler_.get(), SetDialogDelegate(NULL)); |
| 309 delegate_.reset(new CloudPrintWebDialogDelegate(mock_flow_handler_.get(), | 293 delegate_.reset(new CloudPrintWebDialogDelegate(mock_flow_handler_.get(), |
| 310 std::string())); | 294 std::string())); |
| 311 } | 295 } |
| 312 | 296 |
| 313 virtual void TearDown() { | 297 virtual void TearDown() { |
| 314 delegate_.reset(); | 298 delegate_.reset(); |
| 315 if (mock_flow_handler_.get()) | 299 if (mock_flow_handler_.get()) |
| 316 delete mock_flow_handler_.get(); | 300 delete mock_flow_handler_.get(); |
| 317 } | 301 } |
| 318 | 302 |
| 319 base::MessageLoopForUI message_loop_; | 303 content::TestBrowserThreadBundle thread_bundle_; |
| 320 content::TestBrowserThread ui_thread_; | |
| 321 base::WeakPtr<MockCloudPrintFlowHandler> mock_flow_handler_; | 304 base::WeakPtr<MockCloudPrintFlowHandler> mock_flow_handler_; |
| 322 scoped_ptr<CloudPrintWebDialogDelegate> delegate_; | 305 scoped_ptr<CloudPrintWebDialogDelegate> delegate_; |
| 323 }; | 306 }; |
| 324 | 307 |
| 325 TEST_F(CloudPrintWebDialogDelegateTest, BasicChecks) { | 308 TEST_F(CloudPrintWebDialogDelegateTest, BasicChecks) { |
| 326 EXPECT_THAT(delegate_->GetDialogContentURL().spec(), | 309 EXPECT_THAT(delegate_->GetDialogContentURL().spec(), |
| 327 StrEq(chrome::kChromeUICloudPrintResourcesURL)); | 310 StrEq(chrome::kChromeUICloudPrintResourcesURL)); |
| 328 EXPECT_TRUE(delegate_->GetDialogTitle().empty()); | 311 EXPECT_TRUE(delegate_->GetDialogTitle().empty()); |
| 329 | 312 |
| 330 bool close_dialog = false; | 313 bool close_dialog = false; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 341 std::vector<WebUIMessageHandler*> handlers; | 324 std::vector<WebUIMessageHandler*> handlers; |
| 342 delegate_->GetWebUIMessageHandlers(&handlers); | 325 delegate_->GetWebUIMessageHandlers(&handlers); |
| 343 delegate_.reset(); | 326 delegate_.reset(); |
| 344 EXPECT_THAT(mock_flow_handler_.get(), NotNull()); | 327 EXPECT_THAT(mock_flow_handler_.get(), NotNull()); |
| 345 } | 328 } |
| 346 | 329 |
| 347 // Testing for ExternalWebDialogUI needs a mock WebContents and mock | 330 // Testing for ExternalWebDialogUI needs a mock WebContents and mock |
| 348 // CloudPrintWebDialogDelegate (attached to the mock web_contents). | 331 // CloudPrintWebDialogDelegate (attached to the mock web_contents). |
| 349 | 332 |
| 350 // Testing for PrintDialogCloud needs a mock Browser. | 333 // Testing for PrintDialogCloud needs a mock Browser. |
| OLD | NEW |