• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include <string.h>
6 
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/common/url_constants.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "components/dom_distiller/content/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_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"
26 
27 namespace dom_distiller {
28 
29 namespace {
30 const char* kSimpleArticlePath = "/dom_distiller/simple_article.html";
31 }  // namespace
32 
33 class DomDistillerTabUtilsBrowserTest : public InProcessBrowserTest {
34  public:
SetUpCommandLine(CommandLine * command_line)35   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
36     command_line->AppendSwitch(switches::kEnableDomDistiller);
37   }
38 };
39 
40 class WebContentsMainFrameHelper : public content::WebContentsObserver {
41  public:
WebContentsMainFrameHelper(content::WebContents * web_contents,const base::Closure & callback)42   WebContentsMainFrameHelper(content::WebContents* web_contents,
43                              const base::Closure& callback)
44       : callback_(callback) {
45     content::WebContentsObserver::Observe(web_contents);
46   }
47 
DidFinishLoad(int64 frame_id,const GURL & validated_url,bool is_main_frame,content::RenderViewHost * render_view_host)48   virtual void DidFinishLoad(
49       int64 frame_id,
50       const GURL& validated_url,
51       bool is_main_frame,
52       content::RenderViewHost* render_view_host) OVERRIDE {
53     if (is_main_frame && validated_url.scheme() == chrome::kDomDistillerScheme)
54       callback_.Run();
55   }
56 
57  private:
58   base::Closure callback_;
59 };
60 
IN_PROC_BROWSER_TEST_F(DomDistillerTabUtilsBrowserTest,TestSwapWebContents)61 IN_PROC_BROWSER_TEST_F(DomDistillerTabUtilsBrowserTest, TestSwapWebContents) {
62   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
63 
64   content::WebContents* initial_web_contents =
65       browser()->tab_strip_model()->GetActiveWebContents();
66   const GURL& article_url = embedded_test_server()->GetURL(kSimpleArticlePath);
67 
68   // This blocks until the navigation has completely finished.
69   ui_test_utils::NavigateToURL(browser(), article_url);
70 
71   DistillCurrentPageAndView(initial_web_contents);
72 
73   // Wait until the new WebContents has fully navigated.
74   content::WebContents* after_web_contents =
75       browser()->tab_strip_model()->GetActiveWebContents();
76   ASSERT_TRUE(after_web_contents != NULL);
77   base::RunLoop new_url_loaded_runner;
78   scoped_ptr<WebContentsMainFrameHelper> distilled_page_loaded(
79       new WebContentsMainFrameHelper(after_web_contents,
80                                      new_url_loaded_runner.QuitClosure()));
81   new_url_loaded_runner.Run();
82 
83   // Verify the new URL is showing distilled content in a new WebContents.
84   EXPECT_NE(initial_web_contents, after_web_contents);
85   EXPECT_TRUE(after_web_contents->GetLastCommittedURL().SchemeIs(
86       chrome::kDomDistillerScheme));
87   EXPECT_EQ("Test Page Title",
88             base::UTF16ToUTF8(after_web_contents->GetTitle()));
89 }
90 
91 }  // namespace dom_distiller
92