1 // Copyright (c) 2012 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 "chrome/browser/profiles/off_the_record_profile_impl.h"
7 #include "base/prefs/pref_registry_simple.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/run_loop.h"
11 #include "chrome/browser/net/ssl_config_service_manager.h"
12 #include "chrome/browser/prefs/browser_prefs.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/browser_with_test_window_test.h"
15 #include "chrome/test/base/testing_browser_process.h"
16 #include "chrome/test/base/testing_io_thread_state.h"
17 #include "chrome/test/base/testing_pref_service_syncable.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
20 #include "content/public/browser/host_zoom_map.h"
21 #include "net/dns/mock_host_resolver.h"
23 using content::HostZoomMap;
27 class TestingProfileWithHostZoomMap : public TestingProfile {
29 TestingProfileWithHostZoomMap() {
31 HostZoomMap::GetForBrowserContext(this)->AddZoomLevelChangedCallback(
32 base::Bind(&TestingProfileWithHostZoomMap::OnZoomLevelChanged,
33 base::Unretained(this)));
36 virtual ~TestingProfileWithHostZoomMap() {}
38 virtual Profile* GetOffTheRecordProfile() OVERRIDE {
39 if (!off_the_record_profile_)
40 off_the_record_profile_.reset(CreateOffTheRecordProfile());
41 return off_the_record_profile_.get();
44 virtual PrefService* GetOffTheRecordPrefs() OVERRIDE {
49 void OnZoomLevelChanged(const HostZoomMap::ZoomLevelChange& change) {
51 if (change.mode != HostZoomMap::ZOOM_CHANGED_FOR_HOST)
54 HostZoomMap* host_zoom_map = HostZoomMap::GetForBrowserContext(this);
56 double level = change.zoom_level;
57 DictionaryPrefUpdate update(prefs_.get(), prefs::kPerHostZoomLevels);
58 DictionaryValue* host_zoom_dictionary = update.Get();
59 if (level == host_zoom_map->GetDefaultZoomLevel()) {
60 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, NULL);
62 host_zoom_dictionary->SetWithoutPathExpansion(
63 change.host, Value::CreateDoubleValue(level));
67 scoped_ptr<Profile> off_the_record_profile_;
68 scoped_ptr<SSLConfigServiceManager> ssl_config_service_manager_;
70 scoped_ptr<HostZoomMap::Subscription> zoom_subscription_;
72 DISALLOW_COPY_AND_ASSIGN(TestingProfileWithHostZoomMap);
77 // We need to have a BrowserProcess in g_browser_process variable, since
78 // OffTheRecordProfileImpl ctor uses it in
79 // ProfileIOData::InitializeProfileParams.
80 class OffTheRecordProfileImplTest : public BrowserWithTestWindowTest {
82 OffTheRecordProfileImplTest() {}
84 virtual ~OffTheRecordProfileImplTest() {}
86 virtual void SetUp() OVERRIDE {
87 prefs_.reset(new TestingPrefServiceSimple());
88 chrome::RegisterLocalState(prefs_->registry());
90 browser_process()->SetLocalState(prefs_.get());
91 testing_io_thread_state_.reset(new chrome::TestingIOThreadState());
92 testing_io_thread_state_->io_thread_state()->globals()->host_resolver.reset(
93 new net::MockHostResolver());
94 BrowserWithTestWindowTest::SetUp();
97 virtual void TearDown() OVERRIDE {
98 BrowserWithTestWindowTest::TearDown();
99 testing_io_thread_state_.reset();
100 browser_process()->SetLocalState(NULL);
101 DestroyBrowserAndProfile();
105 TestingBrowserProcess* browser_process() {
106 return TestingBrowserProcess::GetGlobal();
109 scoped_ptr<TestingPrefServiceSimple> prefs_;
110 scoped_ptr<chrome::TestingIOThreadState> testing_io_thread_state_;
112 DISALLOW_COPY_AND_ASSIGN(OffTheRecordProfileImplTest);
116 // 1. Host zoom maps of parent profile and child profile are different.
117 // 2. Child host zoom map inherites zoom level at construction.
118 // 3. Change of zoom level doesn't propagate from child to parent.
119 // 4. Change of zoom level propagate from parent to child.
120 TEST_F(OffTheRecordProfileImplTest, GetHostZoomMap) {
121 // Constants for test case.
122 std::string const host("example.com");
123 double const zoom_level_25 = 2.5;
124 double const zoom_level_30 = 3.0;
125 double const zoom_level_40 = 4.0;
127 // Prepare parent profile.
128 scoped_ptr<Profile> parent_profile(new TestingProfileWithHostZoomMap);
129 ASSERT_TRUE(parent_profile.get());
130 ASSERT_TRUE(parent_profile->GetPrefs());
131 ASSERT_TRUE(parent_profile->GetOffTheRecordPrefs());
133 // Prepare parent host zoom map.
134 HostZoomMap* parent_zoom_map =
135 HostZoomMap::GetForBrowserContext(parent_profile.get());
136 ASSERT_TRUE(parent_zoom_map);
138 parent_zoom_map->SetZoomLevelForHost(host, zoom_level_25);
139 ASSERT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
142 // TODO(yosin) We need to wait ProfileImpl::Observe done for
143 // OnZoomLevelChanged.
145 // Prepare child profile as off the record profile.
146 scoped_ptr<OffTheRecordProfileImpl> child_profile(
147 new OffTheRecordProfileImpl(parent_profile.get()));
148 child_profile->InitHostZoomMap();
150 BrowserContextDependencyManager::GetInstance()->
151 CreateBrowserContextServicesForTest(child_profile.get(), false);
153 // Prepare child host zoom map.
154 HostZoomMap* child_zoom_map =
155 HostZoomMap::GetForBrowserContext(child_profile.get());
156 ASSERT_TRUE(child_zoom_map);
159 EXPECT_NE(parent_zoom_map, child_zoom_map);
161 EXPECT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
162 child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
163 "Child must inherit from parent.";
165 child_zoom_map->SetZoomLevelForHost(host, zoom_level_30);
167 child_zoom_map->GetZoomLevelForHostAndScheme("http", host),
170 EXPECT_NE(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
171 child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
172 "Child change must not propagate to parent.";
174 parent_zoom_map->SetZoomLevelForHost(host, zoom_level_40);
176 parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
179 EXPECT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
180 child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
181 "Parent change should propagate to child.";
182 base::RunLoop().RunUntilIdle();