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.
5 #import "chrome/browser/ui/cocoa/history_overlay_controller.h"
7 #import <QuartzCore/QuartzCore.h>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/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"
15 class HistoryOverlayControllerTest : public CocoaTest {
17 virtual void SetUp() {
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_];
31 base::scoped_nsobject<NSView> test_view_;
34 // Tests that when the controller is |-dismiss|ed, the animation runs and then
35 // is removed when the animation completes. The view should be added and
36 // removed at the appropriate times.
37 TEST_F(HistoryOverlayControllerTest, DismissClearsAnimationsAndRemovesView) {
38 NSView* content_view = [test_window() contentView];
39 EXPECT_EQ(1u, [[content_view subviews] count]);
41 base::scoped_nsobject<HistoryOverlayController> controller(
42 [[HistoryOverlayController alloc] initForMode:kHistoryOverlayModeBack]);
43 [controller showPanelForView:test_view()];
44 EXPECT_EQ(2u, [[content_view subviews] count]);
46 scoped_ptr<base::MessagePumpNSRunLoop> message_pump(
47 new base::MessagePumpNSRunLoop);
49 id mock = [OCMockObject partialMockForObject:controller];
50 [mock setExpectationOrderMatters:YES];
51 [[[mock expect] andForwardToRealObject] dismiss];
53 // Called after |-animationDidStop:finished:|.
54 base::MessagePumpNSRunLoop* weak_message_pump = message_pump.get();
55 void (^quit_loop)(NSInvocation* invocation) = ^(NSInvocation* invocation) {
56 weak_message_pump->Quit();
58 // Set up the mock to first forward to the real implementation and then call
59 // the above block to quit the run loop.
60 [[[[mock expect] andForwardToRealObject] andDo:quit_loop]
61 animationDidStop:[OCMArg isNotNil] finished:YES];
63 // CAAnimations must be committed within a run loop. It is not sufficient
64 // to commit them and activate the loop after the fact. Schedule a block to
65 // dismiss the controller from within the run loop, which begins the
67 CFRunLoopPerformBlock(CFRunLoopGetCurrent(),
68 kCFRunLoopDefaultMode,
73 // Run the loop, which will dismiss the overlay.
74 message_pump->Run(NULL);
76 EXPECT_OCMOCK_VERIFY(mock);
78 // After the animation runs, there should be no more animations.
79 EXPECT_FALSE([[controller view] animations]);
81 EXPECT_EQ(1u, [[content_view subviews] count]);