[email protected] | e3eb0a2f | 2011-11-09 00:27:08 | [diff] [blame] | 1 | // 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] | d47af217 | 2011-12-01 23:56:17 | [diff] [blame] | 5 | #import "base/mac/scoped_sending_event.h" |
[email protected] | e3eb0a2f | 2011-11-09 00:27:08 | [diff] [blame] | 6 | |
rsesek | 6a398155c | 2014-10-20 16:35:15 | [diff] [blame] | 7 | #import <Foundation/Foundation.h> |
8 | |||||
9 | #include "base/mac/scoped_nsobject.h" | ||||
[email protected] | e3eb0a2f | 2011-11-09 00:27:08 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
11 | |||||
thakis | c317595 | 2015-10-09 20:34:14 | [diff] [blame] | 12 | @interface ScopedSendingEventTestCrApp : NSApplication <CrAppControlProtocol> { |
rsesek | 6a398155c | 2014-10-20 16:35:15 | [diff] [blame] | 13 | @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] | e3eb0a2f | 2011-11-09 00:27:08 | [diff] [blame] | 23 | namespace { |
24 | |||||
rsesek | 6a398155c | 2014-10-20 16:35:15 | [diff] [blame] | 25 | class ScopedSendingEventTest : public testing::Test { |
26 | public: | ||||
27 | ScopedSendingEventTest() : app_([[ScopedSendingEventTestCrApp alloc] init]) { | ||||
28 | NSApp = app_.get(); | ||||
29 | } | ||||
dcheng | 769b4bf | 2015-01-09 01:47:32 | [diff] [blame] | 30 | ~ScopedSendingEventTest() override { NSApp = nil; } |
rsesek | 6a398155c | 2014-10-20 16:35:15 | [diff] [blame] | 31 | |
32 | private: | ||||
33 | base::scoped_nsobject<ScopedSendingEventTestCrApp> app_; | ||||
34 | }; | ||||
35 | |||||
[email protected] | e3eb0a2f | 2011-11-09 00:27:08 | [diff] [blame] | 36 | // Sets the flag within scope, resets when leaving scope. |
rsesek | 6a398155c | 2014-10-20 16:35:15 | [diff] [blame] | 37 | TEST_F(ScopedSendingEventTest, SetHandlingSendEvent) { |
[email protected] | e3eb0a2f | 2011-11-09 00:27:08 | [diff] [blame] | 38 | id<CrAppProtocol> app = NSApp; |
39 | EXPECT_FALSE([app isHandlingSendEvent]); | ||||
40 | { | ||||
[email protected] | d47af217 | 2011-12-01 23:56:17 | [diff] [blame] | 41 | base::mac::ScopedSendingEvent is_handling_send_event; |
[email protected] | e3eb0a2f | 2011-11-09 00:27:08 | [diff] [blame] | 42 | EXPECT_TRUE([app isHandlingSendEvent]); |
43 | } | ||||
44 | EXPECT_FALSE([app isHandlingSendEvent]); | ||||
45 | } | ||||
46 | |||||
47 | // Nested call restores previous value rather than resetting flag. | ||||
rsesek | 6a398155c | 2014-10-20 16:35:15 | [diff] [blame] | 48 | TEST_F(ScopedSendingEventTest, NestedSetHandlingSendEvent) { |
[email protected] | e3eb0a2f | 2011-11-09 00:27:08 | [diff] [blame] | 49 | id<CrAppProtocol> app = NSApp; |
50 | EXPECT_FALSE([app isHandlingSendEvent]); | ||||
51 | { | ||||
[email protected] | d47af217 | 2011-12-01 23:56:17 | [diff] [blame] | 52 | base::mac::ScopedSendingEvent is_handling_send_event; |
[email protected] | e3eb0a2f | 2011-11-09 00:27:08 | [diff] [blame] | 53 | EXPECT_TRUE([app isHandlingSendEvent]); |
54 | { | ||||
[email protected] | d47af217 | 2011-12-01 23:56:17 | [diff] [blame] | 55 | base::mac::ScopedSendingEvent nested_is_handling_send_event; |
[email protected] | e3eb0a2f | 2011-11-09 00:27:08 | [diff] [blame] | 56 | EXPECT_TRUE([app isHandlingSendEvent]); |
57 | } | ||||
58 | EXPECT_TRUE([app isHandlingSendEvent]); | ||||
59 | } | ||||
60 | EXPECT_FALSE([app isHandlingSendEvent]); | ||||
61 | } | ||||
62 | |||||
63 | } // namespace |