OLD | NEW |
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 "components/zoom/zoom_controller.h" |
5 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
6 #include "chrome/browser/ui/browser.h" | 7 #include "chrome/browser/ui/browser.h" |
7 #include "chrome/browser/ui/browser_finder.h" | 8 #include "chrome/browser/ui/browser_finder.h" |
| 9 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" |
8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 10 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
9 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
10 #include "components/prefs/pref_service.h" | 12 #include "components/prefs/pref_service.h" |
11 #include "components/zoom/test/zoom_test_utils.h" | 13 #include "components/zoom/test/zoom_test_utils.h" |
12 #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()) |
| 40 ->GetZoomLevelDelegate()); |
| 41 ASSERT_TRUE(zoom_prefs); |
| 42 SetUpPrefs(zoom_prefs); |
| 43 |
29 zoom_controller_.reset(new ZoomController(web_contents())); | 44 zoom_controller_.reset(new ZoomController(web_contents())); |
30 | 45 |
31 // 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 |
32 // is only important for tests that call ZoomController::SetZoomLevel(). | 47 // is only important for tests that call ZoomController::SetZoomLevel(). |
33 content::RenderViewHostTester::For(rvh())->CreateTestRenderView( | 48 content::RenderViewHostTester::For(rvh())->CreateTestRenderView( |
34 base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, false); | 49 base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, false); |
35 } | 50 } |
36 | 51 |
| 52 virtual void SetUpPrefs(ChromeZoomLevelPrefs* zoom_prefs) { |
| 53 // These tests assume we are per-origin by default. |
| 54 ASSERT_TRUE(zoom_prefs->GetZoomScopeIsPerOriginPref()); |
| 55 } |
| 56 |
37 void TearDown() override { | 57 void TearDown() override { |
38 zoom_controller_.reset(); | 58 zoom_controller_.reset(); |
39 ChromeRenderViewHostTestHarness::TearDown(); | 59 ChromeRenderViewHostTestHarness::TearDown(); |
40 } | 60 } |
41 | 61 |
42 protected: | 62 protected: |
43 std::unique_ptr<ZoomController> zoom_controller_; | 63 std::unique_ptr<ZoomController> zoom_controller_; |
44 }; | 64 }; |
45 | 65 |
46 TEST_F(ZoomControllerTest, DidNavigateMainFrame) { | 66 TEST_F(ZoomControllerTest, DidNavigateMainFrame) { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 ZoomController::ZOOM_MODE_MANUAL, | 145 ZoomController::ZOOM_MODE_MANUAL, |
126 false /* can_show_bubble */); | 146 false /* can_show_bubble */); |
127 { | 147 { |
128 ZoomChangedWatcher zoom_change_watcher2(zoom_controller_.get(), | 148 ZoomChangedWatcher zoom_change_watcher2(zoom_controller_.get(), |
129 zoom_change_data2); | 149 zoom_change_data2); |
130 zoom_controller_->SetZoomLevel(new_zoom_level2); | 150 zoom_controller_->SetZoomLevel(new_zoom_level2); |
131 zoom_change_watcher2.Wait(); | 151 zoom_change_watcher2.Wait(); |
132 } | 152 } |
133 | 153 |
134 } | 154 } |
| 155 |
| 156 TEST_F(ZoomControllerTest, OnDefaultZoomScopeChanged) { |
| 157 NavigateAndCommit(GURL("about:blank")); |
| 158 double old_zoom_level = zoom_controller_->GetZoomLevel(); |
| 159 double new_zoom_level = old_zoom_level + 1.0; |
| 160 |
| 161 ZoomController::ZoomChangedEventData zoom_change_data1( |
| 162 web_contents(), old_zoom_level, new_zoom_level, |
| 163 ZoomController::ZOOM_MODE_DEFAULT, true /* can_show_bubble */); |
| 164 { |
| 165 ZoomChangedWatcher zoom_change_watcher1(zoom_controller_.get(), |
| 166 zoom_change_data1); |
| 167 zoom_controller_->SetZoomLevel(new_zoom_level); |
| 168 zoom_change_watcher1.Wait(); |
| 169 } |
| 170 |
| 171 // When changing the default scope from per-origin to per-tab, |
| 172 // update the zoom mode from ZOOM_MODE_DEFAULT to ZOOM_MODE_ISOLATED |
| 173 // and keep the previous host zoom level as a temporary level. |
| 174 ZoomController::ZoomChangedEventData zoom_change_data2( |
| 175 web_contents(), new_zoom_level, new_zoom_level, |
| 176 ZoomController::ZOOM_MODE_ISOLATED, true /* can_show_bubble */); |
| 177 { |
| 178 ZoomChangedWatcher zoom_change_watcher2(zoom_controller_.get(), |
| 179 zoom_change_data2); |
| 180 zoom_controller_->OnDefaultZoomScopeChanged(); |
| 181 zoom_change_watcher2.Wait(); |
| 182 |
| 183 content::HostZoomMap* zoom_map = |
| 184 content::HostZoomMap::GetForWebContents(web_contents()); |
| 185 EXPECT_TRUE(zoom_map->UsesTemporaryZoomLevel(process()->GetID(), |
| 186 rvh()->GetRoutingID())); |
| 187 } |
| 188 } |
| 189 |
| 190 class ZoomControllerPerTabTest : public ZoomControllerTest { |
| 191 public: |
| 192 void SetUpPrefs(ChromeZoomLevelPrefs* zoom_prefs) override { |
| 193 // These tests assume we are per-tab by default. |
| 194 zoom_prefs->SetZoomScopeIsPerOriginPref(false); |
| 195 } |
| 196 |
| 197 void TestResetOnNavigation(ZoomController::ZoomMode zoom_mode) { |
| 198 DCHECK(zoom_mode == ZoomController::ZOOM_MODE_DEFAULT || |
| 199 zoom_mode == ZoomController::ZOOM_MODE_MANUAL); |
| 200 NavigateAndCommit(GURL("about:blank")); |
| 201 |
| 202 double old_zoom_level = zoom_controller_->GetZoomLevel(); |
| 203 double new_zoom_level = old_zoom_level + 1.0; |
| 204 |
| 205 zoom_controller_->SetZoomMode(zoom_mode); |
| 206 ZoomController::ZoomChangedEventData zoom_change_data1( |
| 207 web_contents(), old_zoom_level, new_zoom_level, zoom_mode, |
| 208 true /* can_show_bubble */); |
| 209 { |
| 210 ZoomChangedWatcher zoom_change_watcher1(zoom_controller_.get(), |
| 211 zoom_change_data1); |
| 212 zoom_controller_->SetZoomLevel(new_zoom_level); |
| 213 zoom_change_watcher1.Wait(); |
| 214 } |
| 215 |
| 216 // Zoom level and mode are reset. |
| 217 ZoomController::ZoomChangedEventData zoom_change_data2( |
| 218 web_contents(), old_zoom_level, old_zoom_level, |
| 219 ZoomController::ZOOM_MODE_ISOLATED, false /* can_show_bubble */); |
| 220 { |
| 221 ZoomChangedWatcher zoom_change_watcher2(zoom_controller_.get(), |
| 222 zoom_change_data2); |
| 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_watcher2.Wait(); |
| 228 |
| 229 // Since we're in ZOOM_MODE_ISOLATED, we need a temporary zoom level. |
| 230 // The reset should update the temporary zoom level, not clear it. |
| 231 content::HostZoomMap* zoom_map = |
| 232 content::HostZoomMap::GetForWebContents(web_contents()); |
| 233 EXPECT_TRUE(zoom_map->UsesTemporaryZoomLevel(process()->GetID(), |
| 234 rvh()->GetRoutingID())); |
| 235 } |
| 236 } |
| 237 }; |
| 238 |
| 239 TEST_F(ZoomControllerPerTabTest, InitializedState) { |
| 240 EXPECT_EQ(ZoomController::ZOOM_MODE_ISOLATED, zoom_controller_->zoom_mode()); |
| 241 |
| 242 // Since we're in ZOOM_MODE_ISOLATED, we need a temporary zoom level. |
| 243 content::HostZoomMap* zoom_map = |
| 244 content::HostZoomMap::GetForWebContents(web_contents()); |
| 245 EXPECT_TRUE(zoom_map->UsesTemporaryZoomLevel(process()->GetID(), |
| 246 rvh()->GetRoutingID())); |
| 247 } |
| 248 |
| 249 TEST_F(ZoomControllerPerTabTest, DidFinishNavigation) { |
| 250 double zoom_level = zoom_controller_->GetZoomLevel(); |
| 251 ZoomController::ZoomChangedEventData zoom_change_data( |
| 252 web_contents(), zoom_level, zoom_level, |
| 253 ZoomController::ZOOM_MODE_ISOLATED, false); |
| 254 ZoomChangedWatcher zoom_change_watcher(zoom_controller_.get(), |
| 255 zoom_change_data); |
| 256 std::unique_ptr<content::NavigationHandle> navigation_handle = |
| 257 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 258 GURL(), rvh()->GetMainFrame(), true); |
| 259 zoom_controller_->DidFinishNavigation(navigation_handle.get()); |
| 260 zoom_change_watcher.Wait(); |
| 261 } |
| 262 |
| 263 TEST_F(ZoomControllerPerTabTest, Observe_ZoomController) { |
| 264 NavigateAndCommit(GURL("about:blank")); |
| 265 |
| 266 double old_zoom_level = zoom_controller_->GetZoomLevel(); |
| 267 double new_zoom_level = old_zoom_level + 1.0; |
| 268 |
| 269 ZoomController::ZoomChangedEventData zoom_change_data( |
| 270 web_contents(), old_zoom_level, new_zoom_level, |
| 271 ZoomController::ZOOM_MODE_ISOLATED, true /* can_show_bubble */); |
| 272 |
| 273 ZoomChangedWatcher zoom_change_watcher(zoom_controller_.get(), |
| 274 zoom_change_data); |
| 275 zoom_controller_->SetZoomLevel(new_zoom_level); |
| 276 zoom_change_watcher.Wait(); |
| 277 } |
| 278 |
| 279 TEST_F(ZoomControllerPerTabTest, NavigationFromIsolated) { |
| 280 double old_zoom_level = zoom_controller_->GetZoomLevel(); |
| 281 double new_zoom_level = old_zoom_level + 1.0; |
| 282 |
| 283 zoom_controller_->SetZoomLevel(new_zoom_level); |
| 284 |
| 285 // Zoom level and mode are preserved across navigation. |
| 286 ZoomController::ZoomChangedEventData zoom_change_data( |
| 287 web_contents(), new_zoom_level, new_zoom_level, |
| 288 ZoomController::ZOOM_MODE_ISOLATED, false /* can_show_bubble */); |
| 289 ZoomChangedWatcher zoom_change_watcher(zoom_controller_.get(), |
| 290 zoom_change_data); |
| 291 std::unique_ptr<content::NavigationHandle> navigation_handle = |
| 292 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 293 GURL(), rvh()->GetMainFrame(), true); |
| 294 zoom_controller_->DidFinishNavigation(navigation_handle.get()); |
| 295 zoom_change_watcher.Wait(); |
| 296 } |
| 297 |
| 298 TEST_F(ZoomControllerPerTabTest, ResetFromOriginOnNavigation) { |
| 299 TestResetOnNavigation(ZoomController::ZOOM_MODE_DEFAULT); |
| 300 } |
| 301 |
| 302 TEST_F(ZoomControllerPerTabTest, ResetFromManualOnNavigation) { |
| 303 TestResetOnNavigation(ZoomController::ZOOM_MODE_MANUAL); |
| 304 } |
| 305 |
| 306 TEST_F(ZoomControllerPerTabTest, OnDefaultZoomScopeChanged) { |
| 307 NavigateAndCommit(GURL("about:blank")); |
| 308 double zoom_level = zoom_controller_->GetZoomLevel(); |
| 309 |
| 310 // When changing the default scope from per-tab to per-origin, |
| 311 // update the zoom mode from ZOOM_MODE_ISOLATED to ZOOM_MODE_DEFAULT |
| 312 // and remove the temporary level. |
| 313 ZoomController::ZoomChangedEventData zoom_change_data( |
| 314 web_contents(), zoom_level, zoom_level, ZoomController::ZOOM_MODE_DEFAULT, |
| 315 true /* can_show_bubble */); |
| 316 { |
| 317 ZoomChangedWatcher zoom_change_watcher(zoom_controller_.get(), |
| 318 zoom_change_data); |
| 319 zoom_controller_->OnDefaultZoomScopeChanged(); |
| 320 zoom_change_watcher.Wait(); |
| 321 |
| 322 content::HostZoomMap* zoom_map = |
| 323 content::HostZoomMap::GetForWebContents(web_contents()); |
| 324 EXPECT_FALSE(zoom_map->UsesTemporaryZoomLevel(process()->GetID(), |
| 325 rvh()->GetRoutingID())); |
| 326 } |
| 327 } |
OLD | NEW |