1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h"
#import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_folder_controller.h"
#include "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h"
#import "chrome/browser/ui/cocoa/bookmarks/bookmark_folder_target.h"
#include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
#include "chrome/test/base/testing_profile.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#include "third_party/ocmock/gtest_support.h"
@interface OCMockObject(PreventRetainCycle)
- (void)clearRecordersAndExpectations;
@end
@implementation OCMockObject(PreventRetainCycle)
// We need a mechanism to clear the invocation handlers to break a
// retain cycle (see below; search for "retain cycle").
- (void)clearRecordersAndExpectations {
[recorders removeAllObjects];
[expectations removeAllObjects];
}
@end
class BookmarkFolderTargetTest : public CocoaProfileTest {
public:
void SetUp() override {
CocoaProfileTest::SetUp();
ASSERT_TRUE(profile());
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
bmbNode_ = model->bookmark_bar_node();
}
const BookmarkNode* bmbNode_;
};
TEST_F(BookmarkFolderTargetTest, StartWithNothing) {
// Need a fake "button" which has a bookmark node.
id sender = [OCMockObject mockForClass:[BookmarkButton class]];
[[[sender stub] andReturnValue:OCMOCK_VALUE(bmbNode_)] bookmarkNode];
// Fake controller
id controller = [OCMockObject mockForClass:[BookmarkBarFolderController
class]];
// No current folder
[[[controller stub] andReturn:nil] folderController];
// Make sure we get an addNew
[[controller expect] addNewFolderControllerWithParentButton:sender];
base::scoped_nsobject<BookmarkFolderTarget> target(
[[BookmarkFolderTarget alloc] initWithController:controller
profile:profile()]);
[target openBookmarkFolderFromButton:sender];
EXPECT_OCMOCK_VERIFY(controller);
}
TEST_F(BookmarkFolderTargetTest, ReopenSameFolder) {
// Need a fake "button" which has a bookmark node.
id sender = [OCMockObject mockForClass:[BookmarkButton class]];
[[[sender stub] andReturnValue:OCMOCK_VALUE(bmbNode_)] bookmarkNode];
// Fake controller
id controller = [OCMockObject mockForClass:[BookmarkBarFolderController
class]];
// YES a current folder. Self-mock that as well, so "same" will be
// true. Note this creates a retain cycle in OCMockObject; we
// accomodate at the end of this function.
[[[controller stub] andReturn:controller] folderController];
[[[controller stub] andReturn:sender] parentButton];
// The folder is open, so a click should close just that folder (and
// any subfolders).
[[controller expect] closeBookmarkFolder:controller];
base::scoped_nsobject<BookmarkFolderTarget> target(
[[BookmarkFolderTarget alloc] initWithController:controller
profile:profile()]);
[target openBookmarkFolderFromButton:sender];
EXPECT_OCMOCK_VERIFY(controller);
// Our use of OCMockObject means an object can return itself. This
// creates a retain cycle, since OCMock retains all objects used in
// mock creation. Clear out the invocation handlers of all
// OCMockRecorders we used to break the cycles.
[controller clearRecordersAndExpectations];
}
TEST_F(BookmarkFolderTargetTest, ReopenNotSame) {
// Need a fake "button" which has a bookmark node.
id sender = [OCMockObject mockForClass:[BookmarkButton class]];
[[[sender stub] andReturnValue:OCMOCK_VALUE(bmbNode_)] bookmarkNode];
// Fake controller
id controller = [OCMockObject mockForClass:[BookmarkBarFolderController
class]];
// YES a current folder but NOT same.
[[[controller stub] andReturn:controller] folderController];
[[[controller stub] andReturn:nil] parentButton];
// Insure the controller gets a chance to decide which folders to
// close and open.
[[controller expect] addNewFolderControllerWithParentButton:sender];
base::scoped_nsobject<BookmarkFolderTarget> target(
[[BookmarkFolderTarget alloc] initWithController:controller
profile:profile()]);
[target openBookmarkFolderFromButton:sender];
EXPECT_OCMOCK_VERIFY(controller);
// Break retain cycles.
[controller clearRecordersAndExpectations];
}
|