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

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

Powered by Google App Engine
This is Rietveld 408576698