| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_COCOA_COCOA_TEST_HELPER | 5 #ifndef CHROME_BROWSER_COCOA_COCOA_TEST_HELPER |
| 6 #define CHROME_BROWSER_COCOA_COCOA_TEST_HELPER | 6 #define CHROME_BROWSER_COCOA_COCOA_TEST_HELPER |
| 7 | 7 |
| 8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
| 9 | 9 |
| 10 #include "base/debug_util.h" | 10 #include "base/debug_util.h" |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/mac_util.h" | 12 #include "base/mac_util.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #import "base/scoped_nsobject.h" | 14 #import "base/scoped_nsobject.h" |
| 15 #include "chrome/common/mac_app_names.h" | 15 #include "chrome/common/mac_app_names.h" |
| 16 | 16 |
| 17 // Background windows normally will not display things such as focus |
| 18 // rings. This class allows -isKeyWindow to be manipulated to test |
| 19 // such things. |
| 20 |
| 21 @interface CocoaTestHelperWindow : NSWindow { |
| 22 @private |
| 23 BOOL pretendIsKeyWindow_; |
| 24 } |
| 25 |
| 26 // Init a borderless non-defered window with backing store. |
| 27 - (id)initWithContentRect:(NSRect)contentRect; |
| 28 |
| 29 // Init with a default frame. |
| 30 - (id)init; |
| 31 |
| 32 // Set value to return for -isKeyWindow. |
| 33 - (void)setPretendIsKeyWindow:(BOOL)isKeyWindow; |
| 34 |
| 35 - (BOOL)isKeyWindow; |
| 36 |
| 37 @end |
| 38 |
| 17 // A class that initializes Cocoa and sets up resources for many of our | 39 // A class that initializes Cocoa and sets up resources for many of our |
| 18 // Cocoa controller unit tests. It does several key things: | 40 // Cocoa controller unit tests. It does several key things: |
| 19 // - Creates and displays an empty Cocoa window for views to live in | 41 // - Creates and displays an empty Cocoa window for views to live in |
| 20 // - Loads the appropriate bundle so nib loading works. When loading the | 42 // - Loads the appropriate bundle so nib loading works. When loading the |
| 21 // nib in the class being tested, your must use |mac_util::MainAppBundle()| | 43 // nib in the class being tested, your must use |mac_util::MainAppBundle()| |
| 22 // as the bundle. If you do not specify a bundle, your test will likely | 44 // as the bundle. If you do not specify a bundle, your test will likely |
| 23 // fail. | 45 // fail. |
| 24 // It currently does not create an autorelease pool, though that can easily be | 46 // It currently does not create an autorelease pool, though that can easily be |
| 25 // added. If your test wants one, it can derrive from PlatformTest instead of | 47 // added. If your test wants one, it can derrive from PlatformTest instead of |
| 26 // testing::Test. | 48 // testing::Test. |
| 27 | 49 |
| 28 class CocoaTestHelper { | 50 class CocoaTestHelper { |
| 29 public: | 51 public: |
| 30 CocoaTestHelper() { | 52 CocoaTestHelper() { |
| 31 // Look in the Chromium app bundle for resources. | 53 // Look in the Chromium app bundle for resources. |
| 32 FilePath path; | 54 FilePath path; |
| 33 PathService::Get(base::DIR_EXE, &path); | 55 PathService::Get(base::DIR_EXE, &path); |
| 34 path = path.AppendASCII(MAC_BROWSER_APP_NAME); | 56 path = path.AppendASCII(MAC_BROWSER_APP_NAME); |
| 35 mac_util::SetOverrideAppBundlePath(path); | 57 mac_util::SetOverrideAppBundlePath(path); |
| 36 | 58 |
| 37 // Bootstrap Cocoa. It's very unhappy without this. | 59 // Bootstrap Cocoa. It's very unhappy without this. |
| 38 [NSApplication sharedApplication]; | 60 [NSApplication sharedApplication]; |
| 39 | 61 |
| 40 // Create a window. | 62 // Create a window. |
| 41 NSRect frame = NSMakeRect(0, 0, 800, 600); | 63 window_.reset([[CocoaTestHelperWindow alloc] init]); |
| 42 window_.reset([[NSWindow alloc] initWithContentRect:frame | |
| 43 styleMask:0 | |
| 44 backing:NSBackingStoreBuffered | |
| 45 defer:NO]); | |
| 46 if (DebugUtil::BeingDebugged()) { | 64 if (DebugUtil::BeingDebugged()) { |
| 47 [window_ orderFront:nil]; | 65 [window_ orderFront:nil]; |
| 48 } else { | 66 } else { |
| 49 [window_ orderBack:nil]; | 67 [window_ orderBack:nil]; |
| 50 } | 68 } |
| 51 | 69 |
| 52 // Set the duration of AppKit-evaluated animations (such as frame changes) | 70 // Set the duration of AppKit-evaluated animations (such as frame changes) |
| 53 // to zero for testing purposes. That way they take effect immediately. | 71 // to zero for testing purposes. That way they take effect immediately. |
| 54 [[NSAnimationContext currentContext] setDuration:0.0]; | 72 [[NSAnimationContext currentContext] setDuration:0.0]; |
| 55 } | 73 } |
| 56 | 74 |
| 57 // Access the Cocoa window created for the test. | 75 // Access the Cocoa window created for the test. |
| 58 NSWindow* window() const { return window_.get(); } | 76 NSWindow* window() const { return window_.get(); } |
| 59 NSView* contentView() const { return [window_ contentView]; } | 77 NSView* contentView() const { return [window_ contentView]; } |
| 60 | 78 |
| 79 // Set |window_| to pretend to be key and make |aView| its |
| 80 // firstResponder. |
| 81 void makeFirstResponder(NSView* aView) { |
| 82 [window_ setPretendIsKeyWindow:YES]; |
| 83 [window_ makeFirstResponder:aView]; |
| 84 } |
| 85 |
| 86 // Clear |window_| firstResponder and stop pretending to be key. |
| 87 void clearFirstResponder() { |
| 88 [window_ makeFirstResponder:nil]; |
| 89 [window_ setPretendIsKeyWindow:NO]; |
| 90 } |
| 91 |
| 61 private: | 92 private: |
| 62 scoped_nsobject<NSWindow> window_; | 93 scoped_nsobject<CocoaTestHelperWindow> window_; |
| 63 }; | 94 }; |
| 64 | 95 |
| 65 #endif // CHROME_BROWSER_COCOA_COCOA_TEST_HELPER | 96 #endif // CHROME_BROWSER_COCOA_COCOA_TEST_HELPER |
| OLD | NEW |