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/ui/bookmarks/bookmark_utils.h"
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 "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 #if !defined(OS_ANDROID) && !defined(OS_IOS)
16 using base::ASCIIToUTF16
;
17 using bookmarks::BookmarkModel
;
18 using bookmarks::BookmarkNode
;
22 class BookmarkUIUtilsTest
: public testing::Test
{
23 content::TestBrowserThreadBundle thread_bundle_
;
26 TEST_F(BookmarkUIUtilsTest
, HasBookmarkURLs
) {
27 bookmarks::TestBookmarkClient client
;
28 scoped_ptr
<BookmarkModel
> model(client
.CreateModel());
30 std::vector
<const BookmarkNode
*> nodes
;
32 // This tests that |nodes| contains an URL.
33 const BookmarkNode
* page1
= model
->AddURL(model
->bookmark_bar_node(),
35 ASCIIToUTF16("Google"),
36 GURL("http://google.com"));
37 nodes
.push_back(page1
);
38 EXPECT_TRUE(chrome::HasBookmarkURLs(nodes
));
42 // This tests that |nodes| does not contain any URL.
43 const BookmarkNode
* folder1
=
44 model
->AddFolder(model
->bookmark_bar_node(), 0, ASCIIToUTF16("Folder1"));
45 nodes
.push_back(folder1
);
46 EXPECT_FALSE(chrome::HasBookmarkURLs(nodes
));
48 // This verifies if HasBookmarkURLs iterates through immediate children.
49 model
->AddURL(folder1
, 0, ASCIIToUTF16("Foo"), GURL("http://randomsite.com"));
50 EXPECT_TRUE(chrome::HasBookmarkURLs(nodes
));
52 // This verifies that HasBookmarkURLS does not iterate through descendants.
53 // i.e, it should not find an URL inside a two or three level hierarchy.
54 // So we add another folder to |folder1| and add another page to that new
55 // folder to create a two level hierarchy.
57 // But first we have to remove the URL from |folder1|.
58 model
->Remove(folder1
->GetChild(0));
60 const BookmarkNode
* subfolder1
=
61 model
->AddFolder(folder1
, 0, ASCIIToUTF16("Subfolder1"));
63 // Now add the URL to that |subfolder1|.
64 model
->AddURL(subfolder1
, 0, ASCIIToUTF16("BAR"), GURL("http://bar-foo.com"));
65 EXPECT_FALSE(chrome::HasBookmarkURLs(nodes
));
68 TEST_F(BookmarkUIUtilsTest
, HasBookmarkURLsAllowedInIncognitoMode
) {
69 bookmarks::TestBookmarkClient client
;
70 scoped_ptr
<BookmarkModel
> model(client
.CreateModel());
71 TestingProfile profile
;
73 std::vector
<const BookmarkNode
*> nodes
;
75 // This tests that |nodes| contains an disabled-in-incognito URL.
76 const BookmarkNode
* page1
= model
->AddURL(model
->bookmark_bar_node(),
78 ASCIIToUTF16("BookmarkManager"),
79 GURL("chrome://bookmarks"));
80 nodes
.push_back(page1
);
81 EXPECT_FALSE(chrome::HasBookmarkURLsAllowedInIncognitoMode(nodes
, &profile
));
84 // This tests that |nodes| contains an URL that can be opened in incognito
86 const BookmarkNode
* page2
= model
->AddURL(model
->bookmark_bar_node(),
88 ASCIIToUTF16("Google"),
89 GURL("http://google.com"));
90 nodes
.push_back(page2
);
91 EXPECT_TRUE(chrome::HasBookmarkURLsAllowedInIncognitoMode(nodes
, &profile
));
95 // This tests that |nodes| does not contain any URL.
96 const BookmarkNode
* folder1
=
97 model
->AddFolder(model
->bookmark_bar_node(), 0, ASCIIToUTF16("Folder1"));
98 nodes
.push_back(folder1
);
99 EXPECT_FALSE(chrome::HasBookmarkURLsAllowedInIncognitoMode(nodes
, &profile
));
101 // This verifies if HasBookmarkURLsAllowedInIncognitoMode iterates through
102 // immediate children.
103 // Add disabled-in-incognito url.
104 model
->AddURL(folder1
, 0, ASCIIToUTF16("Foo"), GURL("chrome://bookmarks"));
105 EXPECT_FALSE(chrome::HasBookmarkURLsAllowedInIncognitoMode(nodes
, &profile
));
107 model
->AddURL(folder1
, 0, ASCIIToUTF16("Foo"), GURL("http://randomsite.com"));
108 EXPECT_TRUE(chrome::HasBookmarkURLsAllowedInIncognitoMode(nodes
, &profile
));
110 // This verifies that HasBookmarkURLsAllowedInIncognitoMode does not iterate
111 // through descendants.
112 // i.e, it should not find an URL inside a two or three level hierarchy.
113 // So we add another folder to |folder1| and add another page to that new
114 // folder to create a two level hierarchy.
116 // But first we have to remove the URL from |folder1|.
117 model
->Remove(folder1
->GetChild(0));
119 const BookmarkNode
* subfolder1
=
120 model
->AddFolder(folder1
, 0, ASCIIToUTF16("Subfolder1"));
122 // Now add the URL to that |subfolder1|.
123 model
->AddURL(subfolder1
, 0, ASCIIToUTF16("BAR"), GURL("http://bar-foo.com"));
124 EXPECT_FALSE(chrome::HasBookmarkURLsAllowedInIncognitoMode(nodes
, &profile
));