OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/bookmarks/browser/bookmark_model.h" | 5 #include "components/bookmarks/browser/bookmark_model.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
20 #include "components/bookmarks/browser/bookmark_model_observer.h" | 20 #include "components/bookmarks/browser/bookmark_model_observer.h" |
21 #include "components/bookmarks/browser/bookmark_utils.h" | 21 #include "components/bookmarks/browser/bookmark_utils.h" |
22 #include "components/bookmarks/test/bookmark_test_helpers.h" | 22 #include "components/bookmarks/test/bookmark_test_helpers.h" |
23 #include "components/bookmarks/test/test_bookmark_client.h" | 23 #include "components/bookmarks/test/test_bookmark_client.h" |
| 24 #include "components/favicon_base/favicon_callback.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "third_party/skia/include/core/SkBitmap.h" |
25 #include "ui/base/models/tree_node_iterator.h" | 27 #include "ui/base/models/tree_node_iterator.h" |
26 #include "ui/base/models/tree_node_model.h" | 28 #include "ui/base/models/tree_node_model.h" |
| 29 #include "ui/gfx/image/image.h" |
27 #include "url/gurl.h" | 30 #include "url/gurl.h" |
28 | 31 |
29 using base::ASCIIToUTF16; | 32 using base::ASCIIToUTF16; |
30 using base::Time; | 33 using base::Time; |
31 using base::TimeDelta; | 34 using base::TimeDelta; |
32 | 35 |
33 namespace bookmarks { | 36 namespace bookmarks { |
34 namespace { | 37 namespace { |
35 | 38 |
36 // Test cases used to test the removal of extra whitespace when adding | 39 // Test cases used to test the removal of extra whitespace when adding |
(...skipping 1137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1174 PopulateBookmarkNode(&mobile, model.get(), model->mobile_node()); | 1177 PopulateBookmarkNode(&mobile, model.get(), model->mobile_node()); |
1175 | 1178 |
1176 VerifyModelMatchesNode(&bbn, model->bookmark_bar_node()); | 1179 VerifyModelMatchesNode(&bbn, model->bookmark_bar_node()); |
1177 VerifyModelMatchesNode(&other, model->other_node()); | 1180 VerifyModelMatchesNode(&other, model->other_node()); |
1178 VerifyModelMatchesNode(&mobile, model->mobile_node()); | 1181 VerifyModelMatchesNode(&mobile, model->mobile_node()); |
1179 VerifyNoDuplicateIDs(model.get()); | 1182 VerifyNoDuplicateIDs(model.get()); |
1180 } | 1183 } |
1181 } | 1184 } |
1182 | 1185 |
1183 } // namespace | 1186 } // namespace |
| 1187 |
| 1188 class BookmarkModelFaviconTest : public testing::Test, |
| 1189 public BookmarkModelObserver { |
| 1190 public: |
| 1191 BookmarkModelFaviconTest() : model_(client_.CreateModel()) { |
| 1192 model_->AddObserver(this); |
| 1193 } |
| 1194 |
| 1195 // Emulates the favicon getting asynchronously loaded. In production, the |
| 1196 // favicon is asynchronously loaded when BookmarkModel::GetFavicon() is |
| 1197 // called. |
| 1198 void OnFaviconLoaded(BookmarkNode* node, const GURL& icon_url) { |
| 1199 SkBitmap bitmap; |
| 1200 bitmap.allocN32Pixels(16, 16); |
| 1201 bitmap.eraseColor(SK_ColorBLUE); |
| 1202 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap); |
| 1203 |
| 1204 favicon_base::FaviconImageResult image_result; |
| 1205 image_result.image = image; |
| 1206 image_result.icon_url = icon_url; |
| 1207 model_->OnFaviconDataAvailable(node, favicon_base::IconType::FAVICON, |
| 1208 image_result); |
| 1209 } |
| 1210 |
| 1211 bool WasNodeUpdated(const BookmarkNode* node) { |
| 1212 return std::find(updated_nodes_.begin(), updated_nodes_.end(), node) != |
| 1213 updated_nodes_.end(); |
| 1214 } |
| 1215 |
| 1216 void ClearUpdatedNodes() { |
| 1217 updated_nodes_.clear(); |
| 1218 } |
| 1219 |
| 1220 protected: |
| 1221 void BookmarkModelLoaded(BookmarkModel* model, bool ids_reassigned) override { |
| 1222 } |
| 1223 |
| 1224 void BookmarkNodeMoved(BookmarkModel* model, |
| 1225 const BookmarkNode* old_parent, |
| 1226 int old_index, |
| 1227 const BookmarkNode* new_parent, |
| 1228 int new_index) override {} |
| 1229 |
| 1230 void BookmarkNodeAdded(BookmarkModel* model, |
| 1231 const BookmarkNode* parent, |
| 1232 int index) override {} |
| 1233 |
| 1234 void BookmarkNodeRemoved(BookmarkModel* model, |
| 1235 const BookmarkNode* parent, |
| 1236 int old_index, |
| 1237 const BookmarkNode* node, |
| 1238 const std::set<GURL>& removed_urls) override {} |
| 1239 |
| 1240 void BookmarkNodeChanged(BookmarkModel* model, |
| 1241 const BookmarkNode* node) override {} |
| 1242 |
| 1243 void BookmarkNodeFaviconChanged(BookmarkModel* model, |
| 1244 const BookmarkNode* node) override { |
| 1245 updated_nodes_.push_back(node); |
| 1246 } |
| 1247 |
| 1248 void BookmarkNodeChildrenReordered(BookmarkModel* model, |
| 1249 const BookmarkNode* node) override {} |
| 1250 |
| 1251 void BookmarkAllUserNodesRemoved( |
| 1252 BookmarkModel* model, |
| 1253 const std::set<GURL>& removed_urls) override { |
| 1254 } |
| 1255 |
| 1256 TestBookmarkClient client_; |
| 1257 scoped_ptr<BookmarkModel> model_; |
| 1258 std::vector<const BookmarkNode*> updated_nodes_; |
| 1259 |
| 1260 private: |
| 1261 DISALLOW_COPY_AND_ASSIGN(BookmarkModelFaviconTest); |
| 1262 }; |
| 1263 |
| 1264 // Test that BookmarkModel::OnFaviconsChanged() sends a notification that the |
| 1265 // favicon changed to each BookmarkNode which has either a matching page URL |
| 1266 // (e.g. http://www.google.com) or a matching icon URL |
| 1267 // (e.g. http://www.google.com/favicon.ico). |
| 1268 TEST_F(BookmarkModelFaviconTest, FaviconsChangedObserver) { |
| 1269 const BookmarkNode* root = model_->bookmark_bar_node(); |
| 1270 base::string16 kTitle(ASCIIToUTF16("foo")); |
| 1271 GURL kPageURL1("http://www.google.com"); |
| 1272 GURL kPageURL2("http://www.google.ca"); |
| 1273 GURL kPageURL3("http://www.amazon.com"); |
| 1274 GURL kFaviconURL12("http://www.google.com/favicon.ico"); |
| 1275 GURL kFaviconURL3("http://www.amazon.com/favicon.ico"); |
| 1276 |
| 1277 const BookmarkNode* node1 = model_->AddURL(root, 0, kTitle, kPageURL1); |
| 1278 const BookmarkNode* node2 = model_->AddURL(root, 0, kTitle, kPageURL2); |
| 1279 const BookmarkNode* node3 = model_->AddURL(root, 0, kTitle, kPageURL3); |
| 1280 const BookmarkNode* node4 = model_->AddURL(root, 0, kTitle, kPageURL3); |
| 1281 |
| 1282 { |
| 1283 OnFaviconLoaded(AsMutable(node1), kFaviconURL12); |
| 1284 OnFaviconLoaded(AsMutable(node2), kFaviconURL12); |
| 1285 OnFaviconLoaded(AsMutable(node3), kFaviconURL3); |
| 1286 OnFaviconLoaded(AsMutable(node4), kFaviconURL3); |
| 1287 |
| 1288 ClearUpdatedNodes(); |
| 1289 std::set<GURL> changed_page_urls; |
| 1290 changed_page_urls.insert(kPageURL2); |
| 1291 changed_page_urls.insert(kPageURL3); |
| 1292 model_->OnFaviconsChanged(changed_page_urls, GURL()); |
| 1293 ASSERT_EQ(3u, updated_nodes_.size()); |
| 1294 EXPECT_TRUE(WasNodeUpdated(node2)); |
| 1295 EXPECT_TRUE(WasNodeUpdated(node3)); |
| 1296 EXPECT_TRUE(WasNodeUpdated(node4)); |
| 1297 } |
| 1298 |
| 1299 { |
| 1300 // Reset the favicon data because BookmarkModel::OnFaviconsChanged() clears |
| 1301 // the BookmarkNode's favicon data for all of the BookmarkNodes whose |
| 1302 // favicon data changed. |
| 1303 OnFaviconLoaded(AsMutable(node1), kFaviconURL12); |
| 1304 OnFaviconLoaded(AsMutable(node2), kFaviconURL12); |
| 1305 OnFaviconLoaded(AsMutable(node3), kFaviconURL3); |
| 1306 OnFaviconLoaded(AsMutable(node4), kFaviconURL3); |
| 1307 |
| 1308 ClearUpdatedNodes(); |
| 1309 model_->OnFaviconsChanged(std::set<GURL>(), kFaviconURL12); |
| 1310 ASSERT_EQ(2u, updated_nodes_.size()); |
| 1311 EXPECT_TRUE(WasNodeUpdated(node1)); |
| 1312 EXPECT_TRUE(WasNodeUpdated(node2)); |
| 1313 } |
| 1314 |
| 1315 { |
| 1316 OnFaviconLoaded(AsMutable(node1), kFaviconURL12); |
| 1317 OnFaviconLoaded(AsMutable(node2), kFaviconURL12); |
| 1318 OnFaviconLoaded(AsMutable(node3), kFaviconURL3); |
| 1319 OnFaviconLoaded(AsMutable(node4), kFaviconURL3); |
| 1320 |
| 1321 ClearUpdatedNodes(); |
| 1322 std::set<GURL> changed_page_urls; |
| 1323 changed_page_urls.insert(kPageURL1); |
| 1324 model_->OnFaviconsChanged(changed_page_urls, kFaviconURL12); |
| 1325 ASSERT_EQ(2u, updated_nodes_.size()); |
| 1326 EXPECT_TRUE(WasNodeUpdated(node1)); |
| 1327 EXPECT_TRUE(WasNodeUpdated(node2)); |
| 1328 } |
| 1329 } |
| 1330 |
1184 } // namespace bookmarks | 1331 } // namespace bookmarks |
OLD | NEW |