summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/android/disambiguation_popup_helper_unittest.cc
blob: 679a8e26543c72653c6968102f16f550641342d6 (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
// Copyright (c) 2012 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 "content/renderer/android/disambiguation_popup_helper.h"

#include <stddef.h>

#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/web_rect.h"
#include "third_party/blink/public/platform/web_vector.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/size_conversions.h"

// these constants are copied from the implementation class
namespace {
const float kDisambiguationPopupMaxScale = 5.0;
const float kDisambiguationPopupMinScale = 2.0;
}  // unnamed namespace

namespace content {

class DisambiguationPopupHelperUnittest : public testing::Test {
 public:
  DisambiguationPopupHelperUnittest()
      : kScreenSize_(640, 480)
      , kVisibleContentSize_(640, 480)
      , kImplScale_(1) { }
 protected:
  const gfx::Size kScreenSize_;
  const gfx::Size kVisibleContentSize_;
  const float kImplScale_;
};

TEST_F(DisambiguationPopupHelperUnittest, ClipByViewport) {
  gfx::Rect tap_rect(1000, 1000, 10, 10);
  blink::WebVector<blink::WebRect> target_rects(static_cast<size_t>(1));
  target_rects[0] = gfx::Rect(-20, -20, 10, 10);

  gfx::Rect zoom_rect;
  float scale = DisambiguationPopupHelper::ComputeZoomAreaAndScaleFactor(
      tap_rect, target_rects, kScreenSize_, kVisibleContentSize_, kImplScale_,
      &zoom_rect);

  EXPECT_TRUE(gfx::Rect(kVisibleContentSize_).Contains(zoom_rect));
  EXPECT_LE(kDisambiguationPopupMinScale, scale);

  gfx::Size scaled_size = gfx::ScaleToCeiledSize(zoom_rect.size(), scale);
  EXPECT_TRUE(gfx::Rect(kScreenSize_).Contains(gfx::Rect(scaled_size)));
}

TEST_F(DisambiguationPopupHelperUnittest, MiniTarget) {
  gfx::Rect tap_rect(-5, -5, 20, 20);
  blink::WebVector<blink::WebRect> target_rects(static_cast<size_t>(1));
  target_rects[0] = gfx::Rect(10, 10, 1, 1);

  gfx::Rect zoom_rect;
  float scale = DisambiguationPopupHelper::ComputeZoomAreaAndScaleFactor(
      tap_rect, target_rects, kScreenSize_, kVisibleContentSize_, kImplScale_,
      &zoom_rect);

  EXPECT_TRUE(gfx::Rect(kVisibleContentSize_).Contains(zoom_rect));
  EXPECT_EQ(kDisambiguationPopupMaxScale, scale);
  EXPECT_TRUE(zoom_rect.Contains(target_rects[0]));

  gfx::Size scaled_size = gfx::ScaleToCeiledSize(zoom_rect.size(), scale);
  EXPECT_TRUE(gfx::Rect(kScreenSize_).Contains(gfx::Rect(scaled_size)));
}

TEST_F(DisambiguationPopupHelperUnittest, LongLinks) {
  gfx::Rect tap_rect(10, 10, 20, 20);
  blink::WebVector<blink::WebRect> target_rects(static_cast<size_t>(2));
  target_rects[0] = gfx::Rect(15, 15, 1000, 5);
  target_rects[1] = gfx::Rect(15, 25, 1000, 5);

  gfx::Rect zoom_rect;
  float scale = DisambiguationPopupHelper::ComputeZoomAreaAndScaleFactor(
      tap_rect, target_rects, kScreenSize_, kVisibleContentSize_, kImplScale_,
      &zoom_rect);

  EXPECT_TRUE(gfx::Rect(kVisibleContentSize_).Contains(zoom_rect));
  EXPECT_EQ(kDisambiguationPopupMaxScale, scale);
  EXPECT_TRUE(zoom_rect.Contains(tap_rect));

  gfx::Size scaled_size = gfx::ScaleToCeiledSize(zoom_rect.size(), scale);
  EXPECT_TRUE(gfx::Rect(kScreenSize_).Contains(gfx::Rect(scaled_size)));
}

}  // namespace content