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/print_messages.h" | |
24 #include "chrome/common/url_constants.h" | |
25 #include "chrome/test/base/in_process_browser_test.h" | |
26 #include "chrome/test/base/ui_test_utils.h" | |
27 #include "content/public/browser/web_contents.h" | |
28 #include "content/public/browser/web_ui_message_handler.h" | |
29 #include "content/public/common/page_transition_types.h" | |
30 #include "content/public/test/browser_test_utils.h" | |
31 #include "content/public/test/test_navigation_observer.h" | |
32 #include "content/public/test/test_utils.h" | |
33 #include "ui/events/keycodes/keyboard_codes.h" | |
34 #include "url/gurl.h" | |
35 #include "ipc/ipc_message_macros.h" | |
36 | |
37 using content::WebContents; | |
38 using content::WebContentsObserver; | |
39 | |
40 class PrintPreviewObserver; | |
41 class UIDoneLoadingMessageHandler; | |
42 | |
43 /* Need to split declarations & definitions due to forward declaration problem | |
44 */ | |
45 | |
46 class PrintPreviewObserver : public WebContentsObserver { | |
47 public: | |
48 explicit PrintPreviewObserver(WebContents * dialog, Browser * browser); | |
49 virtual ~PrintPreviewObserver(); | |
50 | |
51 void set_quit_closure(const base::Closure &closure); | |
52 void EndLoop(); | |
53 bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
54 PrintPreviewUI * GetUI() { | |
55 return ui_; | |
56 } | |
57 | |
58 WebContents * GetPreviewContents() { | |
59 return web_contents_; | |
60 } | |
61 | |
62 private: | |
63 void OnDidGetPreviewPageCount( | |
64 const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms); | |
65 | |
66 void DidCloneToNewWebContents(WebContents * old_web_contents, | |
67 WebContents * new_web_contents) | |
68 OVERRIDE; | |
69 | |
70 void WebContentsDestroyed(); | |
71 | |
72 Browser * browser_; | |
73 base::Closure closure_; | |
74 WebContents * web_contents_; | |
75 | |
76 PrintPreviewUI * ui_; | |
77 | |
78 int count_; | |
79 | |
80 DISALLOW_COPY_AND_ASSIGN(PrintPreviewObserver); | |
81 }; | |
82 | |
83 class UIDoneLoadingMessageHandler : public content::WebUIMessageHandler { | |
84 public: | |
85 UIDoneLoadingMessageHandler(PrintPreviewObserver * observer); | |
86 virtual ~UIDoneLoadingMessageHandler(); | |
87 void HandleDone(const base::ListValue * /* args */); | |
88 void RegisterMessages() OVERRIDE; | |
89 private: | |
90 PrintPreviewObserver * observer_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(UIDoneLoadingMessageHandler); | |
93 }; | |
94 | |
95 UIDoneLoadingMessageHandler::UIDoneLoadingMessageHandler( | |
96 PrintPreviewObserver * observer) : | |
97 observer_(observer) {} | |
98 | |
99 UIDoneLoadingMessageHandler::~UIDoneLoadingMessageHandler() { | |
100 observer_ = NULL; | |
101 } | |
102 | |
103 void UIDoneLoadingMessageHandler::RegisterMessages() OVERRIDE { | |
104 web_ui()->RegisterMessageCallback( | |
105 "UILoaded", | |
106 base::Bind(&UIDoneLoadingMessageHandler::HandleDone, | |
107 base::Unretained(this))); | |
108 } | |
109 | |
110 void UIDoneLoadingMessageHandler::HandleDone( | |
111 const base::ListValue * /* args */) { | |
112 observer_->EndLoop(); | |
113 } | |
114 | |
115 PrintPreviewObserver::PrintPreviewObserver( | |
116 WebContents * dialog, | |
117 Browser * browser) : | |
118 WebContentsObserver(dialog), browser_(browser), count_(0) {} | |
119 | |
120 PrintPreviewObserver::~PrintPreviewObserver() { | |
121 browser_ = NULL; | |
122 } | |
123 | |
124 void PrintPreviewObserver::set_quit_closure(const base::Closure &closure) { | |
125 closure_ = closure; | |
126 } | |
127 | |
128 void PrintPreviewObserver::EndLoop() { | |
129 base::MessageLoop::current()->PostTask(FROM_HERE, closure_); | |
130 } | |
131 | |
132 bool PrintPreviewObserver::OnMessageReceived(const IPC::Message& message) | |
133 OVERRIDE { | |
134 IPC_BEGIN_MESSAGE_MAP(PrintPreviewObserver, message) | |
135 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, | |
136 OnDidGetPreviewPageCount) | |
137 IPC_MESSAGE_UNHANDLED(break;) | |
138 IPC_END_MESSAGE_MAP(); | |
139 return false; | |
140 } | |
141 | |
142 void PrintPreviewObserver::OnDidGetPreviewPageCount( | |
143 const PrintHostMsg_DidGetPreviewPageCount_Params ¶ms) { | |
144 | |
145 WebContents * tab = browser_->tab_strip_model()->GetActiveWebContents(); | |
146 | |
147 ASSERT_TRUE(tab); | |
148 | |
149 printing::PrintPreviewDialogController * dialog_controller = | |
150 printing::PrintPreviewDialogController::GetInstance(); | |
151 | |
152 ASSERT_TRUE(dialog_controller); | |
153 | |
154 web_contents_ = dialog_controller->GetPrintPreviewForContents(tab); | |
155 | |
156 ASSERT_TRUE(web_contents_ && printing::PrintPreviewDialogController:: | |
157 IsPrintPreviewDialog(web_contents_)); | |
158 | |
159 ui_ = static_cast<PrintPreviewUI *>(web_contents_->GetWebUI()-> | |
160 GetController()); | |
161 | |
162 ASSERT_TRUE(ui_); | |
163 | |
164 ASSERT_TRUE(ui_->web_ui()); | |
165 | |
166 this->Observe(web_contents_); | |
167 | |
168 UIDoneLoadingMessageHandler * handler = new UIDoneLoadingMessageHandler(this); | |
169 | |
170 ui_->web_ui()->AddMessageHandler(handler); | |
171 } | |
172 | |
173 void PrintPreviewObserver::DidCloneToNewWebContents( | |
174 WebContents * old_web_contents, | |
175 WebContents * new_web_contents) | |
176 OVERRIDE { | |
177 this->Observe(new_web_contents); | |
178 } | |
179 | |
180 void PrintPreviewObserver::WebContentsDestroyed() { | |
181 this->EndLoop(); | |
182 } | |
183 | |
184 class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { | |
185 public: | |
186 PrintPreviewPdfGeneratedBrowserTest() {} | |
187 virtual ~PrintPreviewPdfGeneratedBrowserTest() {} | |
188 | |
189 void NavigateAndPreview(std::string file_name) { | |
190 base::FilePath directory("printing"); | |
191 base::FilePath file(file_name); | |
192 | |
193 ui_test_utils::NavigateToURL(browser(), | |
194 ui_test_utils::GetTestUrl(directory, file)); | |
195 | |
196 base::RunLoop loop; | |
197 print_preview_observer_.get()->set_quit_closure(loop.QuitClosure()); | |
198 chrome::Print(browser()); | |
199 loop.Run(); | |
200 } | |
201 | |
202 void Print() { | |
203 base::FilePath test_dir; | |
204 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &test_dir)); | |
Lei Zhang
2014/06/13 22:20:11
You probably want chrome::DIR_TEST_DATA and not ba
| |
205 test_dir = test_dir.AppendASCII("printing"); | |
206 test_dir = test_dir.AppendASCII("dummy.pdf"); | |
207 | |
208 base::RunLoop loop; | |
209 print_preview_observer_.get()->set_quit_closure(loop.QuitClosure()); | |
210 | |
211 print_preview_observer_.get()->GetUI()->handler_->print_to_pdf_path_ = | |
Lei Zhang
2014/06/13 22:20:11
Maybe just call PrintPreviewHandler::FileSelected(
| |
212 test_dir; | |
213 | |
214 print_preview_observer_.get()->GetUI()->handler_->PrintToPdf(); | |
215 | |
216 loop.Run(); | |
217 } | |
218 | |
219 private: | |
220 virtual void SetUpOnMainThread() OVERRIDE { | |
221 WebContents * tab = | |
222 browser()->tab_strip_model()->GetActiveWebContents(); | |
223 ASSERT_TRUE(tab); | |
224 | |
225 print_preview_observer_.reset(new PrintPreviewObserver(tab, browser())); | |
226 chrome::DuplicateTab(browser()); | |
Lei Zhang
2014/06/13 22:20:10
Why duplicate the tab?
| |
227 | |
228 WebContents * initiator_ = | |
229 browser()->tab_strip_model()->GetActiveWebContents(); | |
230 ASSERT_TRUE(initiator_); | |
231 ASSERT_NE(tab, initiator_); | |
232 } | |
233 | |
234 virtual void CleanUpOnMainThread() OVERRIDE { | |
235 print_preview_observer_.reset(); | |
236 } | |
237 | |
238 scoped_ptr<PrintPreviewObserver> print_preview_observer_; | |
239 | |
240 DISALLOW_COPY_AND_ASSIGN(PrintPreviewPdfGeneratedBrowserTest); | |
241 }; | |
242 | |
243 /* when we get the 'UILoaded' message, we should send a message to the UI | |
244 to actually press stuff. Then after stuff has been pressed, another message | |
245 should come that tells the program that everything has been pressed and | |
246 the pdf has been saved somewhere, and comparisons can start */ | |
247 | |
248 IN_PROC_BROWSER_TEST_F(PrintPreviewPdfGeneratedBrowserTest, DummyTest) { | |
249 NavigateAndPreview("test2.html"); | |
250 Print(); | |
251 } | |
OLD | NEW |