summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/page/touch_adjustment_test.cc
blob: c12c96c5f1a65f696064ed502273417cc4b94ef4 (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/page/touch_adjustment.h"

#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/web_screen_info.h"
#include "third_party/blink/renderer/core/testing/core_unit_test_helper.h"

namespace blink {

namespace {

class MockChromeClient : public EmptyChromeClient {
 public:
  MockChromeClient() = default;

  void SetDeviceScaleFactor(float device_scale_factor) {
    screen_info_.device_scale_factor = device_scale_factor;
  }

  WebScreenInfo GetScreenInfo() const override { return screen_info_; }

 private:
  WebScreenInfo screen_info_;
};

}  // namespace

class TouchAdjustmentTest : public RenderingTest {
 protected:
  TouchAdjustmentTest()
      : RenderingTest(SingleChildLocalFrameClient::Create()),
        chrome_client_(MakeGarbageCollected<MockChromeClient>()) {}

  LocalFrame& GetFrame() const { return *GetDocument().GetFrame(); }

  MockChromeClient& GetChromeClient() const override { return *chrome_client_; }

  void SetZoomAndScale(float device_scale_factor,
                       float browser_zoom_factor,
                       float page_scale_factor) {
    device_scale_factor_ = device_scale_factor;
    page_scale_factor_ = page_scale_factor;

    GetChromeClient().SetDeviceScaleFactor(device_scale_factor);
    GetFrame().SetPageZoomFactor(device_scale_factor * browser_zoom_factor);
    GetPage().SetPageScaleFactor(page_scale_factor);
  }

  const LayoutSize max_touch_area_dip_unscaled = LayoutSize(32, 32);

 private:
  Persistent<MockChromeClient> chrome_client_;

  float device_scale_factor_;
  float page_scale_factor_;
};

TEST_F(TouchAdjustmentTest, AdjustmentRangeUpperboundScale) {
  // touch_area is set to always exceed the upper bound so we are really
  // checking the upper bound behavior below.
  LayoutSize touch_area(100, 100);

  LayoutSize result;
  // adjustment range is shrunk to default upper bound (32, 32)
  // when there is no zoom or scale.
  SetZoomAndScale(1 /* dsf */, 1 /* browser_zoom */, 1 /* page_scale */);
  result = GetHitTestRectForAdjustment(GetFrame(), touch_area);
  EXPECT_EQ(result, max_touch_area_dip_unscaled);

  // Browser zoom without dsf change is not changing the upper bound.
  SetZoomAndScale(1 /* dsf */, 2 /* browser_zoom */, 1 /* page_scale */);
  result = GetHitTestRectForAdjustment(GetFrame(), touch_area);
  EXPECT_EQ(result, max_touch_area_dip_unscaled);

  SetZoomAndScale(1 /* dsf */, 0.5,
                  /* browser_zoom */ 1 /* page_scale */);
  result = GetHitTestRectForAdjustment(GetFrame(), touch_area);
  EXPECT_EQ(result, max_touch_area_dip_unscaled);

  // When has page scale factor, upper bound is scaled.
  SetZoomAndScale(1 /* dsf */, 1 /* browser_zoom */, 2 /* page_scale */);
  result = GetHitTestRectForAdjustment(GetFrame(), touch_area);
  EXPECT_EQ(result, max_touch_area_dip_unscaled * (1.f / 2));

  // touch_area is in physical pixel, should change with dsf change.
  SetZoomAndScale(2 /* dsf */, 1 /* browser_zoom */, 1 /* page_scale */);
  result = GetHitTestRectForAdjustment(GetFrame(), touch_area);
  EXPECT_EQ(result, max_touch_area_dip_unscaled * 2.f);

  SetZoomAndScale(0.5 /* dsf */, 1 /* browser_zoom */, 1 /* page_scale */);
  result = GetHitTestRectForAdjustment(GetFrame(), touch_area);
  EXPECT_EQ(result, max_touch_area_dip_unscaled * 0.5f);

  // When DeviceScaleFactorDeprecated() is not 1, zoom-for-dsf is disabled,
  // touch_area should be in dip.
  SetZoomAndScale(2 /* dsf */, 1 /* browser_zoom */, 1 /* page_scale */);
  GetPage().SetDeviceScaleFactorDeprecated(0.5);
  result = GetHitTestRectForAdjustment(GetFrame(), touch_area);
  EXPECT_EQ(result, max_touch_area_dip_unscaled);
}

}  // namespace blink