summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/bookmark_item_unittest.mm
blob: 64ca3c4222818c555a1bd94681ee75fe4619b75f (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright (c) 2010 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 "base/scoped_nsobject.h"
#include "base/sys_string_conversions.h"
#import "chrome/browser/cocoa/bookmark_item.h"
#import "chrome/browser/cocoa/bookmark_manager_controller.h"
#include "chrome/browser/cocoa/browser_test_helper.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

namespace {

::testing::AssertionResult isEqual(id expected, id actual) {
  if(expected == actual || [expected isEqual:actual])
    return ::testing::AssertionSuccess();
  else
    return ::testing::AssertionFailure()
        << "Actual value: " << base::SysNSStringToWide([actual description]);
}

class BookmarkItemTest : public CocoaTest {
 public:
  void SetUp() {
    CocoaTest::SetUp();
    manager_ = [BookmarkManagerController showBookmarkManager:
                   browser_test_helper_.profile()];
    ASSERT_TRUE(manager_);
    model_ = [manager_ bookmarkModel];
    ASSERT_TRUE(model_);
    EXPECT_EQ(browser_test_helper_.profile()->GetBookmarkModel(), model_);
    barNode_ = model_->GetBookmarkBarNode();
    ASSERT_TRUE(barNode_);
    bar_ = [manager_ itemFromNode:barNode_];
    ASSERT_TRUE(bar_);
  }

  void AddTestBookmarks() {
    model_->AddURL(barNode_, 0, L"bookmark0", GURL("http://example.com/0"));
    model_->AddURL(barNode_, 1, L"bookmark1", GURL("http://example.com/1"));
    model_->AddURL(barNode_, 2, L"bookmark3", GURL("http://example.com/3"));
    const BookmarkNode* folder = model_->AddGroup(barNode_, 2, L"group");
    ASSERT_TRUE(folder);
    model_->AddURL(folder, 0, L"nested0", GURL("http://example.com/n0"));
    model_->AddURL(folder, 1, L"nested1", GURL("http://example.com/n1"));
  }

  void TearDown() {
    [manager_ close];
    CocoaTest::TearDown();
  }

  BrowserTestHelper browser_test_helper_;
  BookmarkManagerController* manager_;
  BookmarkModel* model_;
  const BookmarkNode* barNode_;
  const BookmarkItem* bar_;
};

TEST_F(BookmarkItemTest, RootItems) {
  // Check the bookmarks-bar item:
  EXPECT_EQ(barNode_, [bar_ node]);
  EXPECT_EQ(bar_, [manager_ itemFromNode:barNode_]);
  EXPECT_EQ(bar_, [manager_ bookmarkBarItem]);
  EXPECT_TRUE([bar_ isFixed]);
  EXPECT_FALSE([bar_ isFake]);
  EXPECT_FALSE([bar_ parent]);
  EXPECT_TRUE([bar_ isFolder]);
  EXPECT_EQ(0U, [bar_ numberOfChildren]);

  // Check the 'others' item:
  const BookmarkNode* otherNode = model_->other_node();
  ASSERT_TRUE(otherNode);
  EXPECT_NE(barNode_, otherNode);
  BookmarkItem* other = [manager_ itemFromNode:otherNode];
  ASSERT_TRUE(other);
  EXPECT_EQ(otherNode, [other node]);
  EXPECT_EQ(other, [manager_ itemFromNode:otherNode]);
  EXPECT_EQ(other, [manager_ otherBookmarksItem]);
  EXPECT_TRUE([other isFixed]);
  EXPECT_FALSE([other isFake]);
  EXPECT_FALSE([other parent]);
  EXPECT_TRUE([other isFolder]);
  EXPECT_EQ(0U, [other numberOfChildren]);

  EXPECT_NE(bar_, other);
  EXPECT_FALSE(isEqual(bar_, other));
}

TEST_F(BookmarkItemTest, FakeItems) {
  NSImage* icon = [NSImage imageNamed:@"NSApplicationIcon"];
  EXPECT_TRUE(icon);
  FakeBookmarkItem* fake = [[FakeBookmarkItem alloc] initWithTitle:@"Fakir"
                                                              icon:icon
                                                           manager:manager_];
  [fake setChildren:nil];
  [fake autorelease];
  EXPECT_TRUE(isEqual(@"Fakir", [fake title]));
  EXPECT_EQ(icon, [fake icon]);
  EXPECT_TRUE([fake isFake]);
  EXPECT_TRUE([fake isFixed]);
  EXPECT_FALSE([fake parent]);
  EXPECT_FALSE([fake isFolder]);

  NSArray* children = [NSArray arrayWithObject:bar_];
  FakeBookmarkItem* bogus = [[FakeBookmarkItem alloc] initWithTitle:@"Bogus"
                                                               icon:nil
                                                            manager:manager_];
  [bogus setChildren:children];
  [bogus autorelease];
  EXPECT_EQ([BookmarkItem folderIcon], [bogus icon]);
  EXPECT_TRUE([bogus isFolder]);
  EXPECT_TRUE(isEqual(children, [bogus children]));
  ASSERT_EQ(1U, [bogus numberOfChildren]);
  EXPECT_EQ(bar_, [bogus childAtIndex:0]);

  BookmarkItem* otherItem = [manager_ itemFromNode:model_->other_node()];
  children = [NSArray arrayWithObjects:bar_, otherItem, nil];
  [bogus setChildren:children];
  EXPECT_TRUE([bogus isFolder]);
  EXPECT_TRUE(isEqual(children, [bogus children]));
  EXPECT_EQ(2U, [bogus numberOfChildren]);
  EXPECT_EQ(bar_, [bogus childAtIndex:0]);
  EXPECT_EQ(otherItem, [bogus childAtIndex:1]);
}

TEST_F(BookmarkItemTest, AddItems) {
  BookmarkItem* item1 = [bar_ addBookmarkWithTitle:@"item1"
                                               URL:@"http://example.com/item1"
                                           atIndex:0];
  EXPECT_TRUE(isEqual(@"item1", [item1 title]));
  EXPECT_TRUE(isEqual(@"http://example.com/item1", [item1 URLString]));
  EXPECT_TRUE([item1 icon]);
  EXPECT_FALSE([item1 isFixed]);
  EXPECT_FALSE([item1 isFolder]);
  EXPECT_EQ(bar_, [item1 parent]);
  EXPECT_EQ(item1, [bar_ childAtIndex:0]);

  BookmarkItem* folder = [bar_ addFolderWithTitle:@"folder"
                                          atIndex:1];
  EXPECT_TRUE(isEqual(@"folder", [folder title]));
  EXPECT_FALSE([folder URLString]);
  EXPECT_EQ([BookmarkItem folderIcon], [folder icon]);
  EXPECT_FALSE([folder isFixed]);
  EXPECT_TRUE([folder isFolder]);
  ASSERT_EQ(0U, [folder numberOfChildren]);
  EXPECT_EQ(bar_, [folder parent]);
  EXPECT_EQ(folder, [bar_ childAtIndex:1]);

  BookmarkItem* item2 = [folder addBookmarkWithTitle:@"item2"
                                                 URL:@"http://example.com/item2"
                                             atIndex:0];
  EXPECT_TRUE(isEqual(@"item2", [item2 title]));
  EXPECT_TRUE(isEqual(@"http://example.com/item2", [item2 URLString]));
  EXPECT_TRUE([item2 icon]);
  EXPECT_FALSE([item2 isFixed]);
  EXPECT_FALSE([item2 isFolder]);
  EXPECT_EQ(folder, [item2 parent]);
  EXPECT_EQ(item2, [folder childAtIndex:0]);
  EXPECT_EQ(0U, [folder indexOfChild:item2]);
}

TEST_F(BookmarkItemTest, URLEditing) {
  BookmarkItem* item = [bar_ addBookmarkWithTitle:@"URL"
                                              URL:@"http://www.google.com/"
                                          atIndex:0];
  EXPECT_TRUE(item);
  EXPECT_TRUE([@"http://www.google.com/" isEqual:[item URLString]]);
  [item setURLString: @"http://www.google.com/chrome"];
  EXPECT_TRUE([@"http://www.google.com/chrome" isEqual:[item URLString]]);
}

TEST_F(BookmarkItemTest, URLConversion) {
  BookmarkItem* item = [bar_ addBookmarkWithTitle:@"fixable URL"
                                              URL:@"www.google.com"
                                          atIndex:0];
  EXPECT_TRUE(item);
  EXPECT_TRUE([@"http://www.google.com/" isEqual:[item URLString]]);

  item = [bar_ addBookmarkWithTitle:@"bad URL"
                                URL:@"!$%@djd ^%QQQ"
                            atIndex:0];
  EXPECT_FALSE(item);
}

TEST_F(BookmarkItemTest, Hierarchy) {
  AddTestBookmarks();
  ASSERT_EQ(4U, [bar_ numberOfChildren]);
  NSString* barTitle = [bar_ title];

  // Check the bookmarks added to the bar.
  BookmarkItem* item = [bar_ childAtIndex:0];
  EXPECT_TRUE(isEqual(@"bookmark0", [item title]));
  EXPECT_TRUE(isEqual(@"http://example.com/0", [item URLString]));
  EXPECT_FALSE([item isFixed]);
  EXPECT_FALSE([item isFake]);
  EXPECT_EQ(bar_, [item parent]);
  EXPECT_FALSE([item isFolder]);
  EXPECT_TRUE(isEqual(barTitle, [item folderPath]));

  item = [bar_ childAtIndex:1];
  EXPECT_TRUE(isEqual(@"bookmark1", [item title]));
  EXPECT_TRUE(isEqual(@"http://example.com/1", [item URLString]));
  EXPECT_FALSE([item isFixed]);
  EXPECT_FALSE([item isFake]);
  EXPECT_EQ(bar_, [item parent]);
  EXPECT_FALSE([item isFolder]);

  item = [bar_ childAtIndex:3];
  EXPECT_TRUE(isEqual(@"bookmark3", [item title]));
  EXPECT_TRUE(isEqual(@"http://example.com/3", [item URLString]));
  EXPECT_FALSE([item isFixed]);
  EXPECT_FALSE([item isFake]);
  EXPECT_EQ(bar_, [item parent]);
  EXPECT_FALSE([item isFolder]);

  // Check the subfolder.
  BookmarkItem* folder = [bar_ childAtIndex:2];
  EXPECT_TRUE(isEqual(@"group", [folder title]));
  EXPECT_EQ(0U, [[folder URLString] length]);
  EXPECT_FALSE([item isFixed]);
  EXPECT_FALSE([folder isFake]);
  EXPECT_EQ(bar_, [folder parent]);
  EXPECT_TRUE([folder isFolder]);
  ASSERT_EQ(2U, [folder numberOfChildren]);
  EXPECT_TRUE(isEqual(barTitle, [folder folderPath]));

  // Check items in subfolder.
  item = [folder childAtIndex:0];
  EXPECT_TRUE(isEqual(@"nested0", [item title]));
  EXPECT_TRUE(isEqual(@"http://example.com/n0", [item URLString]));
  EXPECT_FALSE([item isFixed]);
  EXPECT_FALSE([item isFake]);
  EXPECT_EQ(folder, [item parent]);
  EXPECT_FALSE([item isFolder]);
  EXPECT_TRUE(isEqual([barTitle stringByAppendingString:@"/group"],
                      [item folderPath]));

  item = [folder childAtIndex:1];
  EXPECT_TRUE(isEqual(@"nested1", [item title]));
  EXPECT_TRUE(isEqual(@"http://example.com/n1", [item URLString]));
  EXPECT_FALSE([item isFixed]);
  EXPECT_FALSE([item isFake]);
  EXPECT_EQ(folder, [item parent]);
  EXPECT_FALSE([item isFolder]);
}

}  // namespace