diff options
-rw-r--r-- | chrome/browser/cocoa/bookmark_editor_controller.h | 1 | ||||
-rw-r--r-- | chrome/browser/cocoa/bookmark_editor_controller.mm | 34 | ||||
-rw-r--r-- | chrome/browser/cocoa/bookmark_editor_controller_unittest.mm | 130 |
3 files changed, 87 insertions, 78 deletions
diff --git a/chrome/browser/cocoa/bookmark_editor_controller.h b/chrome/browser/cocoa/bookmark_editor_controller.h index 1dd9067..055b7fd 100644 --- a/chrome/browser/cocoa/bookmark_editor_controller.h +++ b/chrome/browser/cocoa/bookmark_editor_controller.h @@ -34,7 +34,6 @@ scoped_nsobject<NSString> initialName_; scoped_nsobject<NSString> initialUrl_; - scoped_nsobject<BookmarkTreeBrowserCell> currentEditCell_; } - (id)initWithParentWindow:(NSWindow*)parentWindow diff --git a/chrome/browser/cocoa/bookmark_editor_controller.mm b/chrome/browser/cocoa/bookmark_editor_controller.mm index 93ea8e3..f675fc3 100644 --- a/chrome/browser/cocoa/bookmark_editor_controller.mm +++ b/chrome/browser/cocoa/bookmark_editor_controller.mm @@ -172,6 +172,19 @@ int IndexOfFolderChild(const BookmarkNode* child_node) { } } +- (void)windowWillClose:(NSNotification *)notification { + // If a folder name cell is being edited then force it to end editing + // so that any changes are recorded. + [[self window] makeFirstResponder:nil]; + + // This is probably unnecessary but it feels cleaner since the + // delegate of a text field can be automatically registered for + // notifications. + [nameField_ setDelegate:nil]; + [urlField_ setDelegate:nil]; + [self autorelease]; +} + /* TODO(jrg): // Implementing this informal protocol allows us to open the sheet // somewhere other than at the top of the window. NOTE: this means @@ -248,7 +261,6 @@ int IndexOfFolderChild(const BookmarkNode* child_node) { [cell setTarget:self]; [cell setAction:@selector(cellEditingCompleted:)]; [cell setSendsActionOnEndEditing:YES]; - currentEditCell_.reset([cell retain]); NSMatrix* matrix = [cell matrix]; // Set the delegate so that we get called when editing wants to complete. [matrix setDelegate:self]; @@ -277,7 +289,6 @@ int IndexOfFolderChild(const BookmarkNode* child_node) { BookmarkModel* model = profile_->GetBookmarkModel(); NSString* newTitle = [cell title]; model->SetTitle(bookmarkNode, base::SysNSStringToWide(newTitle)); - currentEditCell_.reset(); } - (void)browserDoubleClicked:(id)sender { @@ -368,24 +379,7 @@ int IndexOfFolderChild(const BookmarkNode* child_node) { - (void)didEndSheet:(NSWindow*)sheet returnCode:(int)returnCode contextInfo:(void*)contextInfo { - // If a folder name cell is being edited then force it to end editing - // so that any changes are recorded. - BookmarkTreeBrowserCell* currentEditCell = currentEditCell_.get(); - if (currentEditCell) { - [self saveFolderNameForCell:currentEditCell]; - currentEditCell_.reset(); - } - // This is probably unnecessary but it feels cleaner since the - // delegate of a text field can be automatically registered for - // notifications. - [nameField_ setDelegate:nil]; - [urlField_ setDelegate:nil]; - - [[self window] orderOut:self]; - - // BookmarkEditor::Show() will create us then run away. Unusually - // for a controller, we are responsible for deallocating ourself. - [self autorelease]; + [sheet close]; } #pragma mark For Unit Test Use Only diff --git a/chrome/browser/cocoa/bookmark_editor_controller_unittest.mm b/chrome/browser/cocoa/bookmark_editor_controller_unittest.mm index 0e1e9b2..9b25f0f 100644 --- a/chrome/browser/cocoa/bookmark_editor_controller_unittest.mm +++ b/chrome/browser/cocoa/bookmark_editor_controller_unittest.mm @@ -12,17 +12,17 @@ #include "testing/gtest/include/gtest/gtest.h" #include "testing/platform_test.h" -class BookmarkEditorControllerTest : public PlatformTest { +class BookmarkEditorControllerTest : public CocoaTest { public: - CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... BrowserTestHelper helper_; const BookmarkNode* default_node_; const BookmarkNode* default_parent_; const char* default_name_; std::wstring default_title_; - scoped_nsobject<BookmarkEditorController> default_controller_; + BookmarkEditorController* default_controller_; - BookmarkEditorControllerTest() { + virtual void SetUp() { + CocoaTest::SetUp(); BookmarkModel* model = helper_.profile()->GetBookmarkModel(); default_parent_ = model->GetBookmarkBarNode(); default_name_ = "http://www.zim-bop-a-dee.com/"; @@ -30,15 +30,20 @@ class BookmarkEditorControllerTest : public PlatformTest { const BookmarkNode* default_node = model->AddURL(default_parent_, 0, default_title_, GURL(default_name_)); - default_controller_.reset([[BookmarkEditorController alloc] - initWithParentWindow:cocoa_helper_.window() - profile:helper_.profile() - parent:default_parent_ - node:default_node - configuration:BookmarkEditor::NO_TREE - handler:nil]); + default_controller_ = [[BookmarkEditorController alloc] + initWithParentWindow:test_window() + profile:helper_.profile() + parent:default_parent_ + node:default_node + configuration:BookmarkEditor::NO_TREE + handler:nil]; [default_controller_ window]; // Forces a nib load } + + virtual void TearDown() { + [default_controller_ close]; + CocoaTest::TearDown(); + } }; TEST_F(BookmarkEditorControllerTest, NoNodeNoTree) { @@ -46,19 +51,20 @@ TEST_F(BookmarkEditorControllerTest, NoNodeNoTree) { const BookmarkNode* parent = model->GetBookmarkBarNode(); const BookmarkNode* node = NULL; - scoped_nsobject<BookmarkEditorController> - controller([[BookmarkEditorController alloc] - initWithParentWindow:cocoa_helper_.window() - profile:helper_.profile() - parent:parent - node:node - configuration:BookmarkEditor::NO_TREE - handler:nil]); + BookmarkEditorController* controller = + [[BookmarkEditorController alloc] + initWithParentWindow:test_window() + profile:helper_.profile() + parent:parent + node:node + configuration:BookmarkEditor::NO_TREE + handler:nil]; EXPECT_NE((NSWindow*)nil, [controller window]); // Forces a nib load EXPECT_EQ(@"", [controller displayName]); EXPECT_EQ(@"", [controller displayURL]); EXPECT_FALSE([controller okButtonEnabled]); + [controller close]; } TEST_F(BookmarkEditorControllerTest, YesNodeShowTree) { @@ -68,14 +74,14 @@ TEST_F(BookmarkEditorControllerTest, YesNodeShowTree) { const BookmarkNode* node = model->AddURL(parent, 0, default_title_, GURL(url_name)); - scoped_nsobject<BookmarkEditorController> - controller([[BookmarkEditorController alloc] - initWithParentWindow:cocoa_helper_.window() - profile:helper_.profile() - parent:parent - node:node - configuration:BookmarkEditor::SHOW_TREE - handler:nil]); + BookmarkEditorController* controller = + [[BookmarkEditorController alloc] + initWithParentWindow:test_window() + profile:helper_.profile() + parent:parent + node:node + configuration:BookmarkEditor::SHOW_TREE + handler:nil]; EXPECT_NE((NSWindow*)nil, [controller window]); // Forces a nib load EXPECT_TRUE([base::SysWideToNSString(default_title_) @@ -83,6 +89,7 @@ TEST_F(BookmarkEditorControllerTest, YesNodeShowTree) { EXPECT_TRUE([[NSString stringWithCString:url_name encoding:NSUTF8StringEncoding] isEqual:[controller displayURL]]); + [controller close]; } TEST_F(BookmarkEditorControllerTest, NoEdit) { @@ -141,11 +148,10 @@ TEST_F(BookmarkEditorControllerTest, EditAndConfirmOKButton) { EXPECT_FALSE([default_controller_ okButtonEnabled]); } -class BookmarkEditorControllerTreeTest : public PlatformTest { +class BookmarkEditorControllerTreeTest : public CocoaTest { public: - CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... BrowserTestHelper helper_; - scoped_nsobject<BookmarkEditorController> default_controller_; + BookmarkEditorController* default_controller_; const BookmarkNode* group_a_; const BookmarkNode* group_b_; const BookmarkNode* group_bb_; @@ -189,20 +195,33 @@ class BookmarkEditorControllerTreeTest : public PlatformTest { model.AddURL(group_c_, 3, L"c-3", GURL("http://c-3.com")); model.AddURL(root, 3, L"d", GURL("http://d-0.com")); + } - default_controller_.reset([[BookmarkEditorController alloc] - initWithParentWindow:cocoa_helper_.window() - profile:helper_.profile() - parent:group_bb_ + virtual BookmarkEditorController* CreateController() { + return [[BookmarkEditorController alloc] + initWithParentWindow:test_window() + profile:helper_.profile() + parent:group_bb_ node:bookmark_bb_3_ - configuration:BookmarkEditor::SHOW_TREE - handler:nil]); - [default_controller_ window]; // Forces a nib load + configuration:BookmarkEditor::SHOW_TREE + handler:nil]; + } + + virtual void SetUp() { + CocoaTest::SetUp(); + default_controller_ = CreateController(); + EXPECT_TRUE([default_controller_ window]); + } + + virtual void TearDown() { + [default_controller_ close]; + CocoaTest::TearDown(); } }; TEST_F(BookmarkEditorControllerTreeTest, VerifyBookmarkTestModel) { BookmarkModel& model(*(helper_.profile()->GetBookmarkModel())); + model.root_node(); const BookmarkNode& root(*model.GetBookmarkBarNode()); EXPECT_EQ(4, root.GetChildCount()); const BookmarkNode* child = root.GetChild(0); @@ -301,17 +320,16 @@ TEST_F(BookmarkEditorControllerTreeTest, AddFolderWithGroupSelected) { class BookmarkEditorControllerTreeNoNodeTest : public BookmarkEditorControllerTreeTest { public: - BookmarkEditorControllerTreeNoNodeTest() { - // Reset the controller so that we have no |node|. - default_controller_.reset([[BookmarkEditorController alloc] - initWithParentWindow:cocoa_helper_.window() - profile:helper_.profile() - parent:group_bb_ + virtual BookmarkEditorController* CreateController() { + return [[BookmarkEditorController alloc] + initWithParentWindow:test_window() + profile:helper_.profile() + parent:group_bb_ node:nil - configuration:BookmarkEditor::SHOW_TREE - handler:nil]); - [default_controller_ window]; // Forces a nib load + configuration:BookmarkEditor::SHOW_TREE + handler:nil]; } + }; TEST_F(BookmarkEditorControllerTreeNoNodeTest, NewBookmarkNoNode) { @@ -327,17 +345,15 @@ TEST_F(BookmarkEditorControllerTreeNoNodeTest, NewBookmarkNoNode) { class BookmarkEditorControllerTreeNoParentTest : public BookmarkEditorControllerTreeTest { public: - BookmarkEditorControllerTreeNoParentTest() { - // Reset the controller so that we have no |node|. - default_controller_.reset([[BookmarkEditorController alloc] - initWithParentWindow:cocoa_helper_.window() - profile:helper_.profile() - parent:nil - node:nil - configuration:BookmarkEditor::SHOW_TREE - handler:nil]); - [default_controller_ window]; // Forces a nib load - } + virtual BookmarkEditorController* CreateController() { + return [[BookmarkEditorController alloc] + initWithParentWindow:test_window() + profile:helper_.profile() + parent:nil + node:nil + configuration:BookmarkEditor::SHOW_TREE + handler:nil]; + } }; TEST_F(BookmarkEditorControllerTreeNoParentTest, AddFolderWithNoGroupSelected) { |