Chromium Code Reviews| 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..64342e88a453378d3cc64c5144f93ca31082b838 |
| --- /dev/null |
| +++ b/chrome/browser/page_cycler/page_cycler_unittest.cc |
| @@ -0,0 +1,348 @@ |
| +// 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/file_util.h" |
| +#include "base/path_service.h" |
| +#include "base/string_split.h" |
| +#include "base/string_util.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/app/chrome_command_ids.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/public/browser/render_view_host.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::RenderViewHost; |
| +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; |
| + |
| +bool LinesAreEqual(std::vector<std::string>& file_a, |
| + std::vector<std::string>& file_b) { |
| + if (file_a.size() != file_b.size()) |
|
Aaron Boodman
2012/05/29 21:39:08
I believe that you can just use std::equal?
Aaron Boodman
2012/05/30 01:08:42
In case this wasn't clear, I meant you can just us
Devlin
2012/05/30 01:45:15
Done.
|
| + return false; |
| + |
| + for (std::vector<std::string>::const_iterator it_a = file_a.begin(), |
| + it_b = file_b.begin(); it_a != file_a.end() && it_b != file_b.end(); |
| + ++it_a, ++it_b) { |
| + if (*it_a != *it_b) |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +// Read the file, and generate a vector of strings. |
| +std::vector<std::string> GetLinesFromFile(FilePath file) { |
| + std::string file_contents; |
| + CHECK(file_util::ReadFileToString(file, &file_contents)); |
| + if (file_contents[file_contents.size() - 1] == '\n') { |
|
Aaron Boodman
2012/05/29 21:39:08
No curly brace is required here.
Devlin
2012/05/30 01:45:15
Done.
|
| + file_contents = file_contents.substr(0, file_contents.size() - 1); |
|
Aaron Boodman
2012/05/29 21:39:08
file_contents.resize()
Devlin
2012/05/30 01:45:15
Done.
|
| + } |
| + |
| + std::vector<std::string> lines; |
| + base::SplitString(file_contents, '\n', &lines); |
| + |
| + return lines; |
| +} |
| + |
| +} // namespace |
| + |
| +class MockPageCycler : public PageCycler { |
| + public: |
| + MockPageCycler(Browser* browser, FilePath urls_file, FilePath errors_file) |
| + : PageCycler(browser, urls_file) { |
| + set_errors_file(errors_file); |
| + } |
| + |
| + MockPageCycler(Browser* browser, |
| + FilePath urls_file, |
| + FilePath errors_file, |
| + FilePath stats_file) |
| + : PageCycler(browser, urls_file) { |
| + set_stats_file(stats_file); |
| + set_errors_file(errors_file); |
| + } |
| + |
| + MOCK_METHOD3(DidFinishLoad, void(int64 frame_id, |
| + const GURL& validated_url, |
| + bool is_main_frame)); |
| + MOCK_METHOD6(DidFailProvisionalLoad, void(int64 frame_id, |
| + bool is_main_frame, |
| + const GURL& validated_url, |
| + int error_code, |
| + const string16& error_description, |
| + RenderViewHost* render_view_host)); |
| + MOCK_METHOD1(RenderViewGone, void(base::TerminationStatus status)); |
| + |
| + void PageCyclerDidFailProvisionalLoad( |
| + int64 frame_id, |
| + bool is_main_frame, |
| + const GURL& validated_url, |
| + int error_code, |
| + const string16& error_description, |
| + RenderViewHost* render_view_host) { |
| + PageCycler::DidFailProvisionalLoad(frame_id, is_main_frame, |
| + validated_url, |
| + error_code, error_description, |
| + render_view_host); |
| + } |
| + |
| + void PageCyclerDidFinishLoad(int64 frame_id, |
| + const GURL& validated_url, |
| + bool is_main_frame) { |
| + PageCycler::DidFinishLoad(frame_id, validated_url, is_main_frame); |
| + } |
| + |
| + private: |
| + virtual ~MockPageCycler() {} |
| + 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, NULL)); |
| + 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(); |
| + |
| + std::vector<std::string> errors_output = |
| + GetLinesFromFile(errors_output_file_path()); |
| + std::vector<std::string> errors_expected = |
| + GetLinesFromFile(errors_expected_file_path()); |
| + ASSERT_TRUE(LinesAreEqual(errors_output, errors_expected)); |
|
Aaron Boodman
2012/05/29 21:39:08
Why not just compare the file contents as strings?
Devlin
2012/05/30 01:45:15
Done.
|
| + file_util::Delete(errors_output_file_path(), false); |
|
Aaron Boodman
2012/05/29 21:39:08
Did you consider using ScopedTempDir here too?
Devlin
2012/05/30 01:45:15
Done.
|
| +} |
| + |
| +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(); |
| + std::vector<std::string> errors_output = |
| + GetLinesFromFile(errors_output_file_path()); |
| + std::vector<std::string> abort_expected = |
| + GetLinesFromFile(abort_expected_file_path()); |
| + ASSERT_TRUE(LinesAreEqual(errors_output, abort_expected)); |
| + 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); |
| +} |