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

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: Uploaded new unittest file that was missing and addressed some petewil 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/test/test_browser_thread_bundle.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace offline_pages {
19
20 // Adapter that intercepts prerender stack calls for testing.
21 class TestAdapter : public PrerenderingLoader::PrerenderingAdapter {
22 public:
23 TestAdapter() {}
24 ~TestAdapter() override {}
25
26 // PrerenderAdapter implementation.
27 bool CanPrerender() const override;
28 bool AddPrerenderForOffline(
29 content::BrowserContext* browser_context,
30 const GURL& url,
31 content::SessionStorageNamespace* session_storage_namespace,
32 const gfx::Size& size) override;
33 void SetObserver(prerender::PrerenderHandle::Observer* observer) override;
34 bool IsPrerendering() const override;
35 void OnCancel() override;
36 content::WebContents* GetPrerenderContents() const override;
37 prerender::FinalStatus GetFinalStatus() const override;
38 bool IsActive() const override;
39 bool IsActive(prerender::PrerenderHandle* handle) const override;
40 void DestroyActive() override;
41
42 // Sets prerendering to be disabled. This will cause the CanPrerender()
43 // to return false.
44 void Disable();
45
46 // Configures mocked prerendering details.
47 void Configure(bool is_prerendering,
48 content::WebContents* contents,
49 prerender::FinalStatus final_status);
50
51 // Returns the observer for test access.
52 prerender::PrerenderHandle::Observer* GetObserver() const {
53 return observer_;
54 }
55
56 private:
57 bool active_;
58 bool disabled_;
59 bool is_prerendering_;
60 prerender::PrerenderHandle::Observer* observer_;
61 content::WebContents* contents_;
62 prerender::FinalStatus final_status_;
63
64 DISALLOW_COPY_AND_ASSIGN(TestAdapter);
65 };
66
67 void TestAdapter::Disable() {
68 disabled_ = true;
69 }
70
71 void TestAdapter::Configure(bool is_prerendering,
72 content::WebContents* contents,
73 prerender::FinalStatus final_status) {
74 is_prerendering_ = is_prerendering;
75 contents_ = contents;
76 final_status_ = final_status;
77 }
78
79 bool TestAdapter::CanPrerender() const {
80 return !disabled_;
81 }
82
83 bool TestAdapter::AddPrerenderForOffline(
84 content::BrowserContext* browser_context,
85 const GURL& url,
86 content::SessionStorageNamespace* session_storage_namespace,
87 const gfx::Size& size) {
88 active_ = true;
89 return true;
90 }
91
92 void TestAdapter::SetObserver(prerender::PrerenderHandle::Observer* observer) {
93 observer_ = observer;
94 }
95
96 bool TestAdapter::IsPrerendering() const {
97 return is_prerendering_;
98 }
99
100 void TestAdapter::OnCancel() {}
101
102 content::WebContents* TestAdapter::GetPrerenderContents() const {
103 return contents_;
104 }
105
106 prerender::FinalStatus TestAdapter::GetFinalStatus() const {
107 return final_status_;
108 }
109
110 bool TestAdapter::IsActive() const {
111 return active_;
112 }
113
114 bool TestAdapter::IsActive(prerender::PrerenderHandle* handle) const {
115 return IsActive();
116 }
117
118 void TestAdapter::DestroyActive() {
119 active_ = false;
120 }
121
122 // Test class.
123 class PrerenderingLoaderTest : public testing::Test {
124 public:
125 PrerenderingLoaderTest();
126 ~PrerenderingLoaderTest() override {}
127
128 void SetUp() override;
129
130 // Returns the PrerenderLoader to test.
131 PrerenderingLoader* loader() const { return loader_.get(); }
132 // Returns the TestAdapter to allow test behavior configuration.
133 TestAdapter* test_adapter() const { return test_adapter_; }
134 bool callback_called() { return callback_called_; }
135 bool did_load() { return did_load_; }
136 Profile* profile() { return &profile_; }
137 void OnLoadDone(bool loaded, 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 bool did_load_;
147
148 DISALLOW_COPY_AND_ASSIGN(PrerenderingLoaderTest);
149 };
150
151 PrerenderingLoaderTest::PrerenderingLoaderTest()
152 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} //,
153
154 void PrerenderingLoaderTest::SetUp() {
155 loader_.reset(new PrerenderingLoader(&profile_));
156 test_adapter_ = new TestAdapter();
157 loader_->SetAdapterForTesting(test_adapter_);
158 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
159 }
160
161 void PrerenderingLoaderTest::OnLoadDone(bool loaded,
162 content::WebContents* web_contents) {
163 callback_called_ = true;
164 did_load_ = loaded;
165 }
166
167 void PrerenderingLoaderTest::PumpLoop() {
168 base::RunLoop().RunUntilIdle();
169 }
170
171 TEST_F(PrerenderingLoaderTest, CanPrerender) {
172 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
173 EXPECT_TRUE(loader()->CanPrerender());
174
175 test_adapter()->Disable();
176 EXPECT_FALSE(loader()->CanPrerender());
177 }
178
179 TEST_F(PrerenderingLoaderTest, StopLoadingWhenIdle) {
180 EXPECT_TRUE(loader()->IsIdle());
181 loader()->StopLoading();
182 EXPECT_TRUE(loader()->IsIdle());
183 }
184
185 TEST_F(PrerenderingLoaderTest, LoadPageLoadFailed) {
186 GURL gurl("http://testit.sea");
187 EXPECT_TRUE(loader()->IsIdle());
188 EXPECT_TRUE(loader()->LoadPage(
189 gurl,
190 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this))));
191 EXPECT_FALSE(loader()->IsIdle());
192
193 test_adapter()->GetObserver()->OnPrerenderDomContentLoaded(
194 nullptr /* handle */);
Pete Williamson 2016/05/10 22:13:15 Handle is not very helpful here. handle to Preren
dougarnett 2016/05/10 23:08:22 Done.
195 PumpLoop();
196 EXPECT_TRUE(callback_called());
197 EXPECT_FALSE(did_load()); // Per no WebContents provided in callback.
Pete Williamson 2016/05/10 22:13:15 // Since we didn't provide a web contents to the t
dougarnett 2016/05/10 23:08:22 Done.
198 }
199
200 TEST_F(PrerenderingLoaderTest, LoadPageLoadSucceeded) {
201 test_adapter()->Configure(
202 true /* is_prerendering */,
203 content::TestWebContents::Create(profile(), nullptr),
204 prerender::FinalStatus::FINAL_STATUS_USED);
205 GURL gurl("http://testit.sea");
206 EXPECT_TRUE(loader()->IsIdle());
207 EXPECT_TRUE(loader()->LoadPage(
208 gurl,
209 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this))));
210
211 test_adapter()->GetObserver()->OnPrerenderDomContentLoaded(
212 nullptr /* handle */);
213 PumpLoop();
214 EXPECT_TRUE(callback_called());
215 EXPECT_TRUE(did_load());
216 }
217
218 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698