| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/offline_pages/prerendering_loader.h" | 5 #include "chrome/browser/android/offline_pages/prerendering_loader.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "content/public/test/web_contents_tester.h" | 15 #include "content/public/test/web_contents_tester.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace offline_pages { | 18 namespace offline_pages { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // Adapter that intercepts prerender stack calls for testing. | 22 // Adapter that intercepts prerender stack calls for testing. |
| 23 class TestAdapter : public PrerenderAdapter { | 23 class TestAdapter : public PrerenderAdapter { |
| 24 public: | 24 public: |
| 25 explicit TestAdapter(PrerenderAdapter::Observer* observer) | 25 explicit TestAdapter(PrerenderAdapter::Observer* observer) |
| 26 : PrerenderAdapter(observer), | 26 : PrerenderAdapter(observer), |
| 27 active_(false), | 27 active_(false), |
| 28 disabled_(false), | |
| 29 fail_start_(false), | 28 fail_start_(false), |
| 30 observer_(observer), | 29 observer_(observer), |
| 31 final_status_(prerender::FINAL_STATUS_MAX) {} | 30 final_status_(prerender::FINAL_STATUS_MAX) {} |
| 32 ~TestAdapter() override {} | 31 ~TestAdapter() override {} |
| 33 | 32 |
| 34 // PrerenderAdapter implementation. | 33 // PrerenderAdapter implementation. |
| 35 bool CanPrerender() const override; | |
| 36 bool StartPrerender( | 34 bool StartPrerender( |
| 37 content::BrowserContext* browser_context, | 35 content::BrowserContext* browser_context, |
| 38 const GURL& url, | 36 const GURL& url, |
| 39 content::SessionStorageNamespace* session_storage_namespace, | 37 content::SessionStorageNamespace* session_storage_namespace, |
| 40 const gfx::Size& size) override; | 38 const gfx::Size& size) override; |
| 41 content::WebContents* GetWebContents() const override; | 39 content::WebContents* GetWebContents() const override; |
| 42 prerender::FinalStatus GetFinalStatus() const override; | 40 prerender::FinalStatus GetFinalStatus() const override; |
| 43 bool IsActive() const override; | 41 bool IsActive() const override; |
| 44 void DestroyActive() override; | 42 void DestroyActive() override; |
| 45 | 43 |
| 46 // Sets prerendering to be disabled. This will cause the CanPrerender() | |
| 47 // to return false. | |
| 48 void Disable(); | |
| 49 | |
| 50 // Sets prerendering to fail start prerender requests. | 44 // Sets prerendering to fail start prerender requests. |
| 51 void FailStart(); | 45 void FailStart(); |
| 52 | 46 |
| 53 // Configures mocked prerendering details. | 47 // Configures mocked prerendering details. |
| 54 void Configure(content::WebContents* web_contents, | 48 void Configure(content::WebContents* web_contents, |
| 55 prerender::FinalStatus final_status); | 49 prerender::FinalStatus final_status); |
| 56 | 50 |
| 57 // Returns the observer for test access. | 51 // Returns the observer for test access. |
| 58 PrerenderAdapter::Observer* GetObserver() const { return observer_; } | 52 PrerenderAdapter::Observer* GetObserver() const { return observer_; } |
| 59 | 53 |
| 60 private: | 54 private: |
| 61 bool active_; | 55 bool active_; |
| 62 bool disabled_; | |
| 63 bool fail_start_; | 56 bool fail_start_; |
| 64 PrerenderAdapter::Observer* observer_; | 57 PrerenderAdapter::Observer* observer_; |
| 65 std::unique_ptr<content::WebContents> web_contents_; | 58 std::unique_ptr<content::WebContents> web_contents_; |
| 66 prerender::FinalStatus final_status_; | 59 prerender::FinalStatus final_status_; |
| 67 | 60 |
| 68 DISALLOW_COPY_AND_ASSIGN(TestAdapter); | 61 DISALLOW_COPY_AND_ASSIGN(TestAdapter); |
| 69 }; | 62 }; |
| 70 | 63 |
| 71 void TestAdapter::Disable() { | |
| 72 disabled_ = true; | |
| 73 } | |
| 74 | |
| 75 void TestAdapter::FailStart() { | 64 void TestAdapter::FailStart() { |
| 76 fail_start_ = true; | 65 fail_start_ = true; |
| 77 } | 66 } |
| 78 | 67 |
| 79 void TestAdapter::Configure(content::WebContents* web_contents, | 68 void TestAdapter::Configure(content::WebContents* web_contents, |
| 80 prerender::FinalStatus final_status) { | 69 prerender::FinalStatus final_status) { |
| 81 web_contents_ = base::WrapUnique(web_contents); | 70 web_contents_ = base::WrapUnique(web_contents); |
| 82 final_status_ = final_status; | 71 final_status_ = final_status; |
| 83 } | 72 } |
| 84 | 73 |
| 85 bool TestAdapter::CanPrerender() const { | |
| 86 return !disabled_; | |
| 87 } | |
| 88 | |
| 89 bool TestAdapter::StartPrerender( | 74 bool TestAdapter::StartPrerender( |
| 90 content::BrowserContext* browser_context, | 75 content::BrowserContext* browser_context, |
| 91 const GURL& url, | 76 const GURL& url, |
| 92 content::SessionStorageNamespace* session_storage_namespace, | 77 content::SessionStorageNamespace* session_storage_namespace, |
| 93 const gfx::Size& size) { | 78 const gfx::Size& size) { |
| 94 if (fail_start_) | 79 if (fail_start_) |
| 95 return false; | 80 return false; |
| 96 active_ = true; | 81 active_ = true; |
| 97 return true; | 82 return true; |
| 98 } | 83 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 callback_called_ = false; | 146 callback_called_ = false; |
| 162 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 163 } | 148 } |
| 164 | 149 |
| 165 void PrerenderingLoaderTest::OnLoadDone(Offliner::RequestStatus load_status, | 150 void PrerenderingLoaderTest::OnLoadDone(Offliner::RequestStatus load_status, |
| 166 content::WebContents* web_contents) { | 151 content::WebContents* web_contents) { |
| 167 callback_called_ = true; | 152 callback_called_ = true; |
| 168 callback_load_status_ = load_status; | 153 callback_load_status_ = load_status; |
| 169 } | 154 } |
| 170 | 155 |
| 171 TEST_F(PrerenderingLoaderTest, CanPrerender) { | |
| 172 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 173 EXPECT_TRUE(loader()->CanPrerender()); | |
| 174 | |
| 175 test_adapter()->Disable(); | |
| 176 EXPECT_FALSE(loader()->CanPrerender()); | |
| 177 } | |
| 178 | |
| 179 TEST_F(PrerenderingLoaderTest, StopLoadingWhenIdle) { | 156 TEST_F(PrerenderingLoaderTest, StopLoadingWhenIdle) { |
| 180 EXPECT_TRUE(loader()->IsIdle()); | 157 EXPECT_TRUE(loader()->IsIdle()); |
| 181 loader()->StopLoading(); | 158 loader()->StopLoading(); |
| 182 EXPECT_TRUE(loader()->IsIdle()); | 159 EXPECT_TRUE(loader()->IsIdle()); |
| 183 } | 160 } |
| 184 | 161 |
| 185 TEST_F(PrerenderingLoaderTest, LoadPageLoadSucceededFromDomContentLoaded) { | 162 TEST_F(PrerenderingLoaderTest, LoadPageLoadSucceededFromDomContentLoaded) { |
| 186 test_adapter()->Configure( | 163 test_adapter()->Configure( |
| 187 content::WebContentsTester::CreateTestWebContents(profile(), NULL), | 164 content::WebContentsTester::CreateTestWebContents(profile(), NULL), |
| 188 prerender::FINAL_STATUS_USED); | 165 prerender::FINAL_STATUS_USED); |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 EXPECT_FALSE(loader()->IsIdle()); | 360 EXPECT_FALSE(loader()->IsIdle()); |
| 384 EXPECT_FALSE(loader()->IsLoaded()); | 361 EXPECT_FALSE(loader()->IsLoaded()); |
| 385 | 362 |
| 386 // Now try another load while first is still active. | 363 // Now try another load while first is still active. |
| 387 EXPECT_FALSE(loader()->LoadPage( | 364 EXPECT_FALSE(loader()->LoadPage( |
| 388 gurl, | 365 gurl, |
| 389 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this)))); | 366 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this)))); |
| 390 EXPECT_FALSE(loader()->IsIdle()); | 367 EXPECT_FALSE(loader()->IsIdle()); |
| 391 } | 368 } |
| 392 | 369 |
| 393 TEST_F(PrerenderingLoaderTest, LoadPageNotAcceptedWhenPrerenderingDisabled) { | |
| 394 test_adapter()->Disable(); | |
| 395 GURL gurl("http://testit.sea"); | |
| 396 EXPECT_TRUE(loader()->IsIdle()); | |
| 397 EXPECT_FALSE(loader()->LoadPage( | |
| 398 gurl, | |
| 399 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this)))); | |
| 400 EXPECT_TRUE(loader()->IsIdle()); | |
| 401 } | |
| 402 | |
| 403 TEST_F(PrerenderingLoaderTest, LoadPageNotAcceptedWhenStartPrerenderFalse) { | 370 TEST_F(PrerenderingLoaderTest, LoadPageNotAcceptedWhenStartPrerenderFalse) { |
| 404 test_adapter()->FailStart(); | 371 test_adapter()->FailStart(); |
| 405 GURL gurl("http://testit.sea"); | 372 GURL gurl("http://testit.sea"); |
| 406 EXPECT_TRUE(loader()->IsIdle()); | 373 EXPECT_TRUE(loader()->IsIdle()); |
| 407 EXPECT_FALSE(loader()->LoadPage( | 374 EXPECT_FALSE(loader()->LoadPage( |
| 408 gurl, | 375 gurl, |
| 409 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this)))); | 376 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this)))); |
| 410 EXPECT_TRUE(loader()->IsIdle()); | 377 EXPECT_TRUE(loader()->IsIdle()); |
| 411 } | 378 } |
| 412 | 379 |
| 413 } // namespace offline_pages | 380 } // namespace offline_pages |
| OLD | NEW |