File: tab_strip_control_button_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (111 lines) | stat: -rw-r--r-- 3,739 bytes parent folder | download | duplicates (6)
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
111
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/tabs/tab_strip_control_button.h"

#include "base/test/bind.h"
#include "chrome/browser/ui/views/tabs/fake_base_tab_strip_controller.h"
#include "chrome/test/views/chrome_views_test_base.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkRegion.h"

class TabStripController;

constexpr int kBorderThickness = 4;

class FrameCondensedController : public FakeBaseTabStripController {
 public:
  bool IsFrameCondensed() const override { return condensed_; }

  void set_condensed(bool condensed) { condensed_ = condensed; }

 private:
  bool condensed_ = false;
};

class TabStripControlButtonTest : public ChromeViewsTestBase {
 public:
  void SetUp() override {
    ChromeViewsTestBase::SetUp();

    tab_strip_controller_ = std::make_unique<FrameCondensedController>();
    button_ = std::make_unique<TabStripControlButton>(
        tab_strip_controller_.get(), base::BindLambdaForTesting([]() {}), u"");
    button_->SetBorder(
        views::CreateEmptyBorder(gfx::Insets::VH(kBorderThickness, 0)));
    button_->SetSize(button_->CalculatePreferredSize({}));
  }

 protected:
  std::unique_ptr<FrameCondensedController> tab_strip_controller_;
  std::unique_ptr<TabStripControlButton> button_;
};

TEST_F(TabStripControlButtonTest, UncondensedFrameHitTestMask) {
  SkPath path;
  button_->GetHitTestMask(&path);
  SkRegion clip_region;
  clip_region.setRect({0, 0, button_->width(), button_->height()});
  SkRegion mask;
  mask.setPath(path, clip_region);

  const int center_x = button_->width() / 2;
  const int center_y = button_->height() / 2;

  const int top = kBorderThickness;
  const int left = 0;
  const int bottom = button_->height() - kBorderThickness - 1;
  const int right = button_->width() - 1;

  EXPECT_TRUE(mask.contains(center_x, center_y));

  // The points where the circle touches the border are in the mask.
  EXPECT_TRUE(mask.contains(center_x, top));
  EXPECT_TRUE(mask.contains(center_x, bottom));
  EXPECT_TRUE(mask.contains(left, center_y));
  EXPECT_TRUE(mask.contains(right, center_y));

  // The four corners of the border are not in the mask.
  EXPECT_FALSE(mask.contains(left, top));
  EXPECT_FALSE(mask.contains(left, bottom));
  EXPECT_FALSE(mask.contains(right, top));
  EXPECT_FALSE(mask.contains(right, bottom));
}

TEST_F(TabStripControlButtonTest, CondensedFrameHitTestMask) {
  tab_strip_controller_->set_condensed(true);

  SkPath path;
  button_->GetHitTestMask(&path);
  SkRegion clip_region;
  clip_region.setRect({0, 0, button_->width(), button_->height()});
  SkRegion mask;
  mask.setPath(path, clip_region);

  const int center_x = button_->width() / 2;
  const int center_y = button_->height() / 2;

  const int top = kBorderThickness;
  const int left = 0;
  const int bottom = button_->height() - kBorderThickness - 1;
  const int right = button_->width() - 1;

  EXPECT_TRUE(mask.contains(center_x, center_y));

  // The points where the circle touches the border are in the mask.
  EXPECT_TRUE(mask.contains(center_x, top));
  EXPECT_TRUE(mask.contains(center_x, bottom));
  EXPECT_TRUE(mask.contains(left, center_y));
  EXPECT_TRUE(mask.contains(right, center_y));

  // The entire top of the view (including the border!) is in the mask.
  EXPECT_TRUE(mask.contains(center_x, 0));
  EXPECT_TRUE(mask.contains(left, 0));
  EXPECT_TRUE(mask.contains(right, 0));

  // The bottom corners of the border are not in the mask.
  EXPECT_FALSE(mask.contains(right, bottom));
  EXPECT_FALSE(mask.contains(left, bottom));
}