Chromium Code Reviews| Index: chrome/test/perf/perf_ui_test_suite.cc |
| diff --git a/chrome/test/perf/perf_ui_test_suite.cc b/chrome/test/perf/perf_ui_test_suite.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d1aed336eada344a1d53ae7269337da45f9cfb55 |
| --- /dev/null |
| +++ b/chrome/test/perf/perf_ui_test_suite.cc |
| @@ -0,0 +1,182 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/test/perf/perf_ui_test_suite.h" |
| + |
| +#include "base/file_util.h" |
| +#include "base/json/json_file_value_serializer.h" |
| +#include "base/lazy_instance.h" |
| +#include "base/logging.h" |
| +#include "base/message_loop.h" |
| +#include "base/path_service.h" |
| +#include "base/string_util.h" |
| +#include "base/threading/platform_thread.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/themes/browser_theme_pack.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "chrome/common/extensions/extension.h" |
| +#include "chrome/test/perf/generate_profile.h" |
| +#include "content/public/test/test_browser_thread.h" |
| + |
| +using content::BrowserThread; |
| +using extensions::Extension; |
| + |
| +namespace { |
| + |
| +const int kNumURLs = 20000; |
| + |
| +const char kThemeExtension[] = "mblmlcbknbnfebdfjnolmcapmdofhmme"; |
| + |
| +base::LazyInstance<base::FilePath> g_default_profile_dir = |
| + LAZY_INSTANCE_INITIALIZER; |
| +base::LazyInstance<base::FilePath> g_complex_profile_dir = |
| + LAZY_INSTANCE_INITIALIZER; |
| + |
| +} // namespace |
| + |
| +PerfUITestSuite::PerfUITestSuite(int argc, char** argv) |
| + : UITestSuite(argc, argv) { |
| +} |
| + |
| +PerfUITestSuite::~PerfUITestSuite() { |
| +} |
| + |
| +base::FilePath PerfUITestSuite::GetPathForProfileType( |
| + ProfileType profile_type) { |
| + switch (profile_type) { |
| + case DEFAULT_THEME: |
| + return g_default_profile_dir.Get(); |
| + case COMPLEX_THEME: |
| + return g_complex_profile_dir.Get(); |
| + default: |
| + NOTREACHED(); |
| + return base::FilePath(); |
| + } |
| +} |
| + |
| +void PerfUITestSuite::Initialize() { |
| + // We do normal initialization afterwards because we want |
|
Randy Smith (Not in Mondays)
2013/04/30 21:27:36
nit: Dangling sentence?
|
| + UITestSuite::Initialize(); |
| + |
| + if (!default_profile_dir_.CreateUniqueTempDir()) { |
| + LOG(FATAL) << "Failed to create default profile directory..."; |
| + } |
| + |
| + // Build a profile in default profile dir. |
| + base::FilePath default_path = |
| + default_profile_dir_.path().AppendASCII("Default"); |
| + if (!GenerateProfile(TOP_SITES, kNumURLs, default_path)) { |
| + LOG(FATAL) << "Failed to generate default profile for tests..."; |
| + } |
| + |
| + g_default_profile_dir.Get() = default_profile_dir_.path(); |
| + |
| + if (!complex_profile_dir_.CreateUniqueTempDir()) { |
| + LOG(FATAL) << "Failed to create complex profile directory..."; |
| + } |
| + |
| + if (!file_util::CopyDirectory(default_path, |
| + complex_profile_dir_.path(), |
| + true)) { |
| + LOG(FATAL) << "Failed to copy data to complex profile directory..."; |
| + } |
| + |
| + base::FilePath complex_profile_default_dir = |
| + complex_profile_dir_.path().AppendASCII("Default"); |
| + |
| + // Copy the Extensions directory from the template into the |
| + // complex_profile_dir dir. |
| + base::FilePath base_data_dir; |
| + if (!PathService::Get(chrome::DIR_TEST_DATA, &base_data_dir)) |
| + LOG(FATAL) << "Failed to fetch test data dir"; |
| + |
| + base_data_dir = base_data_dir.AppendASCII("profiles"); |
| + base_data_dir = base_data_dir.AppendASCII("profile_with_complex_theme"); |
| + base_data_dir = base_data_dir.AppendASCII("Default"); |
| + |
| + base::FilePath extensions_dir = base_data_dir.AppendASCII("Extensions"); |
| + |
| + if (!file_util::CopyDirectory(extensions_dir, |
| + complex_profile_default_dir, |
| + true)) { |
| + LOG(FATAL) << "Failed to copy Extensions from complex profile template"; |
| + } |
| + |
| + // Parse the manifest and make a temporary extension object because the |
| + // theme system takes extensions as input. |
| + base::FilePath extension_base = |
| + complex_profile_default_dir.AppendASCII("Extensions") |
| + .AppendASCII(kThemeExtension) |
| + .AppendASCII("1.1"); |
| + BuildCachedThemePakIn(extension_base); |
| + |
| + ExpandTemplate(base_data_dir, complex_profile_default_dir); |
| + |
| + g_complex_profile_dir.Get() = complex_profile_dir_.path(); |
| +} |
| + |
| +void PerfUITestSuite::BuildCachedThemePakIn( |
| + const base::FilePath& extension_base) { |
| + int error_code = 0; |
| + std::string error; |
| + JSONFileValueSerializer serializer( |
| + extension_base.AppendASCII("manifest.json")); |
| + scoped_ptr<DictionaryValue> valid_value(static_cast<DictionaryValue*>( |
| + serializer.Deserialize(&error_code, &error))); |
| + if (error_code != 0 || !valid_value) |
| + LOG(FATAL) << "Error parsing theme manifest: " << error; |
| + |
| + scoped_refptr<Extension> extension = |
| + Extension::Create(extension_base, |
| + extensions::Manifest::INVALID_LOCATION, |
| + *valid_value, |
| + Extension::NO_FLAGS, |
| + &error); |
| + if (!extension) |
| + LOG(FATAL) << "Error loading theme extension: " << error; |
| + |
| + // Build the "Cached Theme.pak" file in the template. (It's not committed |
| + // both because committing large binary files is bad in a git world and |
| + // because people don't remember to update it anyway, meaning we usually |
| + // have the theme rebuild penalty in our data.) |
| + MessageLoopForUI message_loop_; |
| + content::TestBrowserThread ui_thread_(BrowserThread::UI, &message_loop_); |
| + content::TestBrowserThread file_thread_(BrowserThread::FILE, |
| + &message_loop_); |
| + |
| + scoped_refptr<BrowserThemePack> theme( |
| + BrowserThemePack::BuildFromExtension(extension)); |
| + if (!theme) |
| + LOG(FATAL) << "Failed to load theme from extension"; |
| + |
| + theme->WriteToDisk(extension_base.AppendASCII("Cached Theme.pak")); |
| +} |
| + |
| +void PerfUITestSuite::ExpandTemplate( |
| + const base::FilePath& base_data_dir, |
| + const base::FilePath& complex_profile_default_dir) { |
| + std::string contents; |
| + if (!file_util::ReadFileToString( |
| + base_data_dir.AppendASCII("PreferencesTemplate"), |
| + &contents)) { |
| + LOG(FATAL) << "Failed to read preferences template"; |
| + } |
| + |
| + std::string path; |
| +#if defined(OS_WIN) |
| + path = UTF16ToUTF8(complex_profile_dir_.path().value()); |
| +#else |
| + path = complex_profile_dir_.path().value(); |
| +#endif |
| + |
| + std::vector<std::string> subst; |
| + subst.push_back(path); |
| + std::string expanded = ReplaceStringPlaceholders(contents, subst, NULL); |
| + |
| + if (file_util::WriteFile( |
| + complex_profile_default_dir.AppendASCII("Preferences"), |
| + expanded.c_str(), expanded.size()) == -1) { |
| + LOG(FATAL) << "Failed to write preferences file to profile with theme"; |
| + } |
| +} |