1// Copyright 2015 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 "ui/base/test/nswindow_fullscreen_notification_waiter.h"
6
7@interface NSWindowFullscreenNotificationWaiter ()
8// Exit the RunLoop if there is one and the counts being tracked match.
9- (void)maybeQuitForChangedArg:(int*)changedArg;
10- (void)onEnter:(NSNotification*)notification;
11- (void)onExit:(NSNotification*)notification;
12@end
13
14@implementation NSWindowFullscreenNotificationWaiter
15
16@synthesize enterCount = _enterCount;
17@synthesize exitCount = _exitCount;
18
19- (instancetype)initWithWindow:(NSWindow*)window {
20  if ((self = [super init])) {
21    _window.reset([window retain]);
22    NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
23    [defaultCenter addObserver:self
24                      selector:@selector(onEnter:)
25                          name:NSWindowDidEnterFullScreenNotification
26                        object:window];
27    [defaultCenter addObserver:self
28                      selector:@selector(onExit:)
29                          name:NSWindowDidExitFullScreenNotification
30                        object:window];
31  }
32  return self;
33}
34
35- (void)dealloc {
36  DCHECK(!_runLoop);
37  [[NSNotificationCenter defaultCenter] removeObserver:self];
38  [super dealloc];
39}
40
41- (void)waitForEnterCount:(int)enterCount exitCount:(int)exitCount {
42  if (_enterCount >= enterCount && _exitCount >= exitCount)
43    return;
44
45  _targetEnterCount = enterCount;
46  _targetExitCount = exitCount;
47  _runLoop = std::make_unique<base::RunLoop>();
48  _runLoop->Run();
49  _runLoop.reset();
50}
51
52- (void)maybeQuitForChangedArg:(int*)changedArg {
53  ++*changedArg;
54  if (!_runLoop)
55    return;
56
57  if (_enterCount >= _targetEnterCount && _exitCount >= _targetExitCount)
58    _runLoop->Quit();
59}
60
61- (void)onEnter:(NSNotification*)notification {
62  [self maybeQuitForChangedArg:&_enterCount];
63}
64
65- (void)onExit:(NSNotification*)notification {
66  [self maybeQuitForChangedArg:&_exitCount];
67}
68
69@end
70