blob: 5921e308200c51cdd8ed3281bd46728dde0a82f8 [file] [log] [blame]
[email protected]f9526d12012-10-18 01:55:031// 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 "content/renderer/disambiguation_popup_helper.h"
6
7#include "testing/gtest/include/gtest/gtest.h"
[email protected]5c30b5e02013-05-30 03:46:088#include "third_party/WebKit/public/platform/WebRect.h"
9#include "third_party/WebKit/public/platform/WebVector.h"
tfarina3b0452d2014-12-31 15:20:0910#include "ui/gfx/geometry/rect.h"
tfarinaebe974f02015-01-03 04:25:3211#include "ui/gfx/geometry/size.h"
12#include "ui/gfx/geometry/size_conversions.h"
[email protected]f9526d12012-10-18 01:55:0313
14// these constants are copied from the implementation class
15namespace {
16const float kDisambiguationPopupMaxScale = 5.0;
17const float kDisambiguationPopupMinScale = 2.0;
[email protected]0a8d4275e2013-01-04 22:21:2618} // unnamed namespace
[email protected]f9526d12012-10-18 01:55:0319
20namespace content {
21
22class DisambiguationPopupHelperUnittest : public testing::Test {
23 public:
24 DisambiguationPopupHelperUnittest()
[email protected]70221f02013-01-31 22:17:0725 : kScreenSize_(640, 480)
26 , kVisibleContentSize_(640, 480)
27 , kImplScale_(1) { }
[email protected]f9526d12012-10-18 01:55:0328 protected:
[email protected]70221f02013-01-31 22:17:0729 const gfx::Size kScreenSize_;
30 const gfx::Size kVisibleContentSize_;
31 const float kImplScale_;
[email protected]f9526d12012-10-18 01:55:0332};
33
34TEST_F(DisambiguationPopupHelperUnittest, ClipByViewport) {
35 gfx::Rect tap_rect(1000, 1000, 10, 10);
[email protected]180ef242013-11-07 06:50:4636 blink::WebVector<blink::WebRect> target_rects(static_cast<size_t>(1));
[email protected]f9526d12012-10-18 01:55:0337 target_rects[0] = gfx::Rect(-20, -20, 10, 10);
38
39 gfx::Rect zoom_rect;
40 float scale = DisambiguationPopupHelper::ComputeZoomAreaAndScaleFactor(
[email protected]70221f02013-01-31 22:17:0741 tap_rect, target_rects, kScreenSize_, kVisibleContentSize_, kImplScale_,
42 &zoom_rect);
[email protected]f9526d12012-10-18 01:55:0343
[email protected]70221f02013-01-31 22:17:0744 EXPECT_TRUE(gfx::Rect(kVisibleContentSize_).Contains(zoom_rect));
[email protected]f9526d12012-10-18 01:55:0345 EXPECT_LE(kDisambiguationPopupMinScale, scale);
46
[email protected]01a15a72012-11-10 09:34:2847 gfx::Size scaled_size = ToCeiledSize(ScaleSize(zoom_rect.size(), scale));
[email protected]70221f02013-01-31 22:17:0748 EXPECT_TRUE(gfx::Rect(kScreenSize_).Contains(gfx::Rect(scaled_size)));
[email protected]f9526d12012-10-18 01:55:0349}
50
51TEST_F(DisambiguationPopupHelperUnittest, MiniTarget) {
52 gfx::Rect tap_rect(-5, -5, 20, 20);
[email protected]180ef242013-11-07 06:50:4653 blink::WebVector<blink::WebRect> target_rects(static_cast<size_t>(1));
[email protected]f9526d12012-10-18 01:55:0354 target_rects[0] = gfx::Rect(10, 10, 1, 1);
55
56 gfx::Rect zoom_rect;
57 float scale = DisambiguationPopupHelper::ComputeZoomAreaAndScaleFactor(
[email protected]70221f02013-01-31 22:17:0758 tap_rect, target_rects, kScreenSize_, kVisibleContentSize_, kImplScale_,
59 &zoom_rect);
[email protected]f9526d12012-10-18 01:55:0360
[email protected]70221f02013-01-31 22:17:0761 EXPECT_TRUE(gfx::Rect(kVisibleContentSize_).Contains(zoom_rect));
[email protected]f9526d12012-10-18 01:55:0362 EXPECT_EQ(kDisambiguationPopupMaxScale, scale);
63 EXPECT_TRUE(zoom_rect.Contains(target_rects[0]));
64
[email protected]01a15a72012-11-10 09:34:2865 gfx::Size scaled_size = ToCeiledSize(ScaleSize(zoom_rect.size(), scale));
[email protected]70221f02013-01-31 22:17:0766 EXPECT_TRUE(gfx::Rect(kScreenSize_).Contains(gfx::Rect(scaled_size)));
[email protected]f9526d12012-10-18 01:55:0367}
68
69TEST_F(DisambiguationPopupHelperUnittest, LongLinks) {
70 gfx::Rect tap_rect(10, 10, 20, 20);
[email protected]180ef242013-11-07 06:50:4671 blink::WebVector<blink::WebRect> target_rects(static_cast<size_t>(2));
[email protected]f9526d12012-10-18 01:55:0372 target_rects[0] = gfx::Rect(15, 15, 1000, 5);
73 target_rects[1] = gfx::Rect(15, 25, 1000, 5);
74
75 gfx::Rect zoom_rect;
76 float scale = DisambiguationPopupHelper::ComputeZoomAreaAndScaleFactor(
[email protected]70221f02013-01-31 22:17:0777 tap_rect, target_rects, kScreenSize_, kVisibleContentSize_, kImplScale_,
78 &zoom_rect);
[email protected]f9526d12012-10-18 01:55:0379
[email protected]70221f02013-01-31 22:17:0780 EXPECT_TRUE(gfx::Rect(kVisibleContentSize_).Contains(zoom_rect));
[email protected]f9526d12012-10-18 01:55:0381 EXPECT_EQ(kDisambiguationPopupMaxScale, scale);
82 EXPECT_TRUE(zoom_rect.Contains(tap_rect));
83
[email protected]01a15a72012-11-10 09:34:2884 gfx::Size scaled_size = ToCeiledSize(ScaleSize(zoom_rect.size(), scale));
[email protected]70221f02013-01-31 22:17:0785 EXPECT_TRUE(gfx::Rect(kScreenSize_).Contains(gfx::Rect(scaled_size)));
[email protected]f9526d12012-10-18 01:55:0386}
87
[email protected]0a8d4275e2013-01-04 22:21:2688} // namespace content