Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / dom_distiller / core / distilled_page_prefs_unittests.cc
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
12 namespace dom_distiller {
13
14 namespace {
15
16 class TestingObserver : public DistilledPagePrefs::Observer {
17  public:
18   TestingObserver()
19       : font_(DistilledPagePrefs::SANS_SERIF),
20         theme_(DistilledPagePrefs::LIGHT) {}
21
22   void OnChangeFontFamily(DistilledPagePrefs::FontFamily new_font) override {
23     font_ = new_font;
24   }
25
26   DistilledPagePrefs::FontFamily GetFontFamily() { return font_; }
27
28   void OnChangeTheme(DistilledPagePrefs::Theme new_theme) override {
29     theme_ = new_theme;
30   }
31
32   DistilledPagePrefs::Theme GetTheme() { return theme_; }
33
34  private:
35   DistilledPagePrefs::FontFamily font_;
36   DistilledPagePrefs::Theme theme_;
37 };
38
39 }  // namespace
40
41 class DistilledPagePrefsTest : public testing::Test {
42  protected:
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()));
47   }
48
49   scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_;
50   scoped_ptr<DistilledPagePrefs> distilled_page_prefs_;
51
52  private:
53   base::MessageLoop message_loop_;
54 };
55
56 TEST_F(DistilledPagePrefsTest, TestingOnChangeFontIsBeingCalled) {
57   TestingObserver obs;
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);
67 }
68
69 TEST_F(DistilledPagePrefsTest, TestingMultipleObserversFont) {
70   TestingObserver obs;
71   distilled_page_prefs_->AddObserver(&obs);
72   TestingObserver obs2;
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);
84 }
85
86 TEST_F(DistilledPagePrefsTest, TestingOnChangeThemeIsBeingCalled) {
87   TestingObserver obs;
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);
97 }
98
99 TEST_F(DistilledPagePrefsTest, TestingMultipleObserversTheme) {
100   TestingObserver obs;
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);
114 }
115
116 }  // namespace dom_distiller