1// Copyright 2017 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#import "ios/chrome/browser/ui/bubble/bubble_view.h"
6
7#include "base/ios/ios_util.h"
8#include "ios/chrome/browser/ui/util/ui_util.h"
9#include "testing/gtest/include/gtest/gtest.h"
10#include "testing/gtest_mac.h"
11#include "testing/platform_test.h"
12
13#if !defined(__has_feature) || !__has_feature(objc_arc)
14#error "This file requires ARC support."
15#endif
16
17// Fixture to test BubbleView.
18class BubbleViewTest : public PlatformTest {
19 public:
20  BubbleViewTest()
21      : maxSize_(CGSizeMake(500.0f, 500.0f)),
22        arrowDirection_(BubbleArrowDirectionUp),
23        alignment_(BubbleAlignmentCenter),
24        shortText_(@"I"),
25        longText_(@"Lorem ipsum dolor sit amet, consectetur adipiscing elit.") {
26  }
27
28 protected:
29  // The maximum size of the bubble.
30  const CGSize maxSize_;
31  // The direction that the bubble's arrow points.
32  const BubbleArrowDirection arrowDirection_;
33  // The alignment of the bubble's arrow relative to the rest of the bubble.
34  const BubbleAlignment alignment_;
35  // Text that is shorter than the minimum line width.
36  NSString* shortText_;
37  // Text that is longer than the maximum line width. It should wrap onto
38  // multiple lines.
39  NSString* longText_;
40};
41
42// Test |sizeThatFits| given short text.
43TEST_F(BubbleViewTest, BubbleSizeShortText) {
44  BubbleView* bubble = [[BubbleView alloc] initWithText:shortText_
45                                         arrowDirection:arrowDirection_
46                                              alignment:alignment_];
47  CGSize bubbleSize = [bubble sizeThatFits:maxSize_];
48  // Since the label is shorter than the minimum line width, expect the bubble
49  // to be the minimum width and accommodate one line of text.
50  EXPECT_NEAR(58.0f, bubbleSize.width, 1.0f);
51  EXPECT_NEAR(65.0f, bubbleSize.height, 1.0f);
52}
53
54// Test |sizeThatFits| given text that should wrap onto multiple lines.
55TEST_F(BubbleViewTest, BubbleSizeMultipleLineText) {
56  BubbleView* bubble = [[BubbleView alloc] initWithText:longText_
57                                         arrowDirection:arrowDirection_
58                                              alignment:alignment_];
59  CGSize bubbleSize = [bubble sizeThatFits:maxSize_];
60
61  // The bubble should fit the label, which contains two lines of text.
62  EXPECT_NEAR(329.0f, bubbleSize.width, 1.0f);
63
64  EXPECT_NEAR(83.0f, bubbleSize.height, 2.0f);
65}
66
67// Test that the accessibility label matches the display text.
68TEST_F(BubbleViewTest, Accessibility) {
69  BubbleView* bubble = [[BubbleView alloc] initWithText:longText_
70                                         arrowDirection:arrowDirection_
71                                              alignment:alignment_];
72  UIView* superview = [[UIView alloc] initWithFrame:CGRectZero];
73  // Add the bubble view to the view hierarchy.
74  [superview addSubview:bubble];
75  EXPECT_NSEQ(longText_, bubble.accessibilityLabel);
76}
77