Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / browser_encoding_browsertest.cc
blobf9116f8e960bd0722cf82d8dd8c8a97b93318678
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.
5 #include "base/bind.h"
6 #include "base/files/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/character_encoding.h"
10 #include "chrome/browser/net/url_request_mock_util.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_commands.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/download_manager.h"
20 #include "content/public/browser/navigation_controller.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/notification_source.h"
23 #include "content/public/browser/notification_types.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/test/test_navigation_observer.h"
26 #include "net/test/url_request/url_request_mock_http_job.h"
28 namespace {
30 struct EncodingTestData {
31 const char* file_name;
32 const char* encoding_name;
35 const EncodingTestData kEncodingTestDatas[] = {
36 { "Big5.html", "Big5" },
37 { "EUC-JP.html", "EUC-JP" },
38 { "gb18030.html", "gb18030" },
39 { "iso-8859-1.html", "windows-1252" },
40 { "ISO-8859-2.html", "ISO-8859-2" },
41 { "ISO-8859-4.html", "ISO-8859-4" },
42 { "ISO-8859-5.html", "ISO-8859-5" },
43 { "ISO-8859-6.html", "ISO-8859-6" },
44 { "ISO-8859-7.html", "ISO-8859-7" },
45 { "ISO-8859-8.html", "ISO-8859-8" },
46 { "ISO-8859-13.html", "ISO-8859-13" },
47 { "ISO-8859-15.html", "ISO-8859-15" },
48 { "KOI8-R.html", "KOI8-R" },
49 { "KOI8-U.html", "KOI8-U" },
50 { "macintosh.html", "macintosh" },
51 { "Shift-JIS.html", "Shift_JIS" },
52 { "US-ASCII.html", "windows-1252" }, // http://crbug.com/15801
53 { "UTF-8.html", "UTF-8" },
54 { "UTF-16LE.html", "UTF-16LE" },
55 { "windows-874.html", "windows-874" },
56 { "EUC-KR.html", "EUC-KR" },
57 { "windows-1250.html", "windows-1250" },
58 { "windows-1251.html", "windows-1251" },
59 { "windows-1252.html", "windows-1252" },
60 { "windows-1253.html", "windows-1253" },
61 { "windows-1254.html", "windows-1254" },
62 { "windows-1255.html", "windows-1255" },
63 { "windows-1256.html", "windows-1256" },
64 { "windows-1257.html", "windows-1257" },
65 { "windows-1258.html", "windows-1258" }
68 class SavePackageFinishedObserver : public content::DownloadManager::Observer {
69 public:
70 SavePackageFinishedObserver(content::DownloadManager* manager,
71 const base::Closure& callback)
72 : download_manager_(manager),
73 callback_(callback) {
74 download_manager_->AddObserver(this);
77 ~SavePackageFinishedObserver() override {
78 if (download_manager_)
79 download_manager_->RemoveObserver(this);
82 // DownloadManager::Observer:
83 void OnSavePackageSuccessfullyFinished(content::DownloadManager* manager,
84 content::DownloadItem* item) override {
85 callback_.Run();
87 void ManagerGoingDown(content::DownloadManager* manager) override {
88 download_manager_->RemoveObserver(this);
89 download_manager_ = NULL;
92 private:
93 content::DownloadManager* download_manager_;
94 base::Closure callback_;
96 DISALLOW_COPY_AND_ASSIGN(SavePackageFinishedObserver);
99 } // namespace
101 using content::BrowserThread;
103 static const base::FilePath::CharType* kTestDir =
104 FILE_PATH_LITERAL("encoding_tests");
106 class BrowserEncodingTest
107 : public InProcessBrowserTest,
108 public testing::WithParamInterface<EncodingTestData> {
109 protected:
110 BrowserEncodingTest() {}
112 // Saves the current page and verifies that the output matches the expected
113 // result.
114 void SaveAndCompare(const char* filename_to_write,
115 const base::FilePath& expected) {
116 // Dump the page, the content of dump page should be identical to the
117 // expected result file.
118 base::FilePath full_file_name = save_dir_.AppendASCII(filename_to_write);
119 // We save the page as way of complete HTML file, which requires a directory
120 // name to save sub resources in it. Although this test file does not have
121 // sub resources, but the directory name is still required.
122 scoped_refptr<content::MessageLoopRunner> loop_runner(
123 new content::MessageLoopRunner);
124 SavePackageFinishedObserver observer(
125 content::BrowserContext::GetDownloadManager(browser()->profile()),
126 loop_runner->QuitClosure());
127 browser()->tab_strip_model()->GetActiveWebContents()->SavePage(
128 full_file_name, temp_sub_resource_dir_,
129 content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML);
130 loop_runner->Run();
132 base::FilePath expected_file_name = ui_test_utils::GetTestFilePath(
133 base::FilePath(kTestDir), expected);
135 EXPECT_TRUE(base::ContentsEqual(full_file_name, expected_file_name)) <<
136 "generated_file = " << full_file_name.AsUTF8Unsafe() <<
137 ", expected_file = " << expected_file_name.AsUTF8Unsafe();
140 void SetUpOnMainThread() override {
141 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
142 save_dir_ = temp_dir_.path();
143 temp_sub_resource_dir_ = save_dir_.AppendASCII("sub_resource_files");
145 BrowserThread::PostTask(
146 BrowserThread::IO, FROM_HERE,
147 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
150 base::ScopedTempDir temp_dir_;
151 base::FilePath save_dir_;
152 base::FilePath temp_sub_resource_dir_;
155 // TODO(jnd): 1. Some encodings are missing here. It'll be added later. See
156 // http://crbug.com/13306.
157 // 2. Add more files with multiple encoding name variants for each canonical
158 // encoding name). Webkit layout tests cover some, but testing in the UI test is
159 // also necessary.
160 IN_PROC_BROWSER_TEST_P(BrowserEncodingTest, TestEncodingAliasMapping) {
161 const char* const kAliasTestDir = "alias_mapping";
163 base::FilePath test_dir_path = base::FilePath(kTestDir).AppendASCII(
164 kAliasTestDir);
165 base::FilePath test_file_path(test_dir_path);
166 test_file_path = test_file_path.AppendASCII(
167 GetParam().file_name);
169 GURL url = net::URLRequestMockHTTPJob::GetMockUrl(test_file_path);
170 ui_test_utils::NavigateToURL(browser(), url);
171 EXPECT_EQ(GetParam().encoding_name,
172 browser()->tab_strip_model()->GetActiveWebContents()->
173 GetEncoding());
176 INSTANTIATE_TEST_CASE_P(EncodingAliases,
177 BrowserEncodingTest,
178 testing::ValuesIn(kEncodingTestDatas));
180 // Marked as flaky: see http://crbug.com/44668
181 IN_PROC_BROWSER_TEST_F(BrowserEncodingTest, DISABLED_TestOverrideEncoding) {
182 const char* const kTestFileName = "gb18030_with_iso88591_meta.html";
183 const char* const kExpectedFileName =
184 "expected_gb18030_saved_from_iso88591_meta.html";
185 const char* const kOverrideTestDir = "user_override";
187 base::FilePath test_dir_path =
188 base::FilePath(kTestDir).AppendASCII(kOverrideTestDir);
189 test_dir_path = test_dir_path.AppendASCII(kTestFileName);
190 GURL url = net::URLRequestMockHTTPJob::GetMockUrl(test_dir_path);
191 ui_test_utils::NavigateToURL(browser(), url);
192 content::WebContents* web_contents =
193 browser()->tab_strip_model()->GetActiveWebContents();
194 EXPECT_EQ("windows-1252", web_contents->GetEncoding());
196 // Override the encoding to "gb18030".
197 const std::string selected_encoding =
198 CharacterEncoding::GetCanonicalEncodingNameByAliasName("gb18030");
199 content::TestNavigationObserver navigation_observer(web_contents);
200 web_contents->SetOverrideEncoding(selected_encoding);
201 navigation_observer.Wait();
202 EXPECT_EQ("gb18030", web_contents->GetEncoding());
204 base::FilePath expected_filename =
205 base::FilePath().AppendASCII(kOverrideTestDir).AppendASCII(
206 kExpectedFileName);
207 SaveAndCompare(kTestFileName, expected_filename);
210 // The following encodings are excluded from the auto-detection test because
211 // it's a known issue that the current encoding detector does not detect them:
212 // ISO-8859-4
213 // ISO-8859-13
214 // KOI8-U
215 // macintosh
216 // windows-874
217 // windows-1252
218 // windows-1253
219 // windows-1257
220 // windows-1258
222 // For Hebrew, the expected encoding value is ISO-8859-8-I. See
223 // http://crbug.com/2927 for more details.
225 // This test is failing consistently on ChromeOS, see http://crbug.com/512996.
226 #if defined(OS_CHROMEOS)
227 #define MAYBE_TestEncodingAutoDetect DISABLED_TestEncodingAutoDetect
228 #else
229 #define MAYBE_TestEncodingAutoDetect TestEncodingAutoDetect
230 #endif
231 // TODO(phajdan.jr): See if fix for http://crbug.com/122053 would help here.
232 // Disabled to revert a depending Blink CL. crbug.com/328354, and
233 // crbug.com/510422.
234 IN_PROC_BROWSER_TEST_F(BrowserEncodingTest, DISABLED_TestEncodingAutoDetect) {
235 struct EncodingAutoDetectTestData {
236 const char* test_file_name; // File name of test data.
237 const char* expected_result; // File name of expected results.
238 const char* expected_encoding; // expected encoding.
240 const EncodingAutoDetectTestData kTestDatas[] = {
241 { "Big5_with_no_encoding_specified.html",
242 "expected_Big5_saved_from_no_encoding_specified.html",
243 "Big5" },
244 { "gb18030_with_no_encoding_specified.html",
245 "expected_gb18030_saved_from_no_encoding_specified.html",
246 "gb18030" },
247 { "iso-8859-1_with_no_encoding_specified.html",
248 "expected_iso-8859-1_saved_from_no_encoding_specified.html",
249 "windows-1252" },
250 { "ISO-8859-5_with_no_encoding_specified.html",
251 "expected_ISO-8859-5_saved_from_no_encoding_specified.html",
252 "ISO-8859-5" },
253 { "ISO-8859-6_with_no_encoding_specified.html",
254 "expected_ISO-8859-6_saved_from_no_encoding_specified.html",
255 "ISO-8859-6" },
256 { "ISO-8859-7_with_no_encoding_specified.html",
257 "expected_ISO-8859-7_saved_from_no_encoding_specified.html",
258 "ISO-8859-7" },
259 { "ISO-8859-8_with_no_encoding_specified.html",
260 "expected_ISO-8859-8_saved_from_no_encoding_specified.html",
261 "ISO-8859-8-I" },
262 { "KOI8-R_with_no_encoding_specified.html",
263 "expected_KOI8-R_saved_from_no_encoding_specified.html",
264 "KOI8-R" },
265 { "Shift-JIS_with_no_encoding_specified.html",
266 "expected_Shift-JIS_saved_from_no_encoding_specified.html",
267 "Shift_JIS" },
268 { "UTF-8_with_no_encoding_specified.html",
269 "expected_UTF-8_saved_from_no_encoding_specified.html",
270 "UTF-8" },
271 { "EUC-KR_with_no_encoding_specified.html",
272 "expected_EUC-KR_saved_from_no_encoding_specified.html",
273 "EUC-KR" },
274 { "windows-1251_with_no_encoding_specified.html",
275 "expected_windows-1251_saved_from_no_encoding_specified.html",
276 "windows-1251" },
277 { "windows-1254_with_no_encoding_specified.html",
278 "expected_windows-1254_saved_from_no_encoding_specified.html",
279 "windows-1254" },
280 { "windows-1255_with_no_encoding_specified.html",
281 "expected_windows-1255_saved_from_no_encoding_specified.html",
282 "windows-1255" },
283 { "windows-1256_with_no_encoding_specified.html",
284 "expected_windows-1256_saved_from_no_encoding_specified.html",
285 "windows-1256" }
287 const char* const kAutoDetectDir = "auto_detect";
288 // Directory of the files of expected results.
289 const char* const kExpectedResultDir = "expected_results";
291 base::FilePath test_dir_path =
292 base::FilePath(kTestDir).AppendASCII(kAutoDetectDir);
294 // Set the default charset to one of encodings not supported by the current
295 // auto-detector (Please refer to the above comments) to make sure we
296 // incorrectly decode the page. Now we use ISO-8859-4.
297 browser()->profile()->GetPrefs()->SetString(prefs::kDefaultCharset,
298 "ISO-8859-4");
300 content::WebContents* web_contents =
301 browser()->tab_strip_model()->GetActiveWebContents();
302 for (size_t i = 0; i < arraysize(kTestDatas); ++i) {
303 // Disable auto detect if it is on.
304 browser()->profile()->GetPrefs()->SetBoolean(
305 prefs::kWebKitUsesUniversalDetector, false);
307 base::FilePath test_file_path(test_dir_path);
308 test_file_path = test_file_path.AppendASCII(kTestDatas[i].test_file_name);
309 GURL url = net::URLRequestMockHTTPJob::GetMockUrl(test_file_path);
310 ui_test_utils::NavigateToURL(browser(), url);
312 // Get the encoding used for the page, it must be the default charset we
313 // just set.
314 EXPECT_EQ("ISO-8859-4", web_contents->GetEncoding());
316 // Enable the encoding auto detection.
317 browser()->profile()->GetPrefs()->SetBoolean(
318 prefs::kWebKitUsesUniversalDetector, true);
320 content::TestNavigationObserver observer(web_contents);
321 chrome::Reload(browser(), CURRENT_TAB);
322 observer.Wait();
324 // Re-get the encoding of page. It should return the real encoding now.
325 EXPECT_EQ(kTestDatas[i].expected_encoding, web_contents->GetEncoding());
327 // Dump the page, the content of dump page should be equal with our expect
328 // result file.
329 base::FilePath expected_result_file_name =
330 base::FilePath().AppendASCII(kAutoDetectDir).
331 AppendASCII(kExpectedResultDir).
332 AppendASCII(kTestDatas[i].expected_result);
333 SaveAndCompare(kTestDatas[i].test_file_name, expected_result_file_name);