1 // Copyright 2014 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.
7 #include "base/command_line.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
10 #include "chrome/browser/dom_distiller/tab_utils.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h"
17 #include "components/dom_distiller/content/browser/web_contents_main_frame_observer.h"
18 #include "components/dom_distiller/core/dom_distiller_service.h"
19 #include "components/dom_distiller/core/task_tracker.h"
20 #include "components/dom_distiller/core/url_constants.h"
21 #include "components/dom_distiller/core/url_utils.h"
22 #include "content/public/browser/render_frame_host.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/browser/web_contents_observer.h"
25 #include "content/public/common/isolated_world_ids.h"
26 #include "content/public/test/browser_test_utils.h"
27 #include "content/public/test/test_utils.h"
28 #include "net/test/embedded_test_server/embedded_test_server.h"
29 #include "testing/gtest/include/gtest/gtest.h"
31 namespace dom_distiller
{
34 const char* kSimpleArticlePath
= "/dom_distiller/simple_article.html";
37 class DomDistillerTabUtilsBrowserTest
: public InProcessBrowserTest
{
39 void SetUpOnMainThread() override
{
40 if (!DistillerJavaScriptWorldIdIsSet()) {
41 SetDistillerJavaScriptWorldId(content::ISOLATED_WORLD_ID_CONTENT_END
);
43 InProcessBrowserTest::SetUpOnMainThread();
46 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
47 command_line
->AppendSwitch(switches::kEnableDomDistiller
);
51 // WebContentsMainFrameHelper is used to detect if a distilled page has
52 // finished loading. This is done by checking how many times the title has
53 // been set rather than using "DidFinishLoad" directly due to the content
54 // being set by JavaScript.
55 class WebContentsMainFrameHelper
: public content::WebContentsObserver
{
57 WebContentsMainFrameHelper(content::WebContents
* web_contents
,
58 const base::Closure
& callback
)
59 : callback_(callback
),
61 loaded_distiller_page_(false) {
62 content::WebContentsObserver::Observe(web_contents
);
65 void DidFinishLoad(content::RenderFrameHost
* render_frame_host
,
66 const GURL
& validated_url
) override
{
67 if (!render_frame_host
->GetParent() &&
68 validated_url
.scheme() == kDomDistillerScheme
)
69 loaded_distiller_page_
= true;
72 void TitleWasSet(content::NavigationEntry
* entry
,
73 bool explicit_set
) override
{
74 // The title will be set twice on distilled pages; once for the placeholder
75 // and once when the distillation has finished. Watch for the second time
76 // as a signal that the JavaScript that sets the content has run.
78 if (title_set_count_
>= 2 && loaded_distiller_page_
) {
84 base::Closure callback_
;
86 bool loaded_distiller_page_
;
89 #if (defined(OS_LINUX) && defined(OS_CHROMEOS))
90 #define MAYBE_TestSwapWebContents DISABLED_TestSwapWebContents
92 #define MAYBE_TestSwapWebContents TestSwapWebContents
95 IN_PROC_BROWSER_TEST_F(DomDistillerTabUtilsBrowserTest
,
96 MAYBE_TestSwapWebContents
) {
97 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
99 content::WebContents
* initial_web_contents
=
100 browser()->tab_strip_model()->GetActiveWebContents();
101 const GURL
& article_url
= embedded_test_server()->GetURL(kSimpleArticlePath
);
103 // This blocks until the navigation has completely finished.
104 ui_test_utils::NavigateToURL(browser(), article_url
);
106 DistillCurrentPageAndView(initial_web_contents
);
108 // Wait until the new WebContents has fully navigated.
109 content::WebContents
* after_web_contents
=
110 browser()->tab_strip_model()->GetActiveWebContents();
111 ASSERT_TRUE(after_web_contents
!= NULL
);
112 base::RunLoop new_url_loaded_runner
;
113 scoped_ptr
<WebContentsMainFrameHelper
> distilled_page_loaded(
114 new WebContentsMainFrameHelper(after_web_contents
,
115 new_url_loaded_runner
.QuitClosure()));
116 new_url_loaded_runner
.Run();
118 std::string page_title
;
119 content::ExecuteScriptAndGetValue(after_web_contents
->GetMainFrame(),
120 "document.title")->GetAsString(&page_title
);
122 // Verify the new URL is showing distilled content in a new WebContents.
123 EXPECT_NE(initial_web_contents
, after_web_contents
);
125 after_web_contents
->GetLastCommittedURL().SchemeIs(kDomDistillerScheme
));
126 EXPECT_EQ("Test Page Title", page_title
);
129 IN_PROC_BROWSER_TEST_F(DomDistillerTabUtilsBrowserTest
,
130 TestDistillIntoWebContents
) {
131 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
133 content::WebContents
* source_web_contents
=
134 browser()->tab_strip_model()->GetActiveWebContents();
135 const GURL
& article_url
= embedded_test_server()->GetURL(kSimpleArticlePath
);
137 // This blocks until the navigation has completely finished.
138 ui_test_utils::NavigateToURL(browser(), article_url
);
140 // Create destination WebContents.
141 content::WebContents::CreateParams
create_params(
142 source_web_contents
->GetBrowserContext());
143 content::WebContents
* destination_web_contents
=
144 content::WebContents::Create(create_params
);
145 DCHECK(destination_web_contents
);
147 browser()->tab_strip_model()->AppendWebContents(destination_web_contents
,
149 ASSERT_EQ(destination_web_contents
,
150 browser()->tab_strip_model()->GetWebContentsAt(1));
152 DistillAndView(source_web_contents
, destination_web_contents
);
154 // Wait until the destination WebContents has fully navigated.
155 base::RunLoop new_url_loaded_runner
;
156 scoped_ptr
<WebContentsMainFrameHelper
> distilled_page_loaded(
157 new WebContentsMainFrameHelper(destination_web_contents
,
158 new_url_loaded_runner
.QuitClosure()));
159 new_url_loaded_runner
.Run();
161 // Verify that the source WebContents is showing the original article.
162 EXPECT_EQ(article_url
, source_web_contents
->GetLastCommittedURL());
163 std::string page_title
;
164 content::ExecuteScriptAndGetValue(source_web_contents
->GetMainFrame(),
165 "document.title")->GetAsString(&page_title
);
166 EXPECT_EQ("Test Page Title", page_title
);
168 // Verify the destination WebContents is showing distilled content.
169 EXPECT_TRUE(destination_web_contents
->GetLastCommittedURL().SchemeIs(
170 kDomDistillerScheme
));
171 content::ExecuteScriptAndGetValue(destination_web_contents
->GetMainFrame(),
172 "document.title")->GetAsString(&page_title
);
173 EXPECT_EQ("Test Page Title", page_title
);
175 content::WebContentsDestroyedWatcher
destroyed_watcher(
176 destination_web_contents
);
177 browser()->tab_strip_model()->CloseWebContentsAt(1, 0);
178 destroyed_watcher
.Wait();
181 } // namespace dom_distiller