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 void OnChangeFontFamily(DistilledPagePrefs::FontFamily new_font
) override
{
26 DistilledPagePrefs::FontFamily
GetFontFamily() { return font_
; }
28 void OnChangeTheme(DistilledPagePrefs::Theme new_theme
) override
{
32 DistilledPagePrefs::Theme
GetTheme() { return theme_
; }
35 DistilledPagePrefs::FontFamily font_
;
36 DistilledPagePrefs::Theme theme_
;
41 class DistilledPagePrefsTest
: public testing::Test
{
43 void SetUp() override
{
44 pref_service_
.reset(new user_prefs::TestingPrefServiceSyncable());
45 DistilledPagePrefs::RegisterProfilePrefs(pref_service_
->registry());
46 distilled_page_prefs_
.reset(new DistilledPagePrefs(pref_service_
.get()));
49 scoped_ptr
<user_prefs::TestingPrefServiceSyncable
> pref_service_
;
50 scoped_ptr
<DistilledPagePrefs
> distilled_page_prefs_
;
53 base::MessageLoop message_loop_
;
56 TEST_F(DistilledPagePrefsTest
, TestingOnChangeFontIsBeingCalled
) {
58 distilled_page_prefs_
->AddObserver(&obs
);
59 distilled_page_prefs_
->SetFontFamily(DistilledPagePrefs::MONOSPACE
);
60 EXPECT_EQ(DistilledPagePrefs::SANS_SERIF
, obs
.GetFontFamily());
61 base::RunLoop().RunUntilIdle();
62 EXPECT_EQ(DistilledPagePrefs::MONOSPACE
, obs
.GetFontFamily());
63 distilled_page_prefs_
->SetFontFamily(DistilledPagePrefs::SERIF
);
64 base::RunLoop().RunUntilIdle();
65 EXPECT_EQ(DistilledPagePrefs::SERIF
, obs
.GetFontFamily());
66 distilled_page_prefs_
->RemoveObserver(&obs
);
69 TEST_F(DistilledPagePrefsTest
, TestingMultipleObserversFont
) {
71 distilled_page_prefs_
->AddObserver(&obs
);
73 distilled_page_prefs_
->AddObserver(&obs2
);
74 distilled_page_prefs_
->SetFontFamily(DistilledPagePrefs::SERIF
);
75 base::RunLoop().RunUntilIdle();
76 EXPECT_EQ(DistilledPagePrefs::SERIF
, obs
.GetFontFamily());
77 EXPECT_EQ(DistilledPagePrefs::SERIF
, obs2
.GetFontFamily());
78 distilled_page_prefs_
->RemoveObserver(&obs
);
79 distilled_page_prefs_
->SetFontFamily(DistilledPagePrefs::MONOSPACE
);
80 base::RunLoop().RunUntilIdle();
81 EXPECT_EQ(DistilledPagePrefs::SERIF
, obs
.GetFontFamily());
82 EXPECT_EQ(DistilledPagePrefs::MONOSPACE
, obs2
.GetFontFamily());
83 distilled_page_prefs_
->RemoveObserver(&obs2
);
86 TEST_F(DistilledPagePrefsTest
, TestingOnChangeThemeIsBeingCalled
) {
88 distilled_page_prefs_
->AddObserver(&obs
);
89 distilled_page_prefs_
->SetTheme(DistilledPagePrefs::SEPIA
);
90 EXPECT_EQ(DistilledPagePrefs::LIGHT
, obs
.GetTheme());
91 base::RunLoop().RunUntilIdle();
92 EXPECT_EQ(DistilledPagePrefs::SEPIA
, obs
.GetTheme());
93 distilled_page_prefs_
->SetTheme(DistilledPagePrefs::DARK
);
94 base::RunLoop().RunUntilIdle();
95 EXPECT_EQ(DistilledPagePrefs::DARK
, obs
.GetTheme());
96 distilled_page_prefs_
->RemoveObserver(&obs
);
99 TEST_F(DistilledPagePrefsTest
, TestingMultipleObserversTheme
) {
101 distilled_page_prefs_
->AddObserver(&obs
);
102 TestingObserver obs2
;
103 distilled_page_prefs_
->AddObserver(&obs2
);
104 distilled_page_prefs_
->SetTheme(DistilledPagePrefs::SEPIA
);
105 base::RunLoop().RunUntilIdle();
106 EXPECT_EQ(DistilledPagePrefs::SEPIA
, obs
.GetTheme());
107 EXPECT_EQ(DistilledPagePrefs::SEPIA
, obs2
.GetTheme());
108 distilled_page_prefs_
->RemoveObserver(&obs
);
109 distilled_page_prefs_
->SetTheme(DistilledPagePrefs::LIGHT
);
110 base::RunLoop().RunUntilIdle();
111 EXPECT_EQ(DistilledPagePrefs::SEPIA
, obs
.GetTheme());
112 EXPECT_EQ(DistilledPagePrefs::LIGHT
, obs2
.GetTheme());
113 distilled_page_prefs_
->RemoveObserver(&obs2
);
116 } // namespace dom_distiller