Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Unified Diff: chrome/browser/android/offline_pages/prerendering_loader_unittest.cc

Issue 1968593002: PrerenderingLoader initial integration with PrerenderManager/PrerenderHandle (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address some feedback (some remains for followup) Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/android/offline_pages/prerendering_loader_unittest.cc
diff --git a/chrome/browser/android/offline_pages/prerendering_loader_unittest.cc b/chrome/browser/android/offline_pages/prerendering_loader_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9acfe57a5c2faf7206c8f75f9fdeaee1052478e1
--- /dev/null
+++ b/chrome/browser/android/offline_pages/prerendering_loader_unittest.cc
@@ -0,0 +1,221 @@
+// Copyright 2016 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/browser/android/offline_pages/prerendering_loader.h"
+
+#include "base/bind.h"
+#include "base/memory/ptr_util.h"
+#include "base/memory/ref_counted.h"
+#include "base/run_loop.h"
+#include "chrome/browser/prerender/prerender_handle.h"
+#include "chrome/browser/prerender/prerender_manager.h"
+#include "chrome/browser/prerender/prerender_manager_factory.h"
+#include "chrome/test/base/testing_profile.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "content/public/test/web_contents_tester.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace offline_pages {
+
+// Adapter that intercepts prerender stack calls for testing.
+class TestAdapter : public PrerenderingLoader::PrerenderingAdapter {
+ public:
+ TestAdapter() {}
+ ~TestAdapter() override {}
+
+ // PrerenderAdapter implementation.
+ bool CanPrerender() const override;
+ bool AddPrerenderForOffline(
+ content::BrowserContext* browser_context,
+ const GURL& url,
+ content::SessionStorageNamespace* session_storage_namespace,
+ const gfx::Size& size) override;
+ void SetObserver(prerender::PrerenderHandle::Observer* observer) override;
+ bool IsPrerendering() const override;
+ void OnCancel() override;
+ content::WebContents* GetPrerenderContents() const override;
+ prerender::FinalStatus GetFinalStatus() const override;
+ bool IsActive() const override;
+ bool IsActive(prerender::PrerenderHandle* handle) const override;
+ void DestroyActive() override;
+
+ // Sets prerendering to be disabled. This will cause the CanPrerender()
+ // to return false.
+ void Disable();
+
+ // Configures mocked prerendering details.
+ void Configure(bool is_prerendering,
+ content::WebContents* contents,
+ prerender::FinalStatus final_status);
+
+ // Returns the observer for test access.
+ prerender::PrerenderHandle::Observer* GetObserver() const {
+ return observer_;
+ }
+
+ private:
+ bool active_;
+ bool disabled_;
+ bool is_prerendering_;
+ prerender::PrerenderHandle::Observer* observer_;
+ content::WebContents* contents_;
+ prerender::FinalStatus final_status_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestAdapter);
+};
+
+void TestAdapter::Disable() {
+ disabled_ = true;
+}
+
+void TestAdapter::Configure(bool is_prerendering,
+ content::WebContents* contents,
+ prerender::FinalStatus final_status) {
+ is_prerendering_ = is_prerendering;
+ contents_ = contents;
+ final_status_ = final_status;
+}
+
+bool TestAdapter::CanPrerender() const {
+ return !disabled_;
+}
+
+bool TestAdapter::AddPrerenderForOffline(
+ content::BrowserContext* browser_context,
+ const GURL& url,
+ content::SessionStorageNamespace* session_storage_namespace,
+ const gfx::Size& size) {
+ active_ = true;
+ return true;
+}
+
+void TestAdapter::SetObserver(prerender::PrerenderHandle::Observer* observer) {
+ observer_ = observer;
+}
+
+bool TestAdapter::IsPrerendering() const {
+ return is_prerendering_;
+}
+
+void TestAdapter::OnCancel() {}
+
+content::WebContents* TestAdapter::GetPrerenderContents() const {
+ return contents_;
+}
+
+prerender::FinalStatus TestAdapter::GetFinalStatus() const {
+ return final_status_;
+}
+
+bool TestAdapter::IsActive() const {
+ return active_;
+}
+
+bool TestAdapter::IsActive(prerender::PrerenderHandle* handle) const {
+ return IsActive();
+}
+
+void TestAdapter::DestroyActive() {
+ active_ = false;
+}
+
+// Test class.
+class PrerenderingLoaderTest : public testing::Test {
+ public:
+ PrerenderingLoaderTest();
+ ~PrerenderingLoaderTest() override {}
+
+ void SetUp() override;
+
+ // Returns the PrerenderLoader to test.
+ PrerenderingLoader* loader() const { return loader_.get(); }
+ // Returns the TestAdapter to allow test behavior configuration.
+ TestAdapter* test_adapter() const { return test_adapter_; }
+ bool callback_called() { return callback_called_; }
+ bool did_load() { return did_load_; }
+ Profile* profile() { return &profile_; }
+ void OnLoadDone(bool loaded, content::WebContents* web_contents);
+ void PumpLoop();
+
+ private:
+ content::TestBrowserThreadBundle thread_bundle_;
+ TestingProfile profile_;
+ TestAdapter* test_adapter_;
+ std::unique_ptr<PrerenderingLoader> loader_;
+ bool callback_called_;
+ bool did_load_;
+
+ DISALLOW_COPY_AND_ASSIGN(PrerenderingLoaderTest);
+};
+
+PrerenderingLoaderTest::PrerenderingLoaderTest()
+ : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} //,
+
+void PrerenderingLoaderTest::SetUp() {
+ loader_.reset(new PrerenderingLoader(&profile_));
+ test_adapter_ = new TestAdapter();
+ loader_->SetAdapterForTesting(test_adapter_);
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+}
+
+void PrerenderingLoaderTest::OnLoadDone(bool loaded,
+ content::WebContents* web_contents) {
+ callback_called_ = true;
+ did_load_ = loaded;
+}
+
+void PrerenderingLoaderTest::PumpLoop() {
+ base::RunLoop().RunUntilIdle();
+}
+
+TEST_F(PrerenderingLoaderTest, CanPrerender) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ EXPECT_TRUE(loader()->CanPrerender());
+
+ test_adapter()->Disable();
+ EXPECT_FALSE(loader()->CanPrerender());
+}
+
+TEST_F(PrerenderingLoaderTest, StopLoadingWhenIdle) {
+ EXPECT_TRUE(loader()->IsIdle());
+ loader()->StopLoading();
+ EXPECT_TRUE(loader()->IsIdle());
+}
+
+TEST_F(PrerenderingLoaderTest, LoadPageLoadFailed) {
+ GURL gurl("http://testit.sea");
+ EXPECT_TRUE(loader()->IsIdle());
+ EXPECT_TRUE(loader()->LoadPage(
+ gurl,
+ base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this))));
+ EXPECT_FALSE(loader()->IsIdle());
+
+ test_adapter()->GetObserver()->OnPrerenderDomContentLoaded(
+ nullptr /* PrerenderHandle not mockable */);
+ PumpLoop();
+ EXPECT_TRUE(callback_called());
+ // We did not provide any WebContents for the callback so expect did not load.
+ EXPECT_FALSE(did_load());
+}
+
+TEST_F(PrerenderingLoaderTest, LoadPageLoadSucceeded) {
+ test_adapter()->Configure(
+ true /* is_prerendering */,
+ content::WebContentsTester::CreateTestWebContents(profile(), NULL),
+ prerender::FinalStatus::FINAL_STATUS_USED);
+ GURL gurl("http://testit.sea");
+ EXPECT_TRUE(loader()->IsIdle());
+ EXPECT_TRUE(loader()->LoadPage(
+ gurl,
+ base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this))));
+
+ test_adapter()->GetObserver()->OnPrerenderDomContentLoaded(
+ nullptr /* handle */);
+ PumpLoop();
+ EXPECT_TRUE(callback_called());
+ EXPECT_TRUE(did_load());
+}
+
+} // namespace offline_pages

Powered by Google App Engine
This is Rietveld 408576698