OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string.h> | 5 #include <string.h> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h" | 9 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h" |
10 #include "chrome/browser/dom_distiller/tab_utils.h" | 10 #include "chrome/browser/dom_distiller/tab_utils.h" |
11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
13 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
14 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
15 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
16 #include "components/dom_distiller/content/web_contents_main_frame_observer.h" | 16 #include "components/dom_distiller/content/web_contents_main_frame_observer.h" |
17 #include "components/dom_distiller/core/dom_distiller_service.h" | 17 #include "components/dom_distiller/core/dom_distiller_service.h" |
18 #include "components/dom_distiller/core/task_tracker.h" | 18 #include "components/dom_distiller/core/task_tracker.h" |
19 #include "components/dom_distiller/core/url_constants.h" | 19 #include "components/dom_distiller/core/url_constants.h" |
20 #include "components/dom_distiller/core/url_utils.h" | 20 #include "components/dom_distiller/core/url_utils.h" |
21 #include "content/public/browser/render_frame_host.h" | 21 #include "content/public/browser/render_frame_host.h" |
22 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
23 #include "content/public/browser/web_contents_observer.h" | 23 #include "content/public/browser/web_contents_observer.h" |
24 #include "content/public/test/browser_test_utils.h" | 24 #include "content/public/test/browser_test_utils.h" |
| 25 #include "content/public/test/test_utils.h" |
25 #include "net/test/embedded_test_server/embedded_test_server.h" | 26 #include "net/test/embedded_test_server/embedded_test_server.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
27 | 28 |
28 namespace dom_distiller { | 29 namespace dom_distiller { |
29 | 30 |
30 namespace { | 31 namespace { |
31 const char* kSimpleArticlePath = "/dom_distiller/simple_article.html"; | 32 const char* kSimpleArticlePath = "/dom_distiller/simple_article.html"; |
32 } // namespace | 33 } // namespace |
33 | 34 |
34 class DomDistillerTabUtilsBrowserTest : public InProcessBrowserTest { | 35 class DomDistillerTabUtilsBrowserTest : public InProcessBrowserTest { |
35 public: | 36 public: |
36 void SetUpCommandLine(base::CommandLine* command_line) override { | 37 void SetUpCommandLine(base::CommandLine* command_line) override { |
37 command_line->AppendSwitch(switches::kEnableDomDistiller); | 38 command_line->AppendSwitch(switches::kEnableDomDistiller); |
38 } | 39 } |
39 }; | 40 }; |
40 | 41 |
| 42 // WebContentsMainFrameHelper is used to detect if a distilled page has |
| 43 // finished loading. This is done by checking how many times the title has |
| 44 // been set rather than using "DidFinishLoad" directly due to the content |
| 45 // being set by JavaScript. |
41 class WebContentsMainFrameHelper : public content::WebContentsObserver { | 46 class WebContentsMainFrameHelper : public content::WebContentsObserver { |
42 public: | 47 public: |
43 WebContentsMainFrameHelper(content::WebContents* web_contents, | 48 WebContentsMainFrameHelper(content::WebContents* web_contents, |
44 const base::Closure& callback) | 49 const base::Closure& callback) |
45 : callback_(callback) { | 50 : callback_(callback), |
| 51 title_set_count_(0), |
| 52 loaded_distiller_page_(false) { |
46 content::WebContentsObserver::Observe(web_contents); | 53 content::WebContentsObserver::Observe(web_contents); |
47 } | 54 } |
48 | 55 |
49 void DidFinishLoad(content::RenderFrameHost* render_frame_host, | 56 void DidFinishLoad(content::RenderFrameHost* render_frame_host, |
50 const GURL& validated_url) override { | 57 const GURL& validated_url) override { |
51 if (!render_frame_host->GetParent() && | 58 if (!render_frame_host->GetParent() && |
52 validated_url.scheme() == kDomDistillerScheme) | 59 validated_url.scheme() == kDomDistillerScheme) |
| 60 loaded_distiller_page_ = true; |
| 61 } |
| 62 |
| 63 void TitleWasSet(content::NavigationEntry* entry, |
| 64 bool explicit_set) override { |
| 65 // The title will be set twice on distilled pages; once for the placeholder |
| 66 // and once when the distillation has finished. Watch for the second time |
| 67 // as a signal that the JavaScript that sets the content has run. |
| 68 title_set_count_++; |
| 69 if (title_set_count_ >= 2 && loaded_distiller_page_) { |
53 callback_.Run(); | 70 callback_.Run(); |
| 71 } |
54 } | 72 } |
55 | 73 |
56 private: | 74 private: |
57 base::Closure callback_; | 75 base::Closure callback_; |
| 76 int title_set_count_; |
| 77 bool loaded_distiller_page_; |
58 }; | 78 }; |
59 | 79 |
60 #if (defined(OS_LINUX) && defined(OS_CHROMEOS)) | 80 #if (defined(OS_LINUX) && defined(OS_CHROMEOS)) |
61 #define MAYBE_TestSwapWebContents DISABLED_TestSwapWebContents | 81 #define MAYBE_TestSwapWebContents DISABLED_TestSwapWebContents |
62 #else | 82 #else |
63 #define MAYBE_TestSwapWebContents TestSwapWebContents | 83 #define MAYBE_TestSwapWebContents TestSwapWebContents |
64 #endif | 84 #endif |
65 | 85 |
66 IN_PROC_BROWSER_TEST_F(DomDistillerTabUtilsBrowserTest, | 86 IN_PROC_BROWSER_TEST_F(DomDistillerTabUtilsBrowserTest, |
67 MAYBE_TestSwapWebContents) { | 87 MAYBE_TestSwapWebContents) { |
(...skipping 11 matching lines...) Expand all Loading... |
79 // Wait until the new WebContents has fully navigated. | 99 // Wait until the new WebContents has fully navigated. |
80 content::WebContents* after_web_contents = | 100 content::WebContents* after_web_contents = |
81 browser()->tab_strip_model()->GetActiveWebContents(); | 101 browser()->tab_strip_model()->GetActiveWebContents(); |
82 ASSERT_TRUE(after_web_contents != NULL); | 102 ASSERT_TRUE(after_web_contents != NULL); |
83 base::RunLoop new_url_loaded_runner; | 103 base::RunLoop new_url_loaded_runner; |
84 scoped_ptr<WebContentsMainFrameHelper> distilled_page_loaded( | 104 scoped_ptr<WebContentsMainFrameHelper> distilled_page_loaded( |
85 new WebContentsMainFrameHelper(after_web_contents, | 105 new WebContentsMainFrameHelper(after_web_contents, |
86 new_url_loaded_runner.QuitClosure())); | 106 new_url_loaded_runner.QuitClosure())); |
87 new_url_loaded_runner.Run(); | 107 new_url_loaded_runner.Run(); |
88 | 108 |
| 109 std::string page_title; |
| 110 content::ExecuteScriptAndGetValue(after_web_contents->GetMainFrame(), |
| 111 "document.title")->GetAsString(&page_title); |
| 112 |
89 // Verify the new URL is showing distilled content in a new WebContents. | 113 // Verify the new URL is showing distilled content in a new WebContents. |
90 EXPECT_NE(initial_web_contents, after_web_contents); | 114 EXPECT_NE(initial_web_contents, after_web_contents); |
91 EXPECT_TRUE( | 115 EXPECT_TRUE( |
92 after_web_contents->GetLastCommittedURL().SchemeIs(kDomDistillerScheme)); | 116 after_web_contents->GetLastCommittedURL().SchemeIs(kDomDistillerScheme)); |
93 EXPECT_EQ("Test Page Title", | 117 EXPECT_EQ("Test Page Title", page_title); |
94 base::UTF16ToUTF8(after_web_contents->GetTitle())); | |
95 } | 118 } |
96 | 119 |
97 IN_PROC_BROWSER_TEST_F(DomDistillerTabUtilsBrowserTest, | 120 IN_PROC_BROWSER_TEST_F(DomDistillerTabUtilsBrowserTest, |
98 TestDistillIntoWebContents) { | 121 TestDistillIntoWebContents) { |
99 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 122 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
100 | 123 |
101 content::WebContents* source_web_contents = | 124 content::WebContents* source_web_contents = |
102 browser()->tab_strip_model()->GetActiveWebContents(); | 125 browser()->tab_strip_model()->GetActiveWebContents(); |
103 const GURL& article_url = embedded_test_server()->GetURL(kSimpleArticlePath); | 126 const GURL& article_url = embedded_test_server()->GetURL(kSimpleArticlePath); |
104 | 127 |
(...skipping 16 matching lines...) Expand all Loading... |
121 | 144 |
122 // Wait until the destination WebContents has fully navigated. | 145 // Wait until the destination WebContents has fully navigated. |
123 base::RunLoop new_url_loaded_runner; | 146 base::RunLoop new_url_loaded_runner; |
124 scoped_ptr<WebContentsMainFrameHelper> distilled_page_loaded( | 147 scoped_ptr<WebContentsMainFrameHelper> distilled_page_loaded( |
125 new WebContentsMainFrameHelper(destination_web_contents, | 148 new WebContentsMainFrameHelper(destination_web_contents, |
126 new_url_loaded_runner.QuitClosure())); | 149 new_url_loaded_runner.QuitClosure())); |
127 new_url_loaded_runner.Run(); | 150 new_url_loaded_runner.Run(); |
128 | 151 |
129 // Verify that the source WebContents is showing the original article. | 152 // Verify that the source WebContents is showing the original article. |
130 EXPECT_EQ(article_url, source_web_contents->GetLastCommittedURL()); | 153 EXPECT_EQ(article_url, source_web_contents->GetLastCommittedURL()); |
131 EXPECT_EQ("Test Page Title", | 154 std::string page_title; |
132 base::UTF16ToUTF8(source_web_contents->GetTitle())); | 155 content::ExecuteScriptAndGetValue(source_web_contents->GetMainFrame(), |
| 156 "document.title")->GetAsString(&page_title); |
| 157 EXPECT_EQ("Test Page Title", page_title); |
133 | 158 |
134 // Verify the destination WebContents is showing distilled content. | 159 // Verify the destination WebContents is showing distilled content. |
135 EXPECT_TRUE(destination_web_contents->GetLastCommittedURL().SchemeIs( | 160 EXPECT_TRUE(destination_web_contents->GetLastCommittedURL().SchemeIs( |
136 kDomDistillerScheme)); | 161 kDomDistillerScheme)); |
137 EXPECT_EQ("Test Page Title", | 162 content::ExecuteScriptAndGetValue(destination_web_contents->GetMainFrame(), |
138 base::UTF16ToUTF8(destination_web_contents->GetTitle())); | 163 "document.title")->GetAsString(&page_title); |
| 164 EXPECT_EQ("Test Page Title", page_title); |
139 | 165 |
140 content::WebContentsDestroyedWatcher destroyed_watcher( | 166 content::WebContentsDestroyedWatcher destroyed_watcher( |
141 destination_web_contents); | 167 destination_web_contents); |
142 browser()->tab_strip_model()->CloseWebContentsAt(1, 0); | 168 browser()->tab_strip_model()->CloseWebContentsAt(1, 0); |
143 destroyed_watcher.Wait(); | 169 destroyed_watcher.Wait(); |
144 } | 170 } |
145 | 171 |
146 } // namespace dom_distiller | 172 } // namespace dom_distiller |
OLD | NEW |