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

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 gabadie 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 StartPrerender(
31 content::BrowserContext* browser_context,
32 const GURL& url,
33 content::SessionStorageNamespace* session_storage_namespace,
34 const gfx::Size& size,
35 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 HasHandle(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::StartPrerender(
85 content::BrowserContext* browser_context,
86 const GURL& url,
87 content::SessionStorageNamespace* session_storage_namespace,
88 const gfx::Size& size,
89 prerender::PrerenderHandle::Observer* observer) {
90 observer_ = observer;
91 active_ = true;
92 return true;
93 }
94
95 bool TestAdapter::IsPrerendering() const {
96 return is_prerendering_;
97 }
98
99 content::WebContents* TestAdapter::GetWebContents() const {
100 return contents_;
101 }
102
103 prerender::FinalStatus TestAdapter::GetFinalStatus() const {
104 return final_status_;
105 }
106
107 bool TestAdapter::IsActive() const {
108 return active_;
109 }
110
111 bool TestAdapter::HasHandle(prerender::PrerenderHandle* handle) const {
112 return IsActive();
113 }
114
115 void TestAdapter::DestroyActive() {
116 active_ = false;
117 }
118
119 // Test class.
120 class PrerenderingLoaderTest : public testing::Test {
121 public:
122 PrerenderingLoaderTest();
123 ~PrerenderingLoaderTest() override {}
124
125 void SetUp() override;
126
127 // Returns the PrerenderLoader to test.
128 PrerenderingLoader* loader() const { return loader_.get(); }
129 // Returns the TestAdapter to allow test behavior configuration.
130 TestAdapter* test_adapter() const { return test_adapter_; }
131 bool callback_called() { return callback_called_; }
132 Offliner::RequestStatus load_status() { return load_status_; }
133 Profile* profile() { return &profile_; }
134 void OnLoadDone(Offliner::RequestStatus load_status,
135 content::WebContents* web_contents);
136 void PumpLoop();
137
138 private:
139 content::TestBrowserThreadBundle thread_bundle_;
140 TestingProfile profile_;
141 TestAdapter* test_adapter_;
142 std::unique_ptr<PrerenderingLoader> loader_;
143 bool callback_called_;
144 Offliner::RequestStatus load_status_;
145
146 DISALLOW_COPY_AND_ASSIGN(PrerenderingLoaderTest);
147 };
148
149 PrerenderingLoaderTest::PrerenderingLoaderTest()
150 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
151 load_status_(Offliner::RequestStatus::UNKNOWN) {}
152
153 void PrerenderingLoaderTest::SetUp() {
154 loader_.reset(new PrerenderingLoader(&profile_));
155 test_adapter_ = new TestAdapter();
156 loader_->SetAdapterForTesting(test_adapter_);
157 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
158 }
159
160 void PrerenderingLoaderTest::OnLoadDone(Offliner::RequestStatus load_status,
161 content::WebContents* web_contents) {
162 callback_called_ = true;
163 load_status_ = load_status;
164 }
165
166 void PrerenderingLoaderTest::PumpLoop() {
167 base::RunLoop().RunUntilIdle();
168 }
169
170 TEST_F(PrerenderingLoaderTest, CanPrerender) {
171 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
172 EXPECT_TRUE(loader()->CanPrerender());
173
174 test_adapter()->Disable();
175 EXPECT_FALSE(loader()->CanPrerender());
176 }
177
178 TEST_F(PrerenderingLoaderTest, StopLoadingWhenIdle) {
179 EXPECT_TRUE(loader()->IsIdle());
180 loader()->StopLoading();
181 EXPECT_TRUE(loader()->IsIdle());
182 }
183
184 TEST_F(PrerenderingLoaderTest, LoadPageLoadFailed) {
185 GURL gurl("http://testit.sea");
186 EXPECT_TRUE(loader()->IsIdle());
187 EXPECT_TRUE(loader()->LoadPage(
188 gurl,
189 base::Bind(&PrerenderingLoaderTest::OnLoadDone, base::Unretained(this))));
190 EXPECT_FALSE(loader()->IsIdle());
191
192 test_adapter()->GetObserver()->OnPrerenderDomContentLoaded(
193 nullptr /* PrerenderHandle not mockable */);
194 PumpLoop();
195 EXPECT_TRUE(callback_called());
196 // We did not provide any WebContents for the callback so expect did not load.
197 EXPECT_EQ(Offliner::RequestStatus::FAILED_DO_NOT_RETRY, load_status());
198 }
199
200 TEST_F(PrerenderingLoaderTest, LoadPageLoadSucceeded) {
201 test_adapter()->Configure(
202 true /* is_prerendering */,
203 content::WebContentsTester::CreateTestWebContents(profile(), NULL),
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_EQ(Offliner::RequestStatus::LOADED, load_status());
216 }
217
218 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698