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

Side by Side Diff: chrome/browser/android/offline_pages/prerendering_loader_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698