Index: chrome/browser/page_cycler/page_cycler_browsertest.cc |
diff --git a/chrome/browser/page_cycler/page_cycler_browsertest.cc b/chrome/browser/page_cycler/page_cycler_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0cae0f7275d8bdeeb4e9c68ba11f1c24d5ba9c2e |
--- /dev/null |
+++ b/chrome/browser/page_cycler/page_cycler_browsertest.cc |
@@ -0,0 +1,423 @@ |
+// Copyright (c) 2012 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 "base/command_line.h" |
+#include "base/file_path.h" |
+#include "chrome/browser/page_cycler/page_cycler.h" |
+#include "chrome/browser/ui/browser_list.h" |
+#include "chrome/test/base/in_process_browser_test.h" |
+#include "content/public/common/content_switches.h" |
+#include "chrome/common/chrome_switches.h" |
+ |
+#include "base/file_util.h" |
+#include "base/string_split.h" |
+#include "base/string_util.h" |
+#include "googleurl/src/gurl.h" |
+#include "chrome/test/base/ui_test_utils.h" |
+#include "base/path_service.h" |
+#include "chrome/common/chrome_paths.h" |
+#include "content/public/common/url_constants.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/profiles/profile_manager.h" |
+#include "chrome/test/base/testing_profile.h" |
+#include <unistd.h> |
+#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/profiles/profile_manager.h" |
+ |
+// Basic PageCyclerBrowserTest structure; used in testing most of PageCycler's |
+// functionality. |
+class PageCyclerBrowserTest : public InProcessBrowserTest { |
+ public: |
+ virtual void CleanUpOnMainThread() { |
+ file_util::Delete(temp_path_, true); // recursive |
+ } |
+ |
+ // Initialize file paths within a temporary directory; this should all be |
+ // empty and nonexistent. |
+ virtual void InitFilePaths(FilePath temp_path) { |
+ temp_path_ = temp_path; |
+ urls_file_ = temp_path.AppendASCII("urls_file"); |
+ errors_file_ = temp_path.AppendASCII("errors"); |
+ stats_file_ = temp_path.AppendASCII("stats"); |
+ |
+ ASSERT_FALSE(file_util::PathExists(urls_file_)); |
+ ASSERT_FALSE(file_util::PathExists(errors_file_)); |
+ ASSERT_FALSE(file_util::PathExists(stats_file_)); |
+ } |
+ |
+ // Initialize a PageCycler using either the base fields, or using provided |
+ // ones. |
+ void InitPageCycler() { |
+ page_cycler_ = new PageCycler(browser(), urls_file(), errors_file()); |
+ page_cycler_->set_stats_file(stats_file()); |
+ } |
+ void InitPageCycler(FilePath urls_file, |
+ FilePath errors_file, |
+ FilePath stats_file) { |
+ page_cycler_ = new PageCycler(browser(), urls_file, errors_file); |
+ page_cycler_->set_stats_file(stats_file); |
+ } |
+ |
+ // Get a collection of basic urls which are stored in the test directory. |
+ // NOTE: |test_server| must be started first! |
+ std::vector<GURL> GetURLs() { |
+ std::vector<GURL> urls; |
+ urls.push_back(test_server()->GetURL("files/page_cycler/basic_html.html")); |
+ urls.push_back(test_server()->GetURL("files/page_cycler/basic_js.html")); |
+ urls.push_back(test_server()->GetURL("files/page_cycler/basic_css.html")); |
+ return urls; |
+ } |
+ |
+ // Read the errors file, and generate a vector of error strings. |
+ std::vector<std::string> GetErrorsFromFile() { |
+ std::string error_file_contents; |
+ CHECK(file_util::ReadFileToString(errors_file_, |
+ &error_file_contents)); |
+ LOG(WARNING) << "Contents: \n" << error_file_contents; |
+ |
+ if (error_file_contents[error_file_contents.size() - 1] == '\n') { |
+ error_file_contents = |
+ error_file_contents.substr(0, error_file_contents.size() - 1); |
+ } |
+ |
+ std::vector<std::string> errors; |
+ base::SplitString(error_file_contents, '\n', &errors); |
+ |
+ return errors; |
+ } |
+ |
+ // Convert a vector of GURLs into a newline-separated string, ready to be |
+ // written to the urls file for PageCycler to use. |
+ std::string GetStringFromURLs(std::vector<GURL> urls) { |
+ std::string urls_string; |
+ for (std::vector<GURL>::const_iterator iter = urls.begin(); |
+ iter != urls.end(); ++iter) |
+ urls_string.append(iter->spec() + "\n"); |
+ return urls_string; |
+ } |
+ |
+ FilePath urls_file() { return urls_file_; } |
+ FilePath errors_file() { return errors_file_; } |
+ FilePath stats_file() { return stats_file_; } |
+ PageCycler* page_cycler() { return page_cycler_; } |
+ |
+ protected: |
+ FilePath temp_path_; |
+ FilePath urls_file_; |
+ FilePath errors_file_; |
+ FilePath stats_file_; |
+ PageCycler* page_cycler_; |
+}; |
+ |
+// Structure used for testing PageCycler's ability to playback a series of |
+// URLs given a cache directory. |
+class PageCyclerCachedBrowserTest : public PageCyclerBrowserTest { |
+ public: |
+ // For a cached test, we use the provided user data directory from the test |
+ // directory. |
+ virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
+ InProcessBrowserTest::SetUpCommandLine(command_line); |
+ |
+ FilePath test_dir; |
+ ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir)); |
+ test_dir = test_dir.AppendASCII("page_cycler"); |
+ |
+ FilePath source_data_dir = test_dir.AppendASCII("cached_data_dir"); |
+ CHECK(file_util::PathExists(source_data_dir)); |
+ |
+ CHECK(user_data_dir_.CreateUniqueTempDir()); |
+ |
+ FilePath dest_data_dir = |
+ user_data_dir_.path().AppendASCII("cached_data_dir"); |
+ CHECK(!file_util::PathExists(dest_data_dir)); |
+ |
+ CHECK(file_util::CopyDirectory(source_data_dir, |
+ user_data_dir_.path(), |
+ true)); // recursive. |
+ CHECK(file_util::PathExists(dest_data_dir)); |
+ |
+ command_line->AppendSwitchPath(switches::kUserDataDir, |
+ dest_data_dir); |
+ command_line->AppendSwitch(switches::kPlaybackMode); |
+ } |
+ |
+ virtual void CleanUpOnMainThread() { |
+ file_util::Delete(user_data_dir_.path(), true); // recursive |
+ PageCyclerBrowserTest::CleanUpOnMainThread(); |
+ } |
+ |
+ // Initialize the file paths to use the UserDataDir's urls file, instead |
+ // of one to be written. |
+ virtual void InitFilePaths(FilePath temp_path) OVERRIDE { |
+ urls_file_ = user_data_dir_.path().AppendASCII("cached_data_dir") |
+ .AppendASCII("urls"); |
+ errors_file_ = temp_path.AppendASCII("errors"); |
+ stats_file_ = temp_path.AppendASCII("stats"); |
+ |
+ ASSERT_TRUE(file_util::PathExists(urls_file_)); |
+ ASSERT_FALSE(file_util::PathExists(errors_file_)); |
+ ASSERT_FALSE(file_util::PathExists(stats_file_)); |
+ } |
+ |
+ private: |
+ // The directory storing the copy of the UserDataDir. |
+ ScopedTempDir user_data_dir_; |
+}; |
+ |
+// Structure used to test PageCycler's ability to record the all necessary |
+// information to revisit a series of URLs. |
+class PageCyclerRecordingBrowserTest : public PageCyclerBrowserTest { |
+ public: |
+ virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
+ InProcessBrowserTest::SetUpCommandLine(command_line); |
+ |
+ CHECK(user_data_dir_.CreateUniqueTempDir()); |
+ CHECK(file_util::PathExists(user_data_dir_.path())); |
+ |
+ command_line->AppendSwitchPath(switches::kUserDataDir, |
+ user_data_dir_.path()); |
+ // We have to be in record mode for these tests to work. |
+ command_line->AppendSwitch(switches::kRecordMode); |
+ } |
+ |
+ virtual void CleanUpOnMainThread() { |
+ file_util::Delete(user_data_dir_.path(), true); // recursive |
+ PageCyclerBrowserTest::CleanUpOnMainThread(); |
+ } |
+ |
+ const FilePath& user_data_dir() { return user_data_dir_.path(); } |
+ |
+ private: |
+ ScopedTempDir user_data_dir_; |
+}; |
+ |
+// Sanity check; iterate through a series of URLs and make sure there are no |
+// errors. |
+IN_PROC_BROWSER_TEST_F(PageCyclerBrowserTest, BasicTest) { |
+ const size_t kNumIterations = 3; |
+ ScopedTempDir temp; |
+ ASSERT_TRUE(temp.CreateUniqueTempDir()); |
+ |
+ InitFilePaths(temp.path()); |
+ |
+ ASSERT_TRUE(test_server()->Start()); |
+ |
+ std::string urls_string = GetStringFromURLs(GetURLs());; |
+ |
+ ASSERT_TRUE(file_util::WriteFile(urls_file(), urls_string.c_str(), |
+ urls_string.size())); |
+ |
+ InitPageCycler(); |
+ page_cycler()->Run(kNumIterations); |
+ |
+ ui_test_utils::RunMessageLoop(); |
+ ASSERT_FALSE(file_util::PathExists(errors_file())); |
+ ASSERT_TRUE(file_util::PathExists(stats_file())); |
+} |
+ |
+// Test to make sure that PageCycler will recognize unvisitable URLs, and will |
+// handle them appropriately. |
+IN_PROC_BROWSER_TEST_F(PageCyclerBrowserTest, UnvisitableURL) { |
+ const size_t kNumIterations = 3; |
+ const char kFakeURL[] = "http://www.pleasenoonehavethisurlanytimeinthenext" |
+ "century.com/gibberish"; |
+ ScopedTempDir temp; |
+ ASSERT_TRUE(temp.CreateUniqueTempDir()); |
+ |
+ InitFilePaths(temp.path()); |
+ |
+ ASSERT_TRUE(test_server()->Start()); |
+ |
+ std::vector<GURL> urls = GetURLs(); |
+ urls.push_back(GURL(kFakeURL)); |
+ std::string urls_string = GetStringFromURLs(urls); |
+ |
+ ASSERT_TRUE(file_util::WriteFile(urls_file(), urls_string.c_str(), |
+ urls_string.size())); |
+ |
+ InitPageCycler(); |
+ page_cycler()->Run(kNumIterations); |
+ |
+ ui_test_utils::RunMessageLoop(); |
+ ASSERT_TRUE(file_util::PathExists(errors_file())); |
+ ASSERT_TRUE(file_util::PathExists(stats_file())); |
+ |
+ std::vector<std::string> errors = GetErrorsFromFile(); |
+ |
+ size_t num_errors = errors.size(); |
+ ASSERT_EQ(kNumIterations, num_errors); |
+ |
+ // Check that each error message contains the fake URL (i.e., that it wasn't |
+ // from a valid URL, and that the fake URL was caught each time). |
+ for (std::vector<std::string>::const_iterator iter = errors.begin(); |
+ iter != errors.end(); ++iter) { |
+ ASSERT_NE(iter->find(kFakeURL), std::string::npos); |
+ } |
+} |
+ |
+// Test that PageCycler will remove an invalid URL prior to running. |
+IN_PROC_BROWSER_TEST_F(PageCyclerBrowserTest, InvalidURL) { |
+ const size_t kNumIterations = 1; |
+ const char kBadURL[] = "notarealurl"; |
+ |
+ ScopedTempDir temp; |
+ ASSERT_TRUE(temp.CreateUniqueTempDir()); |
+ |
+ InitFilePaths(temp.path()); |
+ |
+ ASSERT_TRUE(test_server()->Start()); |
+ |
+ std::string urls_string = GetStringFromURLs(GetURLs()); |
+ urls_string.append(kBadURL).append("\n"); |
+ |
+ ASSERT_TRUE(file_util::WriteFile(urls_file(), urls_string.c_str(), |
+ urls_string.size())); |
+ |
+ InitPageCycler(); |
+ page_cycler()->Run(kNumIterations); |
+ |
+ ui_test_utils::RunMessageLoop(); |
+ ASSERT_TRUE(file_util::PathExists(errors_file())); |
+ ASSERT_TRUE(file_util::PathExists(stats_file())); |
+ |
+ std::vector<std::string> errors = GetErrorsFromFile(); |
+ ASSERT_EQ(1u, errors.size()); |
+ |
+ std::string expected_error = "Omitting invalid URL: "; |
+ expected_error.append(kBadURL).append("."); |
+ |
+ ASSERT_FALSE(errors[0].compare(expected_error)); |
+} |
+ |
+// Test that PageCycler will remove a Chrome Error URL prior to running. |
+IN_PROC_BROWSER_TEST_F(PageCyclerBrowserTest, ChromeErrorURL) { |
+ const size_t kNumIterations = 1; |
+ ScopedTempDir temp; |
+ ASSERT_TRUE(temp.CreateUniqueTempDir()); |
+ |
+ InitFilePaths(temp.path()); |
+ |
+ ASSERT_TRUE(test_server()->Start()); |
+ |
+ std::vector<GURL> urls = GetURLs(); |
+ urls.push_back(GURL(content::kUnreachableWebDataURL)); |
+ std::string urls_string = GetStringFromURLs(urls); |
+ |
+ ASSERT_TRUE(file_util::WriteFile(urls_file(), urls_string.c_str(), |
+ urls_string.size())); |
+ |
+ InitPageCycler(); |
+ page_cycler()->Run(kNumIterations); |
+ |
+ ui_test_utils::RunMessageLoop(); |
+ ASSERT_TRUE(file_util::PathExists(errors_file())); |
+ ASSERT_TRUE(file_util::PathExists(stats_file())); |
+ |
+ std::vector<std::string> errors = GetErrorsFromFile(); |
+ ASSERT_EQ(1u, errors.size()); |
+ |
+ std::string expected_error = "Chrome error pages are not allowed as urls. " |
+ "Omitting url: "; |
+ expected_error.append(content::kUnreachableWebDataURL).append("."); |
+ |
+ ASSERT_FALSE(errors[0].compare(expected_error)); |
+} |
+ |
+// Test that PageCycler will visit all the urls from a cache directory |
+// successfully while in playback mode. |
+IN_PROC_BROWSER_TEST_F(PageCyclerCachedBrowserTest, PlaybackMode) { |
+ const size_t kNumIterations = 1; |
+ ScopedTempDir temp; |
+ ASSERT_TRUE(temp.CreateUniqueTempDir()); |
+ |
+ InitFilePaths(temp.path()); |
+ |
+ InitPageCycler(); |
+ |
+ page_cycler()->Run(kNumIterations); |
+ |
+ ui_test_utils::RunMessageLoop(); |
+ ASSERT_TRUE(file_util::PathExists(stats_file())); |
+ ASSERT_FALSE(file_util::PathExists(errors_file())); |
+} |
+ |
+// Test that PageCycler will have a cache miss if a URL is missing from the |
+// cache directory while in playback mode. |
+IN_PROC_BROWSER_TEST_F(PageCyclerCachedBrowserTest, URLNotInCache) { |
+ const size_t kNumIterations = 1; |
+ const char kCacheMissURL[] = "http://www.images.google.com/"; |
+ |
+ ScopedTempDir temp; |
+ ASSERT_TRUE(temp.CreateUniqueTempDir()); |
+ |
+ InitFilePaths(temp.path()); |
+ |
+ std::string urls_string; |
+ ASSERT_TRUE(file_util::ReadFileToString(urls_file(), |
+ &urls_string)); |
+ |
+ urls_string.append("\n").append(kCacheMissURL); |
+ FilePath new_urls_file = temp.path().AppendASCII("urls"); |
+ ASSERT_FALSE(file_util::PathExists(new_urls_file)); |
+ |
+ ASSERT_TRUE(file_util::WriteFile(new_urls_file, urls_string.c_str(), |
+ urls_string.size())); |
+ |
+ InitPageCycler(new_urls_file, errors_file(), stats_file()); |
+ page_cycler()->Run(kNumIterations); |
+ |
+ ui_test_utils::RunMessageLoop(); |
+ ASSERT_TRUE(file_util::PathExists(errors_file())); |
+ ASSERT_TRUE(file_util::PathExists(stats_file())); |
+ |
+ std::vector<std::string> errors = GetErrorsFromFile(); |
+ ASSERT_EQ(1u, errors.size()); |
+ |
+ std::string expected_error; |
+ expected_error.append("Failed to load the page at: ") |
+ .append(kCacheMissURL) |
+ .append(": The requested entry was not found in the cache."); |
+ |
+ ASSERT_FALSE(errors[0].compare(expected_error)); |
+} |
+ |
+// Test that PageCycler will record properly. |
+// NOTE: This test operates by comparing the size of a generated cache |
+// directory to a previously-generated cache directory. If the way or |
+// amount Chrome caches pages changes significantly, this test will fail. |
+// If this happens, please update the cache directory in |
+// chrome/test/data/page_cycler. |
+IN_PROC_BROWSER_TEST_F(PageCyclerRecordingBrowserTest, RecordMode) { |
+ const size_t kNumIterations = 1; |
+ ScopedTempDir temp; |
+ ASSERT_TRUE(temp.CreateUniqueTempDir()); |
+ |
+ InitFilePaths(temp.path()); |
+ |
+ ASSERT_TRUE(test_server()->Start()); |
+ std::string urls_string = GetStringFromURLs(GetURLs()); |
+ |
+ ASSERT_TRUE(file_util::WriteFile(urls_file(), urls_string.c_str(), |
+ urls_string.size())); |
+ |
+ InitPageCycler(); |
+ page_cycler()->Run(kNumIterations); |
+ |
+ ui_test_utils::RunMessageLoop(); |
+ ASSERT_FALSE(file_util::PathExists(errors_file())); |
+ ASSERT_TRUE(file_util::PathExists(stats_file())); |
+ |
+ FilePath test_cache_dir = user_data_dir().AppendASCII("Default") |
+ .AppendASCII("Cache"); |
+ ASSERT_TRUE(file_util::PathExists(test_cache_dir)); |
+ |
+ FilePath expected_cache_dir; |
+ ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &expected_cache_dir)); |
+ expected_cache_dir = expected_cache_dir.AppendASCII("page_cycler") |
+ .AppendASCII("expected_cache"); |
+ ASSERT_TRUE(file_util::PathExists(test_cache_dir)); |
+ |
+ ASSERT_EQ(file_util::ComputeDirectorySize(expected_cache_dir), |
+ file_util::ComputeDirectorySize(test_cache_dir)); |
+} |