Support for process-wide incidents in the safe browsing incident reporting service.
[chromium-blink-merge.git] / content / renderer / disambiguation_popup_helper_unittest.cc
blob8cbbe4b5ab3c308dbbef385f84c56b3a33c68bb5
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.
5 #include "content/renderer/disambiguation_popup_helper.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "third_party/WebKit/public/platform/WebRect.h"
9 #include "third_party/WebKit/public/platform/WebVector.h"
10 #include "ui/gfx/rect.h"
11 #include "ui/gfx/size.h"
12 #include "ui/gfx/size_conversions.h"
14 // these constants are copied from the implementation class
15 namespace {
16 const float kDisambiguationPopupMaxScale = 5.0;
17 const float kDisambiguationPopupMinScale = 2.0;
18 } // unnamed namespace
20 namespace content {
22 class DisambiguationPopupHelperUnittest : public testing::Test {
23 public:
24 DisambiguationPopupHelperUnittest()
25 : kScreenSize_(640, 480)
26 , kVisibleContentSize_(640, 480)
27 , kImplScale_(1) { }
28 protected:
29 const gfx::Size kScreenSize_;
30 const gfx::Size kVisibleContentSize_;
31 const float kImplScale_;
34 TEST_F(DisambiguationPopupHelperUnittest, ClipByViewport) {
35 gfx::Rect tap_rect(1000, 1000, 10, 10);
36 blink::WebVector<blink::WebRect> target_rects(static_cast<size_t>(1));
37 target_rects[0] = gfx::Rect(-20, -20, 10, 10);
39 gfx::Rect zoom_rect;
40 float scale = DisambiguationPopupHelper::ComputeZoomAreaAndScaleFactor(
41 tap_rect, target_rects, kScreenSize_, kVisibleContentSize_, kImplScale_,
42 &zoom_rect);
44 EXPECT_TRUE(gfx::Rect(kVisibleContentSize_).Contains(zoom_rect));
45 EXPECT_LE(kDisambiguationPopupMinScale, scale);
47 gfx::Size scaled_size = ToCeiledSize(ScaleSize(zoom_rect.size(), scale));
48 EXPECT_TRUE(gfx::Rect(kScreenSize_).Contains(gfx::Rect(scaled_size)));
51 TEST_F(DisambiguationPopupHelperUnittest, MiniTarget) {
52 gfx::Rect tap_rect(-5, -5, 20, 20);
53 blink::WebVector<blink::WebRect> target_rects(static_cast<size_t>(1));
54 target_rects[0] = gfx::Rect(10, 10, 1, 1);
56 gfx::Rect zoom_rect;
57 float scale = DisambiguationPopupHelper::ComputeZoomAreaAndScaleFactor(
58 tap_rect, target_rects, kScreenSize_, kVisibleContentSize_, kImplScale_,
59 &zoom_rect);
61 EXPECT_TRUE(gfx::Rect(kVisibleContentSize_).Contains(zoom_rect));
62 EXPECT_EQ(kDisambiguationPopupMaxScale, scale);
63 EXPECT_TRUE(zoom_rect.Contains(target_rects[0]));
65 gfx::Size scaled_size = ToCeiledSize(ScaleSize(zoom_rect.size(), scale));
66 EXPECT_TRUE(gfx::Rect(kScreenSize_).Contains(gfx::Rect(scaled_size)));
69 TEST_F(DisambiguationPopupHelperUnittest, LongLinks) {
70 gfx::Rect tap_rect(10, 10, 20, 20);
71 blink::WebVector<blink::WebRect> target_rects(static_cast<size_t>(2));
72 target_rects[0] = gfx::Rect(15, 15, 1000, 5);
73 target_rects[1] = gfx::Rect(15, 25, 1000, 5);
75 gfx::Rect zoom_rect;
76 float scale = DisambiguationPopupHelper::ComputeZoomAreaAndScaleFactor(
77 tap_rect, target_rects, kScreenSize_, kVisibleContentSize_, kImplScale_,
78 &zoom_rect);
80 EXPECT_TRUE(gfx::Rect(kVisibleContentSize_).Contains(zoom_rect));
81 EXPECT_EQ(kDisambiguationPopupMaxScale, scale);
82 EXPECT_TRUE(zoom_rect.Contains(tap_rect));
84 gfx::Size scaled_size = ToCeiledSize(ScaleSize(zoom_rect.size(), scale));
85 EXPECT_TRUE(gfx::Rect(kScreenSize_).Contains(gfx::Rect(scaled_size)));
88 } // namespace content