blob: 42267ac2fd0772ffbef1d4ecb1bc8ad161cd71f5 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]8a609392009-08-22 08:28:472// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4#include <string>
5
6#include "base/file_util.h"
[email protected]e0785902011-05-19 23:34:177#include "base/scoped_temp_dir.h"
[email protected]719f9732009-09-02 05:04:578#include "chrome/common/pref_names.h"
[email protected]6f870322011-06-27 23:07:099#include "chrome/test/automation/automation_proxy.h"
[email protected]8a609392009-08-22 08:28:4710#include "chrome/test/automation/browser_proxy.h"
11#include "chrome/test/automation/tab_proxy.h"
[email protected]af44e7fb2011-07-29 18:32:3212#include "chrome/test/base/ui_test_utils.h"
[email protected]8a609392009-08-22 08:28:4713#include "chrome/test/ui/ui_test.h"
[email protected]0ffaa482011-07-14 23:41:2814#include "content/browser/download/save_package.h"
[email protected]8aee8ed32011-06-14 20:16:4115#include "content/browser/net/url_request_mock_http_job.h"
[email protected]8a609392009-08-22 08:28:4716
[email protected]719f9732009-09-02 05:04:5717static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("encoding_tests");
[email protected]8a609392009-08-22 08:28:4718
19class BrowserEncodingTest : public UITest {
20 protected:
21 BrowserEncodingTest() : UITest() {}
22
23 // Make sure the content of the page are as expected
24 // after override or auto-detect
25 void CheckFile(const FilePath& generated_file,
26 const FilePath& expected_result_file,
27 bool check_equal) {
[email protected]5a1454362010-03-31 08:56:4728 FilePath expected_result_filepath = ui_test_utils::GetTestFilePath(
29 FilePath(kTestDir), expected_result_file);
[email protected]8a609392009-08-22 08:28:4730
31 ASSERT_TRUE(file_util::PathExists(expected_result_filepath));
32 WaitForGeneratedFileAndCheck(generated_file,
33 expected_result_filepath,
34 true, // We do care whether they are equal.
35 check_equal,
36 true); // Delete the generated file when done.
37 }
38
39 virtual void SetUp() {
40 UITest::SetUp();
[email protected]719f9732009-09-02 05:04:5741 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
42 save_dir_ = temp_dir_.path();
43 temp_sub_resource_dir_ = save_dir_.AppendASCII("sub_resource_files");
[email protected]8a609392009-08-22 08:28:4744 }
45
[email protected]719f9732009-09-02 05:04:5746 ScopedTempDir temp_dir_;
47 FilePath save_dir_;
48 FilePath temp_sub_resource_dir_;
[email protected]8a609392009-08-22 08:28:4749};
50
51// TODO(jnd): 1. Some encodings are missing here. It'll be added later. See
52// http://crbug.com/13306.
53// 2. Add more files with multiple encoding name variants for each canonical
54// encoding name). Webkit layout tests cover some, but testing in the UI test is
55// also necessary.
[email protected]37a176882011-05-13 23:00:2556
57#if defined(OS_MACOSX)
58#define TestEncodingAliasMapping FLAKY_TestEncodingAliasMapping
59#endif
[email protected]ee4461f2010-09-29 09:53:5860TEST_F(BrowserEncodingTest, TestEncodingAliasMapping) {
[email protected]8a609392009-08-22 08:28:4761 struct EncodingTestData {
[email protected]719f9732009-09-02 05:04:5762 const char* file_name;
[email protected]41fc0322009-09-04 22:23:4063 const char* encoding_name;
[email protected]8a609392009-08-22 08:28:4764 };
65
66 const EncodingTestData kEncodingTestDatas[] = {
[email protected]41fc0322009-09-04 22:23:4067 { "Big5.html", "Big5" },
68 { "EUC-JP.html", "EUC-JP" },
69 { "gb18030.html", "gb18030" },
70 { "iso-8859-1.html", "ISO-8859-1" },
71 { "ISO-8859-2.html", "ISO-8859-2" },
72 { "ISO-8859-4.html", "ISO-8859-4" },
73 { "ISO-8859-5.html", "ISO-8859-5" },
74 { "ISO-8859-6.html", "ISO-8859-6" },
75 { "ISO-8859-7.html", "ISO-8859-7" },
76 { "ISO-8859-8.html", "ISO-8859-8" },
77 { "ISO-8859-13.html", "ISO-8859-13" },
78 { "ISO-8859-15.html", "ISO-8859-15" },
79 { "KOI8-R.html", "KOI8-R" },
80 { "KOI8-U.html", "KOI8-U" },
81 { "macintosh.html", "macintosh" },
82 { "Shift-JIS.html", "Shift_JIS" },
[email protected]f73ebdf2009-09-22 02:31:0483 { "US-ASCII.html", "ISO-8859-1" }, // http://crbug.com/15801
[email protected]41fc0322009-09-04 22:23:4084 { "UTF-8.html", "UTF-8" },
85 { "UTF-16LE.html", "UTF-16LE" },
86 { "windows-874.html", "windows-874" },
[email protected]7ebafde2011-09-09 03:38:3187 // http://crbug.com/95963
88 // { "windows-949.html", "windows-949" },
[email protected]41fc0322009-09-04 22:23:4089 { "windows-1250.html", "windows-1250" },
90 { "windows-1251.html", "windows-1251" },
91 { "windows-1252.html", "windows-1252" },
92 { "windows-1253.html", "windows-1253" },
93 { "windows-1254.html", "windows-1254" },
94 { "windows-1255.html", "windows-1255" },
95 { "windows-1256.html", "windows-1256" },
96 { "windows-1257.html", "windows-1257" },
97 { "windows-1258.html", "windows-1258" }
[email protected]8a609392009-08-22 08:28:4798 };
[email protected]719f9732009-09-02 05:04:5799 const char* const kAliasTestDir = "alias_mapping";
[email protected]8a609392009-08-22 08:28:47100
[email protected]9d6dcdf2010-03-26 16:12:10101 scoped_refptr<TabProxy> tab_proxy(GetActiveTab());
102 ASSERT_TRUE(tab_proxy.get());
103
[email protected]719f9732009-09-02 05:04:57104 FilePath test_dir_path = FilePath(kTestDir).AppendASCII(kAliasTestDir);
[email protected]d5360f6382009-08-22 09:37:36105 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kEncodingTestDatas); ++i) {
[email protected]8a609392009-08-22 08:28:47106 FilePath test_file_path(test_dir_path);
[email protected]719f9732009-09-02 05:04:57107 test_file_path = test_file_path.AppendASCII(
108 kEncodingTestDatas[i].file_name);
[email protected]8a609392009-08-22 08:28:47109
[email protected]9d6dcdf2010-03-26 16:12:10110 NavigateToURL(URLRequestMockHTTPJob::GetMockUrl(test_file_path));
[email protected]8a609392009-08-22 08:28:47111
[email protected]41fc0322009-09-04 22:23:40112 std::string encoding;
[email protected]8a609392009-08-22 08:28:47113 EXPECT_TRUE(tab_proxy->GetPageCurrentEncoding(&encoding));
[email protected]7ebafde2011-09-09 03:38:31114 EXPECT_EQ(kEncodingTestDatas[i].encoding_name, encoding);
[email protected]8a609392009-08-22 08:28:47115 }
116}
117
[email protected]e45b40f2010-05-20 18:14:07118// Marked as flaky: see http://crbug.com/44668
119TEST_F(BrowserEncodingTest, FLAKY_TestOverrideEncoding) {
[email protected]719f9732009-09-02 05:04:57120 const char* const kTestFileName = "gb18030_with_iso88591_meta.html";
121 const char* const kExpectedFileName =
122 "expected_gb18030_saved_from_iso88591_meta.html";
123 const char* const kOverrideTestDir = "user_override";
[email protected]8a609392009-08-22 08:28:47124
[email protected]719f9732009-09-02 05:04:57125 FilePath test_dir_path = FilePath(kTestDir).AppendASCII(kOverrideTestDir);
126 test_dir_path = test_dir_path.AppendASCII(kTestFileName);
[email protected]158a0892009-10-14 20:32:19127 GURL url = URLRequestMockHTTPJob::GetMockUrl(test_dir_path);
[email protected]8a609392009-08-22 08:28:47128 scoped_refptr<TabProxy> tab_proxy(GetActiveTab());
129 ASSERT_TRUE(tab_proxy.get());
130 ASSERT_TRUE(tab_proxy->NavigateToURL(url));
131 WaitUntilTabCount(1);
132
133 // Get the encoding declared in the page.
[email protected]41fc0322009-09-04 22:23:40134 std::string encoding;
[email protected]8a609392009-08-22 08:28:47135 EXPECT_TRUE(tab_proxy->GetPageCurrentEncoding(&encoding));
[email protected]41fc0322009-09-04 22:23:40136 EXPECT_EQ(encoding, "ISO-8859-1");
[email protected]8a609392009-08-22 08:28:47137
138 // Override the encoding to "gb18030".
139 int64 last_nav_time = 0;
140 EXPECT_TRUE(tab_proxy->GetLastNavigationTime(&last_nav_time));
[email protected]41fc0322009-09-04 22:23:40141 EXPECT_TRUE(tab_proxy->OverrideEncoding("gb18030"));
[email protected]8a609392009-08-22 08:28:47142 EXPECT_TRUE(tab_proxy->WaitForNavigation(last_nav_time));
143
144 // Re-get the encoding of page. It should be gb18030.
145 EXPECT_TRUE(tab_proxy->GetPageCurrentEncoding(&encoding));
[email protected]41fc0322009-09-04 22:23:40146 EXPECT_EQ(encoding, "gb18030");
[email protected]8a609392009-08-22 08:28:47147
148 // Dump the page, the content of dump page should be identical to the
149 // expected result file.
[email protected]719f9732009-09-02 05:04:57150 FilePath full_file_name = save_dir_.AppendASCII(kTestFileName);
[email protected]8a609392009-08-22 08:28:47151 // We save the page as way of complete HTML file, which requires a directory
152 // name to save sub resources in it. Although this test file does not have
153 // sub resources, but the directory name is still required.
[email protected]828cabe2009-09-26 22:47:11154 EXPECT_TRUE(tab_proxy->SavePage(full_file_name, temp_sub_resource_dir_,
[email protected]8a609392009-08-22 08:28:47155 SavePackage::SAVE_AS_COMPLETE_HTML));
156 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
157 ASSERT_TRUE(browser.get());
[email protected]faf2ee42010-05-11 14:26:17158 EXPECT_TRUE(WaitForDownloadShelfVisible(browser.get()));
[email protected]719f9732009-09-02 05:04:57159 FilePath expected_file_name = FilePath().AppendASCII(kOverrideTestDir);
160 expected_file_name = expected_file_name.AppendASCII(kExpectedFileName);
161 CheckFile(full_file_name, expected_file_name, true);
[email protected]8a609392009-08-22 08:28:47162}
163
164// The following encodings are excluded from the auto-detection test because
165// it's a known issue that the current encoding detector does not detect them:
166// ISO-8859-4
167// ISO-8859-13
168// KOI8-U
169// macintosh
170// windows-874
171// windows-1252
172// windows-1253
173// windows-1257
174// windows-1258
175
176// For Hebrew, the expected encoding value is ISO-8859-8-I. See
177// http://crbug.com/2927 for more details.
[email protected]dbb0203e2010-10-20 07:58:29178// FLAKY / Disabled on CrOS: see http://crbug.com/44666
179#if defined(OS_CHROMEOS)
180#define MAYBE_TestEncodingAutoDetect DISABLED_TestEncodingAutoDetect
181#else
182#define MAYBE_TestEncodingAutoDetect FLAKY_TestEncodingAutoDetect
183#endif
184
185TEST_F(BrowserEncodingTest, MAYBE_TestEncodingAutoDetect) {
[email protected]8a609392009-08-22 08:28:47186 struct EncodingAutoDetectTestData {
[email protected]719f9732009-09-02 05:04:57187 const char* test_file_name; // File name of test data.
188 const char* expected_result; // File name of expected results.
[email protected]41fc0322009-09-04 22:23:40189 const char* expected_encoding; // expected encoding.
[email protected]8a609392009-08-22 08:28:47190 };
191 const EncodingAutoDetectTestData kTestDatas[] = {
[email protected]719f9732009-09-02 05:04:57192 { "Big5_with_no_encoding_specified.html",
193 "expected_Big5_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40194 "Big5" },
[email protected]719f9732009-09-02 05:04:57195 { "gb18030_with_no_encoding_specified.html",
196 "expected_gb18030_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40197 "gb18030" },
[email protected]719f9732009-09-02 05:04:57198 { "iso-8859-1_with_no_encoding_specified.html",
199 "expected_iso-8859-1_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40200 "ISO-8859-1" },
[email protected]719f9732009-09-02 05:04:57201 { "ISO-8859-5_with_no_encoding_specified.html",
202 "expected_ISO-8859-5_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40203 "ISO-8859-5" },
[email protected]719f9732009-09-02 05:04:57204 { "ISO-8859-6_with_no_encoding_specified.html",
205 "expected_ISO-8859-6_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40206 "ISO-8859-6" },
[email protected]719f9732009-09-02 05:04:57207 { "ISO-8859-7_with_no_encoding_specified.html",
208 "expected_ISO-8859-7_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40209 "ISO-8859-7" },
[email protected]719f9732009-09-02 05:04:57210 { "ISO-8859-8_with_no_encoding_specified.html",
211 "expected_ISO-8859-8_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40212 "ISO-8859-8-I" },
[email protected]719f9732009-09-02 05:04:57213 { "KOI8-R_with_no_encoding_specified.html",
214 "expected_KOI8-R_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40215 "KOI8-R" },
[email protected]719f9732009-09-02 05:04:57216 { "Shift-JIS_with_no_encoding_specified.html",
217 "expected_Shift-JIS_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40218 "Shift_JIS" },
[email protected]719f9732009-09-02 05:04:57219 { "UTF-8_with_no_encoding_specified.html",
220 "expected_UTF-8_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40221 "UTF-8" },
[email protected]719f9732009-09-02 05:04:57222 { "windows-949_with_no_encoding_specified.html",
223 "expected_windows-949_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40224 "windows-949" },
[email protected]719f9732009-09-02 05:04:57225 { "windows-1251_with_no_encoding_specified.html",
226 "expected_windows-1251_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40227 "windows-1251" },
[email protected]719f9732009-09-02 05:04:57228 { "windows-1254_with_no_encoding_specified.html",
229 "expected_windows-1254_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40230 "windows-1254" },
[email protected]719f9732009-09-02 05:04:57231 { "windows-1255_with_no_encoding_specified.html",
232 "expected_windows-1255_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40233 "windows-1255" },
[email protected]719f9732009-09-02 05:04:57234 { "windows-1256_with_no_encoding_specified.html",
235 "expected_windows-1256_saved_from_no_encoding_specified.html",
[email protected]41fc0322009-09-04 22:23:40236 "windows-1256" }
[email protected]8a609392009-08-22 08:28:47237 };
[email protected]719f9732009-09-02 05:04:57238 const char* const kAutoDetectDir = "auto_detect";
[email protected]8a609392009-08-22 08:28:47239 // Directory of the files of expected results.
[email protected]719f9732009-09-02 05:04:57240 const char* const kExpectedResultDir = "expected_results";
[email protected]8a609392009-08-22 08:28:47241
242 // Full path of saved file. full_file_name = save_dir_ + file_name[i];
[email protected]719f9732009-09-02 05:04:57243 FilePath full_saved_file_name;
[email protected]8a609392009-08-22 08:28:47244
[email protected]719f9732009-09-02 05:04:57245 FilePath test_dir_path = FilePath(kTestDir).AppendASCII(kAutoDetectDir);
[email protected]8a609392009-08-22 08:28:47246
[email protected]f73ebdf2009-09-22 02:31:04247 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
248 ASSERT_TRUE(browser.get());
249 // Set the default charset to one of encodings not supported by the current
250 // auto-detector (Please refer to the above comments) to make sure we
251 // incorrectly decode the page. Now we use ISO-8859-4.
[email protected]6fd35352010-03-16 08:39:54252 ASSERT_TRUE(browser->SetStringPreference(prefs::kDefaultCharset,
[email protected]ddd231e2010-06-29 20:35:19253 "ISO-8859-4"));
[email protected]f73ebdf2009-09-22 02:31:04254 scoped_refptr<TabProxy> tab(GetActiveTab());
255 ASSERT_TRUE(tab.get());
[email protected]8a609392009-08-22 08:28:47256
[email protected]f73ebdf2009-09-22 02:31:04257 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestDatas);i++) {
[email protected]8a609392009-08-22 08:28:47258 FilePath test_file_path(test_dir_path);
[email protected]719f9732009-09-02 05:04:57259 test_file_path = test_file_path.AppendASCII(kTestDatas[i].test_file_name);
[email protected]8a609392009-08-22 08:28:47260 GURL url =
[email protected]158a0892009-10-14 20:32:19261 URLRequestMockHTTPJob::GetMockUrl(test_file_path);
[email protected]8a609392009-08-22 08:28:47262 ASSERT_TRUE(tab->NavigateToURL(url));
[email protected]8a609392009-08-22 08:28:47263
264 // Disable auto detect if it is on.
265 EXPECT_TRUE(
266 browser->SetBooleanPreference(prefs::kWebKitUsesUniversalDetector,
267 false));
268 EXPECT_TRUE(tab->Reload());
269
270 // Get the encoding used for the page, it must be the default charset we
271 // just set.
[email protected]41fc0322009-09-04 22:23:40272 std::string encoding;
[email protected]8a609392009-08-22 08:28:47273 EXPECT_TRUE(tab->GetPageCurrentEncoding(&encoding));
[email protected]41fc0322009-09-04 22:23:40274 EXPECT_EQ(encoding, "ISO-8859-4");
[email protected]8a609392009-08-22 08:28:47275
276 // Enable the encoding auto detection.
277 EXPECT_TRUE(browser->SetBooleanPreference(
278 prefs::kWebKitUsesUniversalDetector, true));
279 EXPECT_TRUE(tab->Reload());
280
281 // Re-get the encoding of page. It should return the real encoding now.
282 bool encoding_auto_detect = false;
283 EXPECT_TRUE(
284 browser->GetBooleanPreference(prefs::kWebKitUsesUniversalDetector,
285 &encoding_auto_detect));
286 EXPECT_TRUE(encoding_auto_detect);
287 EXPECT_TRUE(tab->GetPageCurrentEncoding(&encoding));
288 EXPECT_EQ(encoding, kTestDatas[i].expected_encoding);
289
290 // Dump the page, the content of dump page should be equal with our expect
291 // result file.
[email protected]719f9732009-09-02 05:04:57292 full_saved_file_name = save_dir_.AppendASCII(kTestDatas[i].test_file_name);
[email protected]8a609392009-08-22 08:28:47293 // Full path of expect result file.
[email protected]719f9732009-09-02 05:04:57294 FilePath expected_result_file_name = FilePath().AppendASCII(kAutoDetectDir);
295 expected_result_file_name = expected_result_file_name.AppendASCII(
296 kExpectedResultDir);
297 expected_result_file_name = expected_result_file_name.AppendASCII(
298 kTestDatas[i].expected_result);
[email protected]828cabe2009-09-26 22:47:11299 EXPECT_TRUE(tab->SavePage(full_saved_file_name, temp_sub_resource_dir_,
[email protected]8a609392009-08-22 08:28:47300 SavePackage::SAVE_AS_COMPLETE_HTML));
[email protected]faf2ee42010-05-11 14:26:17301 EXPECT_TRUE(WaitForDownloadShelfVisible(browser.get()));
[email protected]719f9732009-09-02 05:04:57302 CheckFile(full_saved_file_name, expected_result_file_name, true);
[email protected]8a609392009-08-22 08:28:47303 }
304}