1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/zoom/zoom_controller.h"
#include <optional>
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/pref_service.h"
#include "components/zoom/test/zoom_test_utils.h"
#include "components/zoom/zoom_observer.h"
#include "content/public/browser/host_zoom_map.h"
#include "content/public/test/mock_navigation_handle.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_utils.h"
#include "ipc/ipc_message.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using zoom::ZoomChangedWatcher;
using zoom::ZoomController;
class ZoomControllerTest : public ChromeRenderViewHostTestHarness {
public:
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
zoom_controller_.reset(new ZoomController(
web_contents(), web_contents()->GetPrimaryMainFrame()));
// This call is needed so that the RenderViewHost reports being alive. This
// is only important for tests that call ZoomController::SetZoomLevel().
content::RenderViewHostTester::For(rvh())->CreateTestRenderView();
}
void TearDown() override {
zoom_controller_.reset();
ChromeRenderViewHostTestHarness::TearDown();
}
protected:
std::unique_ptr<ZoomController> zoom_controller_;
};
TEST_F(ZoomControllerTest, DidNavigateMainFrame) {
double zoom_level = zoom_controller_->GetZoomLevel();
ZoomController::ZoomChangedEventData zoom_change_data(
web_contents(),
web_contents()->GetPrimaryMainFrame()->GetFrameTreeNodeId(), zoom_level,
zoom_level, ZoomController::ZOOM_MODE_DEFAULT, false);
ZoomChangedWatcher zoom_change_watcher(zoom_controller_.get(),
zoom_change_data);
content::MockNavigationHandle handle(web_contents());
handle.set_has_committed(true);
handle.set_is_in_primary_main_frame(true);
zoom_controller_->DidFinishNavigation(&handle);
zoom_change_watcher.Wait();
}
TEST_F(ZoomControllerTest, Observe_ZoomController) {
double old_zoom_level = zoom_controller_->GetZoomLevel();
double new_zoom_level = 110.0;
NavigateAndCommit(GURL("about:blank"));
// Changing from default to default so the bubble should not be shown.
ZoomController::ZoomChangedEventData zoom_change_data1(
web_contents(),
web_contents()->GetPrimaryMainFrame()->GetFrameTreeNodeId(),
old_zoom_level, old_zoom_level, ZoomController::ZOOM_MODE_ISOLATED,
false /* can_show_bubble */);
{
ZoomChangedWatcher zoom_change_watcher1(zoom_controller_.get(),
zoom_change_data1);
zoom_controller_->SetZoomMode(ZoomController::ZOOM_MODE_ISOLATED);
zoom_change_watcher1.Wait();
}
ZoomController::ZoomChangedEventData zoom_change_data2(
web_contents(),
web_contents()->GetPrimaryMainFrame()->GetFrameTreeNodeId(),
old_zoom_level, new_zoom_level, ZoomController::ZOOM_MODE_ISOLATED,
true /* can_show_bubble */);
{
ZoomChangedWatcher zoom_change_watcher2(zoom_controller_.get(),
zoom_change_data2);
zoom_controller_->SetZoomLevel(new_zoom_level);
zoom_change_watcher2.Wait();
}
}
TEST_F(ZoomControllerTest, ObserveManualZoomCanShowBubble) {
NavigateAndCommit(GURL("about:blank"));
double old_zoom_level = zoom_controller_->GetZoomLevel();
double new_zoom_level1 = old_zoom_level + 0.5;
double new_zoom_level2 = old_zoom_level + 1.0;
zoom_controller_->SetZoomMode(zoom::ZoomController::ZOOM_MODE_MANUAL);
// By default, the zoom controller will send 'true' for can_show_bubble.
ZoomController::ZoomChangedEventData zoom_change_data1(
web_contents(),
web_contents()->GetPrimaryMainFrame()->GetFrameTreeNodeId(),
old_zoom_level, new_zoom_level1, ZoomController::ZOOM_MODE_MANUAL,
true /* can_show_bubble */);
{
ZoomChangedWatcher zoom_change_watcher1(zoom_controller_.get(),
zoom_change_data1);
zoom_controller_->SetZoomLevel(new_zoom_level1);
zoom_change_watcher1.Wait();
}
// Override default and verify the subsequent event reflects this change.
zoom_controller_->SetShowsNotificationBubble(false);
ZoomController::ZoomChangedEventData zoom_change_data2(
web_contents(),
web_contents()->GetPrimaryMainFrame()->GetFrameTreeNodeId(),
new_zoom_level1, new_zoom_level2, ZoomController::ZOOM_MODE_MANUAL,
false /* can_show_bubble */);
{
ZoomChangedWatcher zoom_change_watcher2(zoom_controller_.get(),
zoom_change_data2);
zoom_controller_->SetZoomLevel(new_zoom_level2);
zoom_change_watcher2.Wait();
}
}
|