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

Side by Side 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: Changes per pasko feedback 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 unified diff | Download patch
OLDNEW
(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/browser/prerender/prerender_handle.h"
12 #include "chrome/browser/prerender/prerender_manager.h"
13 #include "chrome/browser/prerender/prerender_manager_factory.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "content/public/test/web_contents_tester.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace offline_pages {
21
22 // Adapter that intercepts prerender stack calls for testing.
23 class TestAdapter : public PrerenderAdapter {
24 public:
25 TestAdapter() {}
26 ~TestAdapter() override {}
27
28 // PrerenderAdapter implementation.
29 bool CanPrerender() const override;
30 bool AddPrerenderForOffline(
31 content::BrowserContext* browser_context,
32 const GURL& url,
33 content::SessionStorageNamespace* session_storage_namespace,
34 const gfx::Size& size) override;
35 void SetObserver(prerender::PrerenderHandle::Observer* observer) override;
36 bool IsPrerendering() const override;
37 content::WebContents* GetWebContents() const override;
38 prerender::FinalStatus GetFinalStatus() const override;
39 bool IsActive() const override;
40 bool IsActive(prerender::PrerenderHandle* handle) const override;
41 void DestroyActive() override;
42
43 // Sets prerendering to be disabled. This will cause the CanPrerender()
44 // to return false.
45 void Disable();
46
47 // Configures mocked prerendering details.
48 void Configure(bool is_prerendering,
49 content::WebContents* contents,
50 prerender::FinalStatus final_status);
51
52 // Returns the observer for test access.
53 prerender::PrerenderHandle::Observer* GetObserver() const {
54 return observer_;
55 }
56
57 private:
58 bool active_;
59 bool disabled_;
60 bool is_prerendering_;
61 prerender::PrerenderHandle::Observer* observer_;
62 content::WebContents* contents_;
63 prerender::FinalStatus final_status_;
64
65 DISALLOW_COPY_AND_ASSIGN(TestAdapter);
66 };
67
68 void TestAdapter::Disable() {
69 disabled_ = true;
70 }
71
72 void TestAdapter::Configure(bool is_prerendering,
73 content::WebContents* contents,
74 prerender::FinalStatus final_status) {
75 is_prerendering_ = is_prerendering;
76 contents_ = contents;
77 final_status_ = final_status;
78 }
79
80 bool TestAdapter::CanPrerender() const {
81 return !disabled_;
82 }
83
84 bool TestAdapter::AddPrerenderForOffline(
85 content::BrowserContext* browser_context,
86 const GURL& url,
87 content::SessionStorageNamespace* session_storage_namespace,
88 const gfx::Size& size) {
89 active_ = true;
90 return true;
91 }
92
93 void TestAdapter::SetObserver(prerender::PrerenderHandle::Observer* observer) {
94 observer_ = observer;
95 }
96
97 bool TestAdapter::IsPrerendering() const {
98 return is_prerendering_;
99 }
100
101 content::WebContents* TestAdapter::GetWebContents() const {
102 return contents_;
103 }
104
105 prerender::FinalStatus TestAdapter::GetFinalStatus() const {
106 return final_status_;
107 }
108
109 bool TestAdapter::IsActive() const {
110 return active_;
111 }
112
113 bool TestAdapter::IsActive(prerender::PrerenderHandle* handle) const {
114 return IsActive();
115 }
116
117 void TestAdapter::DestroyActive() {
118 active_ = false;
119 }
120
121 // Test class.
122 class PrerenderingLoaderTest : public testing::Test {
123 public:
124 PrerenderingLoaderTest();
125 ~PrerenderingLoaderTest() override {}
126
127 void SetUp() override;
128
129 // Returns the PrerenderLoader to test.
130 PrerenderingLoader* loader() const { return loader_.get(); }
131 // Returns the TestAdapter to allow test behavior configuration.
132 TestAdapter* test_adapter() const { return test_adapter_; }
133 bool callback_called() { return callback_called_; }
134 Offliner::RequestStatus load_status() { return load_status_; }
135 Profile* profile() { return &profile_; }
136 void OnLoadDone(Offliner::RequestStatus load_status,
137 content::WebContents* web_contents);
138 void PumpLoop();
139
140 private:
141 content::TestBrowserThreadBundle thread_bundle_;
142 TestingProfile profile_;
143 TestAdapter* test_adapter_;
144 std::unique_ptr<PrerenderingLoader> loader_;
145 bool callback_called_;
146 Offliner::RequestStatus load_status_;
147
148 DISALLOW_COPY_AND_ASSIGN(PrerenderingLoaderTest);
149 };
150
151 PrerenderingLoaderTest::PrerenderingLoaderTest()
152 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
153 load_status_(Offliner::RequestStatus::UNKNOWN) {}
154
155 void PrerenderingLoaderTest::SetUp() {
156 loader_.reset(new PrerenderingLoader(&profile_));
157 test_adapter_ = new TestAdapter();
158 loader_->SetAdapterForTesting(test_adapter_);
159 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
160 }
161
162 void PrerenderingLoaderTest::OnLoadDone(Offliner::RequestStatus load_status,
163 content::WebContents* web_contents) {
164 callback_called_ = true;
165 load_status_ = load_status;
166 }
167
168 void PrerenderingLoaderTest::PumpLoop() {
169 base::RunLoop().RunUntilIdle();
170 }
171
172 TEST_F(PrerenderingLoaderTest, CanPrerender) {
173 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
174 EXPECT_TRUE(loader()->CanPrerender());
175
176 test_adapter()->Disable();
177 EXPECT_FALSE(loader()->CanPrerender());
178 }
179
180 TEST_F(PrerenderingLoaderTest, StopLoadingWhenIdle) {
181 EXPECT_TRUE(loader()->IsIdle());
182 loader()->StopLoading();
183 EXPECT_TRUE(loader()->IsIdle());
184 }
185
186 TEST_F(PrerenderingLoaderTest, LoadPageLoadFailed) {
187 GURL gurl("http://testit.sea");
188 EXPECT_TRUE(loader()->IsIdle());
189 EXPECT_TRUE(loader()->LoadPage(
190 gurl,
191 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this))));
192 EXPECT_FALSE(loader()->IsIdle());
193
194 test_adapter()->GetObserver()->OnPrerenderDomContentLoaded(
195 nullptr /* PrerenderHandle not mockable */);
196 PumpLoop();
197 EXPECT_TRUE(callback_called());
198 // We did not provide any WebContents for the callback so expect did not load.
199 EXPECT_EQ(Offliner::RequestStatus::FAILED_DO_NOT_RETRY, load_status());
200 }
201
202 TEST_F(PrerenderingLoaderTest, LoadPageLoadSucceeded) {
203 test_adapter()->Configure(
204 true /* is_prerendering */,
205 content::WebContentsTester::CreateTestWebContents(profile(), NULL),
206 prerender::FinalStatus::FINAL_STATUS_USED);
207 GURL gurl("http://testit.sea");
208 EXPECT_TRUE(loader()->IsIdle());
209 EXPECT_TRUE(loader()->LoadPage(
210 gurl,
211 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this))));
212
213 test_adapter()->GetObserver()->OnPrerenderDomContentLoaded(
214 nullptr /* handle */);
215 PumpLoop();
216 EXPECT_TRUE(callback_called());
217 EXPECT_EQ(Offliner::RequestStatus::LOADED, load_status());
218 }
219
220 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698