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

Side by Side Diff: chrome/browser/ui/zoom/zoom_controller_unittest.cc

Issue 2630583002: Add setting to isolate zoom changes by default. (Closed)
Patch Set: Add more tests. Created 3 years, 11 months 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/message_loop/message_loop.h" 5 #include "base/message_loop/message_loop.h"
6 #include "chrome/browser/ui/browser.h" 6 #include "chrome/browser/ui/browser.h"
7 #include "chrome/browser/ui/browser_finder.h" 7 #include "chrome/browser/ui/browser_finder.h"
8 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h"
8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 9 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
9 #include "chrome/test/base/testing_profile.h" 10 #include "chrome/test/base/testing_profile.h"
10 #include "components/prefs/pref_service.h" 11 #include "components/prefs/pref_service.h"
11 #include "components/zoom/test/zoom_test_utils.h" 12 #include "components/zoom/test/zoom_test_utils.h"
12 #include "components/zoom/zoom_controller.h" 13 #include "components/zoom/zoom_controller.h"
13 #include "components/zoom/zoom_observer.h" 14 #include "components/zoom/zoom_observer.h"
14 #include "content/public/browser/host_zoom_map.h" 15 #include "content/public/browser/host_zoom_map.h"
15 #include "content/public/browser/navigation_details.h" 16 #include "content/public/browser/navigation_details.h"
17 #include "content/public/browser/storage_partition.h"
16 #include "content/public/common/frame_navigate_params.h" 18 #include "content/public/common/frame_navigate_params.h"
19 #include "content/public/test/mock_render_process_host.h"
17 #include "content/public/test/test_renderer_host.h" 20 #include "content/public/test/test_renderer_host.h"
18 #include "content/public/test/test_utils.h" 21 #include "content/public/test/test_utils.h"
19 #include "ipc/ipc_message.h" 22 #include "ipc/ipc_message.h"
20 #include "testing/gmock/include/gmock/gmock.h" 23 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
22 25
23 using zoom::ZoomChangedWatcher; 26 using zoom::ZoomChangedWatcher;
24 using zoom::ZoomController; 27 using zoom::ZoomController;
25 28
26 class ZoomControllerTest : public ChromeRenderViewHostTestHarness { 29 class ZoomControllerTest : public ChromeRenderViewHostTestHarness {
27 public: 30 public:
28 void SetUp() override { 31 void SetUp() override {
29 ChromeRenderViewHostTestHarness::SetUp(); 32 ChromeRenderViewHostTestHarness::SetUp();
33
34 // We set the correct zoom scope pref before creating the ZoomController,
35 // so we can test without relying on the updating of the ZoomController's
36 // state in response to a scope change. We have specific tests for that
37 // logic (see OnDefaultZoomScopeChanged).
38 ChromeZoomLevelPrefs* zoom_prefs = static_cast<ChromeZoomLevelPrefs*>(
39 content::BrowserContext::GetDefaultStoragePartition(
40 web_contents()->GetBrowserContext())->GetZoomLevelDelegate());
41 ASSERT_TRUE(zoom_prefs);
42 SetUpPrefs(zoom_prefs);
43
30 zoom_controller_.reset(new ZoomController(web_contents())); 44 zoom_controller_.reset(new ZoomController(web_contents()));
31 45
32 // This call is needed so that the RenderViewHost reports being alive. This 46 // This call is needed so that the RenderViewHost reports being alive. This
33 // is only important for tests that call ZoomController::SetZoomLevel(). 47 // is only important for tests that call ZoomController::SetZoomLevel().
34 content::RenderViewHostTester::For(rvh())->CreateTestRenderView( 48 content::RenderViewHostTester::For(rvh())->CreateTestRenderView(
35 base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, false); 49 base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, false);
36 } 50 }
37 51
52 virtual void SetUpPrefs(ChromeZoomLevelPrefs* zoom_prefs) {
53 // These tests assume we are per-origin by default.
54 ASSERT_TRUE(zoom_prefs->GetIsOriginScopePref());
55 }
56
38 void TearDown() override { 57 void TearDown() override {
39 zoom_controller_.reset(); 58 zoom_controller_.reset();
40 ChromeRenderViewHostTestHarness::TearDown(); 59 ChromeRenderViewHostTestHarness::TearDown();
41 } 60 }
42 61
43 protected: 62 protected:
44 std::unique_ptr<ZoomController> zoom_controller_; 63 std::unique_ptr<ZoomController> zoom_controller_;
45 }; 64 };
46 65
47 TEST_F(ZoomControllerTest, DidNavigateMainFrame) { 66 TEST_F(ZoomControllerTest, DidNavigateMainFrame) {
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 ZoomController::ZOOM_MODE_MANUAL, 143 ZoomController::ZOOM_MODE_MANUAL,
125 false /* can_show_bubble */); 144 false /* can_show_bubble */);
126 { 145 {
127 ZoomChangedWatcher zoom_change_watcher2(zoom_controller_.get(), 146 ZoomChangedWatcher zoom_change_watcher2(zoom_controller_.get(),
128 zoom_change_data2); 147 zoom_change_data2);
129 zoom_controller_->SetZoomLevel(new_zoom_level2); 148 zoom_controller_->SetZoomLevel(new_zoom_level2);
130 zoom_change_watcher2.Wait(); 149 zoom_change_watcher2.Wait();
131 } 150 }
132 151
133 } 152 }
153
154 TEST_F(ZoomControllerTest, OnDefaultZoomScopeChanged) {
155 NavigateAndCommit(GURL("about:blank"));
156 double old_zoom_level = zoom_controller_->GetZoomLevel();
157 double new_zoom_level = old_zoom_level + 1.0;
158
159 ZoomController::ZoomChangedEventData zoom_change_data1(
160 web_contents(),
161 old_zoom_level,
162 new_zoom_level,
163 ZoomController::ZOOM_MODE_DEFAULT,
164 true /* can_show_bubble */);
165 {
166 ZoomChangedWatcher zoom_change_watcher1(zoom_controller_.get(),
167 zoom_change_data1);
168 zoom_controller_->SetZoomLevel(new_zoom_level);
169 zoom_change_watcher1.Wait();
170 }
171
172 // When changing the default scope from per-origin to per-tab,
173 // update the zoom mode from ZOOM_MODE_DEFAULT to ZOOM_MODE_ISOLATED
174 // and keep the previous host zoom level as a temporary level.
175 ZoomController::ZoomChangedEventData zoom_change_data2(
176 web_contents(),
177 new_zoom_level,
178 new_zoom_level,
179 ZoomController::ZOOM_MODE_ISOLATED,
180 true /* can_show_bubble */);
181 {
182 ZoomChangedWatcher zoom_change_watcher2(zoom_controller_.get(),
183 zoom_change_data2);
184 zoom_controller_->OnDefaultZoomScopeChanged();
185 zoom_change_watcher2.Wait();
186
187 content::HostZoomMap* zoom_map =
188 content::HostZoomMap::GetForWebContents(web_contents());
189 EXPECT_TRUE(zoom_map->UsesTemporaryZoomLevel(process()->GetID(),
190 rvh()->GetRoutingID()));
191 }
192 }
193
194 class ZoomControllerPerTabTest : public ZoomControllerTest {
195 public:
196 void SetUpPrefs(ChromeZoomLevelPrefs* zoom_prefs) override {
197 // These tests assume we are per-tab by default.
198 zoom_prefs->SetIsOriginScopePref(false);
199 }
200 };
201
202 TEST_F(ZoomControllerPerTabTest, InitializedState) {
203 EXPECT_EQ(ZoomController::ZOOM_MODE_ISOLATED, zoom_controller_->zoom_mode());
204
205 // Since we're in ZOOM_MODE_ISOLATED, we need a temporary zoom level.
206 content::HostZoomMap* zoom_map =
207 content::HostZoomMap::GetForWebContents(web_contents());
208 EXPECT_TRUE(zoom_map->UsesTemporaryZoomLevel(process()->GetID(),
209 rvh()->GetRoutingID()));
210 }
211
212 TEST_F(ZoomControllerPerTabTest, DidNavigateMainFrame) {
213 double zoom_level = zoom_controller_->GetZoomLevel();
214 ZoomController::ZoomChangedEventData zoom_change_data(
215 web_contents(),
216 zoom_level,
217 zoom_level,
218 ZoomController::ZOOM_MODE_ISOLATED,
219 false);
220 ZoomChangedWatcher zoom_change_watcher(zoom_controller_.get(),
221 zoom_change_data);
222 zoom_controller_->DidNavigateMainFrame(content::LoadCommittedDetails(),
223 content::FrameNavigateParams());
224 zoom_change_watcher.Wait();
225 }
226
227 TEST_F(ZoomControllerPerTabTest, Observe_ZoomController) {
228 NavigateAndCommit(GURL("about:blank"));
229
230 double old_zoom_level = zoom_controller_->GetZoomLevel();
231 double new_zoom_level = old_zoom_level + 1.0;
232
233 ZoomController::ZoomChangedEventData zoom_change_data(
234 web_contents(),
235 old_zoom_level,
236 new_zoom_level,
237 ZoomController::ZOOM_MODE_ISOLATED,
238 true /* can_show_bubble */);
239
240 ZoomChangedWatcher zoom_change_watcher(zoom_controller_.get(),
241 zoom_change_data);
242 zoom_controller_->SetZoomLevel(new_zoom_level);
243 zoom_change_watcher.Wait();
244 }
245
246 TEST_F(ZoomControllerPerTabTest, NavigationFromIsolated) {
247 double old_zoom_level = zoom_controller_->GetZoomLevel();
248 double new_zoom_level = old_zoom_level + 1.0;
249
250 zoom_controller_->SetZoomLevel(new_zoom_level);
251
252 // Zoom level and mode are preserved across navigation.
253 ZoomController::ZoomChangedEventData zoom_change_data(
254 web_contents(),
255 new_zoom_level,
256 new_zoom_level,
257 ZoomController::ZOOM_MODE_ISOLATED,
258 false /* can_show_bubble */);
259 ZoomChangedWatcher zoom_change_watcher(zoom_controller_.get(),
260 zoom_change_data);
261 zoom_controller_->DidNavigateMainFrame(content::LoadCommittedDetails(),
262 content::FrameNavigateParams());
263 zoom_change_watcher.Wait();
264 }
265
266 TEST_F(ZoomControllerPerTabTest, ResetFromManualOnNavigation) {
267 NavigateAndCommit(GURL("about:blank"));
268
269 double old_zoom_level = zoom_controller_->GetZoomLevel();
270 double new_zoom_level = old_zoom_level + 1.0;
271
272 zoom_controller_->SetZoomMode(zoom::ZoomController::ZOOM_MODE_MANUAL);
273 ZoomController::ZoomChangedEventData zoom_change_data1(
274 web_contents(),
275 old_zoom_level,
276 new_zoom_level,
277 ZoomController::ZOOM_MODE_MANUAL,
278 true /* can_show_bubble */);
279 {
280 ZoomChangedWatcher zoom_change_watcher1(zoom_controller_.get(),
281 zoom_change_data1);
282 zoom_controller_->SetZoomLevel(new_zoom_level);
283 zoom_change_watcher1.Wait();
284 }
285
286 // Zoom level and mode are reset.
287 ZoomController::ZoomChangedEventData zoom_change_data2(
288 web_contents(),
289 old_zoom_level,
290 old_zoom_level,
291 ZoomController::ZOOM_MODE_ISOLATED,
292 false /* can_show_bubble */);
293 {
294 ZoomChangedWatcher zoom_change_watcher2(zoom_controller_.get(),
295 zoom_change_data2);
296 zoom_controller_->DidNavigateMainFrame(content::LoadCommittedDetails(),
297 content::FrameNavigateParams());
298 zoom_change_watcher2.Wait();
299
300 // Since we're in ZOOM_MODE_ISOLATED, we need a temporary zoom level.
301 // The reset should update the temporary zoom level, not clear it.
302 content::HostZoomMap* zoom_map =
303 content::HostZoomMap::GetForWebContents(web_contents());
304 EXPECT_TRUE(zoom_map->UsesTemporaryZoomLevel(process()->GetID(),
305 rvh()->GetRoutingID()));
306 }
307 }
308
309 TEST_F(ZoomControllerPerTabTest, OnDefaultZoomScopeChanged) {
310 NavigateAndCommit(GURL("about:blank"));
311 double zoom_level = zoom_controller_->GetZoomLevel();
312
313 // When changing the default scope from per-tab to per-origin,
314 // update the zoom mode from ZOOM_MODE_ISOLATED to ZOOM_MODE_DEFAULT
315 // and remove the temporary level.
316 // TODO See comment in ZoomController::UpdateZoomModeOnScopeChangeIfNeeded
317 // about the resulting per-host zoom level.
318 ZoomController::ZoomChangedEventData zoom_change_data(
319 web_contents(),
320 zoom_level,
321 zoom_level,
322 ZoomController::ZOOM_MODE_DEFAULT,
323 true /* can_show_bubble */);
324 {
325 ZoomChangedWatcher zoom_change_watcher(zoom_controller_.get(),
326 zoom_change_data);
327 zoom_controller_->OnDefaultZoomScopeChanged();
328 zoom_change_watcher.Wait();
329
330 content::HostZoomMap* zoom_map =
331 content::HostZoomMap::GetForWebContents(web_contents());
332 EXPECT_FALSE(zoom_map->UsesTemporaryZoomLevel(process()->GetID(),
333 rvh()->GetRoutingID()));
334 }
335 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698