| 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/guid.h" | 8 #include "base/guid.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h" | 12 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "chrome/common/chrome_isolated_world_ids.h" | 16 #include "chrome/common/chrome_isolated_world_ids.h" |
| 16 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/test/base/in_process_browser_test.h" | 18 #include "chrome/test/base/in_process_browser_test.h" |
| 18 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
| 19 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" | 20 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 "document.getElementById('content').innerHTML)"; | 65 "document.getElementById('content').innerHTML)"; |
| 65 | 66 |
| 66 const char kGetTitle[] = | 67 const char kGetTitle[] = |
| 67 "window.domAutomationController.send(" | 68 "window.domAutomationController.send(" |
| 68 "document.title)"; | 69 "document.title)"; |
| 69 | 70 |
| 70 const char kGetBodyClass[] = | 71 const char kGetBodyClass[] = |
| 71 "window.domAutomationController.send(" | 72 "window.domAutomationController.send(" |
| 72 "document.body.className)"; | 73 "document.body.className)"; |
| 73 | 74 |
| 75 const char kGetFontSize[] = |
| 76 "window.domAutomationController.send(" |
| 77 "window.getComputedStyle(document.documentElement).fontSize)"; |
| 78 |
| 74 const unsigned kDarkToolbarThemeColor = 0xFF1A1A1A; | 79 const unsigned kDarkToolbarThemeColor = 0xFF1A1A1A; |
| 75 | 80 |
| 76 const char kTestDistillerObject[] = | 81 const char kTestDistillerObject[] = |
| 77 "window.domAutomationController.send(" | 82 "window.domAutomationController.send(" |
| 78 "typeof distiller == 'object')"; | 83 "typeof distiller == 'object')"; |
| 79 | 84 |
| 80 void AddEntry(const ArticleEntry& e, FakeDB<ArticleEntry>::EntryMap* map) { | 85 void AddEntry(const ArticleEntry& e, FakeDB<ArticleEntry>::EntryMap* map) { |
| 81 (*map)[e.entry_id()] = e; | 86 (*map)[e.entry_id()] = e; |
| 82 } | 87 } |
| 83 | 88 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 if (expect_distiller_page_) { | 141 if (expect_distiller_page_) { |
| 137 MockDistillerPage* distiller_page = new MockDistillerPage(); | 142 MockDistillerPage* distiller_page = new MockDistillerPage(); |
| 138 EXPECT_CALL(*distiller_page_factory_, CreateDistillerPageImpl()) | 143 EXPECT_CALL(*distiller_page_factory_, CreateDistillerPageImpl()) |
| 139 .WillOnce(testing::Return(distiller_page)); | 144 .WillOnce(testing::Return(distiller_page)); |
| 140 } | 145 } |
| 141 return service.Pass(); | 146 return service.Pass(); |
| 142 } | 147 } |
| 143 | 148 |
| 144 void ViewSingleDistilledPage(const GURL& url, | 149 void ViewSingleDistilledPage(const GURL& url, |
| 145 const std::string& expected_mime_type); | 150 const std::string& expected_mime_type); |
| 151 void PrefTest(bool is_error_page); |
| 146 // Database entries. | 152 // Database entries. |
| 147 static FakeDB<ArticleEntry>::EntryMap* database_model_; | 153 static FakeDB<ArticleEntry>::EntryMap* database_model_; |
| 148 static bool expect_distillation_; | 154 static bool expect_distillation_; |
| 149 static bool expect_distiller_page_; | 155 static bool expect_distiller_page_; |
| 150 static MockDistillerFactory* distiller_factory_; | 156 static MockDistillerFactory* distiller_factory_; |
| 151 }; | 157 }; |
| 152 | 158 |
| 153 FakeDB<ArticleEntry>::EntryMap* | 159 FakeDB<ArticleEntry>::EntryMap* |
| 154 DomDistillerViewerSourceBrowserTest::database_model_; | 160 DomDistillerViewerSourceBrowserTest::database_model_; |
| 155 bool DomDistillerViewerSourceBrowserTest::expect_distillation_ = false; | 161 bool DomDistillerViewerSourceBrowserTest::expect_distillation_ = false; |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 489 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 484 contents, kGetLoadIndicatorClassName, &result)); | 490 contents, kGetLoadIndicatorClassName, &result)); |
| 485 EXPECT_EQ("hidden", result); | 491 EXPECT_EQ("hidden", result); |
| 486 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 492 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 487 contents, kGetContent , &result)); | 493 contents, kGetContent , &result)); |
| 488 EXPECT_THAT(result, HasSubstr("Page 1 content")); | 494 EXPECT_THAT(result, HasSubstr("Page 1 content")); |
| 489 EXPECT_THAT(result, HasSubstr("Page 2 content")); | 495 EXPECT_THAT(result, HasSubstr("Page 2 content")); |
| 490 } | 496 } |
| 491 | 497 |
| 492 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, PrefChange) { | 498 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, PrefChange) { |
| 493 expect_distillation_ = true; | 499 PrefTest(false); |
| 494 expect_distiller_page_ = true; | 500 } |
| 495 GURL view_url("http://www.example.com/1"); | 501 |
| 502 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, PrefChangeError) { |
| 503 PrefTest(true); |
| 504 } |
| 505 |
| 506 void DomDistillerViewerSourceBrowserTest::PrefTest(bool is_error_page) { |
| 507 GURL url; |
| 508 if (is_error_page) { |
| 509 expect_distillation_ = false; |
| 510 expect_distiller_page_ = false; |
| 511 url = GURL("chrome-distiller://bad"); |
| 512 } else { |
| 513 expect_distillation_ = true; |
| 514 expect_distiller_page_ = true; |
| 515 GURL view_url("http://www.example.com/1"); |
| 516 url = url_utils::GetDistillerViewUrlFromUrl(kDomDistillerScheme, view_url); |
| 517 } |
| 496 content::WebContents* contents = | 518 content::WebContents* contents = |
| 497 browser()->tab_strip_model()->GetActiveWebContents(); | 519 browser()->tab_strip_model()->GetActiveWebContents(); |
| 498 const GURL url = | |
| 499 url_utils::GetDistillerViewUrlFromUrl(kDomDistillerScheme, view_url); | |
| 500 ViewSingleDistilledPage(url, "text/html"); | 520 ViewSingleDistilledPage(url, "text/html"); |
| 501 content::WaitForLoadStop(contents); | 521 content::WaitForLoadStop(contents); |
| 502 std::string result; | 522 std::string result; |
| 503 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 523 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 504 contents, kGetBodyClass, &result)); | 524 contents, kGetBodyClass, &result)); |
| 505 EXPECT_EQ("light sans-serif", result); | 525 EXPECT_EQ("light sans-serif", result); |
| 506 | 526 |
| 507 DistilledPagePrefs* distilled_page_prefs = | 527 DistilledPagePrefs* distilled_page_prefs = |
| 508 DomDistillerServiceFactory::GetForBrowserContext( | 528 DomDistillerServiceFactory::GetForBrowserContext( |
| 509 browser()->profile())->GetDistilledPagePrefs(); | 529 browser()->profile())->GetDistilledPagePrefs(); |
| 510 | 530 |
| 531 // Test theme. |
| 511 distilled_page_prefs->SetTheme(DistilledPagePrefs::DARK); | 532 distilled_page_prefs->SetTheme(DistilledPagePrefs::DARK); |
| 512 base::RunLoop().RunUntilIdle(); | 533 base::RunLoop().RunUntilIdle(); |
| 513 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 534 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 514 contents, kGetBodyClass, &result)); | 535 contents, kGetBodyClass, &result)); |
| 515 EXPECT_EQ("dark sans-serif", result); | 536 EXPECT_EQ("dark sans-serif", result); |
| 516 | 537 |
| 517 // Verify that the theme color for the tab is updated as well. | 538 // Verify that the theme color for the tab is updated as well. |
| 518 EXPECT_EQ(kDarkToolbarThemeColor, contents->GetThemeColor()); | 539 EXPECT_EQ(kDarkToolbarThemeColor, contents->GetThemeColor()); |
| 519 | 540 |
| 541 // Test font family. |
| 520 distilled_page_prefs->SetFontFamily(DistilledPagePrefs::SERIF); | 542 distilled_page_prefs->SetFontFamily(DistilledPagePrefs::SERIF); |
| 521 base::RunLoop().RunUntilIdle(); | 543 base::RunLoop().RunUntilIdle(); |
| 522 EXPECT_TRUE( | 544 EXPECT_TRUE( |
| 523 content::ExecuteScriptAndExtractString(contents, kGetBodyClass, &result)); | 545 content::ExecuteScriptAndExtractString(contents, kGetBodyClass, &result)); |
| 524 EXPECT_EQ("dark serif", result); | 546 EXPECT_EQ("dark serif", result); |
| 547 |
| 548 // Test font scaling. |
| 549 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 550 contents, kGetFontSize, &result)); |
| 551 double oldFontSize; |
| 552 base::StringToDouble(result, &oldFontSize); |
| 553 |
| 554 const double kScale = 1.23; |
| 555 distilled_page_prefs->SetFontScaling(kScale); |
| 556 base::RunLoop().RunUntilIdle(); |
| 557 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 558 contents, kGetFontSize, &result)); |
| 559 double fontSize; |
| 560 base::StringToDouble(result, &fontSize); |
| 561 ASSERT_FLOAT_EQ(kScale, fontSize/oldFontSize); |
| 562 } |
| 563 |
| 564 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, PrefPersist) { |
| 565 expect_distillation_ = false; |
| 566 expect_distiller_page_ = false; |
| 567 const GURL url("chrome-distiller://bad"); |
| 568 ui_test_utils::NavigateToURL(browser(), url); |
| 569 content::WebContents* contents = |
| 570 browser()->tab_strip_model()->GetActiveWebContents(); |
| 571 content::WaitForLoadStop(contents); |
| 572 |
| 573 std::string result; |
| 574 DistilledPagePrefs* distilled_page_prefs = |
| 575 DomDistillerServiceFactory::GetForBrowserContext( |
| 576 browser()->profile())->GetDistilledPagePrefs(); |
| 577 |
| 578 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 579 contents, kGetFontSize, &result)); |
| 580 double oldFontSize; |
| 581 base::StringToDouble(result, &oldFontSize); |
| 582 |
| 583 // Set preference. |
| 584 const double kScale = 1.23; |
| 585 distilled_page_prefs->SetTheme(DistilledPagePrefs::DARK); |
| 586 distilled_page_prefs->SetFontFamily(DistilledPagePrefs::SERIF); |
| 587 distilled_page_prefs->SetFontScaling(kScale); |
| 588 |
| 589 base::RunLoop().RunUntilIdle(); |
| 590 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 591 contents, kGetBodyClass, &result)); |
| 592 EXPECT_EQ("dark serif", result); |
| 593 EXPECT_EQ(kDarkToolbarThemeColor, contents->GetThemeColor()); |
| 594 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 595 contents, kGetFontSize, &result)); |
| 596 double fontSize; |
| 597 base::StringToDouble(result, &fontSize); |
| 598 ASSERT_FLOAT_EQ(kScale, fontSize/oldFontSize); |
| 599 |
| 600 // Make sure perf persist across web pages. |
| 601 GURL url2("chrome-distiller://bad2"); |
| 602 ui_test_utils::NavigateToURL(browser(), url2); |
| 603 content::WaitForLoadStop(contents); |
| 604 |
| 605 base::RunLoop().RunUntilIdle(); |
| 606 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 607 contents, kGetBodyClass, &result)); |
| 608 EXPECT_EQ("dark serif", result); |
| 609 EXPECT_EQ(kDarkToolbarThemeColor, contents->GetThemeColor()); |
| 610 |
| 611 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 612 contents, kGetFontSize, &result)); |
| 613 base::StringToDouble(result, &fontSize); |
| 614 ASSERT_FLOAT_EQ(kScale, fontSize/oldFontSize); |
| 525 } | 615 } |
| 526 | 616 |
| 527 } // namespace dom_distiller | 617 } // namespace dom_distiller |
| OLD | NEW |