| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <fstream> | 6 #include <fstream> |
| 7 #include <iostream> | 7 #include <iostream> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 public: | 111 public: |
| 112 PrintPreviewObserver(Browser* browser, | 112 PrintPreviewObserver(Browser* browser, |
| 113 WebContents* dialog, | 113 WebContents* dialog, |
| 114 const base::FilePath& pdf_file_save_path) | 114 const base::FilePath& pdf_file_save_path) |
| 115 : WebContentsObserver(dialog), | 115 : WebContentsObserver(dialog), |
| 116 browser_(browser), | 116 browser_(browser), |
| 117 state_(kWaitingToSendSaveAsPdf), | 117 state_(kWaitingToSendSaveAsPdf), |
| 118 failed_setting_("None"), | 118 failed_setting_("None"), |
| 119 pdf_file_save_path_(pdf_file_save_path) {} | 119 pdf_file_save_path_(pdf_file_save_path) {} |
| 120 | 120 |
| 121 virtual ~PrintPreviewObserver() {} | 121 ~PrintPreviewObserver() override {} |
| 122 | 122 |
| 123 // Sets closure for the observer so that it can end the loop. | 123 // Sets closure for the observer so that it can end the loop. |
| 124 void set_quit_closure(const base::Closure &closure) { | 124 void set_quit_closure(const base::Closure &closure) { |
| 125 quit_closure_ = closure; | 125 quit_closure_ = closure; |
| 126 } | 126 } |
| 127 | 127 |
| 128 // Actually stops the message loop so that the test can proceed. | 128 // Actually stops the message loop so that the test can proceed. |
| 129 void EndLoop() { | 129 void EndLoop() { |
| 130 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_); | 130 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_); |
| 131 } | 131 } |
| 132 | 132 |
| 133 virtual bool OnMessageReceived(const IPC::Message& message) override { | 133 bool OnMessageReceived(const IPC::Message& message) override { |
| 134 IPC_BEGIN_MESSAGE_MAP(PrintPreviewObserver, message) | 134 IPC_BEGIN_MESSAGE_MAP(PrintPreviewObserver, message) |
| 135 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, | 135 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, |
| 136 OnDidGetPreviewPageCount) | 136 OnDidGetPreviewPageCount) |
| 137 IPC_END_MESSAGE_MAP(); | 137 IPC_END_MESSAGE_MAP(); |
| 138 return false; | 138 return false; |
| 139 } | 139 } |
| 140 | 140 |
| 141 // Gets the web contents for the print preview dialog so that the UI and | 141 // Gets the web contents for the print preview dialog so that the UI and |
| 142 // other elements can be accessed. | 142 // other elements can be accessed. |
| 143 WebContents* GetDialog() { | 143 WebContents* GetDialog() { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 } | 215 } |
| 216 | 216 |
| 217 private: | 217 private: |
| 218 // Listens for messages from the print preview dialog. Specifically, it | 218 // Listens for messages from the print preview dialog. Specifically, it |
| 219 // listens for 'UILoadedForTest' and 'UIFailedLoadingForTest.' | 219 // listens for 'UILoadedForTest' and 'UIFailedLoadingForTest.' |
| 220 class UIDoneLoadingMessageHandler : public content::WebUIMessageHandler { | 220 class UIDoneLoadingMessageHandler : public content::WebUIMessageHandler { |
| 221 public: | 221 public: |
| 222 explicit UIDoneLoadingMessageHandler(PrintPreviewObserver* observer) | 222 explicit UIDoneLoadingMessageHandler(PrintPreviewObserver* observer) |
| 223 : observer_(observer) {} | 223 : observer_(observer) {} |
| 224 | 224 |
| 225 virtual ~UIDoneLoadingMessageHandler() {} | 225 ~UIDoneLoadingMessageHandler() override {} |
| 226 | 226 |
| 227 // When a setting has been set succesfully, this is called and the observer | 227 // When a setting has been set succesfully, this is called and the observer |
| 228 // is told to send the next setting to be set. | 228 // is told to send the next setting to be set. |
| 229 void HandleDone(const base::ListValue* /* args */) { | 229 void HandleDone(const base::ListValue* /* args */) { |
| 230 observer_->ManipulatePreviewSettings(); | 230 observer_->ManipulatePreviewSettings(); |
| 231 } | 231 } |
| 232 | 232 |
| 233 // Ends the test because a setting was not set successfully. Called when | 233 // Ends the test because a setting was not set successfully. Called when |
| 234 // this class hears 'UIFailedLoadingForTest.' | 234 // this class hears 'UIFailedLoadingForTest.' |
| 235 void HandleFailure(const base::ListValue* /* args */) { | 235 void HandleFailure(const base::ListValue* /* args */) { |
| 236 FAIL() << "Failed to set: " << observer_->GetFailedSetting(); | 236 FAIL() << "Failed to set: " << observer_->GetFailedSetting(); |
| 237 } | 237 } |
| 238 | 238 |
| 239 // Allows this class to listen for the 'UILoadedForTest' and | 239 // Allows this class to listen for the 'UILoadedForTest' and |
| 240 // 'UIFailedLoadingForTest' messages. These messages are sent by the print | 240 // 'UIFailedLoadingForTest' messages. These messages are sent by the print |
| 241 // preview dialog. 'UILoadedForTest' is sent when a setting has been | 241 // preview dialog. 'UILoadedForTest' is sent when a setting has been |
| 242 // successfully set and its effects have been finalized. | 242 // successfully set and its effects have been finalized. |
| 243 // 'UIFailedLoadingForTest' is sent when the setting could not be set. This | 243 // 'UIFailedLoadingForTest' is sent when the setting could not be set. This |
| 244 // causes the browser test to fail. | 244 // causes the browser test to fail. |
| 245 virtual void RegisterMessages() override { | 245 void RegisterMessages() override { |
| 246 web_ui()->RegisterMessageCallback( | 246 web_ui()->RegisterMessageCallback( |
| 247 "UILoadedForTest", | 247 "UILoadedForTest", |
| 248 base::Bind(&UIDoneLoadingMessageHandler::HandleDone, | 248 base::Bind(&UIDoneLoadingMessageHandler::HandleDone, |
| 249 base::Unretained(this))); | 249 base::Unretained(this))); |
| 250 | 250 |
| 251 web_ui()->RegisterMessageCallback( | 251 web_ui()->RegisterMessageCallback( |
| 252 "UIFailedLoadingForTest", | 252 "UIFailedLoadingForTest", |
| 253 base::Bind(&UIDoneLoadingMessageHandler::HandleFailure, | 253 base::Bind(&UIDoneLoadingMessageHandler::HandleFailure, |
| 254 base::Unretained(this))); | 254 base::Unretained(this))); |
| 255 } | 255 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 270 | 270 |
| 271 PrintPreviewUI* ui = GetUI(); | 271 PrintPreviewUI* ui = GetUI(); |
| 272 ASSERT_TRUE(ui); | 272 ASSERT_TRUE(ui); |
| 273 ASSERT_TRUE(ui->web_ui()); | 273 ASSERT_TRUE(ui->web_ui()); |
| 274 | 274 |
| 275 // The |ui->web_ui()| owns the message handler. | 275 // The |ui->web_ui()| owns the message handler. |
| 276 ui->web_ui()->AddMessageHandler(new UIDoneLoadingMessageHandler(this)); | 276 ui->web_ui()->AddMessageHandler(new UIDoneLoadingMessageHandler(this)); |
| 277 ui->web_ui()->CallJavascriptFunction("onEnableManipulateSettingsForTest"); | 277 ui->web_ui()->CallJavascriptFunction("onEnableManipulateSettingsForTest"); |
| 278 } | 278 } |
| 279 | 279 |
| 280 virtual void DidCloneToNewWebContents( | 280 void DidCloneToNewWebContents(WebContents* old_web_contents, |
| 281 WebContents* old_web_contents, | 281 WebContents* new_web_contents) override { |
| 282 WebContents* new_web_contents) override { | |
| 283 Observe(new_web_contents); | 282 Observe(new_web_contents); |
| 284 } | 283 } |
| 285 | 284 |
| 286 Browser* browser_; | 285 Browser* browser_; |
| 287 base::Closure quit_closure_; | 286 base::Closure quit_closure_; |
| 288 scoped_ptr<PrintPreviewSettings> settings_; | 287 scoped_ptr<PrintPreviewSettings> settings_; |
| 289 | 288 |
| 290 // State of the observer. The state indicates what message to send | 289 // State of the observer. The state indicates what message to send |
| 291 // next. The state advances whenever the message handler calls | 290 // next. The state advances whenever the message handler calls |
| 292 // ManipulatePreviewSettings() on the observer. | 291 // ManipulatePreviewSettings() on the observer. |
| (...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 // waiting for this message and start waiting for the image data. | 693 // waiting for this message and start waiting for the image data. |
| 695 std::cout << "#EOF\n"; | 694 std::cout << "#EOF\n"; |
| 696 std::cout.flush(); | 695 std::cout.flush(); |
| 697 | 696 |
| 698 SendPng(); | 697 SendPng(); |
| 699 Reset(); | 698 Reset(); |
| 700 } | 699 } |
| 701 } | 700 } |
| 702 | 701 |
| 703 } // namespace printing | 702 } // namespace printing |
| OLD | NEW |