summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/cocoa_test_helper.mm
blob: 1b4c92af421f05d4dc81cd86db91a55d7392febd (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
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
// Copyright (c) 2009 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/cocoa/cocoa_test_helper.h"
#import "chrome/browser/chrome_browser_application_mac.h"
#import "base/logging.h"

@implementation CocoaTestHelperWindow

- (id)initWithContentRect:(NSRect)contentRect {
  return [self initWithContentRect:contentRect
                         styleMask:NSBorderlessWindowMask
                           backing:NSBackingStoreBuffered
                             defer:NO];
}

- (id)init {
  return [self initWithContentRect:NSMakeRect(0, 0, 800, 600)];
}

- (void)dealloc {
  // Just a good place to put breakpoints when having problems with
  // unittests and CocoaTestHelperWindow.
  [super dealloc];
}

- (void)makePretendKeyWindowAndSetFirstResponder:(NSResponder*)responder {
  EXPECT_TRUE([self makeFirstResponder:responder]);
  [self setPretendIsKeyWindow:YES];
}

- (void)clearPretendKeyWindowAndFirstResponder {
  [self setPretendIsKeyWindow:NO];
  EXPECT_TRUE([self makeFirstResponder:NSApp]);
}

- (void)setPretendIsKeyWindow:(BOOL)flag {
  pretendIsKeyWindow_ = flag;
}

- (BOOL)isKeyWindow {
  return pretendIsKeyWindow_;
}

@end

CocoaTest::CocoaTest() : called_tear_down_(false), test_window_(nil) {
  BootstrapCocoa();
  // Set the duration of AppKit-evaluated animations (such as frame changes)
  // to zero for testing purposes. That way they take effect immediately.
  [[NSAnimationContext currentContext] setDuration:0.0];
  // Collect the list of windows that were open when the test started so
  // that we don't wait for them to close in TearDown. Has to be done
  // after BootstrapCocoa is called.
  initial_windows_ = ApplicationWindows();
}

CocoaTest::~CocoaTest() {
  // Must call CocoaTest's teardown from your overrides.
  DCHECK(called_tear_down_);
}

void CocoaTest::BootstrapCocoa() {
  // Look in the framework bundle for resources.
  FilePath path;
  PathService::Get(base::DIR_EXE, &path);
  path = path.Append(chrome::kFrameworkName);
  mac_util::SetOverrideAppBundlePath(path);

  // Bootstrap Cocoa. It's very unhappy without this.
  [CrApplication sharedApplication];
}

void CocoaTest::TearDown() {
  called_tear_down_ = true;
  // Call close on our test_window to clean it up if one was opened.
  [test_window_ close];
  test_window_ = nil;

  // Recycle the pool to clean up any stuff that was put on the
  // autorelease pool due to window or windowcontroller closures.
  // Note that many controls (NSTextFields, NSComboboxes etc) may call
  // performSelector:withDelay: to clean up drag handlers and other things.
  // We must spin the event loop a bit to make sure that everything gets cleaned
  // up correctly. We will wait up to one second for windows to clean themselves
  // up (normally only takes one to two loops through the event loop).
  // Radar 5851458 "Closing a window with a NSTextView in it should get rid of
  // it immediately"
  pool_.Recycle();
  NSDate *start_date = [NSDate date];
  const std::vector<NSWindow*> windows_waiting(ApplicationWindows());

  bool loop = windows_waiting.size() > 0;
  while (loop) {
    {
      // Need an autorelease pool to wrap our event loop.
      base::ScopedNSAutoreleasePool pool;
      NSEvent *next_event = [NSApp nextEventMatchingMask:NSAnyEventMask
                                               untilDate:nil
                                                  inMode:NSDefaultRunLoopMode
                                                 dequeue:YES];
      [NSApp sendEvent:next_event];
      [NSApp updateWindows];
    }
    // Check the windows after we have released the event loop pool so that
    // all retains are cleaned up.
    const std::vector<NSWindow*> current_windows(ApplicationWindows());
    std::vector<NSWindow*> windows_left;
    std::set_difference(current_windows.begin(),
                        current_windows.end(),
                        initial_windows_.begin(),
                        initial_windows_.end(),
                        inserter(windows_left, windows_left.begin()));

    if (windows_left.size() == 0) {
      // All our windows are closed.
      break;
    }
    if ([start_date timeIntervalSinceNow] < -1.0) {
      // Took us over a second to shut down, and windows still exist.
      // Log a failure and continue.
      EXPECT_EQ(0U, windows_left.size());
      for (size_t i = 0; i < windows_left.size(); ++i) {
        const char* desc = [[windows_left[i] description] UTF8String];
        LOG(WARNING) << "Didn't close window " << desc;
      }
      break;
    }
  }
  PlatformTest::TearDown();
}

std::vector<NSWindow*> CocoaTest::ApplicationWindows() {
  // This must NOT retain the windows it is returning.
  std::vector<NSWindow*> windows;
  // Must create a pool here because [NSApp windows] has created an array
  // with retains on all the windows in it.
  base::ScopedNSAutoreleasePool pool;
  NSArray *appWindows = [NSApp windows];
  for (NSWindow *window in appWindows) {
    windows.push_back(window);
  }
  return windows;
}

CocoaTestHelperWindow* CocoaTest::test_window() {
  if (!test_window_) {
    test_window_ = [[CocoaTestHelperWindow alloc] init];
    if (DebugUtil::BeingDebugged()) {
      [test_window_ orderFront:nil];
    } else {
      [test_window_ orderBack:nil];
    }
  }
  return test_window_;
}