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/web_contents_main_frame_observer.h"
17 #include "components/dom_distiller/core/dom_distiller_service.h"
18 #include "components/dom_distiller/core/task_tracker.h"
19 #include "components/dom_distiller/core/url_constants.h"
20 #include "components/dom_distiller/core/url_utils.h"
21 #include "content/public/browser/render_frame_host.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/browser/web_contents_observer.h"
24 #include "net/test/embedded_test_server/embedded_test_server.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 namespace dom_distiller
{
30 const char* kSimpleArticlePath
= "/dom_distiller/simple_article.html";
33 class DomDistillerTabUtilsBrowserTest
: public InProcessBrowserTest
{
35 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
36 command_line
->AppendSwitch(switches::kEnableDomDistiller
);
40 class WebContentsMainFrameHelper
: public content::WebContentsObserver
{
42 WebContentsMainFrameHelper(content::WebContents
* web_contents
,
43 const base::Closure
& callback
)
44 : callback_(callback
) {
45 content::WebContentsObserver::Observe(web_contents
);
48 void DidFinishLoad(content::RenderFrameHost
* render_frame_host
,
49 const GURL
& validated_url
) override
{
50 if (!render_frame_host
->GetParent() &&
51 validated_url
.scheme() == kDomDistillerScheme
)
56 base::Closure callback_
;
59 #if (defined(OS_LINUX) && defined(OS_CHROMEOS))
60 #define MAYBE_TestSwapWebContents DISABLED_TestSwapWebContents
62 #define MAYBE_TestSwapWebContents TestSwapWebContents
65 IN_PROC_BROWSER_TEST_F(DomDistillerTabUtilsBrowserTest
,
66 MAYBE_TestSwapWebContents
) {
67 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
69 content::WebContents
* initial_web_contents
=
70 browser()->tab_strip_model()->GetActiveWebContents();
71 const GURL
& article_url
= embedded_test_server()->GetURL(kSimpleArticlePath
);
73 // This blocks until the navigation has completely finished.
74 ui_test_utils::NavigateToURL(browser(), article_url
);
76 DistillCurrentPageAndView(initial_web_contents
);
78 // Wait until the new WebContents has fully navigated.
79 content::WebContents
* after_web_contents
=
80 browser()->tab_strip_model()->GetActiveWebContents();
81 ASSERT_TRUE(after_web_contents
!= NULL
);
82 base::RunLoop new_url_loaded_runner
;
83 scoped_ptr
<WebContentsMainFrameHelper
> distilled_page_loaded(
84 new WebContentsMainFrameHelper(after_web_contents
,
85 new_url_loaded_runner
.QuitClosure()));
86 new_url_loaded_runner
.Run();
88 // Verify the new URL is showing distilled content in a new WebContents.
89 EXPECT_NE(initial_web_contents
, after_web_contents
);
91 after_web_contents
->GetLastCommittedURL().SchemeIs(kDomDistillerScheme
));
92 EXPECT_EQ("Test Page Title",
93 base::UTF16ToUTF8(after_web_contents
->GetTitle()));
96 } // namespace dom_distiller