1 // Copyright 2015 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.
6 #include "base/files/file_path.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/run_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h"
13 #include "chrome/browser/android/offline_pages/offline_page_web_contents_observer.h"
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
15 #include "content/public/browser/navigation_details.h"
16 #include "content/public/test/test_renderer_host.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace offline_pages
{
23 const GURL
kTestURL("http://example.com/");
24 const base::FilePath::CharType kTestFilePath
[] = FILE_PATH_LITERAL(
25 "/archive_dir/offline_page.mhtml");
26 const int64 kTestFileSize
= 123456LL;
28 class TestMHTMLArchiver
: public OfflinePageMHTMLArchiver
{
31 content::WebContents
* web_contents
,
33 const base::FilePath
& archive_dir
);
34 ~TestMHTMLArchiver() override
;
37 void DoGenerateMHTML() override
;
41 DISALLOW_COPY_AND_ASSIGN(TestMHTMLArchiver
);
44 TestMHTMLArchiver::TestMHTMLArchiver(content::WebContents
* web_contents
,
46 const base::FilePath
& archive_dir
)
47 : OfflinePageMHTMLArchiver(web_contents
, archive_dir
), url_(url
) {
50 TestMHTMLArchiver::~TestMHTMLArchiver() {
53 void TestMHTMLArchiver::DoGenerateMHTML() {
54 base::ThreadTaskRunnerHandle::Get()->PostTask(
56 base::Bind(&TestMHTMLArchiver::OnGenerateMHTMLDone
,
57 base::Unretained(this),
59 base::FilePath(kTestFilePath
),
65 class OfflinePageWebContentsObserverTest
:
66 public ChromeRenderViewHostTestHarness
{
68 OfflinePageWebContentsObserverTest();
69 ~OfflinePageWebContentsObserverTest() override
;
71 void SetUp() override
;
72 void TearDown() override
;
74 // Helper to create an archiver and call its CreateArchive.
75 void CreateArchive(const GURL
& url
);
77 // Test tooling methods.
80 OfflinePageWebContentsObserver
* web_contents_observer() const {
81 return web_contents_observer_
;
84 OfflinePageArchiver::ArchiverResult
last_result() const {
89 void OnCreateArchiveDone(OfflinePageArchiver
* archiver
,
90 OfflinePageArchiver::ArchiverResult result
,
92 const base::FilePath
& file_path
,
95 OfflinePageWebContentsObserver
* web_contents_observer_
; // Not owned.
96 scoped_ptr
<TestMHTMLArchiver
> archiver_
;
97 OfflinePageArchiver::ArchiverResult last_result_
;
99 DISALLOW_COPY_AND_ASSIGN(OfflinePageWebContentsObserverTest
);
102 OfflinePageWebContentsObserverTest::OfflinePageWebContentsObserverTest()
103 : last_result_(OfflinePageArchiver::ArchiverResult::
104 ERROR_ARCHIVE_CREATION_FAILED
) {
107 OfflinePageWebContentsObserverTest::~OfflinePageWebContentsObserverTest() {
110 void OfflinePageWebContentsObserverTest::SetUp() {
111 content::RenderViewHostTestHarness::SetUp();
112 OfflinePageWebContentsObserver::CreateForWebContents(web_contents());
113 web_contents_observer_
=
114 OfflinePageWebContentsObserver::FromWebContents(web_contents());
115 ASSERT_FALSE(web_contents_observer_
->is_document_loaded_in_main_frame());
118 void OfflinePageWebContentsObserverTest::TearDown() {
120 content::RenderViewHostTestHarness::TearDown();
123 void OfflinePageWebContentsObserverTest::CreateArchive(const GURL
& url
) {
124 archiver_
.reset(new TestMHTMLArchiver(web_contents(), url
,
125 base::FilePath(kTestFilePath
)));
126 archiver_
->CreateArchive(
127 base::Bind(&OfflinePageWebContentsObserverTest::OnCreateArchiveDone
,
128 base::Unretained(this)));
131 void OfflinePageWebContentsObserverTest::OnCreateArchiveDone(
132 OfflinePageArchiver
* archiver
,
133 OfflinePageArchiver::ArchiverResult result
,
135 const base::FilePath
& file_path
,
137 last_result_
= result
;
140 void OfflinePageWebContentsObserverTest::PumpLoop() {
141 base::RunLoop().RunUntilIdle();
144 TEST_F(OfflinePageWebContentsObserverTest
, LoadMainFrameBeforeCreateArchive
) {
145 // Navigate in main frame. Document was not loaded yet.
146 content::LoadCommittedDetails details
= content::LoadCommittedDetails();
147 details
.is_main_frame
= true;
148 details
.is_in_page
= false;
149 content::FrameNavigateParams params
= content::FrameNavigateParams();
150 web_contents_observer()->DidNavigateMainFrame(details
, params
);
151 ASSERT_FALSE(web_contents_observer()->is_document_loaded_in_main_frame());
153 // Load document in main frame.
154 web_contents_observer()->DocumentLoadedInFrame(main_rfh());
155 ASSERT_TRUE(web_contents_observer()->is_document_loaded_in_main_frame());
157 // Create an archive.
158 CreateArchive(kTestURL
);
161 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED
,
165 TEST_F(OfflinePageWebContentsObserverTest
, LoadMainFrameAfterCreateArchive
) {
166 // Navigate in main frame. Document was not loaded yet.
167 content::LoadCommittedDetails details
= content::LoadCommittedDetails();
168 details
.is_main_frame
= true;
169 details
.is_in_page
= false;
170 content::FrameNavigateParams params
= content::FrameNavigateParams();
171 web_contents_observer()->DidNavigateMainFrame(details
, params
);
172 ASSERT_FALSE(web_contents_observer()->is_document_loaded_in_main_frame());
174 // Create an archive. Waiting for document loaded in main frame.
175 CreateArchive(kTestURL
);
178 EXPECT_NE(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED
,
181 // Load document in main frame.
182 web_contents_observer()->DocumentLoadedInFrame(main_rfh());
183 ASSERT_TRUE(web_contents_observer()->is_document_loaded_in_main_frame());
186 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED
,
190 TEST_F(OfflinePageWebContentsObserverTest
, ResetOnPageNavigation
) {
191 // Navigate in main frame. Document was not loaded yet.
192 content::LoadCommittedDetails details
= content::LoadCommittedDetails();
193 details
.is_main_frame
= true;
194 details
.is_in_page
= false;
195 content::FrameNavigateParams params
= content::FrameNavigateParams();
196 web_contents_observer()->DidNavigateMainFrame(details
, params
);
197 ASSERT_FALSE(web_contents_observer()->is_document_loaded_in_main_frame());
199 // Load document in main frame.
200 web_contents_observer()->DocumentLoadedInFrame(main_rfh());
201 ASSERT_TRUE(web_contents_observer()->is_document_loaded_in_main_frame());
203 // Another navigation should result in waiting for a page load.
204 web_contents_observer()->DidNavigateMainFrame(details
, params
);
205 ASSERT_FALSE(web_contents_observer()->is_document_loaded_in_main_frame());
207 // Create an archive. Waiting for document loaded in main frame.
208 CreateArchive(kTestURL
);
211 EXPECT_NE(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED
,
214 // Load document in main frame.
215 web_contents_observer()->DocumentLoadedInFrame(main_rfh());
216 ASSERT_TRUE(web_contents_observer()->is_document_loaded_in_main_frame());
219 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED
,
223 } // namespace offline_pages