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

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) {
115 return info.url == url_ &&
116 info.path == path_ &&
117 info.received_bytes == num_files_ &&
hendrickson_a 2011/07/08 18:47:37 I think this deserves a comment . . .
achuithb 2011/07/08 21:23:54 Done.
118 info.total_bytes == 0 &&
119 info.state == DownloadItem::COMPLETE;
120 }
121
122 GURL url_;
123 FilePath path_;
124 int64 num_files_;
125 };
126
127 void CheckDownloadHistory(const GURL& url,
128 const FilePath& path,
129 int64 num_files_) {
130 QueryDownloadHistory();
131
132 EXPECT_NE(std::find_if(history_entries_.begin(), history_entries_.end(),
133 DownloadHistoryInfoMatch(url, path, num_files_)),
134 history_entries_.end());
135 }
136
137 std::vector<DownloadHistoryInfo> history_entries_;
138
76 // Path to directory containing test data. 139 // Path to directory containing test data.
77 FilePath test_dir_; 140 FilePath test_dir_;
78 141
79 // Temporary directory we will save pages to. 142 // Temporary directory we will save pages to.
80 ScopedTempDir save_dir_; 143 ScopedTempDir save_dir_;
81 }; 144 };
82 145
83 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { 146 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
84 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 147 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
85 GURL url = URLRequestMockHTTPJob::GetMockUrl( 148 GURL url = URLRequestMockHTTPJob::GetMockUrl(
86 FilePath(kTestDir).Append(file_name)); 149 FilePath(kTestDir).Append(file_name));
87 ui_test_utils::NavigateToURL(browser(), url); 150 ui_test_utils::NavigateToURL(browser(), url);
88 151
89 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); 152 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
90 ASSERT_TRUE(current_tab); 153 ASSERT_TRUE(current_tab);
91 154
92 FilePath full_file_name = save_dir_.path().Append(file_name); 155 FilePath full_file_name = save_dir_.path().Append(file_name);
93 FilePath dir = save_dir_.path().AppendASCII("a_files"); 156 FilePath dir = save_dir_.path().AppendASCII("a_files");
94 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( 157 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage(
95 full_file_name, dir, SavePackage::SAVE_AS_ONLY_HTML)); 158 full_file_name, dir, SavePackage::SAVE_AS_ONLY_HTML));
96 159
97 EXPECT_EQ(url, WaitForSavePackageToFinish()); 160 EXPECT_EQ(url, WaitForSavePackageToFinish());
98 161
99 CheckDownloadUI(full_file_name); 162 CheckDownloadUI(full_file_name);
163 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file.
100 164
101 EXPECT_TRUE(file_util::PathExists(full_file_name)); 165 EXPECT_TRUE(file_util::PathExists(full_file_name));
102 EXPECT_FALSE(file_util::PathExists(dir)); 166 EXPECT_FALSE(file_util::PathExists(dir));
103 EXPECT_TRUE(file_util::ContentsEqual( 167 EXPECT_TRUE(file_util::ContentsEqual(
104 test_dir_.Append(FilePath(kTestDir)).Append(file_name), 168 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
105 full_file_name)); 169 full_file_name));
106 } 170 }
107 171
108 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { 172 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) {
109 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 173 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
110 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( 174 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl(
111 FilePath(kTestDir).Append(file_name)); 175 FilePath(kTestDir).Append(file_name));
112 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( 176 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl(
113 FilePath(kTestDir).Append(file_name)); 177 FilePath(kTestDir).Append(file_name));
114 ui_test_utils::NavigateToURL(browser(), view_source_url); 178 ui_test_utils::NavigateToURL(browser(), view_source_url);
115 179
116 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); 180 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
117 ASSERT_TRUE(current_tab); 181 ASSERT_TRUE(current_tab);
118 182
119 FilePath full_file_name = save_dir_.path().Append(file_name); 183 FilePath full_file_name = save_dir_.path().Append(file_name);
120 FilePath dir = save_dir_.path().AppendASCII("a_files"); 184 FilePath dir = save_dir_.path().AppendASCII("a_files");
121 185
122 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( 186 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage(
123 full_file_name, dir, SavePackage::SAVE_AS_ONLY_HTML)); 187 full_file_name, dir, SavePackage::SAVE_AS_ONLY_HTML));
124 188
125 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish()); 189 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish());
126 190
127 CheckDownloadUI(full_file_name); 191 CheckDownloadUI(full_file_name);
192 CheckDownloadHistory(actual_page_url, full_file_name, 1); // a.htm is 1 file.
128 193
129 EXPECT_TRUE(file_util::PathExists(full_file_name)); 194 EXPECT_TRUE(file_util::PathExists(full_file_name));
130 EXPECT_FALSE(file_util::PathExists(dir)); 195 EXPECT_FALSE(file_util::PathExists(dir));
131 EXPECT_TRUE(file_util::ContentsEqual( 196 EXPECT_TRUE(file_util::ContentsEqual(
132 test_dir_.Append(FilePath(kTestDir)).Append(file_name), 197 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
133 full_file_name)); 198 full_file_name));
134 } 199 }
135 200
136 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { 201 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) {
137 FilePath file_name(FILE_PATH_LITERAL("b.htm")); 202 FilePath file_name(FILE_PATH_LITERAL("b.htm"));
138 GURL url = URLRequestMockHTTPJob::GetMockUrl( 203 GURL url = URLRequestMockHTTPJob::GetMockUrl(
139 FilePath(kTestDir).Append(file_name)); 204 FilePath(kTestDir).Append(file_name));
140 ui_test_utils::NavigateToURL(browser(), url); 205 ui_test_utils::NavigateToURL(browser(), url);
141 206
142 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); 207 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
143 ASSERT_TRUE(current_tab); 208 ASSERT_TRUE(current_tab);
144 209
145 FilePath full_file_name = save_dir_.path().Append(file_name); 210 FilePath full_file_name = save_dir_.path().Append(file_name);
146 FilePath dir = save_dir_.path().AppendASCII("b_files"); 211 FilePath dir = save_dir_.path().AppendASCII("b_files");
147 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( 212 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage(
148 full_file_name, dir, SavePackage::SAVE_AS_COMPLETE_HTML)); 213 full_file_name, dir, SavePackage::SAVE_AS_COMPLETE_HTML));
149 214
150 EXPECT_EQ(url, WaitForSavePackageToFinish()); 215 EXPECT_EQ(url, WaitForSavePackageToFinish());
151 216
152 CheckDownloadUI(full_file_name); 217 CheckDownloadUI(full_file_name);
218 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files.
153 219
154 EXPECT_TRUE(file_util::PathExists(full_file_name)); 220 EXPECT_TRUE(file_util::PathExists(full_file_name));
155 EXPECT_TRUE(file_util::PathExists(dir)); 221 EXPECT_TRUE(file_util::PathExists(dir));
156 EXPECT_TRUE(file_util::TextContentsEqual( 222 EXPECT_TRUE(file_util::TextContentsEqual(
157 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), 223 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
158 full_file_name)); 224 full_file_name));
159 EXPECT_TRUE(file_util::ContentsEqual( 225 EXPECT_TRUE(file_util::ContentsEqual(
160 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 226 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
161 dir.AppendASCII("1.png"))); 227 dir.AppendASCII("1.png")));
162 EXPECT_TRUE(file_util::ContentsEqual( 228 EXPECT_TRUE(file_util::ContentsEqual(
(...skipping 21 matching lines...) Expand all
184 250
185 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); 251 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
186 ASSERT_TRUE(current_tab); 252 ASSERT_TRUE(current_tab);
187 253
188 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( 254 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage(
189 full_file_name, dir, SavePackage::SAVE_AS_COMPLETE_HTML)); 255 full_file_name, dir, SavePackage::SAVE_AS_COMPLETE_HTML));
190 256
191 EXPECT_EQ(url, WaitForSavePackageToFinish()); 257 EXPECT_EQ(url, WaitForSavePackageToFinish());
192 258
193 CheckDownloadUI(full_file_name); 259 CheckDownloadUI(full_file_name);
260 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files.
194 261
195 EXPECT_TRUE(file_util::PathExists(full_file_name)); 262 EXPECT_TRUE(file_util::PathExists(full_file_name));
196 EXPECT_TRUE(file_util::PathExists(dir)); 263 EXPECT_TRUE(file_util::PathExists(dir));
197 EXPECT_TRUE(file_util::TextContentsEqual( 264 EXPECT_TRUE(file_util::TextContentsEqual(
198 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), 265 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
199 full_file_name)); 266 full_file_name));
200 EXPECT_TRUE(file_util::ContentsEqual( 267 EXPECT_TRUE(file_util::ContentsEqual(
201 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 268 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
202 dir.AppendASCII("1.png"))); 269 dir.AppendASCII("1.png")));
203 EXPECT_TRUE(file_util::ContentsEqual( 270 EXPECT_TRUE(file_util::ContentsEqual(
204 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 271 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
205 dir.AppendASCII("1.css"))); 272 dir.AppendASCII("1.css")));
206 } 273 }
207 274
208 } // namespace 275 } // 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