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.
4
5#include "chrome/browser/ui/bookmarks/bookmark_utils.h"
6
7#include "base/strings/utf_string_conversions.h"
8#include "chrome/test/base/testing_profile.h"
9#include "components/bookmarks/browser/bookmark_model.h"
10#include "components/bookmarks/test/test_bookmark_client.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13#if !defined(OS_ANDROID) && !defined(OS_IOS)
14
15using base::ASCIIToUTF16;
16
17namespace {
18
19TEST(BookmarkUIUtilsTest, HasBookmarkURLs) {
20  bookmarks::TestBookmarkClient client;
21  scoped_ptr<BookmarkModel> model(client.CreateModel());
22
23  std::vector<const BookmarkNode*> nodes;
24
25  // This tests that |nodes| contains an URL.
26  const BookmarkNode* page1 = model->AddURL(model->bookmark_bar_node(),
27                                            0,
28                                            ASCIIToUTF16("Google"),
29                                            GURL("http://google.com"));
30  nodes.push_back(page1);
31  EXPECT_TRUE(chrome::HasBookmarkURLs(nodes));
32
33  nodes.clear();
34
35  // This tests that |nodes| does not contain any URL.
36  const BookmarkNode* folder1 =
37      model->AddFolder(model->bookmark_bar_node(), 0, ASCIIToUTF16("Folder1"));
38  nodes.push_back(folder1);
39  EXPECT_FALSE(chrome::HasBookmarkURLs(nodes));
40
41  // This verifies if HasBookmarkURLs iterates through immediate children.
42  model->AddURL(folder1, 0, ASCIIToUTF16("Foo"), GURL("http://randomsite.com"));
43  EXPECT_TRUE(chrome::HasBookmarkURLs(nodes));
44
45  // This verifies that HasBookmarkURLS does not iterate through descendants.
46  // i.e, it should not find an URL inside a two or three level hierarchy.
47  // So we add another folder to |folder1| and add another page to that new
48  // folder to create a two level hierarchy.
49
50  // But first we have to remove the URL from |folder1|.
51  model->Remove(folder1, 0);
52
53  const BookmarkNode* subfolder1 =
54      model->AddFolder(folder1, 0, ASCIIToUTF16("Subfolder1"));
55
56  // Now add the URL to that |subfolder1|.
57  model->AddURL(subfolder1, 0, ASCIIToUTF16("BAR"), GURL("http://bar-foo.com"));
58  EXPECT_FALSE(chrome::HasBookmarkURLs(nodes));
59}
60
61TEST(BookmarkUIUtilsTest, HasBookmarkURLsAllowedInIncognitoMode) {
62  bookmarks::TestBookmarkClient client;
63  scoped_ptr<BookmarkModel> model(client.CreateModel());
64  TestingProfile profile;
65
66  std::vector<const BookmarkNode*> nodes;
67
68  // This tests that |nodes| contains an disabled-in-incognito URL.
69  const BookmarkNode* page1 = model->AddURL(model->bookmark_bar_node(),
70                                            0,
71                                            ASCIIToUTF16("BookmarkManager"),
72                                            GURL("chrome://bookmarks"));
73  nodes.push_back(page1);
74  EXPECT_FALSE(chrome::HasBookmarkURLsAllowedInIncognitoMode(nodes, &profile));
75  nodes.clear();
76
77  // This tests that |nodes| contains an URL that can be opened in incognito
78  // mode.
79  const BookmarkNode* page2 = model->AddURL(model->bookmark_bar_node(),
80                                            0,
81                                            ASCIIToUTF16("Google"),
82                                            GURL("http://google.com"));
83  nodes.push_back(page2);
84  EXPECT_TRUE(chrome::HasBookmarkURLsAllowedInIncognitoMode(nodes, &profile));
85
86  nodes.clear();
87
88  // This tests that |nodes| does not contain any URL.
89  const BookmarkNode* folder1 =
90      model->AddFolder(model->bookmark_bar_node(), 0, ASCIIToUTF16("Folder1"));
91  nodes.push_back(folder1);
92  EXPECT_FALSE(chrome::HasBookmarkURLsAllowedInIncognitoMode(nodes, &profile));
93
94  // This verifies if HasBookmarkURLsAllowedInIncognitoMode iterates through
95  // immediate children.
96  // Add disabled-in-incognito url.
97  model->AddURL(folder1, 0, ASCIIToUTF16("Foo"), GURL("chrome://bookmarks"));
98  EXPECT_FALSE(chrome::HasBookmarkURLsAllowedInIncognitoMode(nodes, &profile));
99  // Add normal url.
100  model->AddURL(folder1, 0, ASCIIToUTF16("Foo"), GURL("http://randomsite.com"));
101  EXPECT_TRUE(chrome::HasBookmarkURLsAllowedInIncognitoMode(nodes, &profile));
102
103  // This verifies that HasBookmarkURLsAllowedInIncognitoMode does not iterate
104  // through descendants.
105  // i.e, it should not find an URL inside a two or three level hierarchy.
106  // So we add another folder to |folder1| and add another page to that new
107  // folder to create a two level hierarchy.
108
109  // But first we have to remove the URL from |folder1|.
110  model->Remove(folder1, 0);
111
112  const BookmarkNode* subfolder1 =
113      model->AddFolder(folder1, 0, ASCIIToUTF16("Subfolder1"));
114
115  // Now add the URL to that |subfolder1|.
116  model->AddURL(subfolder1, 0, ASCIIToUTF16("BAR"), GURL("http://bar-foo.com"));
117  EXPECT_FALSE(chrome::HasBookmarkURLsAllowedInIncognitoMode(nodes, &profile));
118}
119
120}  // namespace
121#endif
122