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 "components/dom_distiller/core/distilled_page_prefs.h" 6 7#include "base/message_loop/message_loop.h" 8#include "base/run_loop.h" 9#include "components/pref_registry/testing_pref_service_syncable.h" 10#include "testing/gtest/include/gtest/gtest.h" 11 12namespace dom_distiller { 13 14namespace { 15 16class TestingObserver : public DistilledPagePrefs::Observer { 17 public: 18 TestingObserver() 19 : font_(DistilledPagePrefs::SANS_SERIF), 20 theme_(DistilledPagePrefs::LIGHT) {} 21 22 virtual void OnChangeFontFamily( 23 DistilledPagePrefs::FontFamily new_font) OVERRIDE { 24 font_ = new_font; 25 } 26 27 DistilledPagePrefs::FontFamily GetFontFamily() { return font_; } 28 29 virtual void OnChangeTheme(DistilledPagePrefs::Theme new_theme) OVERRIDE { 30 theme_ = new_theme; 31 } 32 33 DistilledPagePrefs::Theme GetTheme() { return theme_; } 34 35 private: 36 DistilledPagePrefs::FontFamily font_; 37 DistilledPagePrefs::Theme theme_; 38}; 39 40} // namespace 41 42class DistilledPagePrefsTest : public testing::Test { 43 protected: 44 virtual void SetUp() OVERRIDE { 45 pref_service_.reset(new user_prefs::TestingPrefServiceSyncable()); 46 DistilledPagePrefs::RegisterProfilePrefs(pref_service_->registry()); 47 distilled_page_prefs_.reset(new DistilledPagePrefs(pref_service_.get())); 48 } 49 50 scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_; 51 scoped_ptr<DistilledPagePrefs> distilled_page_prefs_; 52 53 private: 54 base::MessageLoop message_loop_; 55}; 56 57TEST_F(DistilledPagePrefsTest, TestingOnChangeFontIsBeingCalled) { 58 TestingObserver obs; 59 distilled_page_prefs_->AddObserver(&obs); 60 distilled_page_prefs_->SetFontFamily(DistilledPagePrefs::MONOSPACE); 61 EXPECT_EQ(DistilledPagePrefs::SANS_SERIF, obs.GetFontFamily()); 62 base::RunLoop().RunUntilIdle(); 63 EXPECT_EQ(DistilledPagePrefs::MONOSPACE, obs.GetFontFamily()); 64 distilled_page_prefs_->SetFontFamily(DistilledPagePrefs::SERIF); 65 base::RunLoop().RunUntilIdle(); 66 EXPECT_EQ(DistilledPagePrefs::SERIF, obs.GetFontFamily()); 67 distilled_page_prefs_->RemoveObserver(&obs); 68} 69 70TEST_F(DistilledPagePrefsTest, TestingMultipleObserversFont) { 71 TestingObserver obs; 72 distilled_page_prefs_->AddObserver(&obs); 73 TestingObserver obs2; 74 distilled_page_prefs_->AddObserver(&obs2); 75 distilled_page_prefs_->SetFontFamily(DistilledPagePrefs::SERIF); 76 base::RunLoop().RunUntilIdle(); 77 EXPECT_EQ(DistilledPagePrefs::SERIF, obs.GetFontFamily()); 78 EXPECT_EQ(DistilledPagePrefs::SERIF, obs2.GetFontFamily()); 79 distilled_page_prefs_->RemoveObserver(&obs); 80 distilled_page_prefs_->SetFontFamily(DistilledPagePrefs::MONOSPACE); 81 base::RunLoop().RunUntilIdle(); 82 EXPECT_EQ(DistilledPagePrefs::SERIF, obs.GetFontFamily()); 83 EXPECT_EQ(DistilledPagePrefs::MONOSPACE, obs2.GetFontFamily()); 84 distilled_page_prefs_->RemoveObserver(&obs2); 85} 86 87TEST_F(DistilledPagePrefsTest, TestingOnChangeThemeIsBeingCalled) { 88 TestingObserver obs; 89 distilled_page_prefs_->AddObserver(&obs); 90 distilled_page_prefs_->SetTheme(DistilledPagePrefs::SEPIA); 91 EXPECT_EQ(DistilledPagePrefs::LIGHT, obs.GetTheme()); 92 base::RunLoop().RunUntilIdle(); 93 EXPECT_EQ(DistilledPagePrefs::SEPIA, obs.GetTheme()); 94 distilled_page_prefs_->SetTheme(DistilledPagePrefs::DARK); 95 base::RunLoop().RunUntilIdle(); 96 EXPECT_EQ(DistilledPagePrefs::DARK, obs.GetTheme()); 97 distilled_page_prefs_->RemoveObserver(&obs); 98} 99 100TEST_F(DistilledPagePrefsTest, TestingMultipleObserversTheme) { 101 TestingObserver obs; 102 distilled_page_prefs_->AddObserver(&obs); 103 TestingObserver obs2; 104 distilled_page_prefs_->AddObserver(&obs2); 105 distilled_page_prefs_->SetTheme(DistilledPagePrefs::SEPIA); 106 base::RunLoop().RunUntilIdle(); 107 EXPECT_EQ(DistilledPagePrefs::SEPIA, obs.GetTheme()); 108 EXPECT_EQ(DistilledPagePrefs::SEPIA, obs2.GetTheme()); 109 distilled_page_prefs_->RemoveObserver(&obs); 110 distilled_page_prefs_->SetTheme(DistilledPagePrefs::LIGHT); 111 base::RunLoop().RunUntilIdle(); 112 EXPECT_EQ(DistilledPagePrefs::SEPIA, obs.GetTheme()); 113 EXPECT_EQ(DistilledPagePrefs::LIGHT, obs2.GetTheme()); 114 distilled_page_prefs_->RemoveObserver(&obs2); 115} 116 117} // namespace dom_distiller 118