OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/clipboard/clipboard.h" | 5 #include "app/clipboard/clipboard.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
11 #include "chrome/browser/browser_window.h" | 12 #include "chrome/browser/browser_window.h" |
12 #include "chrome/browser/renderer_host/render_view_host.h" | 13 #include "chrome/browser/renderer_host/render_view_host.h" |
13 #include "chrome/browser/tab_contents/tab_contents.h" | 14 #include "chrome/browser/tab_contents/tab_contents.h" |
14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/window_sizer.h" | 16 #include "chrome/browser/window_sizer.h" |
16 #include "chrome/common/chrome_paths.h" | 17 #include "chrome/common/chrome_paths.h" |
17 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/notification_observer.h" | 19 #include "chrome/common/notification_observer.h" |
| 20 #include "chrome/common/notification_type.h" |
19 #include "chrome/test/in_process_browser_test.h" | 21 #include "chrome/test/in_process_browser_test.h" |
20 #include "chrome/test/ui_test_utils.h" | 22 #include "chrome/test/ui_test_utils.h" |
21 #include "gfx/codec/png_codec.h" | 23 #include "gfx/codec/png_codec.h" |
22 | 24 |
| 25 extern base::hash_map<std::string, int> g_test_timeout_overrides; |
| 26 |
23 namespace { | 27 namespace { |
24 | 28 |
25 // Include things like browser frame and scrollbar and make sure we're bigger | 29 // Include things like browser frame and scrollbar and make sure we're bigger |
26 // than the test pdf document. | 30 // than the test pdf document. |
27 static const int kBrowserWidth = 1000; | 31 static const int kBrowserWidth = 1000; |
28 static const int kBrowserHeight = 600; | 32 static const int kBrowserHeight = 600; |
| 33 static const int kLoadingTestTimeoutMs = 60000; |
| 34 |
| 35 // The loading test is really a collection of tests, each one being a different |
| 36 // PDF file. But it would be busy work to add a different test for each file. |
| 37 // Since we run them all in one test, we want a bigger timeout. |
| 38 class IncreaseLoadingTimeout { |
| 39 public: |
| 40 IncreaseLoadingTimeout() { |
| 41 g_test_timeout_overrides["PDFBrowserTest.Loading"] = kLoadingTestTimeoutMs; |
| 42 } |
| 43 }; |
| 44 |
| 45 IncreaseLoadingTimeout g_increase_loading_timeout; |
29 | 46 |
30 class PDFBrowserTest : public InProcessBrowserTest, | 47 class PDFBrowserTest : public InProcessBrowserTest, |
31 public NotificationObserver { | 48 public NotificationObserver { |
32 public: | 49 public: |
33 PDFBrowserTest() | 50 PDFBrowserTest() |
34 : have_plugin_(false), | 51 : snapshot_different_(true), |
35 snapshot_different_(true), | 52 next_dummy_search_value_(0), |
36 next_dummy_search_value_(0) { | 53 load_stop_notification_count_(0) { |
| 54 EnableDOMAutomation(); |
37 } | 55 } |
38 | 56 |
39 protected: | 57 protected: |
40 bool have_plugin() const { return have_plugin_; } | 58 int load_stop_notification_count() const { |
| 59 return load_stop_notification_count_; |
| 60 } |
41 | 61 |
42 virtual void SetUp() { | 62 virtual void SetUp() { |
43 FilePath pdf_path; | 63 FilePath pdf_path; |
44 PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_path); | 64 PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_path); |
45 have_plugin_ = file_util::PathExists(pdf_path); | |
46 InProcessBrowserTest::SetUp(); | 65 InProcessBrowserTest::SetUp(); |
47 } | 66 } |
48 | 67 |
| 68 FilePath GetPDFTestDir() { |
| 69 return FilePath(FilePath::kCurrentDirectory).AppendASCII("plugin"). |
| 70 AppendASCII("pdf"); |
| 71 } |
| 72 |
49 void Load() { | 73 void Load() { |
50 GURL url(ui_test_utils::GetTestUrl( | 74 GURL url(ui_test_utils::GetTestUrl( |
51 FilePath(FilePath::kCurrentDirectory), | 75 GetPDFTestDir(), |
52 FilePath(FILE_PATH_LITERAL("pdf_browsertest.pdf")))); | 76 FilePath(FILE_PATH_LITERAL("pdf_browsertest.pdf")))); |
53 ui_test_utils::NavigateToURL(browser(), url); | 77 ui_test_utils::NavigateToURL(browser(), url); |
54 gfx::Rect bounds(gfx::Rect(0, 0, kBrowserWidth, kBrowserHeight)); | 78 gfx::Rect bounds(gfx::Rect(0, 0, kBrowserWidth, kBrowserHeight)); |
55 | 79 |
56 scoped_ptr<WindowSizer::MonitorInfoProvider> monitor_info( | 80 scoped_ptr<WindowSizer::MonitorInfoProvider> monitor_info( |
57 WindowSizer::CreateDefaultMonitorInfoProvider()); | 81 WindowSizer::CreateDefaultMonitorInfoProvider()); |
58 gfx::Rect screen_bounds = monitor_info->GetPrimaryMonitorBounds(); | 82 gfx::Rect screen_bounds = monitor_info->GetPrimaryMonitorBounds(); |
59 ASSERT_GT(screen_bounds.width(), kBrowserWidth); | 83 ASSERT_GT(screen_bounds.width(), kBrowserWidth); |
60 ASSERT_GT(screen_bounds.height(), kBrowserHeight); | 84 ASSERT_GT(screen_bounds.height(), kBrowserHeight); |
61 | 85 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 command_line->AppendSwitch(switches::kForceInternalPDFPlugin); | 122 command_line->AppendSwitch(switches::kForceInternalPDFPlugin); |
99 } | 123 } |
100 | 124 |
101 // NotificationObserver | 125 // NotificationObserver |
102 virtual void Observe(NotificationType type, | 126 virtual void Observe(NotificationType type, |
103 const NotificationSource& source, | 127 const NotificationSource& source, |
104 const NotificationDetails& details) { | 128 const NotificationDetails& details) { |
105 if (type == NotificationType::TAB_SNAPSHOT_TAKEN) { | 129 if (type == NotificationType::TAB_SNAPSHOT_TAKEN) { |
106 MessageLoopForUI::current()->Quit(); | 130 MessageLoopForUI::current()->Quit(); |
107 FilePath reference = ui_test_utils::GetTestFilePath( | 131 FilePath reference = ui_test_utils::GetTestFilePath( |
108 FilePath(FilePath::kCurrentDirectory), | 132 GetPDFTestDir(), |
109 FilePath().AppendASCII(expected_filename_)); | 133 FilePath().AppendASCII(expected_filename_)); |
110 base::PlatformFileInfo info; | 134 base::PlatformFileInfo info; |
111 ASSERT_TRUE(file_util::GetFileInfo(reference, &info)); | 135 ASSERT_TRUE(file_util::GetFileInfo(reference, &info)); |
112 int size = static_cast<size_t>(info.size); | 136 int size = static_cast<size_t>(info.size); |
113 scoped_array<char> data(new char[size]); | 137 scoped_array<char> data(new char[size]); |
114 ASSERT_EQ(size, file_util::ReadFile(reference, data.get(), size)); | 138 ASSERT_EQ(size, file_util::ReadFile(reference, data.get(), size)); |
115 | 139 |
116 int w, h; | 140 int w, h; |
117 std::vector<unsigned char> decoded; | 141 std::vector<unsigned char> decoded; |
118 ASSERT_TRUE(gfx::PNGCodec::Decode( | 142 ASSERT_TRUE(gfx::PNGCodec::Decode( |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 } | 182 } |
159 | 183 |
160 if (snapshot_different_) { | 184 if (snapshot_different_) { |
161 std::vector<unsigned char> png_data; | 185 std::vector<unsigned char> png_data; |
162 gfx::PNGCodec::EncodeBGRASkBitmap(*bitmap, false, &png_data); | 186 gfx::PNGCodec::EncodeBGRASkBitmap(*bitmap, false, &png_data); |
163 if (file_util::CreateTemporaryFile(&snapshot_filename_)) { | 187 if (file_util::CreateTemporaryFile(&snapshot_filename_)) { |
164 file_util::WriteFile(snapshot_filename_, | 188 file_util::WriteFile(snapshot_filename_, |
165 reinterpret_cast<char*>(&png_data[0]), png_data.size()); | 189 reinterpret_cast<char*>(&png_data[0]), png_data.size()); |
166 } | 190 } |
167 } | 191 } |
| 192 } else if (type == NotificationType::LOAD_STOP) { |
| 193 load_stop_notification_count_++; |
168 } | 194 } |
169 } | 195 } |
170 | 196 |
171 // True if we found the pdf plugin. Needed since only official builders have | |
172 // the pdf plugin, so for the rest, and for other devs, we don't want the test | |
173 // to fail. | |
174 bool have_plugin_; | |
175 // True if the snapshot differed from the expected value. | 197 // True if the snapshot differed from the expected value. |
176 bool snapshot_different_; | 198 bool snapshot_different_; |
177 // Internal variable used to synchronize to the renderer. | 199 // Internal variable used to synchronize to the renderer. |
178 int next_dummy_search_value_; | 200 int next_dummy_search_value_; |
179 // The filename of the bitmap to compare the snapshot to. | 201 // The filename of the bitmap to compare the snapshot to. |
180 std::string expected_filename_; | 202 std::string expected_filename_; |
181 // If the snapshot is different, holds the location where it's saved. | 203 // If the snapshot is different, holds the location where it's saved. |
182 FilePath snapshot_filename_; | 204 FilePath snapshot_filename_; |
| 205 // How many times we've seen NotificationType::LOAD_STOP. |
| 206 int load_stop_notification_count_; |
183 }; | 207 }; |
184 | 208 |
185 #if defined(OS_MACOSX) | 209 #if defined(OS_MACOSX) |
186 // See http://crbug.com/63223 | 210 // See http://crbug.com/63223 |
187 #define MAYBE_Basic FLAKY_Basic | 211 #define MAYBE_Basic FLAKY_Basic |
188 #else | 212 #else |
189 #define MAYBE_Basic Basic | 213 #define MAYBE_Basic Basic |
190 #endif | 214 #endif |
191 | 215 |
192 // Tests basic PDF rendering. This can be broken depending on bad merges with | 216 // Tests basic PDF rendering. This can be broken depending on bad merges with |
193 // the vendor, so it's important that we have basic sanity checking. | 217 // the vendor, so it's important that we have basic sanity checking. |
194 IN_PROC_BROWSER_TEST_F(PDFBrowserTest, MAYBE_Basic) { | 218 IN_PROC_BROWSER_TEST_F(PDFBrowserTest, MAYBE_Basic) { |
195 if (!have_plugin()) | |
196 return; | |
197 | 219 |
198 ASSERT_NO_FATAL_FAILURE(Load()); | 220 ASSERT_NO_FATAL_FAILURE(Load()); |
199 ASSERT_NO_FATAL_FAILURE(WaitForResponse()); | 221 ASSERT_NO_FATAL_FAILURE(WaitForResponse()); |
200 ASSERT_NO_FATAL_FAILURE(VerifySnapshot("pdf_browsertest.png")); | 222 ASSERT_NO_FATAL_FAILURE(VerifySnapshot("pdf_browsertest.png")); |
201 } | 223 } |
202 | 224 |
203 #if defined(OS_MACOSX) | 225 #if defined(OS_MACOSX) |
204 // See http://crbug.com/63223 | 226 // See http://crbug.com/63223 |
205 #define MAYBE_Scroll FLAKY_Scroll | 227 #define MAYBE_Scroll FLAKY_Scroll |
206 #else | 228 #else |
207 #define MAYBE_Scroll Scroll | 229 #define MAYBE_Scroll Scroll |
208 #endif | 230 #endif |
209 | 231 |
210 // Tests that scrolling works. | 232 // Tests that scrolling works. |
211 IN_PROC_BROWSER_TEST_F(PDFBrowserTest, MAYBE_Scroll) { | 233 IN_PROC_BROWSER_TEST_F(PDFBrowserTest, MAYBE_Scroll) { |
212 if (!have_plugin()) | |
213 return; | |
214 | |
215 ASSERT_NO_FATAL_FAILURE(Load()); | 234 ASSERT_NO_FATAL_FAILURE(Load()); |
216 | 235 |
217 // We use wheel mouse event since that's the only one we can easily push to | 236 // We use wheel mouse event since that's the only one we can easily push to |
218 // the renderer. There's no way to push a cross-platform keyboard event at | 237 // the renderer. There's no way to push a cross-platform keyboard event at |
219 // the moment. | 238 // the moment. |
220 WebKit::WebMouseWheelEvent wheel_event; | 239 WebKit::WebMouseWheelEvent wheel_event; |
221 wheel_event.type = WebKit::WebInputEvent::MouseWheel; | 240 wheel_event.type = WebKit::WebInputEvent::MouseWheel; |
222 wheel_event.deltaY = -200; | 241 wheel_event.deltaY = -200; |
223 wheel_event.wheelTicksY = -2; | 242 wheel_event.wheelTicksY = -2; |
224 TabContents* tab_contents = browser()->GetSelectedTabContents(); | 243 TabContents* tab_contents = browser()->GetSelectedTabContents(); |
225 tab_contents->render_view_host()->ForwardWheelEvent(wheel_event); | 244 tab_contents->render_view_host()->ForwardWheelEvent(wheel_event); |
226 ASSERT_NO_FATAL_FAILURE(WaitForResponse()); | 245 ASSERT_NO_FATAL_FAILURE(WaitForResponse()); |
227 ASSERT_NO_FATAL_FAILURE(VerifySnapshot("pdf_browsertest_scroll.png")); | 246 ASSERT_NO_FATAL_FAILURE(VerifySnapshot("pdf_browsertest_scroll.png")); |
228 } | 247 } |
229 | 248 |
230 #if defined(OS_MACOSX) | 249 #if defined(OS_MACOSX) |
231 // See http://crbug.com/63223 | 250 // See http://crbug.com/63223 |
232 #define MAYBE_FindAndCopy FLAKY_FindAndCopy | 251 #define MAYBE_FindAndCopy FLAKY_FindAndCopy |
233 #else | 252 #else |
234 #define MAYBE_FindAndCopy FindAndCopy | 253 #define MAYBE_FindAndCopy FindAndCopy |
235 #endif | 254 #endif |
236 | 255 |
237 IN_PROC_BROWSER_TEST_F(PDFBrowserTest, MAYBE_FindAndCopy) { | 256 IN_PROC_BROWSER_TEST_F(PDFBrowserTest, MAYBE_FindAndCopy) { |
238 if (!have_plugin()) | |
239 return; | |
240 | |
241 ASSERT_NO_FATAL_FAILURE(Load()); | 257 ASSERT_NO_FATAL_FAILURE(Load()); |
242 // Verifies that find in page works. | 258 // Verifies that find in page works. |
243 ASSERT_EQ(3, ui_test_utils::FindInPage( | 259 ASSERT_EQ(3, ui_test_utils::FindInPage( |
244 browser()->GetSelectedTabContents(), UTF8ToUTF16("adipiscing"), true, | 260 browser()->GetSelectedTabContents(), UTF8ToUTF16("adipiscing"), true, |
245 false, NULL)); | 261 false, NULL)); |
246 | 262 |
247 // Verify that copying selected text works. | 263 // Verify that copying selected text works. |
248 Clipboard clipboard; | 264 Clipboard clipboard; |
249 // Reset the clipboard first. | 265 // Reset the clipboard first. |
250 Clipboard::ObjectMap objects; | 266 Clipboard::ObjectMap objects; |
251 Clipboard::ObjectMapParams params; | 267 Clipboard::ObjectMapParams params; |
252 params.push_back(std::vector<char>()); | 268 params.push_back(std::vector<char>()); |
253 objects[Clipboard::CBF_TEXT] = params; | 269 objects[Clipboard::CBF_TEXT] = params; |
254 clipboard.WriteObjects(objects); | 270 clipboard.WriteObjects(objects); |
255 | 271 |
256 browser()->GetSelectedTabContents()->render_view_host()->Copy(); | 272 browser()->GetSelectedTabContents()->render_view_host()->Copy(); |
257 ASSERT_NO_FATAL_FAILURE(WaitForResponse()); | 273 ASSERT_NO_FATAL_FAILURE(WaitForResponse()); |
258 | 274 |
259 std::string text; | 275 std::string text; |
260 clipboard.ReadAsciiText(Clipboard::BUFFER_STANDARD, &text); | 276 clipboard.ReadAsciiText(Clipboard::BUFFER_STANDARD, &text); |
261 ASSERT_EQ("adipiscing", text); | 277 ASSERT_EQ("adipiscing", text); |
262 } | 278 } |
263 | 279 |
| 280 // Tests that loading async pdfs works correctly (i.e. document fully loads). |
| 281 // This also loads all documents that used to crash, to ensure we don't have |
| 282 // regressions. |
| 283 IN_PROC_BROWSER_TEST_F(PDFBrowserTest, Loading) { |
| 284 ASSERT_TRUE(test_server()->Start()); |
| 285 |
| 286 NavigationController* controller = |
| 287 &(browser()->GetSelectedTabContents()->controller()); |
| 288 NotificationRegistrar registrar; |
| 289 registrar.Add(this, |
| 290 NotificationType::LOAD_STOP, |
| 291 Source<NavigationController>(controller)); |
| 292 std::string base_url = std::string("files/plugin/pdf/"); |
| 293 |
| 294 file_util::FileEnumerator file_enumerator( |
| 295 ui_test_utils::GetTestFilePath(GetPDFTestDir(), FilePath()), |
| 296 false, |
| 297 file_util::FileEnumerator::FILES, |
| 298 FILE_PATH_LITERAL("*.pdf")); |
| 299 for (FilePath file_path = file_enumerator.Next(); |
| 300 !file_path.empty(); |
| 301 file_path = file_enumerator.Next()) { |
| 302 std::string filename = WideToASCII(file_path.BaseName().ToWStringHack()); |
| 303 LOG(WARNING) << "PDFBrowserTest.Loading: " << filename; |
| 304 |
| 305 GURL url = test_server()->GetURL(base_url + filename); |
| 306 ui_test_utils::NavigateToURL(browser(), url); |
| 307 |
| 308 while (true) { |
| 309 int last_count = load_stop_notification_count(); |
| 310 // We might get extraneous NotificationType::LOAD_STOP notifications when |
| 311 // doing async loading. This happens when the first loader is cancelled |
| 312 // and before creating a byte-range request loader. |
| 313 |
| 314 bool complete = false; |
| 315 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 316 browser()->GetSelectedTabContents()->render_view_host(), |
| 317 std::wstring(), |
| 318 L"window.domAutomationController.send(plugin.documentLoadComplete())", |
| 319 &complete)); |
| 320 if (complete) |
| 321 break; |
| 322 |
| 323 // Check if the LOAD_STOP notification could have come while we run a |
| 324 // nested message loop for the JS call. |
| 325 if (last_count != load_stop_notification_count()) |
| 326 continue; |
| 327 ui_test_utils::WaitForLoadStop(controller); |
| 328 } |
| 329 } |
| 330 } |
| 331 |
264 } // namespace | 332 } // namespace |
OLD | NEW |