| 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_H_ | 5 #ifndef CHROME_BROWSER_COCOA_COCOA_TEST_HELPER_H_ |
| 6 #define CHROME_BROWSER_COCOA_COCOA_TEST_HELPER_H_ | 6 #define CHROME_BROWSER_COCOA_COCOA_TEST_HELPER_H_ |
| 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" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 // Cocoa controller unit tests. It does several key things: | 40 // Cocoa controller unit tests. It does several key things: |
| 41 // - 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 |
| 42 // - Loads the appropriate bundle so nib loading works. When loading the | 42 // - Loads the appropriate bundle so nib loading works. When loading the |
| 43 // 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()| |
| 44 // 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 |
| 45 // fail. | 45 // fail. |
| 46 // 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 |
| 47 // added. If your test wants one, it can derive from PlatformTest instead of | 47 // added. If your test wants one, it can derive from PlatformTest instead of |
| 48 // testing::Test. | 48 // testing::Test. |
| 49 | 49 |
| 50 class CocoaTestHelper { | 50 // Provides the Cocoa goodness without the extraneous window. |
| 51 // TODO(shess): It might make more sense to have CocoaTest as a |
| 52 // PlatformTest subclass which adds the Cocoa magic, then |
| 53 // CocoaViewTest as a further subclass which provides a convenience |
| 54 // window. |
| 55 class CocoaNoWindowTestHelper { |
| 51 public: | 56 public: |
| 52 CocoaTestHelper() { | 57 CocoaNoWindowTestHelper() { |
| 53 // Look in the framework bundle for resources. | 58 // Look in the framework bundle for resources. |
| 54 FilePath path; | 59 FilePath path; |
| 55 PathService::Get(base::DIR_EXE, &path); | 60 PathService::Get(base::DIR_EXE, &path); |
| 56 path = path.AppendASCII(MAC_BROWSER_APP_NAME); | 61 path = path.AppendASCII(MAC_BROWSER_APP_NAME); |
| 57 path = path.AppendASCII("Contents"); | 62 path = path.AppendASCII("Contents"); |
| 58 path = path.AppendASCII("Frameworks"); | 63 path = path.AppendASCII("Frameworks"); |
| 59 path = path.AppendASCII(MAC_FRAMEWORK_NAME); | 64 path = path.AppendASCII(MAC_FRAMEWORK_NAME); |
| 60 mac_util::SetOverrideAppBundlePath(path); | 65 mac_util::SetOverrideAppBundlePath(path); |
| 61 | 66 |
| 62 // Bootstrap Cocoa. It's very unhappy without this. | 67 // Bootstrap Cocoa. It's very unhappy without this. |
| 63 [NSApplication sharedApplication]; | 68 [NSApplication sharedApplication]; |
| 64 | 69 |
| 65 // Create a window. | 70 // Set the duration of AppKit-evaluated animations (such as frame changes) |
| 71 // to zero for testing purposes. That way they take effect immediately. |
| 72 [[NSAnimationContext currentContext] setDuration:0.0]; |
| 73 } |
| 74 }; |
| 75 |
| 76 class CocoaTestHelper : public CocoaNoWindowTestHelper { |
| 77 public: |
| 78 CocoaTestHelper() { |
| 66 window_.reset([[CocoaTestHelperWindow alloc] init]); | 79 window_.reset([[CocoaTestHelperWindow alloc] init]); |
| 67 if (DebugUtil::BeingDebugged()) { | 80 if (DebugUtil::BeingDebugged()) { |
| 68 [window_ orderFront:nil]; | 81 [window_ orderFront:nil]; |
| 69 } else { | 82 } else { |
| 70 [window_ orderBack:nil]; | 83 [window_ orderBack:nil]; |
| 71 } | 84 } |
| 72 | |
| 73 // Set the duration of AppKit-evaluated animations (such as frame changes) | |
| 74 // to zero for testing purposes. That way they take effect immediately. | |
| 75 [[NSAnimationContext currentContext] setDuration:0.0]; | |
| 76 } | 85 } |
| 77 | 86 |
| 78 // Access the Cocoa window created for the test. | 87 // Access the Cocoa window created for the test. |
| 79 NSWindow* window() const { return window_.get(); } | 88 NSWindow* window() const { return window_.get(); } |
| 80 NSView* contentView() const { return [window_ contentView]; } | 89 NSView* contentView() const { return [window_ contentView]; } |
| 81 | 90 |
| 82 // Set |window_| to pretend to be key and make |aView| its | 91 // Set |window_| to pretend to be key and make |aView| its |
| 83 // firstResponder. | 92 // firstResponder. |
| 84 void makeFirstResponder(NSView* aView) { | 93 void makeFirstResponder(NSView* aView) { |
| 85 [window_ setPretendIsKeyWindow:YES]; | 94 [window_ setPretendIsKeyWindow:YES]; |
| 86 [window_ makeFirstResponder:aView]; | 95 [window_ makeFirstResponder:aView]; |
| 87 } | 96 } |
| 88 | 97 |
| 89 // Clear |window_| firstResponder and stop pretending to be key. | 98 // Clear |window_| firstResponder and stop pretending to be key. |
| 90 void clearFirstResponder() { | 99 void clearFirstResponder() { |
| 91 [window_ makeFirstResponder:nil]; | 100 [window_ makeFirstResponder:nil]; |
| 92 [window_ setPretendIsKeyWindow:NO]; | 101 [window_ setPretendIsKeyWindow:NO]; |
| 93 } | 102 } |
| 94 | 103 |
| 95 private: | 104 private: |
| 96 scoped_nsobject<CocoaTestHelperWindow> window_; | 105 scoped_nsobject<CocoaTestHelperWindow> window_; |
| 97 }; | 106 }; |
| 98 | 107 |
| 99 #endif // CHROME_BROWSER_COCOA_COCOA_TEST_HELPER_H_ | 108 #endif // CHROME_BROWSER_COCOA_COCOA_TEST_HELPER_H_ |
| OLD | NEW |