1 // Copyright (c) 2011 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. 4 5 #import <Cocoa/Cocoa.h> 6 7 #include "base/memory/scoped_nsobject.h" 8 #include "base/sys_string_conversions.h" 9 #import "chrome/browser/ui/cocoa/applescript/bookmark_applescript_utils_unittest.h" 10 #import "chrome/browser/ui/cocoa/applescript/bookmark_folder_applescript.h" 11 #import "chrome/browser/ui/cocoa/applescript/bookmark_item_applescript.h" 12 #import "chrome/browser/ui/cocoa/applescript/constants_applescript.h" 13 #import "chrome/browser/ui/cocoa/applescript/error_applescript.h" 14 #include "googleurl/src/gurl.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 #import "testing/gtest_mac.h" 17 #include "testing/platform_test.h" 18 19 typedef BookmarkAppleScriptTest BookmarkFolderAppleScriptTest; 20 21 namespace { 22 23 // Test all the bookmark folders within. 24 TEST_F(BookmarkFolderAppleScriptTest, BookmarkFolders) { 25 NSArray* bookmarkFolders = [bookmarkBar_.get() bookmarkFolders]; 26 27 EXPECT_EQ(2U, [bookmarkFolders count]); 28 29 BookmarkFolderAppleScript* f1 = [bookmarkFolders objectAtIndex:0]; 30 BookmarkFolderAppleScript* f2 = [bookmarkFolders objectAtIndex:1]; 31 EXPECT_NSEQ(@"f1", [f1 title]); 32 EXPECT_NSEQ(@"f2", [f2 title]); 33 EXPECT_EQ(2, [[f1 index] intValue]); 34 EXPECT_EQ(4, [[f2 index] intValue]); 35 36 for (BookmarkFolderAppleScript* bookmarkFolder in bookmarkFolders) { 37 EXPECT_EQ([bookmarkFolder container], bookmarkBar_.get()); 38 EXPECT_NSEQ(AppleScript::kBookmarkFoldersProperty, 39 [bookmarkFolder containerProperty]); 40 } 41 } 42 43 // Insert a new bookmark folder. 44 TEST_F(BookmarkFolderAppleScriptTest, InsertBookmarkFolder) { 45 // Emulate what applescript would do when inserting a new bookmark folder. 46 // Emulates a script like |set var to make new bookmark folder with 47 // properties {title:"foo"}|. 48 scoped_nsobject<BookmarkFolderAppleScript> bookmarkFolder( 49 [[BookmarkFolderAppleScript alloc] init]); 50 scoped_nsobject<NSNumber> var([[bookmarkFolder.get() uniqueID] copy]); 51 [bookmarkFolder.get() setTitle:@"foo"]; 52 [bookmarkBar_.get() insertInBookmarkFolders:bookmarkFolder.get()]; 53 54 // Represents the bookmark folder after its added. 55 BookmarkFolderAppleScript* bf = 56 [[bookmarkBar_.get() bookmarkFolders] objectAtIndex:2]; 57 EXPECT_NSEQ(@"foo", [bf title]); 58 EXPECT_EQ([bf container], bookmarkBar_.get()); 59 EXPECT_NSEQ(AppleScript::kBookmarkFoldersProperty, 60 [bf containerProperty]); 61 EXPECT_NSEQ(var.get(), [bf uniqueID]); 62 } 63 64 // Insert a new bookmark folder at a particular position. 65 TEST_F(BookmarkFolderAppleScriptTest, InsertBookmarkFolderAtPosition) { 66 // Emulate what applescript would do when inserting a new bookmark folder. 67 // Emulates a script like |set var to make new bookmark folder with 68 // properties {title:"foo"} at after bookmark folder 1|. 69 scoped_nsobject<BookmarkFolderAppleScript> bookmarkFolder( 70 [[BookmarkFolderAppleScript alloc] init]); 71 scoped_nsobject<NSNumber> var([[bookmarkFolder.get() uniqueID] copy]); 72 [bookmarkFolder.get() setTitle:@"foo"]; 73 [bookmarkBar_.get() insertInBookmarkFolders:bookmarkFolder.get() atIndex:1]; 74 75 // Represents the bookmark folder after its added. 76 BookmarkFolderAppleScript* bf = 77 [[bookmarkBar_.get() bookmarkFolders] objectAtIndex:1]; 78 EXPECT_NSEQ(@"foo", [bf title]); 79 EXPECT_EQ([bf container], bookmarkBar_.get()); 80 EXPECT_NSEQ(AppleScript::kBookmarkFoldersProperty, [bf containerProperty]); 81 EXPECT_NSEQ(var.get(), [bf uniqueID]); 82 } 83 84 // Delete bookmark folders. 85 TEST_F(BookmarkFolderAppleScriptTest, DeleteBookmarkFolders) { 86 unsigned int folderCount = 2, itemCount = 3; 87 for (unsigned int i = 0; i < folderCount; ++i) { 88 EXPECT_EQ(folderCount - i, [[bookmarkBar_.get() bookmarkFolders] count]); 89 EXPECT_EQ(itemCount, [[bookmarkBar_.get() bookmarkItems] count]); 90 [bookmarkBar_.get() removeFromBookmarkFoldersAtIndex:0]; 91 } 92 } 93 94 // Test all the bookmark items within. 95 TEST_F(BookmarkFolderAppleScriptTest, BookmarkItems) { 96 NSArray* bookmarkItems = [bookmarkBar_.get() bookmarkItems]; 97 98 EXPECT_EQ(3U, [bookmarkItems count]); 99 100 BookmarkItemAppleScript* i1 = [bookmarkItems objectAtIndex:0]; 101 BookmarkItemAppleScript* i2 = [bookmarkItems objectAtIndex:1]; 102 BookmarkItemAppleScript* i3 = [bookmarkItems objectAtIndex:2]; 103 EXPECT_NSEQ(@"a", [i1 title]); 104 EXPECT_NSEQ(@"d", [i2 title]); 105 EXPECT_NSEQ(@"h", [i3 title]); 106 EXPECT_EQ(1, [[i1 index] intValue]); 107 EXPECT_EQ(3, [[i2 index] intValue]); 108 EXPECT_EQ(5, [[i3 index] intValue]); 109 110 for (BookmarkItemAppleScript* bookmarkItem in bookmarkItems) { 111 EXPECT_EQ([bookmarkItem container], bookmarkBar_.get()); 112 EXPECT_NSEQ(AppleScript::kBookmarkItemsProperty, 113 [bookmarkItem containerProperty]); 114 } 115 } 116 117 // Insert a new bookmark item. 118 TEST_F(BookmarkFolderAppleScriptTest, InsertBookmarkItem) { 119 // Emulate what applescript would do when inserting a new bookmark folder. 120 // Emulates a script like |set var to make new bookmark item with 121 // properties {title:"Google", URL:"http://google.com"}|. 122 scoped_nsobject<BookmarkItemAppleScript> bookmarkItem( 123 [[BookmarkItemAppleScript alloc] init]); 124 scoped_nsobject<NSNumber> var([[bookmarkItem.get() uniqueID] copy]); 125 [bookmarkItem.get() setTitle:@"Google"]; 126 [bookmarkItem.get() setURL:@"http://google.com"]; 127 [bookmarkBar_.get() insertInBookmarkItems:bookmarkItem.get()]; 128 129 // Represents the bookmark item after its added. 130 BookmarkItemAppleScript* bi = 131 [[bookmarkBar_.get() bookmarkItems] objectAtIndex:3]; 132 EXPECT_NSEQ(@"Google", [bi title]); 133 EXPECT_EQ(GURL("http://google.com/"), 134 GURL(base::SysNSStringToUTF8([bi URL]))); 135 EXPECT_EQ([bi container], bookmarkBar_.get()); 136 EXPECT_NSEQ(AppleScript::kBookmarkItemsProperty, [bi containerProperty]); 137 EXPECT_NSEQ(var.get(), [bi uniqueID]); 138 139 // Test to see no bookmark item is created when no/invlid URL is entered. 140 scoped_nsobject<FakeScriptCommand> fakeScriptCommand( 141 [[FakeScriptCommand alloc] init]); 142 bookmarkItem.reset([[BookmarkItemAppleScript alloc] init]); 143 [bookmarkBar_.get() insertInBookmarkItems:bookmarkItem.get()]; 144 EXPECT_EQ((int)AppleScript::errInvalidURL, 145 [fakeScriptCommand.get() scriptErrorNumber]); 146 } 147 148 // Insert a new bookmark item at a particular position. 149 TEST_F(BookmarkFolderAppleScriptTest, InsertBookmarkItemAtPosition) { 150 // Emulate what applescript would do when inserting a new bookmark item. 151 // Emulates a script like |set var to make new bookmark item with 152 // properties {title:"XKCD", URL:"http://xkcd.org} 153 // at after bookmark item 1|. 154 scoped_nsobject<BookmarkItemAppleScript> bookmarkItem( 155 [[BookmarkItemAppleScript alloc] init]); 156 scoped_nsobject<NSNumber> var([[bookmarkItem.get() uniqueID] copy]); 157 [bookmarkItem.get() setTitle:@"XKCD"]; 158 [bookmarkItem.get() setURL:@"http://xkcd.org"]; 159 160 [bookmarkBar_.get() insertInBookmarkItems:bookmarkItem.get() atIndex:1]; 161 162 // Represents the bookmark item after its added. 163 BookmarkItemAppleScript* bi = 164 [[bookmarkBar_.get() bookmarkItems] objectAtIndex:1]; 165 EXPECT_NSEQ(@"XKCD", [bi title]); 166 EXPECT_EQ(GURL("http://xkcd.org/"), 167 GURL(base::SysNSStringToUTF8([bi URL]))); 168 EXPECT_EQ([bi container], bookmarkBar_.get()); 169 EXPECT_NSEQ(AppleScript::kBookmarkItemsProperty, 170 [bi containerProperty]); 171 EXPECT_NSEQ(var.get(), [bi uniqueID]); 172 173 // Test to see no bookmark item is created when no/invlid URL is entered. 174 scoped_nsobject<FakeScriptCommand> fakeScriptCommand( 175 [[FakeScriptCommand alloc] init]); 176 bookmarkItem.reset([[BookmarkItemAppleScript alloc] init]); 177 [bookmarkBar_.get() insertInBookmarkItems:bookmarkItem.get() atIndex:1]; 178 EXPECT_EQ((int)AppleScript::errInvalidURL, 179 [fakeScriptCommand.get() scriptErrorNumber]); 180 } 181 182 // Delete bookmark items. 183 TEST_F(BookmarkFolderAppleScriptTest, DeleteBookmarkItems) { 184 unsigned int folderCount = 2, itemCount = 3; 185 for (unsigned int i = 0; i < itemCount; ++i) { 186 EXPECT_EQ(folderCount, [[bookmarkBar_.get() bookmarkFolders] count]); 187 EXPECT_EQ(itemCount - i, [[bookmarkBar_.get() bookmarkItems] count]); 188 [bookmarkBar_.get() removeFromBookmarkItemsAtIndex:0]; 189 } 190 } 191 192 // Set and get title. 193 TEST_F(BookmarkFolderAppleScriptTest, GetAndSetTitle) { 194 NSArray* bookmarkFolders = [bookmarkBar_.get() bookmarkFolders]; 195 BookmarkFolderAppleScript* folder1 = [bookmarkFolders objectAtIndex:0]; 196 [folder1 setTitle:@"Foo"]; 197 EXPECT_NSEQ(@"Foo", [folder1 title]); 198 } 199 200 } // namespace 201