1 // Copyright 2014 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 #include "ui/views/test/views_test_helper_mac.h"
7 #import <Cocoa/Cocoa.h>
9 #import "base/mac/scoped_nsautorelease_pool.h"
10 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
11 #include "ui/views/test/event_generator_delegate_mac.h"
12 #include "ui/views/widget/widget.h"
17 ViewsTestHelper* ViewsTestHelper::Create(base::MessageLoopForUI* message_loop,
18 ui::ContextFactory* context_factory) {
19 return new ViewsTestHelperMac;
22 ViewsTestHelperMac::ViewsTestHelperMac()
23 : zero_duration_mode_(new ui::ScopedAnimationDurationScaleMode(
24 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)) {
25 test::InitializeMacEventGeneratorDelegate();
27 // Unbundled applications (those without Info.plist) default to
28 // NSApplicationActivationPolicyProhibited, which prohibits the application
29 // obtaining key status or activating windows without user interaction.
30 [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
33 ViewsTestHelperMac::~ViewsTestHelperMac() {
36 void ViewsTestHelperMac::TearDown() {
37 // Ensure all Widgets are closed explicitly in tests. The Widget may be
38 // hosting a Compositor. If that's torn down after the test ContextFactory
39 // then a lot of confusing use-after-free errors result. In browser tests,
40 // this is handled automatically by views::Widget::CloseAllSecondaryWidgets().
41 // Unit tests on Aura may create Widgets owned by a RootWindow that gets torn
42 // down, but on Mac we need to be more explicit.
43 base::mac::ScopedNSAutoreleasePool pool; // Ensure the NSArray is released.
44 NSArray* native_windows = [NSApp windows];
45 for (NSWindow* window : native_windows)
46 DCHECK(!Widget::GetWidgetForNativeWindow(window)) << "Widget not closed.";