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