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