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 ClickStuff(); | |
| 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 enum state { FIRST_MESSAGE_WAIT, SECOND_MESSAGE_WAIT }; | |
|
Lei Zhang
2014/06/16 23:12:40
This can be private. Document what "first message
ivandavid
2014/06/17 00:38:56
I have documented what the enum represents and hav
| |
| 89 UIDoneLoadingMessageHandler(PrintPreviewObserver * observer); | |
| 90 virtual ~UIDoneLoadingMessageHandler(); | |
| 91 void HandleDone(const base::ListValue * /* args */); | |
| 92 void RegisterMessages() OVERRIDE; | |
| 93 private: | |
| 94 PrintPreviewObserver * observer_; | |
| 95 state state_; | |
| 96 | |
| 97 DISALLOW_COPY_AND_ASSIGN(UIDoneLoadingMessageHandler); | |
| 98 }; | |
| 99 | |
| 100 UIDoneLoadingMessageHandler::UIDoneLoadingMessageHandler( | |
| 101 PrintPreviewObserver * observer) : | |
| 102 observer_(observer), state_(FIRST_MESSAGE_WAIT) {} | |
| 103 | |
| 104 UIDoneLoadingMessageHandler::~UIDoneLoadingMessageHandler() { | |
| 105 observer_ = NULL; | |
| 106 } | |
| 107 | |
| 108 void UIDoneLoadingMessageHandler::RegisterMessages() OVERRIDE { | |
| 109 web_ui()->RegisterMessageCallback( | |
| 110 "UILoaded", | |
| 111 base::Bind(&UIDoneLoadingMessageHandler::HandleDone, | |
| 112 base::Unretained(this))); | |
| 113 } | |
| 114 | |
| 115 void UIDoneLoadingMessageHandler::HandleDone( | |
| 116 const base::ListValue * /* args */) { | |
| 117 if(state_ == FIRST_MESSAGE_WAIT) { | |
| 118 state_ = SECOND_MESSAGE_WAIT; | |
| 119 /* send message to print_preview.js */ | |
| 120 observer_->ClickStuff(); | |
| 121 } | |
| 122 else { | |
| 123 observer_->EndLoop(); | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 PrintPreviewObserver::PrintPreviewObserver( | |
| 128 WebContents * dialog, | |
| 129 Browser * browser) : | |
| 130 WebContentsObserver(dialog), browser_(browser), count_(0) {} | |
| 131 | |
| 132 PrintPreviewObserver::~PrintPreviewObserver() { | |
| 133 browser_ = NULL; | |
| 134 } | |
| 135 | |
| 136 void PrintPreviewObserver::set_quit_closure(const base::Closure &closure) { | |
| 137 closure_ = closure; | |
| 138 } | |
| 139 | |
| 140 void PrintPreviewObserver::EndLoop() { | |
| 141 base::MessageLoop::current()->PostTask(FROM_HERE, closure_); | |
| 142 } | |
| 143 | |
| 144 bool PrintPreviewObserver::OnMessageReceived(const IPC::Message& message) | |
| 145 OVERRIDE { | |
| 146 IPC_BEGIN_MESSAGE_MAP(PrintPreviewObserver, message) | |
| 147 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, | |
| 148 OnDidGetPreviewPageCount) | |
| 149 IPC_MESSAGE_UNHANDLED(break;) | |
| 150 IPC_END_MESSAGE_MAP(); | |
| 151 return false; | |
| 152 } | |
| 153 | |
| 154 void PrintPreviewObserver::OnDidGetPreviewPageCount( | |
| 155 const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms) { | |
| 156 | |
| 157 WebContents * tab = browser_->tab_strip_model()->GetActiveWebContents(); | |
| 158 | |
| 159 ASSERT_TRUE(tab); | |
| 160 | |
| 161 printing::PrintPreviewDialogController * dialog_controller = | |
| 162 printing::PrintPreviewDialogController::GetInstance(); | |
| 163 | |
| 164 ASSERT_TRUE(dialog_controller); | |
| 165 | |
| 166 web_contents_ = dialog_controller->GetPrintPreviewForContents(tab); | |
| 167 | |
| 168 ASSERT_TRUE(web_contents_ && printing::PrintPreviewDialogController:: | |
| 169 IsPrintPreviewDialog(web_contents_)); | |
| 170 | |
| 171 ui_ = static_cast<PrintPreviewUI *>(web_contents_->GetWebUI()-> | |
| 172 GetController()); | |
| 173 | |
| 174 ASSERT_TRUE(ui_); | |
| 175 | |
| 176 ASSERT_TRUE(ui_->web_ui()); | |
| 177 | |
| 178 this->Observe(web_contents_); | |
| 179 | |
| 180 UIDoneLoadingMessageHandler * handler = new UIDoneLoadingMessageHandler(this); | |
| 181 | |
| 182 ui_->web_ui()->AddMessageHandler(handler); | |
| 183 } | |
| 184 | |
| 185 void PrintPreviewObserver::ClickStuff() { | |
|
Lei Zhang
2014/06/16 23:12:40
Please give this a better name.
ivandavid
2014/06/17 00:38:56
Renamed to 'onManipulateSettings' since it describ
| |
| 186 ui_->web_ui()->CallJavascriptFunction("onClickStuff"); | |
| 187 } | |
| 188 | |
| 189 void PrintPreviewObserver::DidCloneToNewWebContents( | |
| 190 WebContents * old_web_contents, | |
| 191 WebContents * new_web_contents) | |
| 192 OVERRIDE { | |
| 193 this->Observe(new_web_contents); | |
| 194 } | |
| 195 | |
| 196 void PrintPreviewObserver::WebContentsDestroyed() { | |
| 197 this->EndLoop(); | |
| 198 } | |
| 199 | |
| 200 class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { | |
| 201 public: | |
| 202 PrintPreviewPdfGeneratedBrowserTest() {} | |
| 203 virtual ~PrintPreviewPdfGeneratedBrowserTest() {} | |
| 204 | |
| 205 void NavigateAndPreview(std::string file_name) { | |
| 206 base::FilePath directory("printing"); | |
| 207 base::FilePath file(file_name); | |
| 208 | |
| 209 ui_test_utils::NavigateToURL(browser(), | |
| 210 ui_test_utils::GetTestUrl(directory, file)); | |
| 211 | |
| 212 base::RunLoop loop; | |
| 213 print_preview_observer_.get()->set_quit_closure(loop.QuitClosure()); | |
|
Lei Zhang
2014/06/16 23:12:40
You don't need the .get(), here and below.
ivandavid
2014/06/17 00:38:56
Removed the .get()'s.
| |
| 214 chrome::Print(browser()); | |
| 215 loop.Run(); | |
| 216 } | |
| 217 | |
| 218 void Print() { | |
| 219 base::FilePath test_dir; | |
|
Lei Zhang
2014/06/16 23:12:40
This probably shouldn't be named |test_dir|. On li
ivandavid
2014/06/17 00:38:56
Changed |test_dir| to |pdf_file_path|
Lei Zhang
2014/06/17 00:53:26
How about |pdf_file_save_path| ?
| |
| 220 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir)); | |
| 221 test_dir = test_dir.AppendASCII("printing"); | |
| 222 test_dir = test_dir.AppendASCII("dummy.pdf"); | |
| 223 | |
| 224 base::RunLoop loop; | |
| 225 print_preview_observer_.get()->set_quit_closure(loop.QuitClosure()); | |
| 226 | |
| 227 print_preview_observer_.get()->GetUI()->handler_-> | |
| 228 FileSelected(test_dir, 0, NULL); | |
| 229 | |
| 230 loop.Run(); | |
| 231 } | |
| 232 | |
| 233 private: | |
| 234 virtual void SetUpOnMainThread() OVERRIDE { | |
| 235 WebContents * tab = | |
| 236 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 237 ASSERT_TRUE(tab); | |
| 238 | |
| 239 print_preview_observer_.reset(new PrintPreviewObserver(tab, browser())); | |
| 240 chrome::DuplicateTab(browser()); | |
| 241 | |
| 242 WebContents * initiator_ = | |
| 243 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 244 ASSERT_TRUE(initiator_); | |
| 245 ASSERT_NE(tab, initiator_); | |
| 246 } | |
| 247 | |
| 248 virtual void CleanUpOnMainThread() OVERRIDE { | |
| 249 print_preview_observer_.reset(); | |
| 250 } | |
| 251 | |
| 252 scoped_ptr<PrintPreviewObserver> print_preview_observer_; | |
| 253 | |
| 254 DISALLOW_COPY_AND_ASSIGN(PrintPreviewPdfGeneratedBrowserTest); | |
| 255 }; | |
| 256 | |
| 257 IN_PROC_BROWSER_TEST_F(PrintPreviewPdfGeneratedBrowserTest, DummyTest) { | |
| 258 NavigateAndPreview("test2.html"); | |
| 259 Print(); | |
| 260 } | |
| OLD | NEW |