| 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" | |
| 10 #include "chrome/browser/ui/zoom/zoom_observer.h" | |
| 11 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 9 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 12 #include "chrome/test/base/testing_profile.h" | 10 #include "chrome/test/base/testing_profile.h" |
| 11 #include "components/ui/zoom/zoom_controller.h" |
| 12 #include "components/ui/zoom/zoom_observer.h" |
| 13 #include "content/public/browser/host_zoom_map.h" | 13 #include "content/public/browser/host_zoom_map.h" |
| 14 #include "content/public/browser/navigation_details.h" | 14 #include "content/public/browser/navigation_details.h" |
| 15 #include "content/public/common/frame_navigate_params.h" | 15 #include "content/public/common/frame_navigate_params.h" |
| 16 #include "content/public/test/test_renderer_host.h" | 16 #include "content/public/test/test_renderer_host.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 using components::ZoomController; |
| 22 |
| 21 bool operator==(const ZoomController::ZoomChangedEventData& lhs, | 23 bool operator==(const ZoomController::ZoomChangedEventData& lhs, |
| 22 const ZoomController::ZoomChangedEventData& rhs) { | 24 const ZoomController::ZoomChangedEventData& rhs) { |
| 23 return lhs.web_contents == rhs.web_contents && | 25 return lhs.web_contents == rhs.web_contents && |
| 24 lhs.old_zoom_level == rhs.old_zoom_level && | 26 lhs.old_zoom_level == rhs.old_zoom_level && |
| 25 lhs.new_zoom_level == rhs.new_zoom_level && | 27 lhs.new_zoom_level == rhs.new_zoom_level && |
| 26 lhs.zoom_mode == rhs.zoom_mode && | 28 lhs.zoom_mode == rhs.zoom_mode && |
| 27 lhs.can_show_bubble == rhs.can_show_bubble; | 29 lhs.can_show_bubble == rhs.can_show_bubble; |
| 28 } | 30 } |
| 29 | 31 |
| 30 class TestZoomObserver : public ZoomObserver { | 32 class ZoomChangedWatcher : public components::ZoomObserver { |
| 31 public: | 33 public: |
| 32 MOCK_METHOD1(OnZoomChanged, | 34 ZoomChangedWatcher( |
| 33 void(const ZoomController::ZoomChangedEventData&)); | 35 ZoomController* zoom_controller, |
| 36 const ZoomController::ZoomChangedEventData& expected_event_data) |
| 37 : zoom_controller_(zoom_controller), |
| 38 expected_event_data_(expected_event_data), |
| 39 message_loop_runner_(new content::MessageLoopRunner) { |
| 40 zoom_controller_->AddObserver(this); |
| 41 } |
| 42 ~ZoomChangedWatcher() override { zoom_controller_->RemoveObserver(this); } |
| 43 |
| 44 void Wait() { message_loop_runner_->Run(); } |
| 45 |
| 46 void OnZoomChanged( |
| 47 const ZoomController::ZoomChangedEventData& event_data) override { |
| 48 if (event_data == expected_event_data_) |
| 49 message_loop_runner_->Quit(); |
| 50 } |
| 51 |
| 52 private: |
| 53 ZoomController* zoom_controller_; |
| 54 ZoomController::ZoomChangedEventData expected_event_data_; |
| 55 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(ZoomChangedWatcher); |
| 34 }; | 58 }; |
| 35 | 59 |
| 36 class ZoomControllerTest : public ChromeRenderViewHostTestHarness { | 60 class ZoomControllerTest : public ChromeRenderViewHostTestHarness { |
| 37 public: | 61 public: |
| 38 void SetUp() override { | 62 void SetUp() override { |
| 39 ChromeRenderViewHostTestHarness::SetUp(); | 63 ChromeRenderViewHostTestHarness::SetUp(); |
| 40 zoom_controller_.reset(new ZoomController(web_contents())); | 64 zoom_controller_.reset(new ZoomController(web_contents())); |
| 41 zoom_controller_->AddObserver(&zoom_observer_); | |
| 42 | 65 |
| 43 // This call is needed so that the RenderViewHost reports being alive. This | 66 // This call is needed so that the RenderViewHost reports being alive. This |
| 44 // is only important for tests that call ZoomController::SetZoomLevel(). | 67 // is only important for tests that call ZoomController::SetZoomLevel(). |
| 45 content::RenderViewHostTester::For(rvh())->CreateRenderView( | 68 content::RenderViewHostTester::For(rvh())->CreateRenderView( |
| 46 base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, -1, false); | 69 base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, -1, false); |
| 47 } | 70 } |
| 48 | 71 |
| 49 void TearDown() override { | 72 void TearDown() override { |
| 50 zoom_controller_->RemoveObserver(&zoom_observer_); | |
| 51 zoom_controller_.reset(); | 73 zoom_controller_.reset(); |
| 52 ChromeRenderViewHostTestHarness::TearDown(); | 74 ChromeRenderViewHostTestHarness::TearDown(); |
| 53 } | 75 } |
| 54 | 76 |
| 55 protected: | 77 protected: |
| 56 scoped_ptr<ZoomController> zoom_controller_; | 78 scoped_ptr<ZoomController> zoom_controller_; |
| 57 TestZoomObserver zoom_observer_; | |
| 58 }; | 79 }; |
| 59 | 80 |
| 60 TEST_F(ZoomControllerTest, DidNavigateMainFrame) { | 81 TEST_F(ZoomControllerTest, DidNavigateMainFrame) { |
| 61 double zoom_level = zoom_controller_->GetZoomLevel(); | 82 double zoom_level = zoom_controller_->GetZoomLevel(); |
| 62 ZoomController::ZoomChangedEventData zoom_change_data( | 83 ZoomController::ZoomChangedEventData zoom_change_data( |
| 63 web_contents(), | 84 web_contents(), |
| 64 zoom_level, | 85 zoom_level, |
| 65 zoom_level, | 86 zoom_level, |
| 66 ZoomController::ZOOM_MODE_DEFAULT, | 87 ZoomController::ZOOM_MODE_DEFAULT, |
| 67 false); | 88 false); |
| 68 EXPECT_CALL(zoom_observer_, OnZoomChanged(zoom_change_data)).Times(1); | 89 ZoomChangedWatcher zoom_change_watcher(zoom_controller_.get(), |
| 90 zoom_change_data); |
| 69 zoom_controller_->DidNavigateMainFrame(content::LoadCommittedDetails(), | 91 zoom_controller_->DidNavigateMainFrame(content::LoadCommittedDetails(), |
| 70 content::FrameNavigateParams()); | 92 content::FrameNavigateParams()); |
| 93 zoom_change_watcher.Wait(); |
| 71 } | 94 } |
| 72 | 95 |
| 73 TEST_F(ZoomControllerTest, Observe_ZoomController) { | 96 TEST_F(ZoomControllerTest, Observe_ZoomController) { |
| 74 double old_zoom_level = zoom_controller_->GetZoomLevel(); | 97 double old_zoom_level = zoom_controller_->GetZoomLevel(); |
| 75 double new_zoom_level = 110.0; | 98 double new_zoom_level = 110.0; |
| 76 | 99 |
| 77 NavigateAndCommit(GURL("about:blank")); | 100 NavigateAndCommit(GURL("about:blank")); |
| 78 | 101 |
| 79 ZoomController::ZoomChangedEventData zoom_change_data1( | 102 ZoomController::ZoomChangedEventData zoom_change_data1( |
| 80 web_contents(), | 103 web_contents(), |
| 81 old_zoom_level, | 104 old_zoom_level, |
| 82 old_zoom_level, | 105 old_zoom_level, |
| 83 ZoomController::ZOOM_MODE_ISOLATED, | 106 ZoomController::ZOOM_MODE_ISOLATED, |
| 84 true /* can_show_bubble */); | 107 true /* can_show_bubble */); |
| 85 EXPECT_CALL(zoom_observer_, OnZoomChanged(zoom_change_data1)).Times(1); | |
| 86 | 108 |
| 87 zoom_controller_->SetZoomMode(ZoomController::ZOOM_MODE_ISOLATED); | 109 { |
| 110 ZoomChangedWatcher zoom_change_watcher1(zoom_controller_.get(), |
| 111 zoom_change_data1); |
| 112 zoom_controller_->SetZoomMode(ZoomController::ZOOM_MODE_ISOLATED); |
| 113 zoom_change_watcher1.Wait(); |
| 114 } |
| 88 | 115 |
| 89 ZoomController::ZoomChangedEventData zoom_change_data2( | 116 ZoomController::ZoomChangedEventData zoom_change_data2( |
| 90 web_contents(), | 117 web_contents(), |
| 91 old_zoom_level, | 118 old_zoom_level, |
| 92 new_zoom_level, | 119 new_zoom_level, |
| 93 ZoomController::ZOOM_MODE_ISOLATED, | 120 ZoomController::ZOOM_MODE_ISOLATED, |
| 94 true /* can_show_bubble */); | 121 true /* can_show_bubble */); |
| 95 EXPECT_CALL(zoom_observer_, OnZoomChanged(zoom_change_data2)).Times(1); | |
| 96 | 122 |
| 97 zoom_controller_->SetZoomLevel(new_zoom_level); | 123 { |
| 124 ZoomChangedWatcher zoom_change_watcher2(zoom_controller_.get(), |
| 125 zoom_change_data2); |
| 126 zoom_controller_->SetZoomLevel(new_zoom_level); |
| 127 zoom_change_watcher2.Wait(); |
| 128 } |
| 98 } | 129 } |
| OLD | NEW |