summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/nsview_additions_unittest.mm
blob: a9b74688830ecd8d9a1980a792b67c1095771d59 (plain)
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
// Copyright (c) 2013 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.

#import "chrome/browser/ui/cocoa/nsview_additions.h"

#include "base/mac/scoped_nsobject.h"
#import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"

typedef CocoaTest NSViewChromeAdditionsTest;

@interface ParentView : NSView {
 @private
  int removeCount_;
  int addCount_;
}

@property(readonly, nonatomic) int removeCount;
@property(readonly, nonatomic) int addCount;

@end

@implementation ParentView

@synthesize removeCount = removeCount_;
@synthesize addCount = addCount_;

- (void)willRemoveSubview:(NSView*)view {
  ++removeCount_;
}

- (void)didAddSubview:(NSView*)view {
  ++addCount_;
}

@end

TEST_F(NSViewChromeAdditionsTest, BelowAboveView) {
  base::scoped_nsobject<NSView> parent(
      [[NSView alloc] initWithFrame:NSZeroRect]);
  base::scoped_nsobject<NSView> child1(
      [[NSView alloc] initWithFrame:NSZeroRect]);
  base::scoped_nsobject<NSView> child2(
      [[NSView alloc] initWithFrame:NSZeroRect]);

  [parent addSubview:child1];
  [parent addSubview:child2];
  EXPECT_TRUE([child1 cr_isBelowView:child2]);
  EXPECT_FALSE([child1 cr_isAboveView:child2]);
  EXPECT_FALSE([child2 cr_isBelowView:child1]);
  EXPECT_TRUE([child2 cr_isAboveView:child1]);

  [child1 removeFromSuperview];
  [child2 removeFromSuperview];
  [parent addSubview:child2];
  [parent addSubview:child1];
  EXPECT_FALSE([child1 cr_isBelowView:child2]);
  EXPECT_TRUE([child1 cr_isAboveView:child2]);
  EXPECT_TRUE([child2 cr_isBelowView:child1]);
  EXPECT_FALSE([child2 cr_isAboveView:child1]);
}

TEST_F(NSViewChromeAdditionsTest, EnsurePosition) {
  base::scoped_nsobject<NSView> parent(
      [[NSView alloc] initWithFrame:NSZeroRect]);
  base::scoped_nsobject<NSView> child1(
      [[NSView alloc] initWithFrame:NSZeroRect]);
  base::scoped_nsobject<NSView> child2(
      [[NSView alloc] initWithFrame:NSZeroRect]);

  [parent addSubview:child1];
  [parent cr_ensureSubview:child2
              isPositioned:NSWindowAbove
                relativeTo:child1];
  EXPECT_NSEQ([[parent subviews] objectAtIndex:0], child1);
  EXPECT_NSEQ([[parent subviews] objectAtIndex:1], child2);

  [child2 removeFromSuperview];
  [parent cr_ensureSubview:child2
              isPositioned:NSWindowBelow
                relativeTo:child1];
  EXPECT_NSEQ([[parent subviews] objectAtIndex:0], child2);
  EXPECT_NSEQ([[parent subviews] objectAtIndex:1], child1);
}

// Verify that no view is removed or added when no change is needed.
TEST_F(NSViewChromeAdditionsTest, EnsurePositionNoChange) {
  base::scoped_nsobject<ParentView> parent(
      [[ParentView alloc] initWithFrame:NSZeroRect]);
  base::scoped_nsobject<NSView> child1(
      [[NSView alloc] initWithFrame:NSZeroRect]);
  base::scoped_nsobject<NSView> child2(
      [[NSView alloc] initWithFrame:NSZeroRect]);
  [parent addSubview:child1];
  [parent addSubview:child2];

  EXPECT_EQ(0, [parent removeCount]);
  EXPECT_EQ(2, [parent addCount]);
  [parent cr_ensureSubview:child2
              isPositioned:NSWindowAbove
                relativeTo:child1];
  EXPECT_EQ(0, [parent removeCount]);
  EXPECT_EQ(2, [parent addCount]);
}