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 "chrome/test/base/testing_profile_manager.h"
20 #include "components/keyed_service/content/browser_context_dependency_manager.h"
21 #include "content/public/browser/host_zoom_map.h"
22 #include "net/dns/mock_host_resolver.h"
24 using content::HostZoomMap;
28 class TestingProfileWithHostZoomMap : public TestingProfile {
30 TestingProfileWithHostZoomMap() {
32 HostZoomMap::GetForBrowserContext(this)->AddZoomLevelChangedCallback(
33 base::Bind(&TestingProfileWithHostZoomMap::OnZoomLevelChanged,
34 base::Unretained(this)));
37 virtual ~TestingProfileWithHostZoomMap() {}
39 virtual Profile* GetOffTheRecordProfile() OVERRIDE {
40 if (!off_the_record_profile_)
41 off_the_record_profile_.reset(CreateOffTheRecordProfile());
42 return off_the_record_profile_.get();
45 virtual PrefService* GetOffTheRecordPrefs() OVERRIDE {
50 void OnZoomLevelChanged(const HostZoomMap::ZoomLevelChange& change) {
52 if (change.mode != HostZoomMap::ZOOM_CHANGED_FOR_HOST)
55 HostZoomMap* host_zoom_map = HostZoomMap::GetForBrowserContext(this);
57 double level = change.zoom_level;
58 DictionaryPrefUpdate update(prefs_.get(), prefs::kPerHostZoomLevels);
59 base::DictionaryValue* host_zoom_dictionary = update.Get();
60 if (level == host_zoom_map->GetDefaultZoomLevel()) {
61 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, NULL);
63 host_zoom_dictionary->SetWithoutPathExpansion(
64 change.host, base::Value::CreateDoubleValue(level));
68 scoped_ptr<Profile> off_the_record_profile_;
69 scoped_ptr<SSLConfigServiceManager> ssl_config_service_manager_;
71 scoped_ptr<HostZoomMap::Subscription> zoom_subscription_;
73 DISALLOW_COPY_AND_ASSIGN(TestingProfileWithHostZoomMap);
78 // We need to have a BrowserProcess in g_browser_process variable, since
79 // OffTheRecordProfileImpl ctor uses it in
80 // ProfileIOData::InitializeProfileParams.
81 class OffTheRecordProfileImplTest : public BrowserWithTestWindowTest {
83 OffTheRecordProfileImplTest() {}
85 virtual ~OffTheRecordProfileImplTest() {}
87 virtual void SetUp() OVERRIDE {
88 profile_manager_.reset(new TestingProfileManager(browser_process()));
89 ASSERT_TRUE(profile_manager_->SetUp());
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());
95 BrowserWithTestWindowTest::SetUp();
98 virtual void TearDown() OVERRIDE {
99 DestroyBrowserAndProfile();
100 BrowserWithTestWindowTest::TearDown();
102 testing_io_thread_state_.reset();
104 profile_manager_.reset();
108 TestingBrowserProcess* browser_process() {
109 return TestingBrowserProcess::GetGlobal();
112 scoped_ptr<TestingProfileManager> profile_manager_;
113 scoped_ptr<chrome::TestingIOThreadState> testing_io_thread_state_;
115 DISALLOW_COPY_AND_ASSIGN(OffTheRecordProfileImplTest);
119 // 1. Host zoom maps of parent profile and child profile are different.
120 // 2. Child host zoom map inherites zoom level at construction.
121 // 3. Change of zoom level doesn't propagate from child to parent.
122 // 4. Change of zoom level propagate from parent to child.
123 TEST_F(OffTheRecordProfileImplTest, GetHostZoomMap) {
124 // Constants for test case.
125 std::string const host("example.com");
126 double const zoom_level_25 = 2.5;
127 double const zoom_level_30 = 3.0;
128 double const zoom_level_40 = 4.0;
130 // Prepare parent profile.
131 scoped_ptr<Profile> parent_profile(new TestingProfileWithHostZoomMap);
132 ASSERT_TRUE(parent_profile.get());
133 ASSERT_TRUE(parent_profile->GetPrefs());
134 ASSERT_TRUE(parent_profile->GetOffTheRecordPrefs());
136 // Prepare parent host zoom map.
137 HostZoomMap* parent_zoom_map =
138 HostZoomMap::GetForBrowserContext(parent_profile.get());
139 ASSERT_TRUE(parent_zoom_map);
141 parent_zoom_map->SetZoomLevelForHost(host, zoom_level_25);
142 ASSERT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
145 // TODO(yosin) We need to wait ProfileImpl::Observe done for
146 // OnZoomLevelChanged.
148 // Prepare child profile as off the record profile.
149 scoped_ptr<OffTheRecordProfileImpl> child_profile(
150 new OffTheRecordProfileImpl(parent_profile.get()));
151 child_profile->InitIoData();
152 child_profile->InitHostZoomMap();
154 BrowserContextDependencyManager::GetInstance()->
155 CreateBrowserContextServicesForTest(child_profile.get());
157 // Prepare child host zoom map.
158 HostZoomMap* child_zoom_map =
159 HostZoomMap::GetForBrowserContext(child_profile.get());
160 ASSERT_TRUE(child_zoom_map);
163 EXPECT_NE(parent_zoom_map, child_zoom_map);
165 EXPECT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
166 child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
167 "Child must inherit from parent.";
169 child_zoom_map->SetZoomLevelForHost(host, zoom_level_30);
171 child_zoom_map->GetZoomLevelForHostAndScheme("http", host),
174 EXPECT_NE(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
175 child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
176 "Child change must not propagate to parent.";
178 parent_zoom_map->SetZoomLevelForHost(host, zoom_level_40);
180 parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
183 EXPECT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
184 child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
185 "Parent change should propagate to child.";
186 base::RunLoop().RunUntilIdle();