OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 "chrome/browser/ui/cocoa/history_overlay_controller.h" | |
6 | |
7 #import <QuartzCore/QuartzCore.h> | |
8 | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/message_pump_mac.h" | |
11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | |
12 #import "third_party/ocmock/gtest_support.h" | |
13 #import "third_party/ocmock/OCMock/OCMock.h" | |
14 | |
15 class HistoryOverlayControllerTest : public CocoaTest { | |
16 public: | |
17 void SetUp() { | |
18 CocoaTest::SetUp(); | |
19 | |
20 // The overlay controller shows the panel in the superview of a given | |
21 // view, so create the given view. | |
22 test_view_.reset([[NSView alloc] initWithFrame:NSMakeRect(10, 10, 10, 10)]); | |
23 [[test_window() contentView] addSubview:test_view_]; | |
24 } | |
25 | |
26 NSView* test_view() { | |
27 return test_view_; | |
28 } | |
29 | |
30 private: | |
31 scoped_nsobject<NSView> test_view_; | |
32 }; | |
33 | |
34 // Tests that the controller's view gets removed from the hierarchy when the | |
35 // controller is deallocated. | |
36 TEST_F(HistoryOverlayControllerTest, RemovedViewWhenDeallocated) { | |
37 NSView* content_view = [test_window() contentView]; | |
38 EXPECT_EQ(1u, [[content_view subviews] count]); | |
39 | |
40 scoped_nsobject<HistoryOverlayController> controller( | |
41 [[HistoryOverlayController alloc] initForMode:kHistoryOverlayModeBack]); | |
42 [controller showPanelForView:test_view()]; | |
43 EXPECT_EQ(2u, [[content_view subviews] count]); | |
44 | |
45 controller.reset(); | |
46 EXPECT_EQ(1u, [[content_view subviews] count]); | |
47 } | |
48 | |
49 // Tests that when the controller is |-dismiss|ed, the animation runs and then | |
50 // is removed when the animation completes. | |
51 TEST_F(HistoryOverlayControllerTest, DismissClearsAnimations) { | |
52 scoped_nsobject<HistoryOverlayController> controller( | |
53 [[HistoryOverlayController alloc] initForMode:kHistoryOverlayModeBack]); | |
54 [controller showPanelForView:test_view()]; | |
55 | |
56 scoped_refptr<base::MessagePumpNSRunLoop> message_pump( | |
57 new base::MessagePumpNSRunLoop); | |
58 | |
59 id mock = [OCMockObject partialMockForObject:controller]; | |
60 [mock setExpectationOrderMatters:YES]; | |
61 [[[mock expect] andForwardToRealObject] dismiss]; | |
62 | |
63 // Called after |-animationDidStop:finished:|. | |
64 void (^quit_loop)(NSInvocation* invocation) = ^(NSInvocation* invocation) { | |
65 message_pump->Quit(); | |
66 }; | |
67 // Set up the mock to first forward to the real implementation and then call | |
68 // the above block to quit the run loop. | |
69 [[[[mock expect] andForwardToRealObject] andDo:quit_loop] | |
Nico
2012/09/04 21:05:15
nifty!
| |
70 animationDidStop:[OCMArg isNotNil] finished:YES]; | |
71 | |
72 // CAAnimations must be committed within a run loop. It is not sufficient | |
73 // to commit them and activate the loop after the fact. Schedule a block to | |
74 // dismiss the controller from within the run loop, which begins the | |
75 // animation. | |
76 CFRunLoopPerformBlock(CFRunLoopGetCurrent(), | |
77 kCFRunLoopDefaultMode, | |
78 ^(void) { | |
79 [mock dismiss]; | |
Nico
2012/09/04 21:05:15
This might be indented 2 too much, but I'm not sur
Robert Sesek
2012/09/04 21:06:36
Blocks are indented 4 spaces from the declaration'
| |
80 }); | |
81 | |
82 // Run the loop, which will dismiss the overlay. | |
83 message_pump->Run(NULL); | |
84 | |
85 EXPECT_OCMOCK_VERIFY(mock); | |
86 | |
87 // After the animation runs, there should be no more animations. | |
88 EXPECT_FALSE([[controller view] animations]); | |
89 } | |
OLD | NEW |