• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2011 The Chromium Authors
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 "base/mac/scoped_sending_event.h"
6
7#import <Foundation/Foundation.h>
8
9#include "base/mac/scoped_nsobject.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12#ifdef LEAK_SANITIZER
13#include <sanitizer/lsan_interface.h>
14#endif
15
16@interface ScopedSendingEventTestCrApp : NSApplication <CrAppControlProtocol> {
17 @private
18  BOOL _handlingSendEvent;
19}
20@property(nonatomic, assign, getter=isHandlingSendEvent) BOOL handlingSendEvent;
21@end
22
23@implementation ScopedSendingEventTestCrApp
24@synthesize handlingSendEvent = _handlingSendEvent;
25@end
26
27namespace {
28
29class ScopedSendingEventTest : public testing::Test {
30 public:
31  ScopedSendingEventTest() {
32#ifdef LEAK_SANITIZER
33    // NSApplication's `init` creates a helper object and writes it to an
34    // AppKit-owned static unconditionally. This is not cleaned up on
35    // NSApplication dealloc.
36    // When we create a new NSApplication, as we do when we run multiple
37    // tests in this suite, a new object is created and stomps on the old
38    // static.
39    // This needs a scoped disabler instead of just ignoring the app
40    // object since the leak is a side-effect of object creation.
41    __lsan::ScopedDisabler disable;
42#endif
43    app_.reset([[ScopedSendingEventTestCrApp alloc] init]);
44    NSApp = app_.get();
45  }
46  ~ScopedSendingEventTest() override { NSApp = nil; }
47
48 private:
49  base::scoped_nsobject<ScopedSendingEventTestCrApp> app_;
50};
51
52// Sets the flag within scope, resets when leaving scope.
53TEST_F(ScopedSendingEventTest, SetHandlingSendEvent) {
54  id<CrAppProtocol> app = NSApp;
55  EXPECT_FALSE([app isHandlingSendEvent]);
56  {
57    base::mac::ScopedSendingEvent is_handling_send_event;
58    EXPECT_TRUE([app isHandlingSendEvent]);
59  }
60  EXPECT_FALSE([app isHandlingSendEvent]);
61}
62
63// Nested call restores previous value rather than resetting flag.
64TEST_F(ScopedSendingEventTest, NestedSetHandlingSendEvent) {
65  id<CrAppProtocol> app = NSApp;
66  EXPECT_FALSE([app isHandlingSendEvent]);
67  {
68    base::mac::ScopedSendingEvent is_handling_send_event;
69    EXPECT_TRUE([app isHandlingSendEvent]);
70    {
71      base::mac::ScopedSendingEvent nested_is_handling_send_event;
72      EXPECT_TRUE([app isHandlingSendEvent]);
73    }
74    EXPECT_TRUE([app isHandlingSendEvent]);
75  }
76  EXPECT_FALSE([app isHandlingSendEvent]);
77}
78
79}  // namespace
80