File: popup_view_common_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (110 lines) | stat: -rw-r--r-- 3,887 bytes parent folder | download
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
// Copyright 2016 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 "chrome/browser/ui/autofill/popup_view_common.h"

#include <stddef.h>

#include <memory>

#include "base/macros.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "content/public/browser/web_contents.h"
#include "ui/display/display.h"
#include "ui/gfx/geometry/rect.h"

namespace autofill {

namespace {

// Test class which overrides specific behavior for testing.
class TestPopupViewCommon : public PopupViewCommon {
 public:
  explicit TestPopupViewCommon(const display::Display& display)
      : display_(display) {}

  display::Display GetDisplayNearestPoint(
      const gfx::Point& point,
      gfx::NativeView container_view) override {
    return display_;
  }

 private:
  display::Display display_;
};

}  // namespace

class PopupViewCommonTest : public ChromeRenderViewHostTestHarness {
 public:
  PopupViewCommonTest() {}
  ~PopupViewCommonTest() override {}

 private:
  DISALLOW_COPY_AND_ASSIGN(PopupViewCommonTest);
};

TEST_F(PopupViewCommonTest, CalculatePopupBounds) {
  int desired_width = 40;
  int desired_height = 16;

  // Set up the visible screen space.
  display::Display display(
      0, gfx::Rect(0, 0, 2 * desired_width, 2 * desired_height));
  TestPopupViewCommon view_common(display);

  struct {
    gfx::Rect element_bounds;
    gfx::Rect expected_popup_bounds_ltr;
    // Non-empty only when it differs from the ltr expectation.
    gfx::Rect expected_popup_bounds_rtl;
  } test_cases[] = {
      // The popup grows down and to the end.
      {gfx::Rect(38, 0, 5, 0), gfx::Rect(38, 0, desired_width, desired_height),
       gfx::Rect(3, 0, desired_width, desired_height)},

      // The popup grows down and to the left when there's no room on the right.
      {gfx::Rect(2 * desired_width, 0, 5, 0),
       gfx::Rect(desired_width, 0, desired_width, desired_height)},

      // The popup grows up and to the right.
      {gfx::Rect(0, 2 * desired_height, 5, 0),
       gfx::Rect(0, desired_height, desired_width, desired_height)},

      // The popup grows up and to the left.
      {gfx::Rect(2 * desired_width, 2 * desired_height, 5, 0),
       gfx::Rect(desired_width, desired_height, desired_width, desired_height)},

      // The popup would be partial off the top and left side of the screen.
      {gfx::Rect(-desired_width / 2, -desired_height / 2, 5, 0),
       gfx::Rect(0, 0, desired_width, desired_height)},

      // The popup would be partially off the bottom and the right side of
      // the screen.
      {gfx::Rect(desired_width * 1.5, desired_height * 1.5, 5, 0),
       gfx::Rect((desired_width * 1.5 + 5 - desired_width),
                 (desired_height * 1.5 - desired_height), desired_width,
                 desired_height)},
  };

  for (size_t i = 0; i < arraysize(test_cases); ++i) {
    gfx::Rect actual_popup_bounds = view_common.CalculatePopupBounds(
        desired_width, desired_height, test_cases[i].element_bounds,
        web_contents()->GetNativeView(), /* is_rtl= */ false);
    EXPECT_EQ(test_cases[i].expected_popup_bounds_ltr.ToString(),
              actual_popup_bounds.ToString())
        << "Popup bounds failed to match for ltr test " << i;

    actual_popup_bounds = view_common.CalculatePopupBounds(
        desired_width, desired_height, test_cases[i].element_bounds,
        web_contents()->GetNativeView(), /* is_rtl= */ true);
    gfx::Rect expected_popup_bounds = test_cases[i].expected_popup_bounds_rtl;
    if (expected_popup_bounds.IsEmpty())
      expected_popup_bounds = test_cases[i].expected_popup_bounds_ltr;
    EXPECT_EQ(expected_popup_bounds.ToString(), actual_popup_bounds.ToString())
        << "Popup bounds failed to match for rtl test " << i;
  }
}

}  // namespace autofill