File: framed_browser_window_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 (312 lines) | stat: -rw-r--r-- 12,818 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright (c) 2012 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 <Cocoa/Cocoa.h>

#include "base/debug/debugger.h"
#include "base/mac/scoped_nsobject.h"
#include "chrome/app/chrome_command_ids.h"
#import "chrome/browser/ui/cocoa/browser_window_controller.h"
#import "chrome/browser/ui/cocoa/framed_browser_window.h"
#import "chrome/browser/ui/cocoa/test/cocoa_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"

namespace {
NSString* const kAppleTextDirectionDefaultsKey = @"AppleTextDirection";
NSString* const kForceRTLWritingDirectionDefaultsKey =
    @"NSForceRightToLeftWritingDirection";
}  // namespace

class FramedBrowserWindowTest : public CocoaTest {
 public:
  void SetUp() override {
    CocoaTest::SetUp();
    // Create a window.
    window_ = [[FramedBrowserWindow alloc]
               initWithContentRect:NSMakeRect(0, 0, 800, 600)
                       hasTabStrip:YES];
    if (base::debug::BeingDebugged()) {
      [window_ orderFront:nil];
    } else {
      [window_ orderBack:nil];
    }
  }

  void TearDown() override {
    [window_ close];
    CocoaTest::TearDown();
  }

  // Returns a canonical snapshot of the window.
  NSData* WindowContentsAsTIFF() {
    [window_ display];

    NSView* frameView = [window_ contentView];
    while ([frameView superview]) {
      frameView = [frameView superview];
    }

    // Inset to mask off left and right edges which vary in HighDPI.
    NSRect bounds = NSInsetRect([frameView bounds], 4, 0);

    // On 10.6, the grippy changes appearance slightly when painted the second
    // time in a textured window. Since this test cares about the window title,
    // cut off the bottom of the window.
    bounds.size.height -= 40;
    bounds.origin.y += 40;

    [frameView lockFocus];
    base::scoped_nsobject<NSBitmapImageRep> bitmap(
        [[NSBitmapImageRep alloc] initWithFocusedViewRect:bounds]);
    [frameView unlockFocus];

    return [bitmap TIFFRepresentation];
  }

  FramedBrowserWindow* window_;
};

// Baseline test that the window creates, displays, closes, and
// releases.
TEST_F(FramedBrowserWindowTest, ShowAndClose) {
  [window_ display];
}

// Test that undocumented title-hiding API we're using does the job.
// TODO(crbug.com/655112): Fails on Mac 10.11 Tests.
TEST_F(FramedBrowserWindowTest, DISABLED_DoesHideTitle) {
  // The -display calls are not strictly necessary, but they do
  // make it easier to see what's happening when debugging (without
  // them the changes are never flushed to the screen).

  [window_ setTitle:@""];
  [window_ display];
  NSData* emptyTitleData = WindowContentsAsTIFF();

  [window_ setTitle:@"This is a title"];
  [window_ display];
  NSData* thisTitleData = WindowContentsAsTIFF();

  // The default window with a title should look different from the
  // window with an empty title.
  EXPECT_FALSE([emptyTitleData isEqualToData:thisTitleData]);

  [window_ setShouldHideTitle:YES];
  [window_ setTitle:@""];
  [window_ display];
  [window_ setTitle:@"This is a title"];
  [window_ display];
  NSData* hiddenTitleData = WindowContentsAsTIFF();

  // With our magic setting, the window with a title should look the
  // same as the window with an empty title.
  EXPECT_TRUE([window_ _isTitleHidden]);
  EXPECT_TRUE([emptyTitleData isEqualToData:hiddenTitleData]);
}

// Test to make sure that our window widgets are in the right place.
TEST_F(FramedBrowserWindowTest, WindowWidgetLocation) {
  BOOL yes = YES;
  BOOL no = NO;

  // First without a tabstrip.
  [window_ close];
  window_ = [[FramedBrowserWindow alloc]
             initWithContentRect:NSMakeRect(0, 0, 800, 600)
                     hasTabStrip:NO];
  // Update window layout according to existing layout constraints.
  [window_ layoutIfNeeded];
  id controller = [OCMockObject mockForClass:[BrowserWindowController class]];
  [[[controller stub] andReturnValue:OCMOCK_VALUE(yes)]
      isKindOfClass:[BrowserWindowController class]];
  [[[controller expect] andReturnValue:OCMOCK_VALUE(no)] hasTabStrip];
  [[[controller expect] andReturnValue:OCMOCK_VALUE(yes)] hasTitleBar];
  [[[controller expect] andReturnValue:OCMOCK_VALUE(no)] isTabbedWindow];
  [window_ setWindowController:controller];

  NSView* closeBoxControl = [window_ standardWindowButton:NSWindowCloseButton];
  EXPECT_TRUE(closeBoxControl);
  NSRect closeBoxFrame = [closeBoxControl convertRect:[closeBoxControl bounds]
                                               toView:nil];
  NSRect windowBounds = [window_ frame];
  windowBounds = [[window_ contentView] convertRect:windowBounds fromView:nil];
  windowBounds.origin = NSZeroPoint;
  EXPECT_EQ(NSMaxY(closeBoxFrame),
            NSMaxY(windowBounds) -
                kFramedWindowButtonsWithoutTabStripOffsetFromTop);
  EXPECT_EQ(NSMinX(closeBoxFrame),
            kFramedWindowButtonsWithoutTabStripOffsetFromLeft);

  NSView* miniaturizeControl =
      [window_ standardWindowButton:NSWindowMiniaturizeButton];
  EXPECT_TRUE(miniaturizeControl);
  NSRect miniaturizeFrame =
      [miniaturizeControl convertRect:[miniaturizeControl bounds]
                               toView:nil];
  EXPECT_LT(NSMaxX(closeBoxFrame), NSMinX(miniaturizeFrame));
  EXPECT_EQ(NSMaxY(miniaturizeFrame),
            NSMaxY(windowBounds) -
                kFramedWindowButtonsWithoutTabStripOffsetFromTop);
  EXPECT_EQ(NSMinX(miniaturizeFrame),
            NSMaxX(closeBoxFrame) + [window_ windowButtonsInterButtonSpacing]);
  [window_ setWindowController:nil];

  // Then with a tabstrip.
  [window_ close];
  window_ = [[FramedBrowserWindow alloc]
             initWithContentRect:NSMakeRect(0, 0, 800, 600)
                     hasTabStrip:YES];
  // Update window layout according to existing layout constraints.
  [window_ layoutIfNeeded];
  controller = [OCMockObject mockForClass:[BrowserWindowController class]];
  [[[controller stub] andReturnValue:OCMOCK_VALUE(yes)]
      isKindOfClass:[BrowserWindowController class]];
  [[[controller expect] andReturnValue:OCMOCK_VALUE(yes)] hasTabStrip];
  [[[controller expect] andReturnValue:OCMOCK_VALUE(no)] hasTitleBar];
  [[[controller expect] andReturnValue:OCMOCK_VALUE(yes)] isTabbedWindow];
  [window_ setWindowController:controller];

  closeBoxControl = [window_ standardWindowButton:NSWindowCloseButton];
  EXPECT_TRUE(closeBoxControl);
  closeBoxFrame = [closeBoxControl convertRect:[closeBoxControl bounds]
                                        toView:nil];
  windowBounds = [window_ frame];
  windowBounds = [[window_ contentView] convertRect:windowBounds fromView:nil];
  windowBounds.origin = NSZeroPoint;
  EXPECT_EQ(NSMaxY(closeBoxFrame),
            NSMaxY(windowBounds) -
                kFramedWindowButtonsWithTabStripOffsetFromTop);
  EXPECT_EQ(NSMinX(closeBoxFrame),
            kFramedWindowButtonsWithTabStripOffsetFromLeft);

  miniaturizeControl = [window_ standardWindowButton:NSWindowMiniaturizeButton];
  EXPECT_TRUE(miniaturizeControl);
  miniaturizeFrame = [miniaturizeControl convertRect:[miniaturizeControl bounds]
                                              toView:nil];
  EXPECT_EQ(NSMaxY(miniaturizeFrame),
            NSMaxY(windowBounds) -
                kFramedWindowButtonsWithTabStripOffsetFromTop);
  EXPECT_EQ(NSMinX(miniaturizeFrame),
            NSMaxX(closeBoxFrame) + [window_ windowButtonsInterButtonSpacing]);
  [window_ setWindowController:nil];
}

class FramedBrowserWindowRTLTest : public FramedBrowserWindowTest {
  void SetUp() override {
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    originalAppleTextDirection_ =
        [defaults boolForKey:kAppleTextDirectionDefaultsKey];
    originalRTLWritingDirection_ =
        [defaults boolForKey:kForceRTLWritingDirectionDefaultsKey];
    [defaults setBool:YES forKey:kAppleTextDirectionDefaultsKey];
    [defaults setBool:YES forKey:kForceRTLWritingDirectionDefaultsKey];
    FramedBrowserWindowTest::SetUp();
  }

  void TearDown() override {
    FramedBrowserWindowTest::TearDown();
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:originalAppleTextDirection_
               forKey:kAppleTextDirectionDefaultsKey];
    [defaults setBool:originalRTLWritingDirection_
               forKey:kForceRTLWritingDirectionDefaultsKey];
  }

 private:
  BOOL originalAppleTextDirection_;
  BOOL originalRTLWritingDirection_;
};

// Test to make sure that our window widgets are in the right place.
// Currently, this is the same exact test as above, since RTL button
// layout is behind the ExperimentalMacRTL flag. However, this ensures
// that our calculations are correct for Sierra RTL, which lays out
// the window buttons in reverse by default. See crbug/662079.
TEST_F(FramedBrowserWindowRTLTest, WindowWidgetLocation) {
  BOOL yes = YES;
  BOOL no = NO;

  // First without a tabstrip.
  [window_ close];
  window_ = [[FramedBrowserWindow alloc]
      initWithContentRect:NSMakeRect(0, 0, 800, 600)
              hasTabStrip:NO];
  // Update window layout according to existing layout constraints.
  [window_ layoutIfNeeded];
  id controller = [OCMockObject mockForClass:[BrowserWindowController class]];
  [[[controller stub] andReturnValue:OCMOCK_VALUE(yes)]
      isKindOfClass:[BrowserWindowController class]];
  [[[controller expect] andReturnValue:OCMOCK_VALUE(no)] hasTabStrip];
  [[[controller expect] andReturnValue:OCMOCK_VALUE(yes)] hasTitleBar];
  [[[controller expect] andReturnValue:OCMOCK_VALUE(no)] isTabbedWindow];
  [window_ setWindowController:controller];

  NSView* closeBoxControl = [window_ standardWindowButton:NSWindowCloseButton];
  EXPECT_TRUE(closeBoxControl);
  NSRect closeBoxFrame =
      [closeBoxControl convertRect:[closeBoxControl bounds] toView:nil];
  NSRect windowBounds = [window_ frame];
  windowBounds = [[window_ contentView] convertRect:windowBounds fromView:nil];
  windowBounds.origin = NSZeroPoint;
  EXPECT_EQ(
      NSMaxY(closeBoxFrame),
      NSMaxY(windowBounds) - kFramedWindowButtonsWithoutTabStripOffsetFromTop);
  EXPECT_EQ(NSMinX(closeBoxFrame),
            kFramedWindowButtonsWithoutTabStripOffsetFromLeft);

  NSView* miniaturizeControl =
      [window_ standardWindowButton:NSWindowMiniaturizeButton];
  EXPECT_TRUE(miniaturizeControl);
  NSRect miniaturizeFrame =
      [miniaturizeControl convertRect:[miniaturizeControl bounds] toView:nil];
  EXPECT_LT(NSMaxX(closeBoxFrame), NSMinX(miniaturizeFrame));
  EXPECT_EQ(NSMaxY(miniaturizeFrame),
            NSMaxY(windowBounds) -
                kFramedWindowButtonsWithoutTabStripOffsetFromTop);
  EXPECT_EQ(NSMinX(miniaturizeFrame),
            NSMaxX(closeBoxFrame) + [window_ windowButtonsInterButtonSpacing]);
  [window_ setWindowController:nil];

  // Then with a tabstrip.
  [window_ close];
  window_ = [[FramedBrowserWindow alloc]
             initWithContentRect:NSMakeRect(0, 0, 800, 600)
                     hasTabStrip:YES];
  // Update window layout according to existing layout constraints.
  [window_ layoutIfNeeded];
  controller = [OCMockObject mockForClass:[BrowserWindowController class]];
  [[[controller stub] andReturnValue:OCMOCK_VALUE(yes)]
      isKindOfClass:[BrowserWindowController class]];
  [[[controller expect] andReturnValue:OCMOCK_VALUE(yes)] hasTabStrip];
  [[[controller expect] andReturnValue:OCMOCK_VALUE(no)] hasTitleBar];
  [[[controller expect] andReturnValue:OCMOCK_VALUE(yes)] isTabbedWindow];
  [window_ setWindowController:controller];

  closeBoxControl = [window_ standardWindowButton:NSWindowCloseButton];
  EXPECT_TRUE(closeBoxControl);
  closeBoxFrame = [closeBoxControl convertRect:[closeBoxControl bounds]
                                        toView:nil];
  windowBounds = [window_ frame];
  windowBounds = [[window_ contentView] convertRect:windowBounds fromView:nil];
  windowBounds.origin = NSZeroPoint;
  EXPECT_EQ(NSMaxY(closeBoxFrame),
            NSMaxY(windowBounds) -
                kFramedWindowButtonsWithTabStripOffsetFromTop);
  EXPECT_EQ(NSMinX(closeBoxFrame),
            kFramedWindowButtonsWithTabStripOffsetFromLeft);

  miniaturizeControl = [window_ standardWindowButton:NSWindowMiniaturizeButton];
  EXPECT_TRUE(miniaturizeControl);
  miniaturizeFrame = [miniaturizeControl convertRect:[miniaturizeControl bounds]
                                              toView:nil];
  EXPECT_EQ(NSMaxY(miniaturizeFrame),
            NSMaxY(windowBounds) -
                kFramedWindowButtonsWithTabStripOffsetFromTop);
  EXPECT_EQ(NSMinX(miniaturizeFrame),
            NSMaxX(closeBoxFrame) + [window_ windowButtonsInterButtonSpacing]);
  [window_ setWindowController:nil];
}