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