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.
5 #include "components/dom_distiller/core/distilled_page_prefs.h"
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"
12 namespace dom_distiller
{
16 class TestingObserver
: public DistilledPagePrefs::Observer
{
19 : font_(DistilledPagePrefs::SANS_SERIF
),
20 theme_(DistilledPagePrefs::LIGHT
) {}
22 virtual void OnChangeFontFamily(
23 DistilledPagePrefs::FontFamily new_font
) OVERRIDE
{
27 DistilledPagePrefs::FontFamily
GetFontFamily() { return font_
; }
29 virtual void OnChangeTheme(DistilledPagePrefs::Theme new_theme
) OVERRIDE
{
33 DistilledPagePrefs::Theme
GetTheme() { return theme_
; }
36 DistilledPagePrefs::FontFamily font_
;
37 DistilledPagePrefs::Theme theme_
;
42 class DistilledPagePrefsTest
: public testing::Test
{
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()));
50 scoped_ptr
<user_prefs::TestingPrefServiceSyncable
> pref_service_
;
51 scoped_ptr
<DistilledPagePrefs
> distilled_page_prefs_
;
54 base::MessageLoop message_loop_
;
57 TEST_F(DistilledPagePrefsTest
, TestingOnChangeFontIsBeingCalled
) {
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
);
70 TEST_F(DistilledPagePrefsTest
, TestingMultipleObserversFont
) {
72 distilled_page_prefs_
->AddObserver(&obs
);
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
);
87 TEST_F(DistilledPagePrefsTest
, TestingOnChangeThemeIsBeingCalled
) {
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
);
100 TEST_F(DistilledPagePrefsTest
, TestingMultipleObserversTheme
) {
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
);
117 } // namespace dom_distiller