OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/app/chrome_command_ids.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" |
| 9 #include "base/threading/sequenced_worker_pool.h" |
| 10 #include "base/utf_string_conversions.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/page_cycler/page_cycler.h" |
| 14 #include "chrome/common/chrome_paths.h" |
| 15 #include "chrome/common/url_constants.h" |
| 16 #include "chrome/test/base/browser_with_test_window_test.h" |
| 17 #include "chrome/test/base/testing_pref_service.h" |
| 18 #include "content/test/test_browser_thread.h" |
| 19 #include "net/base/net_errors.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 |
| 23 using ::testing::_; |
| 24 using ::testing::Invoke; |
| 25 using content::TestBrowserThread; |
| 26 using content::WebContentsObserver; |
| 27 using file_util::ContentsEqual; |
| 28 using file_util::PathExists; |
| 29 |
| 30 namespace { |
| 31 const int kFrameID = 1; |
| 32 const bool kIsMainFrame = true; |
| 33 const GURL kAboutURL = GURL(chrome::kAboutBlankURL); |
| 34 const int kSingleIteration = 1; |
| 35 } // namespace |
| 36 |
| 37 class MockPageCycler : public PageCycler { |
| 38 public: |
| 39 MockPageCycler(Browser* browser, FilePath urls_file, FilePath errors_file) |
| 40 : PageCycler(browser, urls_file, errors_file) {} |
| 41 |
| 42 MockPageCycler(Browser* browser, |
| 43 FilePath urls_file, |
| 44 FilePath errors_file, |
| 45 FilePath stats_file) |
| 46 : PageCycler(browser, urls_file, errors_file) { |
| 47 set_stats_file(stats_file); |
| 48 } |
| 49 |
| 50 MOCK_METHOD3(DidFinishLoad, void(int64 frame_id, |
| 51 const GURL& validated_url, |
| 52 bool is_main_frame)); |
| 53 MOCK_METHOD5(DidFailProvisionalLoad, void( |
| 54 int64 frame_id, |
| 55 bool is_main_frame, |
| 56 const GURL& validated_url, |
| 57 int error_code, |
| 58 const string16& error_description)); |
| 59 MOCK_METHOD1(RenderViewGone, void(base::TerminationStatus status)); |
| 60 |
| 61 void PageCyclerPrepareResults(void) { |
| 62 PrepareResults(); |
| 63 } |
| 64 |
| 65 void PageCyclerDidFailProvisionalLoad( |
| 66 int64 frame_id, |
| 67 bool is_main_frame, |
| 68 const GURL& validated_url, |
| 69 int error_code, |
| 70 const string16& error_description) { |
| 71 PageCycler::DidFailProvisionalLoad(frame_id, is_main_frame, |
| 72 validated_url, |
| 73 error_code, error_description); |
| 74 } |
| 75 |
| 76 void PageCyclerDidFinishLoad(int64 frame_id, |
| 77 const GURL& validated_url, |
| 78 bool is_main_frame) { |
| 79 PageCycler::DidFinishLoad(frame_id, validated_url, is_main_frame); |
| 80 } |
| 81 |
| 82 DISALLOW_COPY_AND_ASSIGN(MockPageCycler); |
| 83 }; |
| 84 |
| 85 class PageCyclerTest : public BrowserWithTestWindowTest { |
| 86 public: |
| 87 PageCyclerTest() {} |
| 88 ~PageCyclerTest() {} |
| 89 |
| 90 virtual void SetUp() OVERRIDE; |
| 91 void FailProvisionalLoad(int error_code, string16& error_description); |
| 92 void FinishLoad(); |
| 93 void RunPageCycler(int total_iterations); |
| 94 void PumpLoop(); |
| 95 void CloseBrowser(); |
| 96 |
| 97 MockPageCycler* page_cycler() { |
| 98 return page_cycler_.get(); |
| 99 } |
| 100 |
| 101 void set_page_cycler(MockPageCycler *page_cycler) { |
| 102 page_cycler_ = page_cycler; |
| 103 observers_.AddObserver(page_cycler); |
| 104 } |
| 105 |
| 106 const std::vector<GURL>* urls_for_test() { |
| 107 return page_cycler_->urls_for_test(); |
| 108 } |
| 109 |
| 110 FilePath stats_output_file_path() { |
| 111 return test_data_dir_.AppendASCII("stats_output"); |
| 112 } |
| 113 |
| 114 FilePath abort_expected_file_path() { |
| 115 return test_data_dir_.AppendASCII("abort_expected"); |
| 116 } |
| 117 |
| 118 FilePath errors_output_file_path() { |
| 119 return test_data_dir_.AppendASCII("errors_output"); |
| 120 } |
| 121 |
| 122 FilePath errors_expected_file_path() { |
| 123 return test_data_dir_.AppendASCII("errors_expected"); |
| 124 } |
| 125 |
| 126 FilePath about_url_file_path() { |
| 127 return test_data_dir_.AppendASCII("about_url"); |
| 128 } |
| 129 |
| 130 private: |
| 131 ObserverList<WebContentsObserver> observers_; |
| 132 scoped_refptr<MockPageCycler> page_cycler_; |
| 133 FilePath test_data_dir_; |
| 134 }; |
| 135 |
| 136 void PageCyclerTest::SetUp() { |
| 137 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); |
| 138 test_data_dir_ = test_data_dir_.AppendASCII("page_cycler"); |
| 139 |
| 140 BrowserWithTestWindowTest::SetUp(); |
| 141 AddTab(browser(), kAboutURL); |
| 142 ASSERT_FALSE(browser()->GetSelectedWebContents() == NULL); |
| 143 } |
| 144 |
| 145 void PageCyclerTest::FailProvisionalLoad(int error_code, |
| 146 string16& error_description) { |
| 147 FOR_EACH_OBSERVER(WebContentsObserver, |
| 148 observers_, |
| 149 DidFailProvisionalLoad(kFrameID, kIsMainFrame, kAboutURL, error_code, |
| 150 error_description)); |
| 151 PumpLoop(); |
| 152 } |
| 153 |
| 154 void PageCyclerTest::FinishLoad() { |
| 155 FOR_EACH_OBSERVER(WebContentsObserver, |
| 156 observers_, |
| 157 DidFinishLoad(kFrameID, kAboutURL, kIsMainFrame)); |
| 158 PumpLoop(); |
| 159 } |
| 160 |
| 161 void PageCyclerTest::RunPageCycler(int total_iterations) { |
| 162 page_cycler_->Run(total_iterations); |
| 163 PumpLoop(); |
| 164 } |
| 165 |
| 166 void PageCyclerTest::PumpLoop() { |
| 167 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 168 message_loop()->RunAllPending(); |
| 169 } |
| 170 |
| 171 void PageCyclerTest::CloseBrowser() { |
| 172 browser()->OnWindowClosing(); |
| 173 DestroyBrowser(); |
| 174 PumpLoop(); |
| 175 } |
| 176 |
| 177 TEST_F(PageCyclerTest, FailProvisionalLoads) { |
| 178 ASSERT_TRUE(PathExists(errors_expected_file_path())); |
| 179 ASSERT_TRUE(PathExists(about_url_file_path())); |
| 180 |
| 181 set_page_cycler(new MockPageCycler(browser(), |
| 182 about_url_file_path(), |
| 183 errors_output_file_path())); |
| 184 RunPageCycler(kSingleIteration); |
| 185 |
| 186 // Page cycler expects browser to automatically start loading the first page. |
| 187 EXPECT_CALL(*page_cycler(), DidFinishLoad(kFrameID, kAboutURL, kIsMainFrame)) |
| 188 .WillOnce(Invoke(page_cycler(), |
| 189 &MockPageCycler::PageCyclerDidFinishLoad)); |
| 190 FinishLoad(); |
| 191 |
| 192 // DNS server fail error message. |
| 193 string16 error_string = |
| 194 string16(ASCIIToUTF16(net::ErrorToString(net::ERR_DNS_SERVER_FAILED))); |
| 195 EXPECT_CALL(*page_cycler(), |
| 196 DidFailProvisionalLoad(kFrameID, kIsMainFrame, _, |
| 197 net::ERR_DNS_SERVER_FAILED, error_string)) |
| 198 .WillOnce(Invoke(page_cycler(), |
| 199 &MockPageCycler::PageCyclerDidFailProvisionalLoad)); |
| 200 FailProvisionalLoad(net::ERR_DNS_SERVER_FAILED, error_string); |
| 201 |
| 202 // DNS time-out error message. |
| 203 error_string = string16( |
| 204 ASCIIToUTF16(net::ErrorToString(net::ERR_DNS_TIMED_OUT))); |
| 205 EXPECT_CALL(*page_cycler(), |
| 206 DidFailProvisionalLoad(kFrameID, |
| 207 kIsMainFrame, _, net::ERR_DNS_TIMED_OUT, |
| 208 error_string)) |
| 209 .WillOnce(Invoke(page_cycler(), |
| 210 &MockPageCycler::PageCyclerDidFailProvisionalLoad)); |
| 211 |
| 212 FailProvisionalLoad(net::ERR_DNS_TIMED_OUT, error_string); |
| 213 |
| 214 // DNS time-out error message. |
| 215 error_string = string16( |
| 216 ASCIIToUTF16(net::ErrorToString(net::ERR_INVALID_URL))); |
| 217 EXPECT_CALL(*page_cycler(), |
| 218 DidFailProvisionalLoad(kFrameID, kIsMainFrame, _, |
| 219 net::ERR_INVALID_URL, error_string)) |
| 220 .WillOnce(Invoke(page_cycler(), |
| 221 &MockPageCycler::PageCyclerDidFailProvisionalLoad)); |
| 222 FailProvisionalLoad(net::ERR_INVALID_URL, error_string); |
| 223 |
| 224 PumpLoop(); |
| 225 ASSERT_TRUE(ContentsEqual(errors_output_file_path(), |
| 226 errors_expected_file_path())); |
| 227 file_util::Delete(errors_output_file_path(), false); |
| 228 } |
| 229 |
| 230 TEST_F(PageCyclerTest, StatsFile) { |
| 231 const int kNumLoads = 4; |
| 232 |
| 233 ASSERT_TRUE(PathExists(errors_expected_file_path())); |
| 234 ASSERT_TRUE(PathExists(about_url_file_path())); |
| 235 |
| 236 if (PathExists(stats_output_file_path())) |
| 237 file_util::Delete(stats_output_file_path(), false); |
| 238 ASSERT_FALSE(PathExists(stats_output_file_path())); |
| 239 |
| 240 set_page_cycler(new MockPageCycler(browser(), about_url_file_path(), |
| 241 errors_output_file_path())); |
| 242 page_cycler()->set_stats_file(stats_output_file_path()); |
| 243 RunPageCycler(kSingleIteration); |
| 244 |
| 245 for (int i = 0; i < kNumLoads; ++i) { |
| 246 EXPECT_CALL(*page_cycler(), DidFinishLoad( |
| 247 kFrameID, kAboutURL, kIsMainFrame)) |
| 248 .WillOnce(Invoke(page_cycler(), |
| 249 &MockPageCycler::PageCyclerDidFinishLoad)); |
| 250 FinishLoad(); |
| 251 } |
| 252 |
| 253 PumpLoop(); |
| 254 EXPECT_FALSE(PathExists(errors_output_file_path())); |
| 255 ASSERT_TRUE(PathExists(stats_output_file_path())); |
| 256 file_util::Delete(stats_output_file_path(), false); |
| 257 } |
| 258 |
| 259 TEST_F(PageCyclerTest, KillBrowserAndAbort) { |
| 260 ASSERT_TRUE(PathExists(abort_expected_file_path())); |
| 261 ASSERT_TRUE(PathExists(errors_expected_file_path())); |
| 262 ASSERT_TRUE(PathExists(about_url_file_path())); |
| 263 |
| 264 set_page_cycler(new MockPageCycler(browser(), |
| 265 about_url_file_path(), |
| 266 errors_output_file_path())); |
| 267 RunPageCycler(kSingleIteration); |
| 268 |
| 269 EXPECT_CALL(*page_cycler(), DidFinishLoad(kFrameID, kAboutURL, kIsMainFrame)) |
| 270 .WillOnce(Invoke(page_cycler(), |
| 271 &MockPageCycler::PageCyclerDidFinishLoad)); |
| 272 message_loop()->RunAllPending(); |
| 273 |
| 274 FinishLoad(); |
| 275 |
| 276 CloseBrowser(); |
| 277 PumpLoop(); |
| 278 ASSERT_TRUE(ContentsEqual(errors_output_file_path(), |
| 279 abort_expected_file_path())); |
| 280 file_util::Delete(errors_output_file_path(), false); |
| 281 } |
| 282 |
| 283 TEST_F(PageCyclerTest, MultipleIterations) { |
| 284 const int kMultipleIterations = 3; |
| 285 const int kNumLoads = 10; |
| 286 |
| 287 ASSERT_TRUE(PathExists(about_url_file_path())); |
| 288 |
| 289 set_page_cycler(new MockPageCycler(browser(), |
| 290 about_url_file_path(), |
| 291 errors_output_file_path())); |
| 292 page_cycler()->set_stats_file(stats_output_file_path()); |
| 293 RunPageCycler(kMultipleIterations); |
| 294 |
| 295 EXPECT_CALL(*page_cycler(), DidFinishLoad(kFrameID, kAboutURL, kIsMainFrame)) |
| 296 .WillRepeatedly(Invoke(page_cycler(), |
| 297 &MockPageCycler::PageCyclerDidFinishLoad)); |
| 298 |
| 299 for (int i = 0; i < kNumLoads; ++i) |
| 300 FinishLoad(); |
| 301 |
| 302 PumpLoop(); |
| 303 EXPECT_FALSE(PathExists(errors_output_file_path())); |
| 304 ASSERT_TRUE(PathExists(stats_output_file_path())); |
| 305 file_util::Delete(stats_output_file_path(), false); |
| 306 } |
OLD | NEW |