Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
|
nyquist
2014/07/09 23:49:03
I don't think you want to delete this unittest.
sunangel
2014/07/10 14:31:52
Done.
| |
| 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/bind.h" | |
| 8 #include "base/memory/weak_ptr.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/observer_list.h" | |
| 11 #include "base/prefs/pref_registry_simple.h" | |
| 12 #include "base/prefs/testing_pref_service.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "base/values.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace { | |
| 18 const char kThemePref[] = "dom_distiller.theme"; | |
| 19 } | |
| 20 | |
| 21 namespace dom_distiller { | |
| 22 | |
| 23 class TestingDistilledPagePrefs { | |
| 24 public: | |
| 25 // Possible themes for distilled page. | |
| 26 enum Theme { | |
| 27 kLight, | |
| 28 kDark, | |
| 29 kSepia | |
| 30 }; | |
| 31 | |
| 32 class Observer { | |
| 33 public: | |
| 34 virtual void OnChangeTheme(Theme theme) = 0; | |
| 35 }; | |
| 36 | |
| 37 void AddObserver(Observer* obs); | |
| 38 void RemoveObserver(Observer* obs); | |
| 39 int GetNumberOfObservers(); | |
| 40 | |
| 41 TestingDistilledPagePrefs(); | |
| 42 ~TestingDistilledPagePrefs(); | |
| 43 | |
| 44 void SetTheme(Theme new_theme); | |
| 45 Theme GetTheme(); | |
| 46 | |
| 47 private: | |
| 48 void NotifyOnChangeTheme(Theme theme); | |
| 49 void RegisterPrefs(); | |
| 50 | |
| 51 ObserverList<Observer> observers_; | |
| 52 TestingPrefServiceSimple* pref_service_; | |
| 53 | |
| 54 base::WeakPtrFactory<TestingDistilledPagePrefs> weak_ptr_factory_; | |
| 55 }; | |
| 56 | |
| 57 class TestingDistilledPagePrefsObserver | |
| 58 : public TestingDistilledPagePrefs::Observer { | |
| 59 public: | |
| 60 virtual void OnChangeTheme(TestingDistilledPagePrefs::Theme) OVERRIDE; | |
| 61 TestingDistilledPagePrefs::Theme GetTheme(); | |
| 62 | |
| 63 private: | |
| 64 TestingDistilledPagePrefs::Theme theme; | |
| 65 }; | |
| 66 | |
| 67 TestingDistilledPagePrefs::TestingDistilledPagePrefs() | |
| 68 : pref_service_(new TestingPrefServiceSimple()), | |
| 69 weak_ptr_factory_(this) { | |
| 70 RegisterPrefs(); | |
| 71 } | |
| 72 | |
| 73 TestingDistilledPagePrefs::~TestingDistilledPagePrefs() { | |
| 74 pref_service_->~TestingPrefServiceSimple(); | |
| 75 } | |
| 76 | |
| 77 void TestingDistilledPagePrefs::AddObserver(Observer* obs) { | |
| 78 observers_.AddObserver(obs); | |
| 79 } | |
| 80 | |
| 81 void TestingDistilledPagePrefs::RemoveObserver(Observer* obs) { | |
| 82 observers_.RemoveObserver(obs); | |
| 83 } | |
| 84 | |
| 85 int TestingDistilledPagePrefs::GetNumberOfObservers() { | |
| 86 ObserverListBase<dom_distiller::TestingDistilledPagePrefs::Observer>::Iterator * iter = new ObserverListBase<TestingDistilledPagePrefs::Observer>::Iterator(obs ervers_); | |
| 87 Observer* obs; | |
| 88 int count = 0; | |
| 89 while((obs = iter->GetNext()) != NULL) { | |
| 90 count++; | |
| 91 } | |
| 92 //iter->ObserverListBase<dom_distiller::TestingDistilledPagePrefs::Observer>:: ~Iterator(); | |
| 93 return count; | |
| 94 } | |
| 95 | |
| 96 void TestingDistilledPagePrefs::RegisterPrefs() { | |
| 97 pref_service_->registry()->RegisterIntegerPref(kThemePref, Theme::kLight); | |
| 98 } | |
| 99 | |
| 100 | |
| 101 void TestingDistilledPagePrefs::SetTheme(Theme new_theme) { | |
| 102 pref_service_->SetUserPref(kThemePref, new base::FundamentalValue(new_theme)); | |
| 103 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&TestingDistilled PagePrefs::NotifyOnChangeTheme, weak_ptr_factory_.GetWeakPtr(), new_theme)); | |
| 104 } | |
| 105 | |
| 106 TestingDistilledPagePrefs::Theme TestingDistilledPagePrefs::GetTheme() { | |
| 107 int temp; | |
| 108 pref_service_->GetUserPref(kThemePref)->GetAsInteger(&temp); | |
| 109 return (Theme) temp; | |
| 110 } | |
| 111 | |
| 112 void TestingDistilledPagePrefs::NotifyOnChangeTheme(Theme theme) { | |
| 113 FOR_EACH_OBSERVER(Observer, observers_, OnChangeTheme(theme)); | |
| 114 } | |
| 115 | |
| 116 void TestingDistilledPagePrefsObserver::OnChangeTheme( | |
| 117 TestingDistilledPagePrefs::Theme new_theme) { | |
| 118 theme = new_theme; | |
| 119 } | |
| 120 | |
| 121 TestingDistilledPagePrefs::Theme TestingDistilledPagePrefsObserver::GetTheme() { | |
| 122 return theme; | |
| 123 } | |
| 124 | |
| 125 class DistilledPagePrefsTest : public testing::Test { | |
| 126 }; | |
| 127 | |
| 128 TEST_F(DistilledPagePrefsTest, TestingOnChangeThemeIsBeingCalled) { | |
| 129 scoped_ptr<TestingDistilledPagePrefs> distilled_page_prefs(new TestingDistille dPagePrefs); | |
| 130 TestingDistilledPagePrefsObserver* obs = new TestingDistilledPagePrefsObserver (); | |
| 131 distilled_page_prefs->AddObserver(obs); | |
| 132 distilled_page_prefs->SetTheme(TestingDistilledPagePrefs::Theme::kDark); | |
| 133 base::RunLoop run_loop; | |
| 134 run_loop.RunUntilIdle(); | |
| 135 EXPECT_EQ(distilled_page_prefs->GetTheme(), obs->GetTheme()); | |
| 136 } | |
| 137 | |
| 138 TEST_F(DistilledPagePrefsTest, TestingAddRemoveObserver) { | |
| 139 /* scoped_ptr<TestingDistilledPagePrefs> distilled_page_prefs(new TestingDistil ledPagePrefs); | |
| 140 TestingDistilledPagePrefsObserver* obs = new TestingDistilledPagePrefsObserver (); | |
| 141 int count = 0; | |
| 142 ObserverListBase<TestingDistilledPagePrefs::Observer>::Iterator iter = new Obs erverListBase<TestingDistilledPagePrefs::Observer>::Iterator(distilled_page_pref s->observers_); | |
| 143 distilled_page_prefs->AddObserver(obs);*/ | |
| 144 // EXPECT_EQ(distilled_page_prefs->observers_. | |
| 145 /* base::MessageLoop* message_loop(base::MessageLoop::Type::TYPE_DEFAULT); | |
| 146 distilled_page_prefs->SetTheme(TestingDistilledPagePrefs::Theme::kSepia, messa ge_loop);*/ | |
| 147 } | |
| 148 | |
| 149 } // namespace dom_distiller | |
| OLD | NEW |