| 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 "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
| 6 #include "base/prefs/pref_service.h" | 6 #include "base/prefs/pref_service.h" |
| 7 #include "chrome/browser/ui/browser.h" | 7 #include "chrome/browser/ui/browser.h" |
| 8 #include "chrome/browser/ui/browser_finder.h" | 8 #include "chrome/browser/ui/browser_finder.h" |
| 9 #include "chrome/browser/ui/zoom/zoom_controller.h" | 9 #include "chrome/browser/ui/zoom/zoom_controller.h" |
| 10 #include "chrome/browser/ui/zoom/zoom_observer.h" | 10 #include "chrome/browser/ui/zoom/zoom_observer.h" |
| 11 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/browser/host_zoom_map.h" | 14 #include "content/public/browser/host_zoom_map.h" |
| 15 #include "content/public/browser/navigation_details.h" | 15 #include "content/public/browser/navigation_details.h" |
| 16 #include "content/public/common/frame_navigate_params.h" | 16 #include "content/public/common/frame_navigate_params.h" |
| 17 #include "content/public/test/test_utils.h" | 17 #include "content/public/test/test_utils.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 bool operator==(const ZoomController::ZoomChangedEventData& lhs, |
| 22 const ZoomController::ZoomChangedEventData& rhs) { |
| 23 return lhs.web_contents == rhs.web_contents && |
| 24 lhs.old_zoom_level == rhs.old_zoom_level && |
| 25 lhs.new_zoom_level == rhs.new_zoom_level && |
| 26 lhs.zoom_mode == rhs.zoom_mode && |
| 27 lhs.can_show_bubble == rhs.can_show_bubble; |
| 28 } |
| 29 |
| 21 class TestZoomObserver : public ZoomObserver { | 30 class TestZoomObserver : public ZoomObserver { |
| 22 public: | 31 public: |
| 23 MOCK_METHOD2(OnZoomChanged, void(content::WebContents*, bool)); | 32 MOCK_METHOD1(OnZoomChanged, |
| 33 void(const ZoomController::ZoomChangedEventData&)); |
| 24 }; | 34 }; |
| 25 | 35 |
| 26 class ZoomControllerTest : public ChromeRenderViewHostTestHarness { | 36 class ZoomControllerTest : public ChromeRenderViewHostTestHarness { |
| 27 public: | 37 public: |
| 28 virtual void SetUp() OVERRIDE { | 38 virtual void SetUp() OVERRIDE { |
| 29 ChromeRenderViewHostTestHarness::SetUp(); | 39 ChromeRenderViewHostTestHarness::SetUp(); |
| 30 zoom_controller_.reset(new ZoomController(web_contents())); | 40 zoom_controller_.reset(new ZoomController(web_contents())); |
| 31 zoom_controller_->set_observer(&zoom_observer_); | 41 zoom_controller_->AddObserver(&zoom_observer_); |
| 32 } | 42 } |
| 33 | 43 |
| 34 virtual void TearDown() OVERRIDE { | 44 virtual void TearDown() OVERRIDE { |
| 45 zoom_controller_->RemoveObserver(&zoom_observer_); |
| 35 zoom_controller_.reset(); | 46 zoom_controller_.reset(); |
| 36 ChromeRenderViewHostTestHarness::TearDown(); | 47 ChromeRenderViewHostTestHarness::TearDown(); |
| 37 } | 48 } |
| 38 | 49 |
| 39 protected: | 50 protected: |
| 40 scoped_ptr<ZoomController> zoom_controller_; | 51 scoped_ptr<ZoomController> zoom_controller_; |
| 41 TestZoomObserver zoom_observer_; | 52 TestZoomObserver zoom_observer_; |
| 42 }; | 53 }; |
| 43 | 54 |
| 44 TEST_F(ZoomControllerTest, DidNavigateMainFrame) { | 55 TEST_F(ZoomControllerTest, DidNavigateMainFrame) { |
| 45 EXPECT_CALL(zoom_observer_, OnZoomChanged(web_contents(), false)).Times(1); | 56 double zoom_level = zoom_controller_->GetZoomLevel(); |
| 57 ZoomController::ZoomChangedEventData zoom_change_data( |
| 58 web_contents(), |
| 59 zoom_level, |
| 60 zoom_level, |
| 61 ZoomController::ZOOM_MODE_DEFAULT, |
| 62 false); |
| 63 EXPECT_CALL(zoom_observer_, OnZoomChanged(zoom_change_data)).Times(1); |
| 46 zoom_controller_->DidNavigateMainFrame(content::LoadCommittedDetails(), | 64 zoom_controller_->DidNavigateMainFrame(content::LoadCommittedDetails(), |
| 47 content::FrameNavigateParams()); | 65 content::FrameNavigateParams()); |
| 48 } | 66 } |
| 49 | 67 |
| 50 TEST_F(ZoomControllerTest, OnPreferenceChanged) { | 68 TEST_F(ZoomControllerTest, OnPreferenceChanged) { |
| 51 EXPECT_CALL(zoom_observer_, OnZoomChanged(web_contents(), false)).Times(1); | 69 double zoom_level = zoom_controller_->GetZoomLevel(); |
| 52 profile()->GetPrefs()->SetDouble(prefs::kDefaultZoomLevel, 110.0f); | 70 // Note that while the change in the default zoom level triggers an event, |
| 71 // the current zoom level for this web contents does not change since the |
| 72 // default zoom level in HostZoomMap is not updated. |
| 73 // TODO(wjmaclean) Make sure changes to the default zoom level in preferences |
| 74 // propagate to HostZoomMap. http://crbug.com/391484 |
| 75 ZoomController::ZoomChangedEventData zoom_change_data( |
| 76 web_contents(), |
| 77 zoom_level, |
| 78 zoom_level, |
| 79 ZoomController::ZOOM_MODE_DEFAULT, |
| 80 false); |
| 81 EXPECT_CALL(zoom_observer_, OnZoomChanged(zoom_change_data)).Times(1); |
| 82 profile()->GetPrefs()->SetDouble(prefs::kDefaultZoomLevel, 110.0); |
| 53 } | 83 } |
| 54 | 84 |
| 55 TEST_F(ZoomControllerTest, Observe) { | 85 TEST_F(ZoomControllerTest, Observe) { |
| 56 EXPECT_CALL(zoom_observer_, OnZoomChanged(web_contents(), false)).Times(1); | 86 double new_zoom_level = 110.0; |
| 87 // When the event is initiated from HostZoomMap, the old zoom level is not |
| 88 // available. |
| 89 ZoomController::ZoomChangedEventData zoom_change_data( |
| 90 web_contents(), |
| 91 new_zoom_level, |
| 92 new_zoom_level, |
| 93 ZoomController::ZOOM_MODE_DEFAULT, |
| 94 false); |
| 95 EXPECT_CALL(zoom_observer_, OnZoomChanged(zoom_change_data)).Times(1); |
| 57 | 96 |
| 58 content::HostZoomMap* host_zoom_map = | 97 content::HostZoomMap* host_zoom_map = |
| 59 content::HostZoomMap::GetForBrowserContext( | 98 content::HostZoomMap::GetForBrowserContext( |
| 60 web_contents()->GetBrowserContext()); | 99 web_contents()->GetBrowserContext()); |
| 61 | 100 |
| 62 host_zoom_map->SetZoomLevelForHost(std::string(), 110.0f); | 101 host_zoom_map->SetZoomLevelForHost(std::string(), new_zoom_level); |
| 63 } | 102 } |
| 103 |
| 104 TEST_F(ZoomControllerTest, Observe_ZoomController) { |
| 105 double old_zoom_level = zoom_controller_->GetZoomLevel(); |
| 106 double new_zoom_level = 110.0; |
| 107 |
| 108 ZoomController::ZoomChangedEventData zoom_change_data1( |
| 109 web_contents(), |
| 110 old_zoom_level, |
| 111 old_zoom_level, |
| 112 ZoomController::ZOOM_MODE_ISOLATED, |
| 113 true /* can_show_bubble */); |
| 114 EXPECT_CALL(zoom_observer_, OnZoomChanged(zoom_change_data1)).Times(1); |
| 115 |
| 116 zoom_controller_->SetZoomMode(ZoomController::ZOOM_MODE_ISOLATED); |
| 117 |
| 118 ZoomController::ZoomChangedEventData zoom_change_data2( |
| 119 web_contents(), |
| 120 old_zoom_level, |
| 121 new_zoom_level, |
| 122 ZoomController::ZOOM_MODE_ISOLATED, |
| 123 true /* can_show_bubble */); |
| 124 EXPECT_CALL(zoom_observer_, OnZoomChanged(zoom_change_data2)).Times(1); |
| 125 |
| 126 zoom_controller_->SetZoomLevel(new_zoom_level); |
| 127 } |
| OLD | NEW |