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

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

Powered by Google App Engine
This is Rietveld 408576698