| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/offline_pages/prerendering_loader.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "chrome/test/base/testing_profile.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "content/public/test/web_contents_tester.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace offline_pages { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Adapter that intercepts prerender stack calls for testing. |
| 22 class TestAdapter : public PrerenderAdapter { |
| 23 public: |
| 24 explicit TestAdapter(PrerenderAdapter::Observer* observer) |
| 25 : PrerenderAdapter(observer), observer_(observer) {} |
| 26 ~TestAdapter() override {} |
| 27 |
| 28 // PrerenderAdapter implementation. |
| 29 bool CanPrerender() const override; |
| 30 bool StartPrerender( |
| 31 content::BrowserContext* browser_context, |
| 32 const GURL& url, |
| 33 content::SessionStorageNamespace* session_storage_namespace, |
| 34 const gfx::Size& size) override; |
| 35 content::WebContents* GetWebContents() const override; |
| 36 prerender::FinalStatus GetFinalStatus() const override; |
| 37 bool IsActive() const override; |
| 38 void DestroyActive() override; |
| 39 |
| 40 // Sets prerendering to be disabled. This will cause the CanPrerender() |
| 41 // to return false. |
| 42 void Disable(); |
| 43 |
| 44 // Configures mocked prerendering details. |
| 45 void Configure(content::WebContents* web_contents, |
| 46 prerender::FinalStatus final_status); |
| 47 |
| 48 // Returns the observer for test access. |
| 49 PrerenderAdapter::Observer* GetObserver() const { return observer_; } |
| 50 |
| 51 private: |
| 52 bool active_; |
| 53 bool disabled_; |
| 54 PrerenderAdapter::Observer* observer_; |
| 55 content::WebContents* web_contents_; |
| 56 prerender::FinalStatus final_status_; |
| 57 |
| 58 DISALLOW_COPY_AND_ASSIGN(TestAdapter); |
| 59 }; |
| 60 |
| 61 void TestAdapter::Disable() { |
| 62 disabled_ = true; |
| 63 } |
| 64 |
| 65 void TestAdapter::Configure(content::WebContents* web_contents, |
| 66 prerender::FinalStatus final_status) { |
| 67 web_contents_ = web_contents; |
| 68 final_status_ = final_status; |
| 69 } |
| 70 |
| 71 bool TestAdapter::CanPrerender() const { |
| 72 return !disabled_; |
| 73 } |
| 74 |
| 75 bool TestAdapter::StartPrerender( |
| 76 content::BrowserContext* browser_context, |
| 77 const GURL& url, |
| 78 content::SessionStorageNamespace* session_storage_namespace, |
| 79 const gfx::Size& size) { |
| 80 active_ = true; |
| 81 return true; |
| 82 } |
| 83 |
| 84 content::WebContents* TestAdapter::GetWebContents() const { |
| 85 return web_contents_; |
| 86 } |
| 87 |
| 88 prerender::FinalStatus TestAdapter::GetFinalStatus() const { |
| 89 return final_status_; |
| 90 } |
| 91 |
| 92 bool TestAdapter::IsActive() const { |
| 93 return active_; |
| 94 } |
| 95 |
| 96 void TestAdapter::DestroyActive() { |
| 97 active_ = false; |
| 98 } |
| 99 |
| 100 void PumpLoop() { |
| 101 base::RunLoop().RunUntilIdle(); |
| 102 } |
| 103 |
| 104 } // namespace |
| 105 |
| 106 // Test class. |
| 107 class PrerenderingLoaderTest : public testing::Test { |
| 108 public: |
| 109 PrerenderingLoaderTest(); |
| 110 ~PrerenderingLoaderTest() override {} |
| 111 |
| 112 void SetUp() override; |
| 113 |
| 114 // Returns the PrerenderLoader to test. |
| 115 PrerenderingLoader* loader() const { return loader_.get(); } |
| 116 // Returns the TestAdapter to allow test behavior configuration. |
| 117 TestAdapter* test_adapter() const { return test_adapter_; } |
| 118 bool callback_called() { return callback_called_; } |
| 119 Offliner::RequestStatus callback_load_status() { |
| 120 return callback_load_status_; |
| 121 } |
| 122 Profile* profile() { return &profile_; } |
| 123 void OnLoadDone(Offliner::RequestStatus load_status, |
| 124 content::WebContents* web_contents); |
| 125 |
| 126 private: |
| 127 content::TestBrowserThreadBundle thread_bundle_; |
| 128 TestingProfile profile_; |
| 129 TestAdapter* test_adapter_; |
| 130 std::unique_ptr<PrerenderingLoader> loader_; |
| 131 bool callback_called_; |
| 132 Offliner::RequestStatus callback_load_status_; |
| 133 |
| 134 DISALLOW_COPY_AND_ASSIGN(PrerenderingLoaderTest); |
| 135 }; |
| 136 |
| 137 PrerenderingLoaderTest::PrerenderingLoaderTest() |
| 138 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 139 callback_load_status_(Offliner::RequestStatus::UNKNOWN) {} |
| 140 |
| 141 void PrerenderingLoaderTest::SetUp() { |
| 142 loader_.reset(new PrerenderingLoader(&profile_)); |
| 143 test_adapter_ = new TestAdapter(loader_.get()); |
| 144 loader_->SetAdapterForTesting(base::WrapUnique(test_adapter_)); |
| 145 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 146 } |
| 147 |
| 148 void PrerenderingLoaderTest::OnLoadDone(Offliner::RequestStatus load_status, |
| 149 content::WebContents* web_contents) { |
| 150 callback_called_ = true; |
| 151 callback_load_status_ = load_status; |
| 152 } |
| 153 |
| 154 TEST_F(PrerenderingLoaderTest, CanPrerender) { |
| 155 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 156 EXPECT_TRUE(loader()->CanPrerender()); |
| 157 |
| 158 test_adapter()->Disable(); |
| 159 EXPECT_FALSE(loader()->CanPrerender()); |
| 160 } |
| 161 |
| 162 TEST_F(PrerenderingLoaderTest, StopLoadingWhenIdle) { |
| 163 EXPECT_TRUE(loader()->IsIdle()); |
| 164 loader()->StopLoading(); |
| 165 EXPECT_TRUE(loader()->IsIdle()); |
| 166 } |
| 167 |
| 168 TEST_F(PrerenderingLoaderTest, LoadPageLoadSucceededFromDomContentLoaded) { |
| 169 test_adapter()->Configure( |
| 170 content::WebContentsTester::CreateTestWebContents(profile(), NULL), |
| 171 prerender::FinalStatus::FINAL_STATUS_USED); |
| 172 GURL gurl("http://testit.sea"); |
| 173 EXPECT_TRUE(loader()->IsIdle()); |
| 174 EXPECT_FALSE(loader()->IsLoaded()); |
| 175 EXPECT_TRUE(loader()->LoadPage( |
| 176 gurl, |
| 177 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this)))); |
| 178 |
| 179 test_adapter()->GetObserver()->OnPrerenderDomContentLoaded(); |
| 180 PumpLoop(); |
| 181 EXPECT_FALSE(loader()->IsIdle()); |
| 182 EXPECT_TRUE(loader()->IsLoaded()); |
| 183 EXPECT_TRUE(callback_called()); |
| 184 EXPECT_EQ(Offliner::RequestStatus::LOADED, callback_load_status()); |
| 185 |
| 186 loader()->StopLoading(); |
| 187 EXPECT_TRUE(loader()->IsIdle()); |
| 188 EXPECT_FALSE(loader()->IsLoaded()); |
| 189 } |
| 190 |
| 191 TEST_F(PrerenderingLoaderTest, LoadPageLoadSucceededFromPrerenderStopLoading) { |
| 192 test_adapter()->Configure( |
| 193 content::WebContentsTester::CreateTestWebContents(profile(), NULL), |
| 194 prerender::FinalStatus::FINAL_STATUS_USED); |
| 195 GURL gurl("http://testit.sea"); |
| 196 EXPECT_TRUE(loader()->IsIdle()); |
| 197 EXPECT_FALSE(loader()->IsLoaded()); |
| 198 EXPECT_TRUE(loader()->LoadPage( |
| 199 gurl, |
| 200 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this)))); |
| 201 |
| 202 test_adapter()->GetObserver()->OnPrerenderStart(); |
| 203 PumpLoop(); |
| 204 EXPECT_FALSE(loader()->IsIdle()); |
| 205 EXPECT_FALSE(loader()->IsLoaded()); |
| 206 |
| 207 test_adapter()->GetObserver()->OnPrerenderStopLoading(); |
| 208 PumpLoop(); |
| 209 EXPECT_FALSE(loader()->IsIdle()); |
| 210 EXPECT_TRUE(loader()->IsLoaded()); |
| 211 EXPECT_TRUE(callback_called()); |
| 212 EXPECT_EQ(Offliner::RequestStatus::LOADED, callback_load_status()); |
| 213 |
| 214 // Consider Prerenderer stops (eg, times out) before Loader is done with it. |
| 215 test_adapter()->GetObserver()->OnPrerenderStop(); |
| 216 PumpLoop(); |
| 217 EXPECT_TRUE(loader()->IsIdle()); |
| 218 EXPECT_FALSE(loader()->IsLoaded()); |
| 219 EXPECT_EQ(Offliner::RequestStatus::CANCELED, callback_load_status()); |
| 220 } |
| 221 |
| 222 TEST_F(PrerenderingLoaderTest, LoadPageLoadFailedNoContent) { |
| 223 test_adapter()->Configure( |
| 224 nullptr /* web_contents */, |
| 225 prerender::FinalStatus::FINAL_STATUS_MEMORY_LIMIT_EXCEEDED); |
| 226 GURL gurl("http://testit.sea"); |
| 227 EXPECT_TRUE(loader()->IsIdle()); |
| 228 EXPECT_TRUE(loader()->LoadPage( |
| 229 gurl, |
| 230 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this)))); |
| 231 EXPECT_FALSE(loader()->IsIdle()); |
| 232 EXPECT_FALSE(loader()->IsLoaded()); |
| 233 |
| 234 test_adapter()->GetObserver()->OnPrerenderDomContentLoaded(); |
| 235 PumpLoop(); |
| 236 EXPECT_TRUE(loader()->IsIdle()); |
| 237 EXPECT_TRUE(callback_called()); |
| 238 // We did not provide any WebContents for the callback so expect did not load. |
| 239 EXPECT_EQ(Offliner::RequestStatus::FAILED, callback_load_status()); |
| 240 |
| 241 // Stopped event causes no harm. |
| 242 test_adapter()->GetObserver()->OnPrerenderStop(); |
| 243 PumpLoop(); |
| 244 } |
| 245 |
| 246 TEST_F(PrerenderingLoaderTest, LoadPageLoadCanceledFromStopLoading) { |
| 247 GURL gurl("http://testit.sea"); |
| 248 EXPECT_TRUE(loader()->IsIdle()); |
| 249 EXPECT_TRUE(loader()->LoadPage( |
| 250 gurl, |
| 251 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this)))); |
| 252 EXPECT_FALSE(loader()->IsIdle()); |
| 253 EXPECT_FALSE(loader()->IsLoaded()); |
| 254 |
| 255 loader()->StopLoading(); |
| 256 PumpLoop(); |
| 257 EXPECT_TRUE(loader()->IsIdle()); |
| 258 EXPECT_TRUE(callback_called()); |
| 259 EXPECT_EQ(Offliner::RequestStatus::CANCELED, callback_load_status()); |
| 260 } |
| 261 |
| 262 TEST_F(PrerenderingLoaderTest, LoadPageNotAcceptedWhenNotIdle) { |
| 263 GURL gurl("http://testit.sea"); |
| 264 EXPECT_TRUE(loader()->IsIdle()); |
| 265 EXPECT_TRUE(loader()->LoadPage( |
| 266 gurl, |
| 267 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this)))); |
| 268 EXPECT_FALSE(loader()->IsIdle()); |
| 269 EXPECT_FALSE(loader()->IsLoaded()); |
| 270 |
| 271 // Now try another load while first is still active. |
| 272 EXPECT_FALSE(loader()->LoadPage( |
| 273 gurl, |
| 274 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this)))); |
| 275 EXPECT_FALSE(loader()->IsIdle()); |
| 276 } |
| 277 |
| 278 TEST_F(PrerenderingLoaderTest, LoadPageNotAcceptedWhenPrerenderingDisabled) { |
| 279 test_adapter()->Disable(); |
| 280 GURL gurl("http://testit.sea"); |
| 281 EXPECT_TRUE(loader()->IsIdle()); |
| 282 EXPECT_FALSE(loader()->LoadPage( |
| 283 gurl, |
| 284 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this)))); |
| 285 EXPECT_TRUE(loader()->IsIdle()); |
| 286 } |
| 287 |
| 288 } // namespace offline_pages |
| OLD | NEW |