File: bubble_anchor_helper_views_unittest.mm

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 (120 lines) | stat: -rw-r--r-- 4,480 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
111
112
113
114
115
116
117
118
119
120
// Copyright 2017 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/cocoa/bubble_anchor_helper_views.h"

#import <Cocoa/Cocoa.h>

#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/test/material_design_controller_test_api.h"
#include "ui/views/bubble/bubble_dialog_delegate.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget.h"

namespace {

// Anchor offset in screen space (origin at bottom left of screen).
constexpr int kHorizOffset = 210;
constexpr int kVertOffset = 320;

class TestBubbleDialogDelegateView : public views::BubbleDialogDelegateView {
 public:
  explicit TestBubbleDialogDelegateView(views::BubbleBorder::Arrow arrow)
      : BubbleDialogDelegateView(nullptr, arrow) {
    set_close_on_deactivate(false);
    set_shadow(views::BubbleBorder::NO_ASSETS);
    int screen_height = NSMaxY([[[NSScreen screens] firstObject] frame]);
    SetAnchorRect(gfx::Rect(kHorizOffset, screen_height - kVertOffset, 0, 0));
  }

  ~TestBubbleDialogDelegateView() override {}

  // BubbleDialogDelegateView overrides:
  gfx::Size GetPreferredSize() const override { return gfx::Size(200, 150); }

 private:
  DISALLOW_COPY_AND_ASSIGN(TestBubbleDialogDelegateView);
};

NSWindow* ShowAnchoredBubble(NSWindow* parent,
                             views::BubbleBorder::Arrow arrow) {
  TestBubbleDialogDelegateView* bubble =
      new TestBubbleDialogDelegateView(arrow);
  bubble->set_parent_window([parent contentView]);
  views::BubbleDialogDelegateView::CreateBubble(bubble);
  bubble->GetWidget()->Show();
  KeepBubbleAnchored(bubble);
  return bubble->GetWidget()->GetNativeWindow();
}

}  // namespace

using BubbleAnchorHelperViewsTest = views::ViewsTestBase;

// Test that KeepBubbleAnchored(..) actually keeps the bubble anchored upon a
// resize of the parent window.
TEST_F(BubbleAnchorHelperViewsTest, AnchoringFixed) {
  // Use MD anchoring since the arithmetic is simpler (no arrows).
  ui::test::MaterialDesignControllerTestAPI md_test_api(
      ui::MaterialDesignController::MATERIAL_NORMAL);
  md_test_api.SetSecondaryUiMaterial(true);

  // Released when closed.
  NSRect parent_frame = NSMakeRect(100, 200, 300, 400);
  NSWindow* parent =
      [[NSWindow alloc] initWithContentRect:parent_frame
                                  styleMask:NSBorderlessWindowMask
                                    backing:NSBackingStoreBuffered
                                      defer:NO];
  [parent makeKeyAndOrderFront:nil];

  NSWindow* child = ShowAnchoredBubble(parent, views::BubbleBorder::TOP_RIGHT);

  // Anchored TOP_RIGHT, so Max X/Y should be anchored.
  EXPECT_EQ(kHorizOffset, NSMaxX([child frame]));
  EXPECT_EQ(kVertOffset, NSMaxY([child frame]));

  // Resize the parent from the top right.
  parent_frame.size.width += 50;
  parent_frame.size.height += 60;
  [parent setFrame:parent_frame display:YES animate:NO];
  EXPECT_EQ(kHorizOffset + 50, NSMaxX([child frame]));
  EXPECT_EQ(kVertOffset + 60, NSMaxY([child frame]));

  // Resize from the bottom left (no change).
  parent_frame.size.width -= 50;
  parent_frame.size.height -= 60;
  parent_frame = NSOffsetRect(parent_frame, 50, 60);
  [parent setFrame:parent_frame display:YES animate:NO];
  EXPECT_EQ(kHorizOffset + 50, NSMaxX([child frame]));
  EXPECT_EQ(kVertOffset + 60, NSMaxY([child frame]));

  // Move the window.
  parent_frame = NSOffsetRect(parent_frame, -50, -60);
  [parent setFrame:parent_frame display:YES animate:NO];
  EXPECT_EQ(kHorizOffset, NSMaxX([child frame]));
  EXPECT_EQ(kVertOffset, NSMaxY([child frame]));
  [child close];

  child = ShowAnchoredBubble(parent, views::BubbleBorder::TOP_LEFT);

  // Anchored TOP_LEFT, so MinX / MaxY should be anchored.
  EXPECT_EQ(kHorizOffset, NSMinX([child frame]));
  EXPECT_EQ(kVertOffset, NSMaxY([child frame]));

  // Resize the parent from right (no change).
  parent_frame.size.width += 50;
  [parent setFrame:parent_frame display:YES animate:NO];
  EXPECT_EQ(kHorizOffset, NSMinX([child frame]));
  EXPECT_EQ(kVertOffset, NSMaxY([child frame]));

  // Resize the parent from the left.
  parent_frame.size.width -= 50;
  parent_frame.origin.x += 50;
  [parent setFrame:parent_frame display:YES animate:NO];
  EXPECT_EQ(kHorizOffset + 50, NSMinX([child frame]));
  EXPECT_EQ(kVertOffset, NSMaxY([child frame]));

  [parent close];  // Takes |child| with it.
}