Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 7 #include "base/files/file_path.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/path_service.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "chrome/app/chrome_command_ids.h" | |
| 14 #include "chrome/browser/net/referrer.h" | |
| 15 #include "chrome/browser/printing/print_preview_dialog_controller.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "chrome/browser/ui/browser.h" | |
| 18 #include "chrome/browser/ui/browser_commands.h" | |
| 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 20 #include "chrome/browser/ui/webui/print_preview/print_preview_handler.h" | |
| 21 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | |
| 22 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" | |
| 23 #include "chrome/common/chrome_paths.h" | |
| 24 #include "chrome/common/print_messages.h" | |
| 25 #include "chrome/common/url_constants.h" | |
| 26 #include "chrome/test/base/in_process_browser_test.h" | |
| 27 #include "chrome/test/base/ui_test_utils.h" | |
| 28 #include "content/public/browser/web_contents.h" | |
| 29 #include "content/public/browser/web_ui_message_handler.h" | |
| 30 #include "content/public/common/page_transition_types.h" | |
| 31 #include "content/public/test/browser_test_utils.h" | |
| 32 #include "content/public/test/test_navigation_observer.h" | |
| 33 #include "content/public/test/test_utils.h" | |
| 34 #include "ui/events/keycodes/keyboard_codes.h" | |
| 35 #include "url/gurl.h" | |
| 36 #include "ipc/ipc_message_macros.h" | |
| 37 | |
| 38 using content::WebContents; | |
| 39 using content::WebContentsObserver; | |
| 40 | |
| 41 class PrintPreviewObserver; | |
| 42 class UIDoneLoadingMessageHandler; | |
| 43 | |
| 44 /* Need to split declarations & definitions due to forward declaration problem | |
| 45 */ | |
| 46 | |
| 47 class PrintPreviewObserver : public WebContentsObserver { | |
| 48 public: | |
| 49 explicit PrintPreviewObserver(WebContents * dialog, Browser * browser); | |
| 50 virtual ~PrintPreviewObserver(); | |
| 51 | |
| 52 void set_quit_closure(const base::Closure &closure); | |
| 53 void EndLoop(); | |
| 54 bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
| 55 PrintPreviewUI * GetUI() { | |
| 56 return ui_; | |
| 57 } | |
| 58 | |
| 59 WebContents * GetPreviewContents() { | |
| 60 return web_contents_; | |
| 61 } | |
| 62 | |
| 63 void ManipulatePreviewSettings(); | |
|
Lei Zhang
2014/06/17 00:53:26
Add a comment to explain what this does.
| |
| 64 | |
| 65 private: | |
| 66 void OnDidGetPreviewPageCount( | |
| 67 const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms); | |
| 68 | |
| 69 void DidCloneToNewWebContents(WebContents * old_web_contents, | |
| 70 WebContents * new_web_contents) | |
| 71 OVERRIDE; | |
| 72 | |
| 73 void WebContentsDestroyed(); | |
| 74 | |
| 75 Browser * browser_; | |
| 76 base::Closure closure_; | |
| 77 WebContents * web_contents_; | |
| 78 | |
| 79 PrintPreviewUI * ui_; | |
| 80 | |
| 81 int count_; | |
| 82 | |
| 83 DISALLOW_COPY_AND_ASSIGN(PrintPreviewObserver); | |
| 84 }; | |
| 85 | |
| 86 class UIDoneLoadingMessageHandler : public content::WebUIMessageHandler { | |
| 87 public: | |
| 88 explicit UIDoneLoadingMessageHandler(PrintPreviewObserver * observer); | |
|
Lei Zhang
2014/06/17 00:53:26
nit: (all over) "Foo* foo", not "Foo * foo"
| |
| 89 virtual ~UIDoneLoadingMessageHandler(); | |
| 90 void HandleDone(const base::ListValue * /* args */); | |
| 91 void RegisterMessages() OVERRIDE; | |
| 92 private: | |
| 93 enum state { WAITING_FOR_FIRST_MESSAGE, WAITING_FOR_SECOND_MESSAGE }; | |
|
Lei Zhang
2014/06/17 00:53:26
Let's format it this way:
enum State {
// Initi
| |
| 94 //After receiving the first message, the handler | |
| 95 //tells the observer to send the desired print settings to | |
| 96 //print_preview.js. After the second one, the pdf is saved | |
| 97 | |
| 98 PrintPreviewObserver * observer_; | |
| 99 state state_; | |
| 100 | |
| 101 DISALLOW_COPY_AND_ASSIGN(UIDoneLoadingMessageHandler); | |
| 102 }; | |
| 103 | |
| 104 UIDoneLoadingMessageHandler::UIDoneLoadingMessageHandler( | |
| 105 PrintPreviewObserver * observer) : | |
| 106 observer_(observer), state_(WAITING_FOR_FIRST_MESSAGE) {} | |
| 107 | |
| 108 UIDoneLoadingMessageHandler::~UIDoneLoadingMessageHandler() { | |
| 109 observer_ = NULL; | |
| 110 } | |
| 111 | |
| 112 void UIDoneLoadingMessageHandler::RegisterMessages() OVERRIDE { | |
| 113 web_ui()->RegisterMessageCallback( | |
| 114 "UILoaded", | |
| 115 base::Bind(&UIDoneLoadingMessageHandler::HandleDone, | |
| 116 base::Unretained(this))); | |
| 117 } | |
| 118 | |
| 119 void UIDoneLoadingMessageHandler::HandleDone( | |
| 120 const base::ListValue * /* args */) { | |
| 121 if (state_ == WAITING_FOR_FIRST_MESSAGE) { | |
| 122 state_ = WAITING_FOR_SECOND_MESSAGE; | |
| 123 /* send message to print_preview.js */ | |
| 124 observer_->ManipulatePreviewSettings(); | |
| 125 } else { | |
| 126 observer_->EndLoop(); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 PrintPreviewObserver::PrintPreviewObserver( | |
| 131 WebContents * dialog, | |
| 132 Browser * browser) : | |
| 133 WebContentsObserver(dialog), browser_(browser), count_(0) {} | |
| 134 | |
| 135 PrintPreviewObserver::~PrintPreviewObserver() { | |
| 136 browser_ = NULL; | |
| 137 } | |
| 138 | |
| 139 void PrintPreviewObserver::set_quit_closure(const base::Closure &closure) { | |
| 140 closure_ = closure; | |
| 141 } | |
| 142 | |
| 143 void PrintPreviewObserver::EndLoop() { | |
| 144 base::MessageLoop::current()->PostTask(FROM_HERE, closure_); | |
| 145 } | |
| 146 | |
| 147 bool PrintPreviewObserver::OnMessageReceived(const IPC::Message& message) | |
| 148 OVERRIDE { | |
| 149 IPC_BEGIN_MESSAGE_MAP(PrintPreviewObserver, message) | |
| 150 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, | |
| 151 OnDidGetPreviewPageCount) | |
| 152 IPC_MESSAGE_UNHANDLED(break;) | |
| 153 IPC_END_MESSAGE_MAP(); | |
| 154 return false; | |
| 155 } | |
| 156 | |
| 157 void PrintPreviewObserver::OnDidGetPreviewPageCount( | |
| 158 const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms) { | |
| 159 | |
| 160 WebContents * tab = browser_->tab_strip_model()->GetActiveWebContents(); | |
| 161 | |
| 162 ASSERT_TRUE(tab); | |
| 163 | |
| 164 printing::PrintPreviewDialogController * dialog_controller = | |
| 165 printing::PrintPreviewDialogController::GetInstance(); | |
| 166 | |
| 167 ASSERT_TRUE(dialog_controller); | |
| 168 | |
| 169 web_contents_ = dialog_controller->GetPrintPreviewForContents(tab); | |
| 170 | |
| 171 ASSERT_TRUE(web_contents_ && printing::PrintPreviewDialogController:: | |
| 172 IsPrintPreviewDialog(web_contents_)); | |
| 173 | |
| 174 ui_ = static_cast<PrintPreviewUI *>(web_contents_->GetWebUI()-> | |
| 175 GetController()); | |
| 176 | |
| 177 ASSERT_TRUE(ui_); | |
| 178 | |
| 179 ASSERT_TRUE(ui_->web_ui()); | |
| 180 | |
| 181 this->Observe(web_contents_); | |
| 182 | |
| 183 UIDoneLoadingMessageHandler * handler = new UIDoneLoadingMessageHandler(this); | |
| 184 | |
| 185 ui_->web_ui()->AddMessageHandler(handler); | |
| 186 } | |
| 187 | |
| 188 void PrintPreviewObserver::ManipulatePreviewSettings() { | |
| 189 ui_->web_ui()->CallJavascriptFunction("onManipulateSettings"); | |
| 190 } | |
| 191 | |
| 192 void PrintPreviewObserver::DidCloneToNewWebContents( | |
| 193 WebContents * old_web_contents, | |
| 194 WebContents * new_web_contents) | |
| 195 OVERRIDE { | |
| 196 this->Observe(new_web_contents); | |
| 197 } | |
| 198 | |
| 199 void PrintPreviewObserver::WebContentsDestroyed() { | |
| 200 this->EndLoop(); | |
| 201 } | |
| 202 | |
| 203 class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { | |
| 204 public: | |
| 205 PrintPreviewPdfGeneratedBrowserTest() {} | |
| 206 virtual ~PrintPreviewPdfGeneratedBrowserTest() {} | |
| 207 | |
| 208 void NavigateAndPreview(std::string file_name) { | |
| 209 base::FilePath directory("printing"); | |
| 210 base::FilePath file(file_name); | |
| 211 | |
| 212 ui_test_utils::NavigateToURL(browser(), | |
| 213 ui_test_utils::GetTestUrl(directory, file)); | |
| 214 | |
| 215 base::RunLoop loop; | |
| 216 print_preview_observer_->set_quit_closure(loop.QuitClosure()); | |
| 217 chrome::Print(browser()); | |
| 218 loop.Run(); | |
| 219 } | |
| 220 | |
| 221 void Print() { | |
| 222 base::FilePath pdf_file_path; | |
| 223 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &pdf_file_path)); | |
| 224 pdf_file_path = pdf_file_path.AppendASCII("printing"); | |
| 225 pdf_file_path = pdf_file_path.AppendASCII("dummy.pdf"); | |
|
Lei Zhang
2014/06/17 00:53:26
Actually, you don't want to litter DIR_TEST_DATA w
| |
| 226 | |
| 227 base::RunLoop loop; | |
| 228 print_preview_observer_->set_quit_closure(loop.QuitClosure()); | |
| 229 | |
| 230 print_preview_observer_->GetUI()->handler_-> | |
| 231 FileSelected(pdf_file_path, 0, NULL); | |
| 232 | |
| 233 loop.Run(); | |
| 234 } | |
| 235 | |
| 236 private: | |
| 237 virtual void SetUpOnMainThread() OVERRIDE { | |
| 238 WebContents * tab = | |
| 239 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 240 ASSERT_TRUE(tab); | |
| 241 | |
| 242 print_preview_observer_.reset(new PrintPreviewObserver(tab, browser())); | |
| 243 chrome::DuplicateTab(browser()); | |
| 244 | |
| 245 WebContents * initiator_ = | |
| 246 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 247 ASSERT_TRUE(initiator_); | |
| 248 ASSERT_NE(tab, initiator_); | |
| 249 } | |
| 250 | |
| 251 virtual void CleanUpOnMainThread() OVERRIDE { | |
| 252 print_preview_observer_.reset(); | |
| 253 } | |
| 254 | |
| 255 scoped_ptr<PrintPreviewObserver> print_preview_observer_; | |
| 256 | |
| 257 DISALLOW_COPY_AND_ASSIGN(PrintPreviewPdfGeneratedBrowserTest); | |
| 258 }; | |
| 259 | |
| 260 IN_PROC_BROWSER_TEST_F(PrintPreviewPdfGeneratedBrowserTest, DummyTest) { | |
| 261 NavigateAndPreview("test2.html"); | |
| 262 Print(); | |
| 263 } | |
| OLD | NEW |