| Index: chrome/browser/page_cycler/page_cycler_unittest.cc
|
| diff --git a/chrome/browser/page_cycler/page_cycler_unittest.cc b/chrome/browser/page_cycler/page_cycler_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..7924867c3d5a95bc6f42661ca2df550f8780b3ae
|
| --- /dev/null
|
| +++ b/chrome/browser/page_cycler/page_cycler_unittest.cc
|
| @@ -0,0 +1,306 @@
|
| +// 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 "chrome/app/chrome_command_ids.h"
|
| +
|
| +#include "base/file_util.h"
|
| +#include "base/path_service.h"
|
| +#include "base/threading/sequenced_worker_pool.h"
|
| +#include "base/utf_string_conversions.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "chrome/browser/browser_process.h"
|
| +#include "chrome/browser/page_cycler/page_cycler.h"
|
| +#include "chrome/common/chrome_paths.h"
|
| +#include "chrome/common/url_constants.h"
|
| +#include "chrome/test/base/browser_with_test_window_test.h"
|
| +#include "chrome/test/base/testing_pref_service.h"
|
| +#include "content/test/test_browser_thread.h"
|
| +#include "net/base/net_errors.h"
|
| +#include "testing/gmock/include/gmock/gmock.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +using ::testing::_;
|
| +using ::testing::Invoke;
|
| +using content::TestBrowserThread;
|
| +using content::WebContentsObserver;
|
| +using file_util::ContentsEqual;
|
| +using file_util::PathExists;
|
| +
|
| +namespace {
|
| +const int kFrameID = 1;
|
| +const bool kIsMainFrame = true;
|
| +const GURL kAboutURL = GURL(chrome::kAboutBlankURL);
|
| +const int kSingleIteration = 1;
|
| +} // namespace
|
| +
|
| +class MockPageCycler : public PageCycler {
|
| + public:
|
| + MockPageCycler(Browser* browser, FilePath urls_file, FilePath errors_file)
|
| + : PageCycler(browser, urls_file, errors_file) {}
|
| +
|
| + MockPageCycler(Browser* browser,
|
| + FilePath urls_file,
|
| + FilePath errors_file,
|
| + FilePath stats_file)
|
| + : PageCycler(browser, urls_file, errors_file) {
|
| + set_stats_file(stats_file);
|
| + }
|
| +
|
| + MOCK_METHOD3(DidFinishLoad, void(int64 frame_id,
|
| + const GURL& validated_url,
|
| + bool is_main_frame));
|
| + MOCK_METHOD5(DidFailProvisionalLoad, void(
|
| + int64 frame_id,
|
| + bool is_main_frame,
|
| + const GURL& validated_url,
|
| + int error_code,
|
| + const string16& error_description));
|
| + MOCK_METHOD1(RenderViewGone, void(base::TerminationStatus status));
|
| +
|
| + void PageCyclerPrepareResults(void) {
|
| + PrepareResults();
|
| + }
|
| +
|
| + void PageCyclerDidFailProvisionalLoad(
|
| + int64 frame_id,
|
| + bool is_main_frame,
|
| + const GURL& validated_url,
|
| + int error_code,
|
| + const string16& error_description) {
|
| + PageCycler::DidFailProvisionalLoad(frame_id, is_main_frame,
|
| + validated_url,
|
| + error_code, error_description);
|
| + }
|
| +
|
| + void PageCyclerDidFinishLoad(int64 frame_id,
|
| + const GURL& validated_url,
|
| + bool is_main_frame) {
|
| + PageCycler::DidFinishLoad(frame_id, validated_url, is_main_frame);
|
| + }
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(MockPageCycler);
|
| +};
|
| +
|
| +class PageCyclerTest : public BrowserWithTestWindowTest {
|
| + public:
|
| + PageCyclerTest() {}
|
| + ~PageCyclerTest() {}
|
| +
|
| + virtual void SetUp() OVERRIDE;
|
| + void FailProvisionalLoad(int error_code, string16& error_description);
|
| + void FinishLoad();
|
| + void RunPageCycler(int total_iterations);
|
| + void PumpLoop();
|
| + void CloseBrowser();
|
| +
|
| + MockPageCycler* page_cycler() {
|
| + return page_cycler_.get();
|
| + }
|
| +
|
| + void set_page_cycler(MockPageCycler *page_cycler) {
|
| + page_cycler_ = page_cycler;
|
| + observers_.AddObserver(page_cycler);
|
| + }
|
| +
|
| + const std::vector<GURL>* urls_for_test() {
|
| + return page_cycler_->urls_for_test();
|
| + }
|
| +
|
| + FilePath stats_output_file_path() {
|
| + return test_data_dir_.AppendASCII("stats_output");
|
| + }
|
| +
|
| + FilePath abort_expected_file_path() {
|
| + return test_data_dir_.AppendASCII("abort_expected");
|
| + }
|
| +
|
| + FilePath errors_output_file_path() {
|
| + return test_data_dir_.AppendASCII("errors_output");
|
| + }
|
| +
|
| + FilePath errors_expected_file_path() {
|
| + return test_data_dir_.AppendASCII("errors_expected");
|
| + }
|
| +
|
| + FilePath about_url_file_path() {
|
| + return test_data_dir_.AppendASCII("about_url");
|
| + }
|
| +
|
| + private:
|
| + ObserverList<WebContentsObserver> observers_;
|
| + scoped_refptr<MockPageCycler> page_cycler_;
|
| + FilePath test_data_dir_;
|
| +};
|
| +
|
| +void PageCyclerTest::SetUp() {
|
| + PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
|
| + test_data_dir_ = test_data_dir_.AppendASCII("page_cycler");
|
| +
|
| + BrowserWithTestWindowTest::SetUp();
|
| + AddTab(browser(), kAboutURL);
|
| + ASSERT_FALSE(browser()->GetSelectedWebContents() == NULL);
|
| +}
|
| +
|
| +void PageCyclerTest::FailProvisionalLoad(int error_code,
|
| + string16& error_description) {
|
| + FOR_EACH_OBSERVER(WebContentsObserver,
|
| + observers_,
|
| + DidFailProvisionalLoad(kFrameID, kIsMainFrame, kAboutURL, error_code,
|
| + error_description));
|
| + PumpLoop();
|
| +}
|
| +
|
| +void PageCyclerTest::FinishLoad() {
|
| + FOR_EACH_OBSERVER(WebContentsObserver,
|
| + observers_,
|
| + DidFinishLoad(kFrameID, kAboutURL, kIsMainFrame));
|
| + PumpLoop();
|
| +}
|
| +
|
| +void PageCyclerTest::RunPageCycler(int total_iterations) {
|
| + page_cycler_->Run(total_iterations);
|
| + PumpLoop();
|
| +}
|
| +
|
| +void PageCyclerTest::PumpLoop() {
|
| + content::BrowserThread::GetBlockingPool()->FlushForTesting();
|
| + message_loop()->RunAllPending();
|
| +}
|
| +
|
| +void PageCyclerTest::CloseBrowser() {
|
| + browser()->OnWindowClosing();
|
| + DestroyBrowser();
|
| + PumpLoop();
|
| +}
|
| +
|
| +TEST_F(PageCyclerTest, FailProvisionalLoads) {
|
| + ASSERT_TRUE(PathExists(errors_expected_file_path()));
|
| + ASSERT_TRUE(PathExists(about_url_file_path()));
|
| +
|
| + set_page_cycler(new MockPageCycler(browser(),
|
| + about_url_file_path(),
|
| + errors_output_file_path()));
|
| + RunPageCycler(kSingleIteration);
|
| +
|
| + // Page cycler expects browser to automatically start loading the first page.
|
| + EXPECT_CALL(*page_cycler(), DidFinishLoad(kFrameID, kAboutURL, kIsMainFrame))
|
| + .WillOnce(Invoke(page_cycler(),
|
| + &MockPageCycler::PageCyclerDidFinishLoad));
|
| + FinishLoad();
|
| +
|
| + // DNS server fail error message.
|
| + string16 error_string =
|
| + string16(ASCIIToUTF16(net::ErrorToString(net::ERR_DNS_SERVER_FAILED)));
|
| + EXPECT_CALL(*page_cycler(),
|
| + DidFailProvisionalLoad(kFrameID, kIsMainFrame, _,
|
| + net::ERR_DNS_SERVER_FAILED, error_string))
|
| + .WillOnce(Invoke(page_cycler(),
|
| + &MockPageCycler::PageCyclerDidFailProvisionalLoad));
|
| + FailProvisionalLoad(net::ERR_DNS_SERVER_FAILED, error_string);
|
| +
|
| + // DNS time-out error message.
|
| + error_string = string16(
|
| + ASCIIToUTF16(net::ErrorToString(net::ERR_DNS_TIMED_OUT)));
|
| + EXPECT_CALL(*page_cycler(),
|
| + DidFailProvisionalLoad(kFrameID,
|
| + kIsMainFrame, _, net::ERR_DNS_TIMED_OUT,
|
| + error_string))
|
| + .WillOnce(Invoke(page_cycler(),
|
| + &MockPageCycler::PageCyclerDidFailProvisionalLoad));
|
| +
|
| + FailProvisionalLoad(net::ERR_DNS_TIMED_OUT, error_string);
|
| +
|
| + // DNS time-out error message.
|
| + error_string = string16(
|
| + ASCIIToUTF16(net::ErrorToString(net::ERR_INVALID_URL)));
|
| + EXPECT_CALL(*page_cycler(),
|
| + DidFailProvisionalLoad(kFrameID, kIsMainFrame, _,
|
| + net::ERR_INVALID_URL, error_string))
|
| + .WillOnce(Invoke(page_cycler(),
|
| + &MockPageCycler::PageCyclerDidFailProvisionalLoad));
|
| + FailProvisionalLoad(net::ERR_INVALID_URL, error_string);
|
| +
|
| + PumpLoop();
|
| + ASSERT_TRUE(ContentsEqual(errors_output_file_path(),
|
| + errors_expected_file_path()));
|
| + file_util::Delete(errors_output_file_path(), false);
|
| +}
|
| +
|
| +TEST_F(PageCyclerTest, StatsFile) {
|
| + const int kNumLoads = 4;
|
| +
|
| + ASSERT_TRUE(PathExists(errors_expected_file_path()));
|
| + ASSERT_TRUE(PathExists(about_url_file_path()));
|
| +
|
| + if (PathExists(stats_output_file_path()))
|
| + file_util::Delete(stats_output_file_path(), false);
|
| + ASSERT_FALSE(PathExists(stats_output_file_path()));
|
| +
|
| + set_page_cycler(new MockPageCycler(browser(), about_url_file_path(),
|
| + errors_output_file_path()));
|
| + page_cycler()->set_stats_file(stats_output_file_path());
|
| + RunPageCycler(kSingleIteration);
|
| +
|
| + for (int i = 0; i < kNumLoads; ++i) {
|
| + EXPECT_CALL(*page_cycler(), DidFinishLoad(
|
| + kFrameID, kAboutURL, kIsMainFrame))
|
| + .WillOnce(Invoke(page_cycler(),
|
| + &MockPageCycler::PageCyclerDidFinishLoad));
|
| + FinishLoad();
|
| + }
|
| +
|
| + PumpLoop();
|
| + EXPECT_FALSE(PathExists(errors_output_file_path()));
|
| + ASSERT_TRUE(PathExists(stats_output_file_path()));
|
| + file_util::Delete(stats_output_file_path(), false);
|
| +}
|
| +
|
| +TEST_F(PageCyclerTest, KillBrowserAndAbort) {
|
| + ASSERT_TRUE(PathExists(abort_expected_file_path()));
|
| + ASSERT_TRUE(PathExists(errors_expected_file_path()));
|
| + ASSERT_TRUE(PathExists(about_url_file_path()));
|
| +
|
| + set_page_cycler(new MockPageCycler(browser(),
|
| + about_url_file_path(),
|
| + errors_output_file_path()));
|
| + RunPageCycler(kSingleIteration);
|
| +
|
| + EXPECT_CALL(*page_cycler(), DidFinishLoad(kFrameID, kAboutURL, kIsMainFrame))
|
| + .WillOnce(Invoke(page_cycler(),
|
| + &MockPageCycler::PageCyclerDidFinishLoad));
|
| + message_loop()->RunAllPending();
|
| +
|
| + FinishLoad();
|
| +
|
| + CloseBrowser();
|
| + PumpLoop();
|
| + ASSERT_TRUE(ContentsEqual(errors_output_file_path(),
|
| + abort_expected_file_path()));
|
| + file_util::Delete(errors_output_file_path(), false);
|
| +}
|
| +
|
| +TEST_F(PageCyclerTest, MultipleIterations) {
|
| + const int kMultipleIterations = 3;
|
| + const int kNumLoads = 10;
|
| +
|
| + ASSERT_TRUE(PathExists(about_url_file_path()));
|
| +
|
| + set_page_cycler(new MockPageCycler(browser(),
|
| + about_url_file_path(),
|
| + errors_output_file_path()));
|
| + page_cycler()->set_stats_file(stats_output_file_path());
|
| + RunPageCycler(kMultipleIterations);
|
| +
|
| + EXPECT_CALL(*page_cycler(), DidFinishLoad(kFrameID, kAboutURL, kIsMainFrame))
|
| + .WillRepeatedly(Invoke(page_cycler(),
|
| + &MockPageCycler::PageCyclerDidFinishLoad));
|
| +
|
| + for (int i = 0; i < kNumLoads; ++i)
|
| + FinishLoad();
|
| +
|
| + PumpLoop();
|
| + EXPECT_FALSE(PathExists(errors_output_file_path()));
|
| + ASSERT_TRUE(PathExists(stats_output_file_path()));
|
| + file_util::Delete(stats_output_file_path(), false);
|
| +}
|
|
|