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 #include <vector> |
9 | 10 |
10 #include "base/debug_util.h" | 11 #include "base/debug_util.h" |
11 #include "base/file_path.h" | 12 #include "base/file_path.h" |
12 #include "base/mac_util.h" | 13 #include "base/mac_util.h" |
13 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #import "base/scoped_nsautorelease_pool.h" |
14 #import "base/scoped_nsobject.h" | 16 #import "base/scoped_nsobject.h" |
| 17 #include "base/scoped_ptr.h" |
15 #include "chrome/common/chrome_constants.h" | 18 #include "chrome/common/chrome_constants.h" |
| 19 #include "testing/platform_test.h" |
16 | 20 |
17 // Background windows normally will not display things such as focus | 21 // Background windows normally will not display things such as focus |
18 // rings. This class allows -isKeyWindow to be manipulated to test | 22 // rings. This class allows -isKeyWindow to be manipulated to test |
19 // such things. | 23 // such things. |
20 | |
21 @interface CocoaTestHelperWindow : NSWindow { | 24 @interface CocoaTestHelperWindow : NSWindow { |
22 @private | 25 @private |
23 BOOL pretendIsKeyWindow_; | 26 BOOL pretendIsKeyWindow_; |
24 } | 27 } |
25 | 28 |
26 // Init a borderless non-deferred window with a backing store. | 29 // Init a borderless non-deferred window with a backing store. |
27 - (id)initWithContentRect:(NSRect)contentRect; | 30 - (id)initWithContentRect:(NSRect)contentRect; |
28 | 31 |
29 // Init with a default frame. | 32 // Init with a default frame. |
30 - (id)init; | 33 - (id)init; |
31 | 34 |
| 35 // Sets the responder passed in as first responder, and sets the window |
| 36 // so that it will return "YES" if asked if it key window. It does not actually |
| 37 // make the window key. |
| 38 - (void)makePretendKeyWindowAndSetFirstResponder:(NSResponder*)responder; |
| 39 |
| 40 // Clears the first responder duty for the window and returns the window |
| 41 // to being non-key. |
| 42 - (void)clearPretendKeyWindowAndFirstResponder; |
| 43 |
32 // Set value to return for -isKeyWindow. | 44 // Set value to return for -isKeyWindow. |
33 - (void)setPretendIsKeyWindow:(BOOL)isKeyWindow; | 45 - (void)setPretendIsKeyWindow:(BOOL)isKeyWindow; |
34 | 46 |
35 - (BOOL)isKeyWindow; | 47 - (BOOL)isKeyWindow; |
36 | 48 |
37 @end | 49 @end |
38 | 50 |
| 51 // A test class that all tests that depend on AppKit should inherit from. |
| 52 // Sets up NSApplication and paths correctly, and makes sure that any windows |
| 53 // created in the test are closed down properly by the test. If you need to |
| 54 // inherit from a different test class, but need to set up the AppKit runtime |
| 55 // environment, you can call BootstrapCocoa directly from your test class. You |
| 56 // will have to deal with windows on your own though. |
| 57 class CocoaTest : public PlatformTest { |
| 58 public: |
| 59 // Sets up AppKit and paths correctly for unit tests. If you can't inherit |
| 60 // from CocoaTest but are going to be using any AppKit features directly, |
| 61 // or indirectly, you should be calling this from the c'tor or SetUp methods |
| 62 // of your test class. |
| 63 static void BootstrapCocoa(); |
| 64 |
| 65 CocoaTest(); |
| 66 virtual ~CocoaTest(); |
| 67 |
| 68 // Must be called by subclasses that override TearDown. We verify that it |
| 69 // is called in our destructor. Takes care of making sure that all windows |
| 70 // are closed off correctly. If your tests open windows, they must be sure |
| 71 // to close them before CocoaTest::TearDown is called. A standard way of doing |
| 72 // this would be to create them in SetUp (after calling CocoaTest::Setup) and |
| 73 // then close them in TearDown before calling CocoaTest::TearDown. |
| 74 virtual void TearDown(); |
| 75 |
| 76 // Retuns a test window that can be used by views and other UI objects |
| 77 // as part of their tests. Is created lazily, and will be closed correctly |
| 78 // in CocoaTest::TearDown. Note that it is a CocoaTestHelperWindow which |
| 79 // has special handling for being Key. |
| 80 CocoaTestHelperWindow* test_window(); |
| 81 |
| 82 private: |
| 83 // Return a vector of currently open windows. Note that it is a vector |
| 84 // instead of an NSArray because we don't want any retains placed on the |
| 85 // windows in it and that the windows in this list may no longer be valid |
| 86 // NSWindows any time after this returns. You can only use the pointer values |
| 87 // in the vector for comparison purposes. |
| 88 static std::vector<NSWindow*> ApplicationWindows(); |
| 89 |
| 90 bool called_tear_down_; |
| 91 base::ScopedNSAutoreleasePool pool_; |
| 92 std::vector<NSWindow*> initial_windows_; |
| 93 // Strong. Lazily created. This isn't wrapped in a scoped_nsobject because |
| 94 // we want to call [close] to destroy it rather than calling [release]. We |
| 95 // want to verify that [close] is actually removing our window and that it's |
| 96 // not hanging around because releaseWhenClosed was set to "no" on the window. |
| 97 // It isn't wrapped in a different wrapper class to close it because we |
| 98 // need to close it at a very specific time; just before we enter our clean |
| 99 // up loop in TearDown. |
| 100 CocoaTestHelperWindow* test_window_; |
| 101 }; |
| 102 |
| 103 // A macro defining a standard set of tests to run on a view. Since we can't |
| 104 // inherit tests, this macro saves us a lot of duplicate code. Handles simply |
| 105 // displaying the view to make sure it won't crash, as well as removing it |
| 106 // from a window. All tests that work with NSView subclasses and/or |
| 107 // NSViewController subclasses should use it. |
| 108 #define TEST_VIEW(test_fixture, view_member_name) \ |
| 109 TEST_F(test_fixture, AddRemove##test_fixture) { \ |
| 110 scoped_nsobject<NSView> view([view_member_name retain]); \ |
| 111 EXPECT_EQ([test_window() contentView], [view_member_name superview]); \ |
| 112 [view_member_name removeFromSuperview]; \ |
| 113 EXPECT_FALSE([view_member_name superview]); \ |
| 114 } \ |
| 115 TEST_F(test_fixture, Display##test_fixture) { \ |
| 116 [view_member_name display]; \ |
| 117 } |
| 118 |
| 119 // The classes below are deprecated and will be removed shortly. Do not write |
| 120 // any tests based on them. |
| 121 |
39 // A class that initializes Cocoa and sets up resources for many of our | 122 // A class that initializes Cocoa and sets up resources for many of our |
40 // Cocoa controller unit tests. It does several key things: | 123 // Cocoa controller unit tests. It does several key things: |
41 // - Creates and displays an empty Cocoa window for views to live in | 124 // - Creates and displays an empty Cocoa window for views to live in |
42 // - Loads the appropriate bundle so nib loading works. When loading the | 125 // - Loads the appropriate bundle so nib loading works. When loading the |
43 // nib in the class being tested, your must use |mac_util::MainAppBundle()| | 126 // 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 | 127 // as the bundle. If you do not specify a bundle, your test will likely |
45 // fail. | 128 // fail. |
46 // It currently does not create an autorelease pool, though that can easily be | 129 // 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 | 130 // added. If your test wants one, it can derive from PlatformTest instead of |
48 // testing::Test. | 131 // testing::Test. |
49 | 132 |
50 // Provides the Cocoa goodness without the extraneous window. | 133 // Provides the Cocoa goodness without the extraneous window. |
51 // TODO(shess): It might make more sense to have CocoaTest as a | 134 |
52 // PlatformTest subclass which adds the Cocoa magic, then | 135 // DEPRECATED |
53 // CocoaViewTest as a further subclass which provides a convenience | 136 // TODO(dmaclach): remove as soon as I can get my other CLs in that get rid |
54 // window. | 137 // of any dependencies on this. 10/30/09 at the latest. |
55 class CocoaNoWindowTestHelper { | 138 class CocoaNoWindowTestHelper { |
56 public: | 139 public: |
57 CocoaNoWindowTestHelper() { | 140 CocoaNoWindowTestHelper() { |
58 // Look in the framework bundle for resources. | 141 // Look in the framework bundle for resources. |
59 FilePath path; | 142 FilePath path; |
60 PathService::Get(base::DIR_EXE, &path); | 143 PathService::Get(base::DIR_EXE, &path); |
61 path = path.Append(chrome::kFrameworkName); | 144 path = path.Append(chrome::kFrameworkName); |
62 mac_util::SetOverrideAppBundlePath(path); | 145 mac_util::SetOverrideAppBundlePath(path); |
63 | 146 |
64 // Bootstrap Cocoa. It's very unhappy without this. | 147 // Bootstrap Cocoa. It's very unhappy without this. |
65 [NSApplication sharedApplication]; | 148 [NSApplication sharedApplication]; |
66 | 149 |
67 // Set the duration of AppKit-evaluated animations (such as frame changes) | 150 // Set the duration of AppKit-evaluated animations (such as frame changes) |
68 // to zero for testing purposes. That way they take effect immediately. | 151 // to zero for testing purposes. That way they take effect immediately. |
69 [[NSAnimationContext currentContext] setDuration:0.0]; | 152 [[NSAnimationContext currentContext] setDuration:0.0]; |
70 } | 153 } |
71 }; | 154 }; |
72 | 155 |
| 156 // DEPRECATED |
| 157 // TODO(dmaclach): remove as soon as I can get my other CLs in that get rid |
| 158 // of any dependencies on this. 10/30/09 at the latest. |
73 class CocoaTestHelper : public CocoaNoWindowTestHelper { | 159 class CocoaTestHelper : public CocoaNoWindowTestHelper { |
74 public: | 160 public: |
75 CocoaTestHelper() { | 161 CocoaTestHelper() { |
76 window_.reset([[CocoaTestHelperWindow alloc] init]); | 162 window_.reset([[CocoaTestHelperWindow alloc] init]); |
77 if (DebugUtil::BeingDebugged()) { | 163 if (DebugUtil::BeingDebugged()) { |
78 [window_ orderFront:nil]; | 164 [window_ orderFront:nil]; |
79 } else { | 165 } else { |
80 [window_ orderBack:nil]; | 166 [window_ orderBack:nil]; |
81 } | 167 } |
82 } | 168 } |
83 | 169 |
84 // Access the Cocoa window created for the test. | 170 // Access the Cocoa window created for the test. |
85 NSWindow* window() const { return window_.get(); } | 171 NSWindow* window() const { return window_.get(); } |
86 NSView* contentView() const { return [window_ contentView]; } | 172 NSView* contentView() const { return [window_ contentView]; } |
87 | 173 |
88 // Set |window_| to pretend to be key and make |aView| its | 174 // Set |window_| to pretend to be key and make |aView| its |
89 // firstResponder. | 175 // firstResponder. |
90 void makeFirstResponder(NSView* aView) { | 176 void makeFirstResponder(NSView* aView) { |
| 177 [window_ makeFirstResponder:aView]; |
91 [window_ setPretendIsKeyWindow:YES]; | 178 [window_ setPretendIsKeyWindow:YES]; |
92 [window_ makeFirstResponder:aView]; | |
93 } | 179 } |
94 | 180 |
95 // Clear |window_| firstResponder and stop pretending to be key. | 181 // Clear |window_| firstResponder and stop pretending to be key. |
96 void clearFirstResponder() { | 182 void clearFirstResponder() { |
| 183 [window_ setPretendIsKeyWindow:NO]; |
97 [window_ makeFirstResponder:nil]; | 184 [window_ makeFirstResponder:nil]; |
98 [window_ setPretendIsKeyWindow:NO]; | |
99 } | 185 } |
100 | 186 |
101 private: | 187 private: |
102 scoped_nsobject<CocoaTestHelperWindow> window_; | 188 scoped_nsobject<CocoaTestHelperWindow> window_; |
103 }; | 189 }; |
104 | 190 |
105 #endif // CHROME_BROWSER_COCOA_COCOA_TEST_HELPER_H_ | 191 #endif // CHROME_BROWSER_COCOA_COCOA_TEST_HELPER_H_ |
OLD | NEW |