OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 package org.chromium.chrome.browser.bookmark; | |
6 | |
7 import android.test.UiThreadTest; | |
8 import android.test.suitebuilder.annotation.SmallTest; | |
9 | |
10 import org.chromium.base.ThreadUtils; | |
11 import org.chromium.base.test.util.Feature; | |
12 import org.chromium.chrome.browser.bookmark.BookmarksBridge.BookmarkItem; | |
13 import org.chromium.chrome.browser.profiles.Profile; | |
14 import org.chromium.components.bookmarks.BookmarkId; | |
15 import org.chromium.content.browser.test.NativeLibraryTestBase; | |
16 import org.chromium.content.browser.test.util.Criteria; | |
17 import org.chromium.content.browser.test.util.CriteriaHelper; | |
18 | |
19 import java.util.ArrayList; | |
20 import java.util.Arrays; | |
21 import java.util.HashMap; | |
22 import java.util.List; | |
23 | |
24 /** | |
25 * Tests for bookmark bridge | |
26 */ | |
27 public class BookmarksBridgeTest extends NativeLibraryTestBase { | |
28 | |
29 private BookmarksBridge mBookmarksBridge; | |
30 private BookmarkId mMobileNode; | |
31 private BookmarkId mOtherNode; | |
32 private BookmarkId mDesktopNode; | |
33 | |
34 @Override | |
35 protected void setUp() throws Exception { | |
36 super.setUp(); | |
37 loadNativeLibraryAndInitBrowserProcess(); | |
38 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
39 @Override | |
40 public void run() { | |
41 Profile profile = Profile.getLastUsedProfile(); | |
42 mBookmarksBridge = new BookmarksBridge(profile); | |
43 mBookmarksBridge.loadEmptyPartnerBookmarkShimForTesting(); | |
44 } | |
45 }); | |
46 | |
47 CriteriaHelper.pollForUIThreadCriteria(new Criteria() { | |
48 @Override | |
49 public boolean isSatisfied() { | |
50 return mBookmarksBridge.isBookmarkModelLoaded(); | |
51 } | |
52 }); | |
53 | |
54 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
55 @Override | |
56 public void run() { | |
57 mMobileNode = mBookmarksBridge.getMobileFolderId(); | |
58 mDesktopNode = mBookmarksBridge.getDesktopFolderId(); | |
59 mOtherNode = mBookmarksBridge.getOtherFolderId(); | |
60 } | |
61 }); | |
62 } | |
63 | |
64 @UiThreadTest | |
65 @SmallTest | |
66 @Feature({"Bookmark"}) | |
67 public void testAddBookmarksAndFolders() { | |
68 BookmarkId bookmarkA = mBookmarksBridge.addBookmark(mDesktopNode, 0, "a"
, "http://a.com"); | |
69 verifyBookmark(bookmarkA, "a", "http://a.com/", false, mDesktopNode); | |
70 BookmarkId bookmarkB = mBookmarksBridge.addBookmark(mOtherNode, 0, "b",
"http://b.com"); | |
71 verifyBookmark(bookmarkB, "b", "http://b.com/", false, mOtherNode); | |
72 BookmarkId bookmarkC = mBookmarksBridge.addBookmark(mMobileNode, 0, "c",
"http://c.com"); | |
73 verifyBookmark(bookmarkC, "c", "http://c.com/", false, mMobileNode); | |
74 BookmarkId folderA = mBookmarksBridge.addFolder(mOtherNode, 0, "fa"); | |
75 verifyBookmark(folderA, "fa", null, true, mOtherNode); | |
76 BookmarkId folderB = mBookmarksBridge.addFolder(mDesktopNode, 0, "fb"); | |
77 verifyBookmark(folderB, "fb", null, true, mDesktopNode); | |
78 BookmarkId folderC = mBookmarksBridge.addFolder(mMobileNode, 0, "fc"); | |
79 verifyBookmark(folderC, "fc", null, true, mMobileNode); | |
80 BookmarkId bookmarkAA = mBookmarksBridge.addBookmark(folderA, 0, "aa", "
http://aa.com"); | |
81 verifyBookmark(bookmarkAA, "aa", "http://aa.com/", false, folderA); | |
82 BookmarkId folderAA = mBookmarksBridge.addFolder(folderA, 0, "faa"); | |
83 verifyBookmark(folderAA, "faa", null, true, folderA); | |
84 } | |
85 | |
86 private void verifyBookmark(BookmarkId idToVerify, String expectedTitle, | |
87 String expectedUrl, boolean isFolder, BookmarkId expectedParent) { | |
88 assertNotNull(idToVerify); | |
89 BookmarkItem item = mBookmarksBridge.getBookmarkById(idToVerify); | |
90 assertEquals(expectedTitle, item.getTitle()); | |
91 assertEquals(item.isFolder(), isFolder); | |
92 if (!isFolder) assertEquals(expectedUrl, item.getUrl()); | |
93 assertEquals(item.getParentId(), expectedParent); | |
94 } | |
95 | |
96 @UiThreadTest | |
97 @SmallTest | |
98 @Feature({"Bookmark"}) | |
99 public void testGetAllFoldersWithDepths() { | |
100 BookmarkId folderA = mBookmarksBridge.addFolder(mMobileNode, 0, "a"); | |
101 BookmarkId folderB = mBookmarksBridge.addFolder(mDesktopNode, 0, "b"); | |
102 BookmarkId folderC = mBookmarksBridge.addFolder(mOtherNode, 0, "c"); | |
103 BookmarkId folderAA = mBookmarksBridge.addFolder(folderA, 0, "aa"); | |
104 BookmarkId folderBA = mBookmarksBridge.addFolder(folderB, 0, "ba"); | |
105 BookmarkId folderAAA = mBookmarksBridge.addFolder(folderAA, 0, "aaa"); | |
106 BookmarkId folderAAAA = mBookmarksBridge.addFolder(folderAAA, 0, "aaaa")
; | |
107 | |
108 mBookmarksBridge.addBookmark(mMobileNode, 0, "ua", "http://www.google.co
m"); | |
109 mBookmarksBridge.addBookmark(mDesktopNode, 0, "ua", "http://www.google.c
om"); | |
110 mBookmarksBridge.addBookmark(mOtherNode, 0, "ua", "http://www.google.com
"); | |
111 mBookmarksBridge.addBookmark(folderA, 0, "ua", "http://www.medium.com"); | |
112 | |
113 // Map folders to depths as expected results | |
114 HashMap<BookmarkId, Integer> idToDepth = new HashMap<BookmarkId, Integer
>(); | |
115 idToDepth.put(mMobileNode, 0); | |
116 idToDepth.put(folderA, 1); | |
117 idToDepth.put(folderAA, 2); | |
118 idToDepth.put(folderAAA, 3); | |
119 idToDepth.put(folderAAAA, 4); | |
120 idToDepth.put(mDesktopNode, 0); | |
121 idToDepth.put(folderB, 1); | |
122 idToDepth.put(folderBA, 2); | |
123 idToDepth.put(mOtherNode, 0); | |
124 idToDepth.put(folderC, 1); | |
125 | |
126 List<BookmarkId> folderList = new ArrayList<BookmarkId>(); | |
127 List<Integer> depthList = new ArrayList<Integer>(); | |
128 mBookmarksBridge.getAllFoldersWithDepths(folderList, depthList); | |
129 verifyFolderDepths(folderList, depthList, idToDepth); | |
130 } | |
131 | |
132 @UiThreadTest | |
133 @SmallTest | |
134 @Feature({"Bookmark"}) | |
135 public void testGetMoveDestinations() { | |
136 BookmarkId folderA = mBookmarksBridge.addFolder(mMobileNode, 0, "a"); | |
137 BookmarkId folderB = mBookmarksBridge.addFolder(mDesktopNode, 0, "b"); | |
138 BookmarkId folderC = mBookmarksBridge.addFolder(mOtherNode, 0, "c"); | |
139 BookmarkId folderAA = mBookmarksBridge.addFolder(folderA, 0, "aa"); | |
140 BookmarkId folderBA = mBookmarksBridge.addFolder(folderB, 0, "ba"); | |
141 BookmarkId folderAAA = mBookmarksBridge.addFolder(folderAA, 0, "aaa"); | |
142 | |
143 mBookmarksBridge.addBookmark(mMobileNode, 0, "ua", "http://www.google.co
m"); | |
144 mBookmarksBridge.addBookmark(mDesktopNode, 0, "ua", "http://www.google.c
om"); | |
145 mBookmarksBridge.addBookmark(mOtherNode, 0, "ua", "http://www.google.com
"); | |
146 mBookmarksBridge.addBookmark(folderA, 0, "ua", "http://www.medium.com"); | |
147 | |
148 // Map folders to depths as expected results | |
149 HashMap<BookmarkId, Integer> idToDepth = new HashMap<BookmarkId, Integer
>(); | |
150 | |
151 List<BookmarkId> folderList = new ArrayList<BookmarkId>(); | |
152 List<Integer> depthList = new ArrayList<Integer>(); | |
153 | |
154 mBookmarksBridge.getMoveDestinations(folderList, depthList, Arrays.asLis
t(folderA)); | |
155 idToDepth.put(mMobileNode, 0); | |
156 idToDepth.put(mDesktopNode, 0); | |
157 idToDepth.put(folderB, 1); | |
158 idToDepth.put(folderBA, 2); | |
159 idToDepth.put(mOtherNode, 0); | |
160 idToDepth.put(folderC, 1); | |
161 verifyFolderDepths(folderList, depthList, idToDepth); | |
162 | |
163 mBookmarksBridge.getMoveDestinations(folderList, depthList, Arrays.asLis
t(folderB)); | |
164 idToDepth.put(mMobileNode, 0); | |
165 idToDepth.put(folderA, 1); | |
166 idToDepth.put(folderAA, 2); | |
167 idToDepth.put(folderAAA, 3); | |
168 idToDepth.put(mDesktopNode, 0); | |
169 idToDepth.put(mOtherNode, 0); | |
170 idToDepth.put(folderC, 1); | |
171 verifyFolderDepths(folderList, depthList, idToDepth); | |
172 | |
173 mBookmarksBridge.getMoveDestinations(folderList, depthList, Arrays.asLis
t(folderC)); | |
174 idToDepth.put(mMobileNode, 0); | |
175 idToDepth.put(folderA, 1); | |
176 idToDepth.put(folderAA, 2); | |
177 idToDepth.put(folderAAA, 3); | |
178 idToDepth.put(mDesktopNode, 0); | |
179 idToDepth.put(folderB, 1); | |
180 idToDepth.put(folderBA, 2); | |
181 idToDepth.put(mOtherNode, 0); | |
182 verifyFolderDepths(folderList, depthList, idToDepth); | |
183 | |
184 mBookmarksBridge.getMoveDestinations(folderList, depthList, Arrays.asLis
t(folderBA)); | |
185 idToDepth.put(mMobileNode, 0); | |
186 idToDepth.put(folderA, 1); | |
187 idToDepth.put(folderAA, 2); | |
188 idToDepth.put(folderAAA, 3); | |
189 idToDepth.put(mDesktopNode, 0); | |
190 idToDepth.put(folderB, 1); | |
191 idToDepth.put(mOtherNode, 0); | |
192 idToDepth.put(folderC, 1); | |
193 verifyFolderDepths(folderList, depthList, idToDepth); | |
194 | |
195 mBookmarksBridge.getMoveDestinations(folderList, depthList, | |
196 Arrays.asList(folderAA, folderC)); | |
197 idToDepth.put(mMobileNode, 0); | |
198 idToDepth.put(folderA, 1); | |
199 idToDepth.put(mDesktopNode, 0); | |
200 idToDepth.put(folderB, 1); | |
201 idToDepth.put(folderBA, 2); | |
202 idToDepth.put(mOtherNode, 0); | |
203 verifyFolderDepths(folderList, depthList, idToDepth); | |
204 } | |
205 | |
206 private void verifyFolderDepths(List<BookmarkId> folderList, List<Integer> d
epthList, | |
207 HashMap<BookmarkId, Integer> idToDepth) { | |
208 assertEquals(folderList.size(), depthList.size()); | |
209 assertEquals(folderList.size(), idToDepth.size()); | |
210 for (int i = 0; i < folderList.size(); i++) { | |
211 BookmarkId folder = folderList.get(i); | |
212 Integer depth = depthList.get(i); | |
213 assertNotNull(folder); | |
214 assertNotNull(depthList.get(i)); | |
215 assertTrue("Folder list contains non-folder elements: ", | |
216 mBookmarksBridge.getBookmarkById(folder).isFolder()); | |
217 assertTrue("Returned list contained unexpected key: ", idToDepth.con
tainsKey(folder)); | |
218 assertEquals(idToDepth.get(folder), depth); | |
219 idToDepth.remove(folder); | |
220 } | |
221 assertEquals(idToDepth.size(), 0); | |
222 folderList.clear(); | |
223 depthList.clear(); | |
224 } | |
225 } | |
OLD | NEW |