blob: 52f18c6f5fb17279da545f6ddaaebf5a45be3fb0 [file] [log] [blame]
[email protected]e3eb0a2f2011-11-09 00:27:081// Copyright (c) 2011 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
[email protected]d47af2172011-12-01 23:56:175#import "base/mac/scoped_sending_event.h"
[email protected]e3eb0a2f2011-11-09 00:27:086
rsesek6a398155c2014-10-20 16:35:157#import <Foundation/Foundation.h>
8
9#include "base/mac/scoped_nsobject.h"
[email protected]e3eb0a2f2011-11-09 00:27:0810#include "testing/gtest/include/gtest/gtest.h"
11
thakisc3175952015-10-09 20:34:1412@interface ScopedSendingEventTestCrApp : NSApplication <CrAppControlProtocol> {
rsesek6a398155c2014-10-20 16:35:1513 @private
14 BOOL handlingSendEvent_;
15}
16@property(nonatomic, assign, getter=isHandlingSendEvent) BOOL handlingSendEvent;
17@end
18
19@implementation ScopedSendingEventTestCrApp
20@synthesize handlingSendEvent = handlingSendEvent_;
21@end
22
[email protected]e3eb0a2f2011-11-09 00:27:0823namespace {
24
rsesek6a398155c2014-10-20 16:35:1525class ScopedSendingEventTest : public testing::Test {
26 public:
27 ScopedSendingEventTest() : app_([[ScopedSendingEventTestCrApp alloc] init]) {
28 NSApp = app_.get();
29 }
dcheng769b4bf2015-01-09 01:47:3230 ~ScopedSendingEventTest() override { NSApp = nil; }
rsesek6a398155c2014-10-20 16:35:1531
32 private:
33 base::scoped_nsobject<ScopedSendingEventTestCrApp> app_;
34};
35
[email protected]e3eb0a2f2011-11-09 00:27:0836// Sets the flag within scope, resets when leaving scope.
rsesek6a398155c2014-10-20 16:35:1537TEST_F(ScopedSendingEventTest, SetHandlingSendEvent) {
[email protected]e3eb0a2f2011-11-09 00:27:0838 id<CrAppProtocol> app = NSApp;
39 EXPECT_FALSE([app isHandlingSendEvent]);
40 {
[email protected]d47af2172011-12-01 23:56:1741 base::mac::ScopedSendingEvent is_handling_send_event;
[email protected]e3eb0a2f2011-11-09 00:27:0842 EXPECT_TRUE([app isHandlingSendEvent]);
43 }
44 EXPECT_FALSE([app isHandlingSendEvent]);
45}
46
47// Nested call restores previous value rather than resetting flag.
rsesek6a398155c2014-10-20 16:35:1548TEST_F(ScopedSendingEventTest, NestedSetHandlingSendEvent) {
[email protected]e3eb0a2f2011-11-09 00:27:0849 id<CrAppProtocol> app = NSApp;
50 EXPECT_FALSE([app isHandlingSendEvent]);
51 {
[email protected]d47af2172011-12-01 23:56:1752 base::mac::ScopedSendingEvent is_handling_send_event;
[email protected]e3eb0a2f2011-11-09 00:27:0853 EXPECT_TRUE([app isHandlingSendEvent]);
54 {
[email protected]d47af2172011-12-01 23:56:1755 base::mac::ScopedSendingEvent nested_is_handling_send_event;
[email protected]e3eb0a2f2011-11-09 00:27:0856 EXPECT_TRUE([app isHandlingSendEvent]);
57 }
58 EXPECT_TRUE([app isHandlingSendEvent]);
59 }
60 EXPECT_FALSE([app isHandlingSendEvent]);
61}
62
63} // namespace