OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2009 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 "base/file_path.h" | |
6 #include "base/scoped_temp_dir.h" | |
7 #include "chrome/browser/browser.h" | |
8 #include "chrome/browser/browser_window.h" | |
9 #include "chrome/browser/net/url_request_mock_http_job.h" | |
10 #include "chrome/browser/tab_contents/tab_contents.h" | |
11 #include "chrome/common/chrome_paths.h" | |
12 #include "chrome/common/notification_service.h" | |
13 #include "chrome/test/in_process_browser_test.h" | |
14 #include "chrome/test/ui_test_utils.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); | |
18 | |
19 namespace { | |
20 | |
21 class SavePageFinishedObserver : public NotificationObserver { | |
22 public: | |
23 SavePageFinishedObserver() { | |
24 registrar_.Add(this, NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | |
25 NotificationService::AllSources()); | |
26 ui_test_utils::RunMessageLoop(); | |
27 } | |
28 | |
29 GURL page_url() const { return page_url_; } | |
30 | |
31 virtual void Observe(NotificationType type, | |
32 const NotificationSource& source, | |
33 const NotificationDetails& details) { | |
34 if (type == NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED) { | |
35 page_url_ = *Details<GURL>(details).ptr(); | |
36 MessageLoopForUI::current()->Quit(); | |
37 } else { | |
38 NOTREACHED(); | |
39 } | |
40 } | |
41 | |
42 private: | |
43 NotificationRegistrar registrar_; | |
44 | |
45 GURL page_url_; | |
46 | |
47 DISALLOW_COPY_AND_ASSIGN(SavePageFinishedObserver); | |
48 }; | |
49 | |
50 class SavePageBrowserTest : public InProcessBrowserTest { | |
51 protected: | |
52 void SetUp() { | |
53 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); | |
54 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); | |
55 InProcessBrowserTest::SetUp(); | |
56 } | |
57 | |
58 // Path to directory containing test data. | |
59 FilePath test_dir_; | |
60 | |
61 // Temporary directory we will save pages to. | |
62 ScopedTempDir save_dir_; | |
63 }; | |
64 | |
65 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { | |
66 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | |
67 GURL url = URLRequestMockHTTPJob::GetMockUrl( | |
68 FilePath(kTestDir).Append(file_name).ToWStringHack()); | |
69 ui_test_utils::NavigateToURL(browser(), url); | |
70 | |
71 TabContents* current_tab = browser()->GetSelectedTabContents(); | |
72 ASSERT_TRUE(current_tab); | |
73 | |
74 FilePath full_file_name = save_dir_.path().Append(file_name); | |
75 FilePath dir = save_dir_.path().AppendASCII("a_files"); | |
76 current_tab->SavePage(full_file_name.ToWStringHack(), dir.ToWStringHack(), | |
77 SavePackage::SAVE_AS_ONLY_HTML); | |
78 | |
79 SavePageFinishedObserver observer; | |
80 | |
81 EXPECT_EQ(url, observer.page_url()); | |
82 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | |
83 EXPECT_TRUE(file_util::PathExists(full_file_name)); | |
84 EXPECT_FALSE(file_util::PathExists(dir)); | |
85 EXPECT_TRUE(file_util::ContentsEqual( | |
86 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | |
87 full_file_name)); | |
88 } | |
89 | |
90 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { | |
91 FilePath file_name(FILE_PATH_LITERAL("b.htm")); | |
92 GURL url = URLRequestMockHTTPJob::GetMockUrl( | |
93 FilePath(kTestDir).Append(file_name).ToWStringHack()); | |
94 ui_test_utils::NavigateToURL(browser(), url); | |
95 | |
96 TabContents* current_tab = browser()->GetSelectedTabContents(); | |
97 ASSERT_TRUE(current_tab); | |
98 | |
99 FilePath full_file_name = save_dir_.path().Append(file_name); | |
100 FilePath dir = save_dir_.path().AppendASCII("b_files"); | |
101 current_tab->SavePage(full_file_name.ToWStringHack(), dir.ToWStringHack(), | |
102 SavePackage::SAVE_AS_COMPLETE_HTML); | |
103 | |
104 SavePageFinishedObserver observer; | |
105 | |
106 EXPECT_EQ(url, observer.page_url()); | |
107 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | |
108 EXPECT_TRUE(file_util::PathExists(full_file_name)); | |
109 EXPECT_TRUE(file_util::PathExists(dir)); | |
110 // TODO(phajdan.jr): Also check the saved html file against golden version. | |
Paul Godavari
2009/07/31 22:11:26
Bug number.
| |
111 EXPECT_TRUE(file_util::ContentsEqual( | |
112 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | |
113 dir.AppendASCII("1.png"))); | |
114 EXPECT_TRUE(file_util::ContentsEqual( | |
115 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), | |
116 dir.AppendASCII("1.css"))); | |
117 } | |
118 | |
119 } // namespace | |
OLD | NEW |