Home | History | Annotate | Download | only in dom_distiller
      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/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"
     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:
     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:
     42   WebContentsMainFrameHelper(content::WebContents* web_contents,
     43                              const base::Closure& callback)
     44       : callback_(callback) {
     45     content::WebContentsObserver::Observe(web_contents);
     46   }
     47 
     48   virtual void DidFinishLoad(content::RenderFrameHost* render_frame_host,
     49                              const GURL& validated_url) OVERRIDE {
     50     if (!render_frame_host->GetParent() &&
     51         validated_url.scheme() == kDomDistillerScheme)
     52       callback_.Run();
     53   }
     54 
     55  private:
     56   base::Closure callback_;
     57 };
     58 
     59 IN_PROC_BROWSER_TEST_F(DomDistillerTabUtilsBrowserTest, TestSwapWebContents) {
     60   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
     61 
     62   content::WebContents* initial_web_contents =
     63       browser()->tab_strip_model()->GetActiveWebContents();
     64   const GURL& article_url = embedded_test_server()->GetURL(kSimpleArticlePath);
     65 
     66   // This blocks until the navigation has completely finished.
     67   ui_test_utils::NavigateToURL(browser(), article_url);
     68 
     69   DistillCurrentPageAndView(initial_web_contents);
     70 
     71   // Wait until the new WebContents has fully navigated.
     72   content::WebContents* after_web_contents =
     73       browser()->tab_strip_model()->GetActiveWebContents();
     74   ASSERT_TRUE(after_web_contents != NULL);
     75   base::RunLoop new_url_loaded_runner;
     76   scoped_ptr<WebContentsMainFrameHelper> distilled_page_loaded(
     77       new WebContentsMainFrameHelper(after_web_contents,
     78                                      new_url_loaded_runner.QuitClosure()));
     79   new_url_loaded_runner.Run();
     80 
     81   // Verify the new URL is showing distilled content in a new WebContents.
     82   EXPECT_NE(initial_web_contents, after_web_contents);
     83   EXPECT_TRUE(
     84       after_web_contents->GetLastCommittedURL().SchemeIs(kDomDistillerScheme));
     85   EXPECT_EQ("Test Page Title",
     86             base::UTF16ToUTF8(after_web_contents->GetTitle()));
     87 }
     88 
     89 }  // namespace dom_distiller
     90