Home | History | Annotate | Download | only in zoom
      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
      3 // found in the LICENSE file.
      4 
      5 #include "base/message_loop/message_loop.h"
      6 #include "base/prefs/pref_service.h"
      7 #include "chrome/browser/ui/browser.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/common/pref_names.h"
     12 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
     13 #include "chrome/test/base/testing_profile.h"
     14 #include "content/public/browser/host_zoom_map.h"
     15 #include "content/public/browser/navigation_details.h"
     16 #include "content/public/common/frame_navigate_params.h"
     17 #include "content/public/test/test_renderer_host.h"
     18 #include "content/public/test/test_utils.h"
     19 #include "testing/gmock/include/gmock/gmock.h"
     20 #include "testing/gtest/include/gtest/gtest.h"
     21 
     22 bool operator==(const ZoomController::ZoomChangedEventData& lhs,
     23                 const ZoomController::ZoomChangedEventData& rhs) {
     24   return lhs.web_contents == rhs.web_contents &&
     25          lhs.old_zoom_level == rhs.old_zoom_level &&
     26          lhs.new_zoom_level == rhs.new_zoom_level &&
     27          lhs.zoom_mode == rhs.zoom_mode &&
     28          lhs.can_show_bubble == rhs.can_show_bubble;
     29 }
     30 
     31 class TestZoomObserver : public ZoomObserver {
     32  public:
     33   MOCK_METHOD1(OnZoomChanged,
     34                void(const ZoomController::ZoomChangedEventData&));
     35 };
     36 
     37 class ZoomControllerTest : public ChromeRenderViewHostTestHarness {
     38  public:
     39   virtual void SetUp() OVERRIDE {
     40     ChromeRenderViewHostTestHarness::SetUp();
     41     zoom_controller_.reset(new ZoomController(web_contents()));
     42     zoom_controller_->AddObserver(&zoom_observer_);
     43 
     44     // This call is needed so that the RenderViewHost reports being alive. This
     45     // is only important for tests that call ZoomController::SetZoomLevel().
     46     content::RenderViewHostTester::For(rvh())->CreateRenderView(
     47         base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, -1, false);
     48   }
     49 
     50   virtual void TearDown() OVERRIDE {
     51     zoom_controller_->RemoveObserver(&zoom_observer_);
     52     zoom_controller_.reset();
     53     ChromeRenderViewHostTestHarness::TearDown();
     54   }
     55 
     56  protected:
     57   scoped_ptr<ZoomController> zoom_controller_;
     58   TestZoomObserver zoom_observer_;
     59 };
     60 
     61 TEST_F(ZoomControllerTest, DidNavigateMainFrame) {
     62   double zoom_level = zoom_controller_->GetZoomLevel();
     63   ZoomController::ZoomChangedEventData zoom_change_data(
     64       web_contents(),
     65       zoom_level,
     66       zoom_level,
     67       ZoomController::ZOOM_MODE_DEFAULT,
     68       false);
     69   EXPECT_CALL(zoom_observer_, OnZoomChanged(zoom_change_data)).Times(1);
     70   zoom_controller_->DidNavigateMainFrame(content::LoadCommittedDetails(),
     71                                          content::FrameNavigateParams());
     72 }
     73 
     74 TEST_F(ZoomControllerTest, Observe) {
     75   double new_zoom_level = 110.0;
     76   // When the event is initiated from HostZoomMap, the old zoom level is not
     77   // available.
     78   ZoomController::ZoomChangedEventData zoom_change_data(
     79       web_contents(),
     80       new_zoom_level,
     81       new_zoom_level,
     82       ZoomController::ZOOM_MODE_DEFAULT,
     83       false);
     84   EXPECT_CALL(zoom_observer_, OnZoomChanged(zoom_change_data)).Times(1);
     85 
     86   content::HostZoomMap* host_zoom_map =
     87       content::HostZoomMap::GetDefaultForBrowserContext(
     88           web_contents()->GetBrowserContext());
     89 
     90   host_zoom_map->SetZoomLevelForHost(std::string(), new_zoom_level);
     91 }
     92 
     93 TEST_F(ZoomControllerTest, Observe_ZoomController) {
     94   double old_zoom_level = zoom_controller_->GetZoomLevel();
     95   double new_zoom_level = 110.0;
     96 
     97   NavigateAndCommit(GURL("about:blank"));
     98 
     99   ZoomController::ZoomChangedEventData zoom_change_data1(
    100       web_contents(),
    101       old_zoom_level,
    102       old_zoom_level,
    103       ZoomController::ZOOM_MODE_ISOLATED,
    104       true /* can_show_bubble */);
    105   EXPECT_CALL(zoom_observer_, OnZoomChanged(zoom_change_data1)).Times(1);
    106 
    107   zoom_controller_->SetZoomMode(ZoomController::ZOOM_MODE_ISOLATED);
    108 
    109   ZoomController::ZoomChangedEventData zoom_change_data2(
    110       web_contents(),
    111       old_zoom_level,
    112       new_zoom_level,
    113       ZoomController::ZOOM_MODE_ISOLATED,
    114       true /* can_show_bubble */);
    115   EXPECT_CALL(zoom_observer_, OnZoomChanged(zoom_change_data2)).Times(1);
    116 
    117   zoom_controller_->SetZoomLevel(new_zoom_level);
    118 }
    119