| 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 "chrome/browser/dom_distiller/dom_distiller_service_factory.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 11 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 12 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
| 13 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "components/dom_distiller/content/dom_distiller_viewer_source.h" | 16 #include "components/dom_distiller/content/dom_distiller_viewer_source.h" |
| 17 #include "components/dom_distiller/core/distiller.h" |
| 18 #include "components/dom_distiller/core/dom_distiller_service.h" |
| 19 #include "components/dom_distiller/core/dom_distiller_store.h" |
| 20 #include "components/dom_distiller/core/dom_distiller_test_util.h" |
| 21 #include "components/dom_distiller/core/fake_db.h" |
| 22 #include "components/dom_distiller/core/fake_distiller.h" |
| 23 #include "components/dom_distiller/core/task_tracker.h" |
| 16 #include "content/public/browser/render_view_host.h" | 24 #include "content/public/browser/render_view_host.h" |
| 17 #include "content/public/browser/url_data_source.h" | 25 #include "content/public/browser/url_data_source.h" |
| 18 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/browser/web_contents_observer.h" | 27 #include "content/public/browser/web_contents_observer.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 29 |
| 22 namespace dom_distiller { | 30 namespace dom_distiller { |
| 23 | 31 |
| 32 using test::FakeDB; |
| 33 using test::FakeDistiller; |
| 34 using test::MockDistillerFactory; |
| 35 using test::util::CreateStoreWithFakeDB; |
| 36 |
| 37 namespace { |
| 38 |
| 39 void AddEntry(const ArticleEntry& e, FakeDB::EntryMap* map) { |
| 40 (*map)[e.entry_id()] = e; |
| 41 } |
| 42 |
| 43 ArticleEntry CreateEntry(std::string entry_id, std::string page_url) { |
| 44 ArticleEntry entry; |
| 45 entry.set_entry_id(entry_id); |
| 46 if (!page_url.empty()) { |
| 47 ArticleEntryPage* page = entry.add_pages(); |
| 48 page->set_url(page_url); |
| 49 } |
| 50 return entry; |
| 51 } |
| 52 |
| 53 } // namespace |
| 54 |
| 24 // WebContents observer that stores reference to the current |RenderViewHost|. | 55 // WebContents observer that stores reference to the current |RenderViewHost|. |
| 25 class LoadSuccessObserver : public content::WebContentsObserver { | 56 class LoadSuccessObserver : public content::WebContentsObserver { |
| 26 public: | 57 public: |
| 27 explicit LoadSuccessObserver(content::WebContents* contents) | 58 explicit LoadSuccessObserver(content::WebContents* contents) |
| 28 : content::WebContentsObserver(contents), | 59 : content::WebContentsObserver(contents), |
| 29 validated_url_(GURL()), | 60 validated_url_(GURL()), |
| 30 finished_load_(false), | 61 finished_load_(false), |
| 31 load_failed_(false), | 62 load_failed_(false), |
| 63 web_contents_(contents), |
| 32 render_view_host_(NULL) {} | 64 render_view_host_(NULL) {} |
| 33 | 65 |
| 34 virtual void DidFinishLoad(int64 frame_id, | 66 virtual void DidFinishLoad(int64 frame_id, |
| 35 const GURL& validated_url, | 67 const GURL& validated_url, |
| 36 bool is_main_frame, | 68 bool is_main_frame, |
| 37 content::RenderViewHost* render_view_host) | 69 content::RenderViewHost* render_view_host) |
| 38 OVERRIDE { | 70 OVERRIDE { |
| 39 validated_url_ = validated_url; | 71 validated_url_ = validated_url; |
| 40 finished_load_ = true; | 72 finished_load_ = true; |
| 41 render_view_host_ = render_view_host; | 73 render_view_host_ = render_view_host; |
| 42 } | 74 } |
| 43 | 75 |
| 44 virtual void DidFailProvisionalLoad(int64 frame_id, | 76 virtual void DidFailProvisionalLoad(int64 frame_id, |
| 45 const base::string16& frame_unique_name, | 77 const base::string16& frame_unique_name, |
| 46 bool is_main_frame, | 78 bool is_main_frame, |
| 47 const GURL& validated_url, | 79 const GURL& validated_url, |
| 48 int error_code, | 80 int error_code, |
| 49 const base::string16& error_description, | 81 const base::string16& error_description, |
| 50 content::RenderViewHost* render_view_host) | 82 content::RenderViewHost* render_view_host) |
| 51 OVERRIDE { | 83 OVERRIDE { |
| 52 load_failed_ = true; | 84 load_failed_ = true; |
| 53 } | 85 } |
| 54 | 86 |
| 55 const GURL& validated_url() const { return validated_url_; } | 87 const GURL& validated_url() const { return validated_url_; } |
| 56 bool finished_load() const { return finished_load_; } | 88 bool finished_load() const { return finished_load_; } |
| 57 bool load_failed() const { return load_failed_; } | 89 bool load_failed() const { return load_failed_; } |
| 90 content::WebContents* web_contents() const { return web_contents_; } |
| 58 | 91 |
| 59 const content::RenderViewHost* render_view_host() const { | 92 const content::RenderViewHost* render_view_host() const { |
| 60 return render_view_host_; | 93 return render_view_host_; |
| 61 } | 94 } |
| 62 | 95 |
| 63 private: | 96 private: |
| 64 GURL validated_url_; | 97 GURL validated_url_; |
| 65 bool finished_load_; | 98 bool finished_load_; |
| 66 bool load_failed_; | 99 bool load_failed_; |
| 100 content::WebContents* web_contents_; |
| 67 content::RenderViewHost* render_view_host_; | 101 content::RenderViewHost* render_view_host_; |
| 68 | 102 |
| 69 DISALLOW_COPY_AND_ASSIGN(LoadSuccessObserver); | 103 DISALLOW_COPY_AND_ASSIGN(LoadSuccessObserver); |
| 70 }; | 104 }; |
| 71 | 105 |
| 72 class DomDistillerViewerSourceBrowserTest : public InProcessBrowserTest { | 106 class DomDistillerViewerSourceBrowserTest : public InProcessBrowserTest { |
| 73 public: | 107 public: |
| 74 DomDistillerViewerSourceBrowserTest() {} | 108 DomDistillerViewerSourceBrowserTest() {} |
| 75 virtual ~DomDistillerViewerSourceBrowserTest() {} | 109 virtual ~DomDistillerViewerSourceBrowserTest() {} |
| 76 | 110 |
| 111 virtual void SetUpOnMainThread() OVERRIDE { |
| 112 database_model_ = new FakeDB::EntryMap; |
| 113 } |
| 114 |
| 115 virtual void CleanUpOnMainThread() OVERRIDE { delete database_model_; } |
| 116 |
| 77 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 117 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 78 command_line->AppendSwitch(switches::kEnableDomDistiller); | 118 command_line->AppendSwitch(switches::kEnableDomDistiller); |
| 79 } | 119 } |
| 120 |
| 121 static BrowserContextKeyedService* Build(content::BrowserContext* context) { |
| 122 FakeDB* fake_db = new FakeDB(database_model_); |
| 123 MockDistillerFactory* factory = new MockDistillerFactory(); |
| 124 DomDistillerContextKeyedService* service = |
| 125 new DomDistillerContextKeyedService( |
| 126 scoped_ptr<DomDistillerStoreInterface>( |
| 127 CreateStoreWithFakeDB(fake_db, FakeDB::EntryMap())), |
| 128 scoped_ptr<DistillerFactory>(factory)); |
| 129 fake_db->InitCallback(true); |
| 130 fake_db->LoadCallback(true); |
| 131 if (expect_distillation_) { |
| 132 // There will only be destillation of an article if the database contains |
| 133 // the article. |
| 134 FakeDistiller* distiller = new FakeDistiller(true); |
| 135 EXPECT_CALL(*factory, CreateDistillerImpl()) |
| 136 .WillOnce(testing::Return(distiller)); |
| 137 } |
| 138 return service; |
| 139 } |
| 140 |
| 141 void ViewSingleDistilledPage(); |
| 142 |
| 143 // Database entries. |
| 144 static FakeDB::EntryMap* database_model_; |
| 145 static bool expect_distillation_; |
| 80 }; | 146 }; |
| 81 | 147 |
| 148 FakeDB::EntryMap* DomDistillerViewerSourceBrowserTest::database_model_; |
| 149 bool DomDistillerViewerSourceBrowserTest::expect_distillation_ = false; |
| 150 |
| 82 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings | 151 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings |
| 83 // are enabled. | 152 // are enabled when the article exists in the database. |
| 84 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, NoWebUIBindings) { | 153 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, |
| 154 NoWebUIBindingsArticleExists) { |
| 155 // Ensure there is one item in the database, which will trigger distillation. |
| 156 ArticleEntry entry = CreateEntry("DISTILLED", "http://example.com/1"); |
| 157 AddEntry(entry, database_model_); |
| 158 expect_distillation_ = true; |
| 159 ViewSingleDistilledPage(); |
| 160 } |
| 161 |
| 162 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings |
| 163 // are enabled when the article is not found. |
| 164 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, |
| 165 NoWebUIBindingsArticleNotFound) { |
| 166 // The article does not exist, so assume no distillation will happen. |
| 167 expect_distillation_ = false; |
| 168 ViewSingleDistilledPage(); |
| 169 } |
| 170 |
| 171 void DomDistillerViewerSourceBrowserTest::ViewSingleDistilledPage() { |
| 172 // Create the service. |
| 173 DomDistillerContextKeyedService* service = |
| 174 static_cast<DomDistillerContextKeyedService*>( |
| 175 dom_distiller::DomDistillerServiceFactory::GetInstance() |
| 176 ->SetTestingFactoryAndUse(browser()->profile(), &Build)); |
| 177 |
| 85 // Ensure the source is registered. | 178 // Ensure the source is registered. |
| 86 // TODO(nyquist): Remove when the source is always registered on startup. | 179 // TODO(nyquist): Remove when the source is always registered on startup. |
| 87 content::URLDataSource::Add( | 180 DomDistillerViewerSource* source = |
| 88 browser()->profile(), | 181 new DomDistillerViewerSource(service, chrome::kDomDistillerScheme); |
| 89 new DomDistillerViewerSource(chrome::kDomDistillerScheme)); | 182 content::URLDataSource::Add(browser()->profile(), source); |
| 90 | 183 |
| 91 // Setup observer to inspect the RenderViewHost after committed navigation. | 184 // Setup observer to inspect the RenderViewHost after committed navigation. |
| 92 content::WebContents* contents = | 185 content::WebContents* contents = |
| 93 browser()->tab_strip_model()->GetActiveWebContents(); | 186 browser()->tab_strip_model()->GetActiveWebContents(); |
| 94 LoadSuccessObserver observer(contents); | 187 LoadSuccessObserver observer(contents); |
| 95 | 188 |
| 96 // Navigate to a URL which the source should respond to. | 189 // Navigate to a URL which the source should respond to. |
| 97 std::string url_without_scheme = "://distilled"; | 190 std::string url_without_scheme = "://distilled"; |
| 98 GURL url(chrome::kDomDistillerScheme + url_without_scheme); | 191 GURL url(chrome::kDomDistillerScheme + url_without_scheme); |
| 99 ui_test_utils::NavigateToURL(browser(), url); | 192 ui_test_utils::NavigateToURL(browser(), url); |
| 100 | 193 |
| 101 // A navigation should have succeeded to the correct URL. | 194 // A navigation should have succeeded to the correct URL. |
| 102 ASSERT_FALSE(observer.load_failed()); | 195 ASSERT_FALSE(observer.load_failed()); |
| 103 ASSERT_TRUE(observer.finished_load()); | 196 ASSERT_TRUE(observer.finished_load()); |
| 104 ASSERT_EQ(url, observer.validated_url()); | 197 ASSERT_EQ(url, observer.validated_url()); |
| 105 // Ensure no bindings. | 198 // Ensure no bindings. |
| 106 const content::RenderViewHost* render_view_host = observer.render_view_host(); | 199 const content::RenderViewHost* render_view_host = observer.render_view_host(); |
| 107 ASSERT_EQ(0, render_view_host->GetEnabledBindings()); | 200 ASSERT_EQ(0, render_view_host->GetEnabledBindings()); |
| 201 // The MIME-type should always be text/html. |
| 202 EXPECT_EQ("text/html", observer.web_contents()->GetContentsMimeType()); |
| 108 } | 203 } |
| 109 | 204 |
| 110 } // namespace dom_distiller | 205 } // namespace dom_distiller |
| OLD | NEW |