| 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 #include "base/scoped_nsautorelease_pool.h" | 5 #include "base/scoped_nsautorelease_pool.h" |
| 6 #include "base/scoped_nsobject.h" | 6 #include "base/scoped_nsobject.h" |
| 7 #include "chrome/browser/browser.h" | 7 #include "chrome/browser/browser.h" |
| 8 #include "chrome/browser/cocoa/browser_test_helper.h" | 8 #include "chrome/browser/cocoa/browser_test_helper.h" |
| 9 #include "chrome/browser/cocoa/cocoa_test_helper.h" | 9 #include "chrome/browser/cocoa/cocoa_test_helper.h" |
| 10 #import "chrome/browser/cocoa/keyword_editor_cocoa_controller.h" | 10 #import "chrome/browser/cocoa/keyword_editor_cocoa_controller.h" |
| 11 #include "chrome/test/testing_profile.h" | 11 #include "chrome/test/testing_profile.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "testing/platform_test.h" | 13 #include "testing/platform_test.h" |
| 14 | 14 |
| 15 @interface FakeKeywordEditorController : KeywordEditorCocoaController { | 15 @interface FakeKeywordEditorController : KeywordEditorCocoaController { |
| 16 @public | 16 @public |
| 17 BOOL changed_; | 17 BOOL modelChanged_; |
| 18 } | 18 } |
| 19 - (void)modelChanged; |
| 20 - (BOOL)hasModelChanged; |
| 19 - (KeywordEditorModelObserver*)observer; | 21 - (KeywordEditorModelObserver*)observer; |
| 20 @end | 22 @end |
| 21 | 23 |
| 22 @implementation FakeKeywordEditorController | 24 @implementation FakeKeywordEditorController |
| 25 |
| 23 - (void)modelChanged { | 26 - (void)modelChanged { |
| 24 changed_ = YES; | 27 modelChanged_ = YES; |
| 25 } | 28 } |
| 29 |
| 30 - (BOOL)hasModelChanged { |
| 31 return modelChanged_; |
| 32 } |
| 33 |
| 26 - (KeywordEditorModelObserver*)observer { | 34 - (KeywordEditorModelObserver*)observer { |
| 27 return observer_.get(); | 35 return observer_.get(); |
| 28 } | 36 } |
| 37 |
| 29 @end | 38 @end |
| 30 | 39 |
| 31 // TODO(rsesek): Figure out a good way to test this class (crbug.com/21640). | 40 // TODO(rsesek): Figure out a good way to test this class (crbug.com/21640). |
| 32 | 41 |
| 33 namespace { | 42 namespace { |
| 34 | 43 |
| 35 class KeywordEditorCocoaControllerTest : public PlatformTest { | 44 class KeywordEditorCocoaControllerTest : public CocoaTest { |
| 36 public: | 45 public: |
| 37 void SetUp() { | 46 virtual void SetUp() { |
| 47 CocoaTest::SetUp(); |
| 38 TestingProfile* profile = | 48 TestingProfile* profile = |
| 39 static_cast<TestingProfile*>(browser_helper_.profile()); | 49 static_cast<TestingProfile*>(browser_helper_.profile()); |
| 40 profile->CreateTemplateURLModel(); | 50 profile->CreateTemplateURLModel(); |
| 41 controller_.reset( | 51 |
| 42 [[FakeKeywordEditorController alloc] initWithProfile:profile]); | 52 controller_ = [[FakeKeywordEditorController alloc] initWithProfile:profile]; |
| 53 } |
| 54 |
| 55 virtual void TearDown() { |
| 56 [controller_ close]; |
| 57 CocoaTest::TearDown(); |
| 43 } | 58 } |
| 44 | 59 |
| 45 // Helper to count the keyword editors. | 60 // Helper to count the keyword editors. |
| 46 NSUInteger CountKeywordEditors() { | 61 NSUInteger CountKeywordEditors() { |
| 47 base::ScopedNSAutoreleasePool pool; | 62 base::ScopedNSAutoreleasePool pool; |
| 48 NSUInteger count = 0; | 63 NSUInteger count = 0; |
| 49 for (NSWindow* window in [NSApp windows]) { | 64 for (NSWindow* window in [NSApp windows]) { |
| 50 id controller = [window windowController]; | 65 id controller = [window windowController]; |
| 51 if ([controller isKindOfClass:[KeywordEditorCocoaController class]]) { | 66 if ([controller isKindOfClass:[KeywordEditorCocoaController class]]) { |
| 52 ++count; | 67 ++count; |
| 53 } | 68 } |
| 54 } | 69 } |
| 55 return count; | 70 return count; |
| 56 } | 71 } |
| 57 | 72 |
| 58 CocoaTestHelper cocoa_helper_; | |
| 59 BrowserTestHelper browser_helper_; | 73 BrowserTestHelper browser_helper_; |
| 60 scoped_nsobject<FakeKeywordEditorController> controller_; | 74 FakeKeywordEditorController* controller_; |
| 61 }; | 75 }; |
| 62 | 76 |
| 63 TEST_F(KeywordEditorCocoaControllerTest, TestModelChanged) { | 77 TEST_F(KeywordEditorCocoaControllerTest, TestModelChanged) { |
| 64 EXPECT_FALSE(controller_.get()->changed_); | 78 EXPECT_FALSE([controller_ hasModelChanged]); |
| 65 KeywordEditorModelObserver* observer = [controller_ observer]; | 79 KeywordEditorModelObserver* observer = [controller_ observer]; |
| 66 observer->OnTemplateURLModelChanged(); | 80 observer->OnTemplateURLModelChanged(); |
| 67 EXPECT_TRUE(controller_.get()->changed_); | 81 EXPECT_TRUE([controller_ hasModelChanged]); |
| 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 } | 82 } |
| 109 | 83 |
| 110 // Test that +showKeywordEditor brings up the existing editor and | 84 // Test that +showKeywordEditor brings up the existing editor and |
| 111 // creates one if needed. | 85 // creates one if needed. |
| 112 TEST_F(KeywordEditorCocoaControllerTest, ShowKeywordEditor) { | 86 TEST_F(KeywordEditorCocoaControllerTest, ShowKeywordEditor) { |
| 113 // No outstanding editors. | 87 // No outstanding editors. |
| 114 Profile* profile(browser_helper_.profile()); | 88 Profile* profile(browser_helper_.profile()); |
| 115 KeywordEditorCocoaController* sharedInstance = | 89 KeywordEditorCocoaController* sharedInstance = |
| 116 [KeywordEditorCocoaController sharedInstanceForProfile:profile]; | 90 [KeywordEditorCocoaController sharedInstanceForProfile:profile]; |
| 117 EXPECT_TRUE(nil == sharedInstance); | 91 EXPECT_TRUE(nil == sharedInstance); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 // Get a new editor, should be different from the previous one. | 126 // Get a new editor, should be different from the previous one. |
| 153 [KeywordEditorCocoaController showKeywordEditor:profile]; | 127 [KeywordEditorCocoaController showKeywordEditor:profile]; |
| 154 KeywordEditorCocoaController* newSharedInstance = | 128 KeywordEditorCocoaController* newSharedInstance = |
| 155 [KeywordEditorCocoaController sharedInstanceForProfile:profile]; | 129 [KeywordEditorCocoaController sharedInstanceForProfile:profile]; |
| 156 EXPECT_TRUE(sharedInstance != newSharedInstance); | 130 EXPECT_TRUE(sharedInstance != newSharedInstance); |
| 157 EXPECT_EQ(CountKeywordEditors(), 1U); | 131 EXPECT_EQ(CountKeywordEditors(), 1U); |
| 158 [newSharedInstance close]; | 132 [newSharedInstance close]; |
| 159 } | 133 } |
| 160 | 134 |
| 161 } // namespace | 135 } // namespace |
| OLD | NEW |