Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: chrome/browser/download/save_page_browsertest.cc

Issue 7277073: Support for adding save page download items into downloads history. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/file_path.h" 5 #include "base/file_path.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/path_service.h" 7 #include "base/path_service.h"
8 #include "base/scoped_temp_dir.h" 8 #include "base/scoped_temp_dir.h"
9 #include "chrome/app/chrome_command_ids.h" 9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/download/download_history.h"
10 #include "chrome/browser/download/download_item.h" 11 #include "chrome/browser/download/download_item.h"
12 #include "chrome/browser/download/download_manager.h"
13 #include "chrome/browser/history/download_history_info.h"
14 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h" 16 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/download/download_tab_helper.h" 17 #include "chrome/browser/ui/download/download_tab_helper.h"
14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
15 #include "chrome/browser/ui/webui/active_downloads_ui.h" 19 #include "chrome/browser/ui/webui/active_downloads_ui.h"
16 #include "chrome/common/chrome_paths.h" 20 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/url_constants.h" 21 #include "chrome/common/url_constants.h"
18 #include "chrome/test/in_process_browser_test.h" 22 #include "chrome/test/in_process_browser_test.h"
19 #include "chrome/test/ui_test_utils.h" 23 #include "chrome/test/ui_test_utils.h"
20 #include "content/browser/net/url_request_mock_http_job.h" 24 #include "content/browser/net/url_request_mock_http_job.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 found = true; 70 found = true;
67 break; 71 break;
68 } 72 }
69 } 73 }
70 EXPECT_TRUE(found); 74 EXPECT_TRUE(found);
71 #else 75 #else
72 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 76 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
73 #endif 77 #endif
74 } 78 }
75 79
80 void QueryDownloadHistory() {
81 DownloadManager* download_manager =
82 browser()->profile()->GetDownloadManager();
83 ASSERT_TRUE(download_manager);
84
85 // Query the history system.
86 download_manager->download_history()->Load(
87 NewCallback(this,
88 &SavePageBrowserTest::OnQueryDownloadEntriesComplete));
89
90 // Run message loop until a quit message is sent from
91 // OnQueryDownloadEntriesComplete().
92 ui_test_utils::RunMessageLoop();
93 }
94
95 void OnQueryDownloadEntriesComplete(
96 std::vector<DownloadHistoryInfo>* entries) {
97 history_entries_ = *entries;
98
99 // Indicate thet we have received the history and can continue.
100 MessageLoopForUI::current()->Quit();
101 }
102
103 struct DownloadHistoryInfoMatch
104 : public std::unary_function<DownloadHistoryInfo, bool> {
105
106 DownloadHistoryInfoMatch(const GURL& url,
107 const FilePath& path,
108 int64 num_files)
109 : url_(url),
110 path_(path),
111 num_files_(num_files) {
112 }
113
114 bool operator() (const DownloadHistoryInfo& info) const {
115 return info.url == url_ &&
116 info.path == path_ &&
117 // For save packages, received bytes is actually the number of files.
118 info.received_bytes == num_files_ &&
119 info.total_bytes == 0 &&
120 info.state == DownloadItem::COMPLETE;
121 }
122
123 GURL url_;
124 FilePath path_;
125 int64 num_files_;
126 };
127
128 void CheckDownloadHistory(const GURL& url,
129 const FilePath& path,
130 int64 num_files_) {
131 QueryDownloadHistory();
132
133 EXPECT_NE(std::find_if(history_entries_.begin(), history_entries_.end(),
134 DownloadHistoryInfoMatch(url, path, num_files_)),
135 history_entries_.end());
136 }
137
138 std::vector<DownloadHistoryInfo> history_entries_;
139
76 // Path to directory containing test data. 140 // Path to directory containing test data.
77 FilePath test_dir_; 141 FilePath test_dir_;
78 142
79 // Temporary directory we will save pages to. 143 // Temporary directory we will save pages to.
80 ScopedTempDir save_dir_; 144 ScopedTempDir save_dir_;
81 }; 145 };
82 146
83 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { 147 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
84 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 148 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
85 GURL url = URLRequestMockHTTPJob::GetMockUrl( 149 GURL url = URLRequestMockHTTPJob::GetMockUrl(
86 FilePath(kTestDir).Append(file_name)); 150 FilePath(kTestDir).Append(file_name));
87 ui_test_utils::NavigateToURL(browser(), url); 151 ui_test_utils::NavigateToURL(browser(), url);
88 152
89 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); 153 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
90 ASSERT_TRUE(current_tab); 154 ASSERT_TRUE(current_tab);
91 155
92 FilePath full_file_name = save_dir_.path().Append(file_name); 156 FilePath full_file_name = save_dir_.path().Append(file_name);
93 FilePath dir = save_dir_.path().AppendASCII("a_files"); 157 FilePath dir = save_dir_.path().AppendASCII("a_files");
94 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( 158 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage(
95 full_file_name, dir, SavePackage::SAVE_AS_ONLY_HTML)); 159 full_file_name, dir, SavePackage::SAVE_AS_ONLY_HTML));
96 160
97 EXPECT_EQ(url, WaitForSavePackageToFinish()); 161 EXPECT_EQ(url, WaitForSavePackageToFinish());
98 162
99 CheckDownloadUI(full_file_name); 163 CheckDownloadUI(full_file_name);
164 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file.
100 165
101 EXPECT_TRUE(file_util::PathExists(full_file_name)); 166 EXPECT_TRUE(file_util::PathExists(full_file_name));
102 EXPECT_FALSE(file_util::PathExists(dir)); 167 EXPECT_FALSE(file_util::PathExists(dir));
103 EXPECT_TRUE(file_util::ContentsEqual( 168 EXPECT_TRUE(file_util::ContentsEqual(
104 test_dir_.Append(FilePath(kTestDir)).Append(file_name), 169 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
105 full_file_name)); 170 full_file_name));
106 } 171 }
107 172
108 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { 173 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) {
109 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 174 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
110 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( 175 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl(
111 FilePath(kTestDir).Append(file_name)); 176 FilePath(kTestDir).Append(file_name));
112 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( 177 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl(
113 FilePath(kTestDir).Append(file_name)); 178 FilePath(kTestDir).Append(file_name));
114 ui_test_utils::NavigateToURL(browser(), view_source_url); 179 ui_test_utils::NavigateToURL(browser(), view_source_url);
115 180
116 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); 181 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
117 ASSERT_TRUE(current_tab); 182 ASSERT_TRUE(current_tab);
118 183
119 FilePath full_file_name = save_dir_.path().Append(file_name); 184 FilePath full_file_name = save_dir_.path().Append(file_name);
120 FilePath dir = save_dir_.path().AppendASCII("a_files"); 185 FilePath dir = save_dir_.path().AppendASCII("a_files");
121 186
122 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( 187 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage(
123 full_file_name, dir, SavePackage::SAVE_AS_ONLY_HTML)); 188 full_file_name, dir, SavePackage::SAVE_AS_ONLY_HTML));
124 189
125 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish()); 190 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish());
126 191
127 CheckDownloadUI(full_file_name); 192 CheckDownloadUI(full_file_name);
193 CheckDownloadHistory(actual_page_url, full_file_name, 1); // a.htm is 1 file.
128 194
129 EXPECT_TRUE(file_util::PathExists(full_file_name)); 195 EXPECT_TRUE(file_util::PathExists(full_file_name));
130 EXPECT_FALSE(file_util::PathExists(dir)); 196 EXPECT_FALSE(file_util::PathExists(dir));
131 EXPECT_TRUE(file_util::ContentsEqual( 197 EXPECT_TRUE(file_util::ContentsEqual(
132 test_dir_.Append(FilePath(kTestDir)).Append(file_name), 198 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
133 full_file_name)); 199 full_file_name));
134 } 200 }
135 201
136 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { 202 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) {
137 FilePath file_name(FILE_PATH_LITERAL("b.htm")); 203 FilePath file_name(FILE_PATH_LITERAL("b.htm"));
138 GURL url = URLRequestMockHTTPJob::GetMockUrl( 204 GURL url = URLRequestMockHTTPJob::GetMockUrl(
139 FilePath(kTestDir).Append(file_name)); 205 FilePath(kTestDir).Append(file_name));
140 ui_test_utils::NavigateToURL(browser(), url); 206 ui_test_utils::NavigateToURL(browser(), url);
141 207
142 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); 208 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
143 ASSERT_TRUE(current_tab); 209 ASSERT_TRUE(current_tab);
144 210
145 FilePath full_file_name = save_dir_.path().Append(file_name); 211 FilePath full_file_name = save_dir_.path().Append(file_name);
146 FilePath dir = save_dir_.path().AppendASCII("b_files"); 212 FilePath dir = save_dir_.path().AppendASCII("b_files");
147 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( 213 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage(
148 full_file_name, dir, SavePackage::SAVE_AS_COMPLETE_HTML)); 214 full_file_name, dir, SavePackage::SAVE_AS_COMPLETE_HTML));
149 215
150 EXPECT_EQ(url, WaitForSavePackageToFinish()); 216 EXPECT_EQ(url, WaitForSavePackageToFinish());
151 217
152 CheckDownloadUI(full_file_name); 218 CheckDownloadUI(full_file_name);
219 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files.
153 220
154 EXPECT_TRUE(file_util::PathExists(full_file_name)); 221 EXPECT_TRUE(file_util::PathExists(full_file_name));
155 EXPECT_TRUE(file_util::PathExists(dir)); 222 EXPECT_TRUE(file_util::PathExists(dir));
156 EXPECT_TRUE(file_util::TextContentsEqual( 223 EXPECT_TRUE(file_util::TextContentsEqual(
157 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), 224 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
158 full_file_name)); 225 full_file_name));
159 EXPECT_TRUE(file_util::ContentsEqual( 226 EXPECT_TRUE(file_util::ContentsEqual(
160 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 227 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
161 dir.AppendASCII("1.png"))); 228 dir.AppendASCII("1.png")));
162 EXPECT_TRUE(file_util::ContentsEqual( 229 EXPECT_TRUE(file_util::ContentsEqual(
(...skipping 21 matching lines...) Expand all
184 251
185 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); 252 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
186 ASSERT_TRUE(current_tab); 253 ASSERT_TRUE(current_tab);
187 254
188 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( 255 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage(
189 full_file_name, dir, SavePackage::SAVE_AS_COMPLETE_HTML)); 256 full_file_name, dir, SavePackage::SAVE_AS_COMPLETE_HTML));
190 257
191 EXPECT_EQ(url, WaitForSavePackageToFinish()); 258 EXPECT_EQ(url, WaitForSavePackageToFinish());
192 259
193 CheckDownloadUI(full_file_name); 260 CheckDownloadUI(full_file_name);
261 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files.
194 262
195 EXPECT_TRUE(file_util::PathExists(full_file_name)); 263 EXPECT_TRUE(file_util::PathExists(full_file_name));
196 EXPECT_TRUE(file_util::PathExists(dir)); 264 EXPECT_TRUE(file_util::PathExists(dir));
197 EXPECT_TRUE(file_util::TextContentsEqual( 265 EXPECT_TRUE(file_util::TextContentsEqual(
198 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), 266 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
199 full_file_name)); 267 full_file_name));
200 EXPECT_TRUE(file_util::ContentsEqual( 268 EXPECT_TRUE(file_util::ContentsEqual(
201 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 269 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
202 dir.AppendASCII("1.png"))); 270 dir.AppendASCII("1.png")));
203 EXPECT_TRUE(file_util::ContentsEqual( 271 EXPECT_TRUE(file_util::ContentsEqual(
204 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 272 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
205 dir.AppendASCII("1.css"))); 273 dir.AppendASCII("1.css")));
206 } 274 }
207 275
208 } // namespace 276 } // namespace
OLDNEW
« chrome/browser/download/save_package.cc ('K') | « chrome/browser/download/save_package.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698