Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: chrome/browser/cocoa/keyword_editor_cocoa_controller_unittest.mm

Issue 220040: [Mac] Enable "Edit Search Engines" in Omnibox context menu. (Closed)
Patch Set: nop to make sure I'm logging in. Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "base/scoped_nsautorelease_pool.h"
5 #include "base/scoped_nsobject.h" 6 #include "base/scoped_nsobject.h"
6 #include "chrome/browser/browser.h" 7 #include "chrome/browser/browser.h"
7 #include "chrome/browser/cocoa/browser_test_helper.h" 8 #include "chrome/browser/cocoa/browser_test_helper.h"
8 #include "chrome/browser/cocoa/cocoa_test_helper.h" 9 #include "chrome/browser/cocoa/cocoa_test_helper.h"
9 #import "chrome/browser/cocoa/keyword_editor_cocoa_controller.h" 10 #import "chrome/browser/cocoa/keyword_editor_cocoa_controller.h"
10 #include "chrome/test/testing_profile.h" 11 #include "chrome/test/testing_profile.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/platform_test.h" 13 #include "testing/platform_test.h"
13 14
14 @interface FakeKeywordEditorController : KeywordEditorCocoaController { 15 @interface FakeKeywordEditorController : KeywordEditorCocoaController {
15 @public 16 @public
16 BOOL changed_; 17 BOOL changed_;
17 } 18 }
18 - (KeywordEditorModelObserver*)observer; 19 - (KeywordEditorModelObserver*)observer;
19 @end 20 @end
20 21
21 @implementation FakeKeywordEditorController 22 @implementation FakeKeywordEditorController
22 - (void)modelChanged { 23 - (void)modelChanged {
23 changed_ = YES; 24 changed_ = YES;
24 } 25 }
25 - (KeywordEditorModelObserver*)observer { 26 - (KeywordEditorModelObserver*)observer {
26 return observer_.get(); 27 return observer_.get();
27 } 28 }
28 @end 29 @end
29 30
30 // TODO(rsesek): Figure out a good way to test this class (crbug.com/21640). 31 // TODO(rsesek): Figure out a good way to test this class (crbug.com/21640).
31 32
33 namespace {
34
32 class KeywordEditorCocoaControllerTest : public PlatformTest { 35 class KeywordEditorCocoaControllerTest : public PlatformTest {
33 public: 36 public:
34 void SetUp() { 37 void SetUp() {
35 TestingProfile* profile = 38 TestingProfile* profile =
36 static_cast<TestingProfile*>(browser_helper_.profile()); 39 static_cast<TestingProfile*>(browser_helper_.profile());
37 profile->CreateTemplateURLModel(); 40 profile->CreateTemplateURLModel();
38 controller_.reset( 41 controller_.reset(
39 [[FakeKeywordEditorController alloc] initWithProfile:profile]); 42 [[FakeKeywordEditorController alloc] initWithProfile:profile]);
40 } 43 }
41 44
45 // Helper to count the keyword editors.
46 NSUInteger CountKeywordEditors() {
47 base::ScopedNSAutoreleasePool pool;
48 NSUInteger count = 0;
49 for (NSWindow* window in [NSApp windows]) {
50 id controller = [window windowController];
51 if ([controller isKindOfClass:[KeywordEditorCocoaController class]]) {
52 ++count;
53 }
54 }
55 return count;
56 }
57
42 CocoaTestHelper cocoa_helper_; 58 CocoaTestHelper cocoa_helper_;
43 BrowserTestHelper browser_helper_; 59 BrowserTestHelper browser_helper_;
44 scoped_nsobject<FakeKeywordEditorController> controller_; 60 scoped_nsobject<FakeKeywordEditorController> controller_;
45 }; 61 };
46 62
47 TEST_F(KeywordEditorCocoaControllerTest, TestModelChanged) { 63 TEST_F(KeywordEditorCocoaControllerTest, TestModelChanged) {
48 EXPECT_FALSE(controller_.get()->changed_); 64 EXPECT_FALSE(controller_.get()->changed_);
49 KeywordEditorModelObserver* observer = [controller_ observer]; 65 KeywordEditorModelObserver* observer = [controller_ observer];
50 observer->OnTemplateURLModelChanged(); 66 observer->OnTemplateURLModelChanged();
51 EXPECT_TRUE(controller_.get()->changed_); 67 EXPECT_TRUE(controller_.get()->changed_);
52 } 68 }
69
70 // Test that the window shows correctly, and the controller is
71 // released correctly.
72 TEST_F(KeywordEditorCocoaControllerTest, ShowAndCloseWindow) {
73 // |controller_| is the only reference.
74 EXPECT_EQ([controller_.get() retainCount], 1U);
75
76 // TODO(shess): This test verifies that it leaks no windows. Work
77 // to push this expectation up into the unit testing framework.
78
79 const NSUInteger initial_window_count([[NSApp windows] count]);
80
81 // Explicit autorelease pool here because [NSApp windows] returns an
82 // autorelease immutable NSArray, which otherwise pins the window.
83 {
84 base::ScopedNSAutoreleasePool pool;
85
86 // -showWindow: brings up the window (which retains
87 // |controller_|).
88 [controller_.get() showWindow:nil];
89 EXPECT_EQ([[NSApp windows] count], initial_window_count+1);
90
91 // In regular usage, our scoped reference would not exist and
92 // |controller_| would manage itself once -showWindow: is called.
93 // This means that we need another reference to balance things
94 // out.
95 [controller_.get() retain];
96
97 // Closing the window should leave us with the single reference.
98 [controller_.get() close];
99 }
100
101 // |controller_| still has a handle on the window, drop the last
102 // reference so we can check that we didn't leak a window.
103 EXPECT_EQ([controller_.get() retainCount], 1U);
104 controller_.reset();
105
106 // All created windows should be gone.
107 EXPECT_EQ([[NSApp windows] count], initial_window_count);
108 }
109
110 // Test that +showKeywordEditor brings up the existing editor and
111 // creates one if needed.
112 TEST_F(KeywordEditorCocoaControllerTest, ShowKeywordEditor) {
113 // No outstanding editors.
114 Profile* profile(browser_helper_.profile());
115 KeywordEditorCocoaController* sharedInstance =
116 [KeywordEditorCocoaController sharedInstanceForProfile:profile];
117 EXPECT_TRUE(nil == sharedInstance);
118 EXPECT_EQ(CountKeywordEditors(), 0U);
119
120 const NSUInteger initial_window_count([[NSApp windows] count]);
121
122 // The window unwinds using -autorelease, so we need to introduce an
123 // autorelease pool to really test whether it went away or not.
124 {
125 base::ScopedNSAutoreleasePool pool;
126
127 // +showKeywordEditor: creates a new controller.
128 [KeywordEditorCocoaController showKeywordEditor:profile];
129 sharedInstance =
130 [KeywordEditorCocoaController sharedInstanceForProfile:profile];
131 EXPECT_TRUE(sharedInstance);
132 EXPECT_EQ(CountKeywordEditors(), 1U);
133
134 // Another call doesn't create another controller.
135 [KeywordEditorCocoaController showKeywordEditor:profile];
136 EXPECT_TRUE(sharedInstance ==
137 [KeywordEditorCocoaController sharedInstanceForProfile:profile]);
138 EXPECT_EQ(CountKeywordEditors(), 1U);
139
140 [sharedInstance close];
141 }
142
143 // No outstanding editors.
144 sharedInstance =
145 [KeywordEditorCocoaController sharedInstanceForProfile:profile];
146 EXPECT_TRUE(nil == sharedInstance);
147 EXPECT_EQ(CountKeywordEditors(), 0U);
148
149 // Windows we created should be gone.
150 EXPECT_EQ([[NSApp windows] count], initial_window_count);
151
152 // Get a new editor, should be different from the previous one.
153 [KeywordEditorCocoaController showKeywordEditor:profile];
154 KeywordEditorCocoaController* newSharedInstance =
155 [KeywordEditorCocoaController sharedInstanceForProfile:profile];
156 EXPECT_TRUE(sharedInstance != newSharedInstance);
157 EXPECT_EQ(CountKeywordEditors(), 1U);
158 [newSharedInstance close];
159 }
160
161 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/keyword_editor_cocoa_controller.mm ('k') | chrome/browser/cocoa/preferences_window_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698