Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Side by Side Diff: chrome/browser/profiles/off_the_record_profile_impl_unittest.cc

Issue 393133002: Migrate HostZoomMap to live in StoragePartition. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments; patch for landing. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/profiles/off_the_record_profile_impl.h"
6
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/prefs/browser_prefs.h"
12 #include "chrome/browser/ui/zoom/chrome_zoom_level_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 "content/public/browser/host_zoom_map.h"
21 #include "content/public/common/page_zoom.h"
22 #include "net/dns/mock_host_resolver.h"
23
24 using content::HostZoomMap;
25
26 namespace {
27
28 class TestingProfileWithHostZoomMap : public TestingProfile {
29 public:
30 TestingProfileWithHostZoomMap() {
31 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
32 zoom_subscription_ = host_zoom_map->AddZoomLevelChangedCallback(
33 base::Bind(&TestingProfileWithHostZoomMap::OnZoomLevelChanged,
34 base::Unretained(this)));
35 zoom_level_prefs_.reset(
36 new chrome::ChromeZoomLevelPrefs(GetPrefs(), GetPath()));
37 zoom_level_prefs_->InitPrefsAndCopyToHostZoomMap(GetPath(), host_zoom_map);
38 }
39
40 ~TestingProfileWithHostZoomMap() override {}
41
42 // Profile overrides:
43 PrefService* GetOffTheRecordPrefs() override { return GetPrefs(); }
44
45 private:
46 void OnZoomLevelChanged(const HostZoomMap::ZoomLevelChange& change) {
47
48 if (change.mode != HostZoomMap::ZOOM_CHANGED_FOR_HOST)
49 return;
50
51 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
52
53 double level = change.zoom_level;
54 std::string per_host_zoom_levels(prefs::kPartitionPerHostZoomLevels);
55 per_host_zoom_levels.append(".0");
56 DictionaryPrefUpdate update(GetPrefs(),
57 prefs::kPartitionPerHostZoomLevels);
58 base::DictionaryValue* host_zoom_dictionary = update.Get();
59 if (content::ZoomValuesEqual(level, host_zoom_map->GetDefaultZoomLevel())) {
60 host_zoom_dictionary->RemoveWithoutPathExpansion(change.host, NULL);
61 } else {
62 host_zoom_dictionary->SetWithoutPathExpansion(
63 change.host, new base::FundamentalValue(level));
64 }
65 }
66
67 scoped_ptr<HostZoomMap::Subscription> zoom_subscription_;
68 scoped_ptr<chrome::ChromeZoomLevelPrefs> zoom_level_prefs_;
69
70 DISALLOW_COPY_AND_ASSIGN(TestingProfileWithHostZoomMap);
71 };
72
73 } // namespace
74
75 // We need to have a BrowserProcess in g_browser_process variable, since
76 // OffTheRecordProfileImpl ctor uses it in
77 // ProfileIOData::InitializeProfileParams.
78 class OffTheRecordProfileImplTest : public BrowserWithTestWindowTest {
79 protected:
80 OffTheRecordProfileImplTest() {}
81
82 ~OffTheRecordProfileImplTest() override {}
83
84 // testing::Test overrides:
85 void SetUp() override {
86 profile_manager_.reset(new TestingProfileManager(browser_process()));
87 ASSERT_TRUE(profile_manager_->SetUp());
88
89 testing_io_thread_state_.reset(new chrome::TestingIOThreadState());
90 testing_io_thread_state_->io_thread_state()->globals()->host_resolver.reset(
91 new net::MockHostResolver());
92
93 BrowserWithTestWindowTest::SetUp();
94 }
95
96 void TearDown() override {
97 BrowserWithTestWindowTest::TearDown();
98
99 testing_io_thread_state_.reset();
100
101 profile_manager_.reset();
102 }
103
104 // BrowserWithTestWindowTest overrides:
105 TestingProfile* CreateProfile() override {
106 return new TestingProfileWithHostZoomMap;
107 }
108
109 private:
110 TestingBrowserProcess* browser_process() {
111 return TestingBrowserProcess::GetGlobal();
112 }
113
114 scoped_ptr<TestingProfileManager> profile_manager_;
115 scoped_ptr<chrome::TestingIOThreadState> testing_io_thread_state_;
116
117 DISALLOW_COPY_AND_ASSIGN(OffTheRecordProfileImplTest);
118 };
119
120 // Test four things:
121 // 1. Host zoom maps of parent profile and child profile are different.
122 // 2. Child host zoom map inherites zoom level at construction.
123 // 3. Change of zoom level doesn't propagate from child to parent.
124 // 4. Change of zoom level propagate from parent to child.
125 TEST_F(OffTheRecordProfileImplTest, GetHostZoomMap) {
126 // Constants for test case.
127 const std::string host("example.com");
128 const double zoom_level_25 = 2.5;
129 const double zoom_level_30 = 3.0;
130 const double zoom_level_40 = 4.0;
131
132 // The TestingProfile from CreateProfile above is the parent.
133 TestingProfile* parent_profile = GetProfile();
134 ASSERT_TRUE(parent_profile);
135 ASSERT_TRUE(parent_profile->GetPrefs());
136 ASSERT_TRUE(parent_profile->GetOffTheRecordPrefs());
137
138 // Prepare parent host zoom map.
139 HostZoomMap* parent_zoom_map =
140 HostZoomMap::GetDefaultForBrowserContext(parent_profile);
141 ASSERT_TRUE(parent_zoom_map);
142
143 parent_zoom_map->SetZoomLevelForHost(host, zoom_level_25);
144 ASSERT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
145 zoom_level_25);
146
147 // TODO(yosin) We need to wait ProfileImpl::Observe done for
148 // OnZoomLevelChanged.
149
150 // Prepare an off the record profile owned by the parent profile.
151 parent_profile->SetOffTheRecordProfile(
152 scoped_ptr<Profile>(new OffTheRecordProfileImpl(parent_profile)));
153 OffTheRecordProfileImpl* child_profile =
154 static_cast<OffTheRecordProfileImpl*>(
155 parent_profile->GetOffTheRecordProfile());
156 child_profile->InitIoData();
157 child_profile->InitHostZoomMap();
158
159 // Prepare child host zoom map.
160 HostZoomMap* child_zoom_map =
161 HostZoomMap::GetDefaultForBrowserContext(child_profile);
162 ASSERT_TRUE(child_zoom_map);
163
164 // Verify.
165 EXPECT_NE(parent_zoom_map, child_zoom_map);
166
167 EXPECT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
168 child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
169 "Child must inherit from parent.";
170
171 child_zoom_map->SetZoomLevelForHost(host, zoom_level_30);
172 ASSERT_EQ(
173 child_zoom_map->GetZoomLevelForHostAndScheme("http", host),
174 zoom_level_30);
175
176 EXPECT_NE(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
177 child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
178 "Child change must not propagate to parent.";
179
180 parent_zoom_map->SetZoomLevelForHost(host, zoom_level_40);
181 ASSERT_EQ(
182 parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
183 zoom_level_40);
184
185 EXPECT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
186 child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
187 "Parent change should propagate to child.";
188 base::RunLoop().RunUntilIdle();
189 }
OLDNEW
« no previous file with comments | « chrome/browser/profiles/off_the_record_profile_impl.cc ('k') | chrome/browser/profiles/profile.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698