OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/test/ui/ui_test.h" | 5 #include "chrome/test/ui/ui_test.h" |
6 | 6 |
7 #if defined(OS_POSIX) | 7 #if defined(OS_POSIX) |
8 #include <signal.h> | 8 #include <signal.h> |
9 #include <sys/types.h> | 9 #include <sys/types.h> |
10 #endif | 10 #endif |
11 | 11 |
12 #include <set> | 12 #include <set> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "app/sql/connection.h" | 15 #include "app/sql/connection.h" |
16 #include "base/base_switches.h" | 16 #include "base/base_switches.h" |
17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
18 #include "base/file_path.h" | 18 #include "base/file_path.h" |
19 #include "base/file_util.h" | 19 #include "base/file_util.h" |
20 #include "base/path_service.h" | 20 #include "base/path_service.h" |
21 #include "base/platform_thread.h" | 21 #include "base/platform_thread.h" |
22 #include "base/process_util.h" | 22 #include "base/process_util.h" |
23 #include "base/scoped_ptr.h" | 23 #include "base/scoped_ptr.h" |
| 24 #include "base/scoped_temp_dir.h" |
24 #include "base/string_util.h" | 25 #include "base/string_util.h" |
25 #include "base/test/test_file_util.h" | 26 #include "base/test/test_file_util.h" |
26 #include "base/time.h" | 27 #include "base/time.h" |
27 #include "chrome/app/chrome_dll_resource.h" | 28 #include "chrome/app/chrome_dll_resource.h" |
28 #include "chrome/browser/net/url_fixer_upper.h" | 29 #include "chrome/browser/net/url_fixer_upper.h" |
29 #include "chrome/common/chrome_constants.h" | 30 #include "chrome/common/chrome_constants.h" |
30 #include "chrome/common/chrome_paths.h" | 31 #include "chrome/common/chrome_paths.h" |
31 #include "chrome/common/chrome_switches.h" | 32 #include "chrome/common/chrome_switches.h" |
32 #include "chrome/common/debug_flags.h" | 33 #include "chrome/common/debug_flags.h" |
33 #include "chrome/common/logging_chrome.h" | 34 #include "chrome/common/logging_chrome.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 clear_profile_(true), | 119 clear_profile_(true), |
119 include_testing_id_(true), | 120 include_testing_id_(true), |
120 enable_file_cookies_(true), | 121 enable_file_cookies_(true), |
121 profile_type_(UITestBase::DEFAULT_THEME), | 122 profile_type_(UITestBase::DEFAULT_THEME), |
122 shutdown_type_(UITestBase::WINDOW_CLOSE), | 123 shutdown_type_(UITestBase::WINDOW_CLOSE), |
123 test_start_time_(Time::NowFromSystemTime()), | 124 test_start_time_(Time::NowFromSystemTime()), |
124 command_execution_timeout_ms_(kCommandExecutionTimeout), | 125 command_execution_timeout_ms_(kCommandExecutionTimeout), |
125 action_timeout_ms_(kWaitForActionMsec), | 126 action_timeout_ms_(kWaitForActionMsec), |
126 action_max_timeout_ms_(kWaitForActionMaxMsec), | 127 action_max_timeout_ms_(kWaitForActionMaxMsec), |
127 sleep_timeout_ms_(kWaitForActionMsec), | 128 sleep_timeout_ms_(kWaitForActionMsec), |
128 terminate_timeout_ms_(kWaitForTerminateMsec) { | 129 terminate_timeout_ms_(kWaitForTerminateMsec), |
| 130 temp_profile_dir_(new ScopedTempDir()) { |
129 PathService::Get(chrome::DIR_APP, &browser_directory_); | 131 PathService::Get(chrome::DIR_APP, &browser_directory_); |
130 PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_); | 132 PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_); |
131 } | 133 } |
132 | 134 |
133 UITestBase::UITestBase(MessageLoop::Type msg_loop_type) | 135 UITestBase::UITestBase(MessageLoop::Type msg_loop_type) |
134 : launch_arguments_(CommandLine::ARGUMENTS_ONLY), | 136 : launch_arguments_(CommandLine::ARGUMENTS_ONLY), |
135 expected_errors_(0), | 137 expected_errors_(0), |
136 expected_crashes_(0), | 138 expected_crashes_(0), |
137 homepage_(L"about:blank"), | 139 homepage_(L"about:blank"), |
138 wait_for_initial_loads_(true), | 140 wait_for_initial_loads_(true), |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 | 354 |
353 void UITestBase::StopHttpServer() { | 355 void UITestBase::StopHttpServer() { |
354 scoped_ptr<CommandLine> cmd_line(CreateHttpServerCommandLine()); | 356 scoped_ptr<CommandLine> cmd_line(CreateHttpServerCommandLine()); |
355 ASSERT_TRUE(cmd_line.get()); | 357 ASSERT_TRUE(cmd_line.get()); |
356 cmd_line->AppendSwitchWithValue("server", "stop"); | 358 cmd_line->AppendSwitchWithValue("server", "stop"); |
357 RunCommand(*cmd_line.get()); | 359 RunCommand(*cmd_line.get()); |
358 } | 360 } |
359 | 361 |
360 void UITestBase::LaunchBrowser(const CommandLine& arguments, | 362 void UITestBase::LaunchBrowser(const CommandLine& arguments, |
361 bool clear_profile) { | 363 bool clear_profile) { |
362 #if defined(OS_POSIX) | 364 if (clear_profile || !temp_profile_dir_->IsValid()) { |
363 const char* alternative_userdir = getenv("CHROME_UI_TESTS_USER_DATA_DIR"); | 365 temp_profile_dir_.reset(new ScopedTempDir()); |
364 #else | 366 ASSERT_TRUE(temp_profile_dir_->CreateUniqueTempDir()); |
365 const FilePath::StringType::value_type* const alternative_userdir = NULL; | |
366 #endif | |
367 | 367 |
368 if (alternative_userdir) { | 368 // Update the information about user data directory location on the ui_test |
369 user_data_dir_ = FilePath(alternative_userdir); | 369 // side. Using PathService seems to be the most reliable, consistent way |
370 } else { | 370 // to do that. |
371 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_); | 371 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir())); |
372 } | 372 } |
373 | 373 |
374 // Clear user data directory to make sure test environment is consistent | |
375 // We balk on really short (absolute) user_data_dir directory names, because | |
376 // we're worried that they'd accidentally be root or something. | |
377 ASSERT_LT(10, static_cast<int>(user_data_dir_.value().size())) << | |
378 "The user data directory name passed into this test was too " | |
379 "short to delete safely. Please check the user-data-dir " | |
380 "argument and try again."; | |
381 if (clear_profile) | |
382 ASSERT_TRUE(file_util::DieFileDie(user_data_dir_, true)); | |
383 | |
384 if (!template_user_data_.empty()) { | 374 if (!template_user_data_.empty()) { |
385 // Recursively copy the template directory to the user_data_dir. | 375 // Recursively copy the template directory to the user_data_dir. |
386 ASSERT_TRUE(file_util::CopyRecursiveDirNoCache( | 376 ASSERT_TRUE(file_util::CopyRecursiveDirNoCache( |
387 template_user_data_, | 377 template_user_data_, |
388 user_data_dir_)); | 378 user_data_dir())); |
389 // If we're using the complex theme data, we need to write the | 379 // If we're using the complex theme data, we need to write the |
390 // user_data_dir_ to our preferences file. | 380 // user_data_dir_ to our preferences file. |
391 if (profile_type_ == UITestBase::COMPLEX_THEME) { | 381 if (profile_type_ == UITestBase::COMPLEX_THEME) { |
392 RewritePreferencesFile(user_data_dir_); | 382 RewritePreferencesFile(user_data_dir()); |
393 } | 383 } |
394 | 384 |
395 // Update the history file to include recent dates. | 385 // Update the history file to include recent dates. |
396 UpdateHistoryDates(); | 386 UpdateHistoryDates(); |
397 } | 387 } |
398 | 388 |
399 ASSERT_TRUE(LaunchBrowserHelper(arguments, false, &process_)); | 389 ASSERT_TRUE(LaunchBrowserHelper(arguments, false, &process_)); |
400 process_id_ = base::GetProcId(process_); | 390 process_id_ = base::GetProcId(process_); |
401 } | 391 } |
402 | 392 |
(...skipping 614 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1017 // Rewrite prefs file. | 1007 // Rewrite prefs file. |
1018 std::vector<string16> subst; | 1008 std::vector<string16> subst; |
1019 subst.push_back(WideToUTF16(user_data_dir_w)); | 1009 subst.push_back(WideToUTF16(user_data_dir_w)); |
1020 const std::string prefs_string = | 1010 const std::string prefs_string = |
1021 UTF16ToASCII(ReplaceStringPlaceholders(format_string, subst, NULL)); | 1011 UTF16ToASCII(ReplaceStringPlaceholders(format_string, subst, NULL)); |
1022 EXPECT_TRUE(file_util::WriteFile(pref_path, prefs_string.c_str(), | 1012 EXPECT_TRUE(file_util::WriteFile(pref_path, prefs_string.c_str(), |
1023 prefs_string.size())); | 1013 prefs_string.size())); |
1024 file_util::EvictFileFromSystemCache(pref_path); | 1014 file_util::EvictFileFromSystemCache(pref_path); |
1025 } | 1015 } |
1026 | 1016 |
| 1017 FilePath UITestBase::user_data_dir() const { |
| 1018 EXPECT_TRUE(temp_profile_dir_->IsValid()); |
| 1019 return temp_profile_dir_->path(); |
| 1020 } |
| 1021 |
1027 // static | 1022 // static |
1028 FilePath UITestBase::ComputeTypicalUserDataSource(ProfileType profile_type) { | 1023 FilePath UITestBase::ComputeTypicalUserDataSource(ProfileType profile_type) { |
1029 FilePath source_history_file; | 1024 FilePath source_history_file; |
1030 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, | 1025 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, |
1031 &source_history_file)); | 1026 &source_history_file)); |
1032 source_history_file = source_history_file.AppendASCII("profiles"); | 1027 source_history_file = source_history_file.AppendASCII("profiles"); |
1033 switch (profile_type) { | 1028 switch (profile_type) { |
1034 case UITestBase::DEFAULT_THEME: | 1029 case UITestBase::DEFAULT_THEME: |
1035 source_history_file = source_history_file.AppendASCII("typical_history"); | 1030 source_history_file = source_history_file.AppendASCII("typical_history"); |
1036 break; | 1031 break; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1119 command_line.AppendSwitch(switches::kNoFirstRun); | 1114 command_line.AppendSwitch(switches::kNoFirstRun); |
1120 | 1115 |
1121 // No default browser check, it would create an info-bar (if we are not the | 1116 // No default browser check, it would create an info-bar (if we are not the |
1122 // default browser) that could conflicts with some tests expectations. | 1117 // default browser) that could conflicts with some tests expectations. |
1123 command_line.AppendSwitch(switches::kNoDefaultBrowserCheck); | 1118 command_line.AppendSwitch(switches::kNoDefaultBrowserCheck); |
1124 | 1119 |
1125 // This is a UI test. | 1120 // This is a UI test. |
1126 command_line.AppendSwitchWithValue(switches::kTestType, | 1121 command_line.AppendSwitchWithValue(switches::kTestType, |
1127 ASCIIToWide(kUITestType)); | 1122 ASCIIToWide(kUITestType)); |
1128 | 1123 |
| 1124 // Tell the browser to use a temporary directory just for this test. |
| 1125 command_line.AppendSwitchWithValue(switches::kUserDataDir, |
| 1126 user_data_dir().ToWStringHack()); |
| 1127 |
1129 // We need cookies on file:// for things like the page cycler. | 1128 // We need cookies on file:// for things like the page cycler. |
1130 if (enable_file_cookies_) | 1129 if (enable_file_cookies_) |
1131 command_line.AppendSwitch(switches::kEnableFileCookies); | 1130 command_line.AppendSwitch(switches::kEnableFileCookies); |
1132 | 1131 |
1133 if (dom_automation_enabled_) | 1132 if (dom_automation_enabled_) |
1134 command_line.AppendSwitch(switches::kDomAutomationController); | 1133 command_line.AppendSwitch(switches::kDomAutomationController); |
1135 | 1134 |
1136 if (include_testing_id_) { | 1135 if (include_testing_id_) { |
1137 command_line.AppendSwitchWithValue(switches::kTestingChannelID, | 1136 command_line.AppendSwitchWithValue(switches::kTestingChannelID, |
1138 ASCIIToWide(server_->channel_id())); | 1137 ASCIIToWide(server_->channel_id())); |
(...skipping 16 matching lines...) Expand all Loading... |
1155 if (silent_dump_on_dcheck_) | 1154 if (silent_dump_on_dcheck_) |
1156 command_line.AppendSwitch(switches::kSilentDumpOnDCHECK); | 1155 command_line.AppendSwitch(switches::kSilentDumpOnDCHECK); |
1157 if (disable_breakpad_) | 1156 if (disable_breakpad_) |
1158 command_line.AppendSwitch(switches::kDisableBreakpad); | 1157 command_line.AppendSwitch(switches::kDisableBreakpad); |
1159 if (!homepage_.empty()) | 1158 if (!homepage_.empty()) |
1160 command_line.AppendSwitchWithValue(switches::kHomePage, | 1159 command_line.AppendSwitchWithValue(switches::kHomePage, |
1161 homepage_); | 1160 homepage_); |
1162 // Don't try to fetch web resources during UI testing. | 1161 // Don't try to fetch web resources during UI testing. |
1163 command_line.AppendSwitch(switches::kDisableWebResources); | 1162 command_line.AppendSwitch(switches::kDisableWebResources); |
1164 | 1163 |
1165 if (!user_data_dir_.empty()) | |
1166 command_line.AppendSwitchWithValue(switches::kUserDataDir, | |
1167 user_data_dir_.ToWStringHack()); | |
1168 if (!js_flags_.empty()) | 1164 if (!js_flags_.empty()) |
1169 command_line.AppendSwitchWithValue(switches::kJavaScriptFlags, | 1165 command_line.AppendSwitchWithValue(switches::kJavaScriptFlags, |
1170 js_flags_); | 1166 js_flags_); |
1171 if (!log_level_.empty()) | 1167 if (!log_level_.empty()) |
1172 command_line.AppendSwitchWithValue(switches::kLoggingLevel, log_level_); | 1168 command_line.AppendSwitchWithValue(switches::kLoggingLevel, log_level_); |
1173 | 1169 |
1174 command_line.AppendSwitch(switches::kMetricsRecordingOnly); | 1170 command_line.AppendSwitch(switches::kMetricsRecordingOnly); |
1175 | 1171 |
1176 if (!CommandLine::ForCurrentProcess()->HasSwitch(kEnableErrorDialogs)) | 1172 if (!CommandLine::ForCurrentProcess()->HasSwitch(kEnableErrorDialogs)) |
1177 command_line.AppendSwitch(switches::kEnableLogging); | 1173 command_line.AppendSwitch(switches::kEnableLogging); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1224 #endif | 1220 #endif |
1225 | 1221 |
1226 return started; | 1222 return started; |
1227 } | 1223 } |
1228 | 1224 |
1229 void UITestBase::UpdateHistoryDates() { | 1225 void UITestBase::UpdateHistoryDates() { |
1230 // Migrate the times in the segment_usage table to yesterday so we get | 1226 // Migrate the times in the segment_usage table to yesterday so we get |
1231 // actual thumbnails on the NTP. | 1227 // actual thumbnails on the NTP. |
1232 sql::Connection db; | 1228 sql::Connection db; |
1233 FilePath history = | 1229 FilePath history = |
1234 user_data_dir_.AppendASCII("Default").AppendASCII("History"); | 1230 user_data_dir().AppendASCII("Default").AppendASCII("History"); |
1235 // Not all test profiles have a history file. | 1231 // Not all test profiles have a history file. |
1236 if (!file_util::PathExists(history)) | 1232 if (!file_util::PathExists(history)) |
1237 return; | 1233 return; |
1238 | 1234 |
1239 ASSERT_TRUE(db.Open(history)); | 1235 ASSERT_TRUE(db.Open(history)); |
1240 Time yesterday = Time::Now() - TimeDelta::FromDays(1); | 1236 Time yesterday = Time::Now() - TimeDelta::FromDays(1); |
1241 std::string yesterday_str = Int64ToString(yesterday.ToInternalValue()); | 1237 std::string yesterday_str = Int64ToString(yesterday.ToInternalValue()); |
1242 std::string query = StringPrintf( | 1238 std::string query = StringPrintf( |
1243 "UPDATE segment_usage " | 1239 "UPDATE segment_usage " |
1244 "SET time_slot = %s " | 1240 "SET time_slot = %s " |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1526 set_ui_test_name(ASCIIToWide(test_name)); | 1522 set_ui_test_name(ASCIIToWide(test_name)); |
1527 } | 1523 } |
1528 UITestBase::SetUp(); | 1524 UITestBase::SetUp(); |
1529 PlatformTest::SetUp(); | 1525 PlatformTest::SetUp(); |
1530 } | 1526 } |
1531 | 1527 |
1532 void UITest::TearDown() { | 1528 void UITest::TearDown() { |
1533 UITestBase::TearDown(); | 1529 UITestBase::TearDown(); |
1534 PlatformTest::TearDown(); | 1530 PlatformTest::TearDown(); |
1535 } | 1531 } |
OLD | NEW |