| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/sync/test/integration/bookmarks_helper.h" | 5 #include "chrome/browser/sync/test/integration/bookmarks_helper.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
| 24 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
| 25 #include "base/synchronization/waitable_event.h" | 25 #include "base/synchronization/waitable_event.h" |
| 26 #include "base/task/cancelable_task_tracker.h" | 26 #include "base/task/cancelable_task_tracker.h" |
| 27 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 27 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 28 #include "chrome/browser/bookmarks/managed_bookmark_service_factory.h" | 28 #include "chrome/browser/bookmarks/managed_bookmark_service_factory.h" |
| 29 #include "chrome/browser/favicon/favicon_service_factory.h" | 29 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 30 #include "chrome/browser/history/history_service_factory.h" | 30 #include "chrome/browser/history/history_service_factory.h" |
| 31 #include "chrome/browser/profiles/profile.h" | 31 #include "chrome/browser/profiles/profile.h" |
| 32 #include "chrome/browser/sync/profile_sync_service_factory.h" | 32 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 33 #include "chrome/browser/sync/test/integration/await_match_status_change_checker
.h" | |
| 34 #include "chrome/browser/sync/test/integration/multi_client_status_change_checke
r.h" | |
| 35 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" | 33 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" |
| 36 #include "chrome/browser/sync/test/integration/single_client_status_change_check
er.h" | |
| 37 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" | 34 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" |
| 38 #include "chrome/browser/sync/test/integration/sync_test.h" | 35 #include "chrome/browser/sync/test/integration/sync_test.h" |
| 39 #include "chrome/common/chrome_paths.h" | 36 #include "chrome/common/chrome_paths.h" |
| 40 #include "components/bookmarks/browser/bookmark_client.h" | 37 #include "components/bookmarks/browser/bookmark_client.h" |
| 41 #include "components/bookmarks/browser/bookmark_model.h" | 38 #include "components/bookmarks/browser/bookmark_model.h" |
| 42 #include "components/bookmarks/browser/bookmark_model_observer.h" | 39 #include "components/bookmarks/browser/bookmark_model_observer.h" |
| 43 #include "components/bookmarks/browser/bookmark_utils.h" | 40 #include "components/bookmarks/browser/bookmark_utils.h" |
| 44 #include "components/bookmarks/managed/managed_bookmark_service.h" | 41 #include "components/bookmarks/managed/managed_bookmark_service.h" |
| 45 #include "components/browser_sync/profile_sync_service.h" | 42 #include "components/browser_sync/profile_sync_service.h" |
| 46 #include "components/favicon/core/favicon_service.h" | 43 #include "components/favicon/core/favicon_service.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 69 bool RunOnDBThread(history::HistoryBackend* backend, | 66 bool RunOnDBThread(history::HistoryBackend* backend, |
| 70 history::HistoryDatabase* db) override { | 67 history::HistoryDatabase* db) override { |
| 71 content::RunAllPendingInMessageLoop(); | 68 content::RunAllPendingInMessageLoop(); |
| 72 done_->Signal(); | 69 done_->Signal(); |
| 73 return true; | 70 return true; |
| 74 } | 71 } |
| 75 | 72 |
| 76 void DoneRunOnMainThread() override {} | 73 void DoneRunOnMainThread() override {} |
| 77 | 74 |
| 78 private: | 75 private: |
| 79 ~HistoryEmptyTask() override {} | |
| 80 | |
| 81 base::WaitableEvent* done_; | 76 base::WaitableEvent* done_; |
| 82 }; | 77 }; |
| 83 | 78 |
| 84 // Helper class used to wait for changes to take effect on the favicon of a | 79 // Helper class used to wait for changes to take effect on the favicon of a |
| 85 // particular bookmark node in a particular bookmark model. | 80 // particular bookmark node in a particular bookmark model. |
| 86 class FaviconChangeObserver : public bookmarks::BookmarkModelObserver { | 81 class FaviconChangeObserver : public bookmarks::BookmarkModelObserver { |
| 87 public: | 82 public: |
| 88 FaviconChangeObserver(BookmarkModel* model, const BookmarkNode* node) | 83 FaviconChangeObserver(BookmarkModel* model, const BookmarkNode* node) |
| 89 : model_(model), | 84 : model_(model), |
| 90 node_(node), | 85 node_(node), |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 struct FaviconData { | 210 struct FaviconData { |
| 216 FaviconData() { | 211 FaviconData() { |
| 217 } | 212 } |
| 218 | 213 |
| 219 FaviconData(const gfx::Image& favicon_image, | 214 FaviconData(const gfx::Image& favicon_image, |
| 220 const GURL& favicon_url) | 215 const GURL& favicon_url) |
| 221 : image(favicon_image), | 216 : image(favicon_image), |
| 222 icon_url(favicon_url) { | 217 icon_url(favicon_url) { |
| 223 } | 218 } |
| 224 | 219 |
| 225 ~FaviconData() { | |
| 226 } | |
| 227 | |
| 228 gfx::Image image; | 220 gfx::Image image; |
| 229 GURL icon_url; | 221 GURL icon_url; |
| 230 }; | 222 }; |
| 231 | 223 |
| 232 // Gets the favicon and icon URL associated with |node| in |model|. | 224 // Gets the favicon and icon URL associated with |node| in |model|. |
| 233 FaviconData GetFaviconData(BookmarkModel* model, | 225 FaviconData GetFaviconData(BookmarkModel* model, |
| 234 const BookmarkNode* node) { | 226 const BookmarkNode* node) { |
| 235 // If a favicon wasn't explicitly set for a particular URL, simply return its | 227 // If a favicon wasn't explicitly set for a particular URL, simply return its |
| 236 // blank favicon. | 228 // blank favicon. |
| 237 if (!urls_with_favicons_ || | 229 if (!urls_with_favicons_ || |
| (...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 781 | 773 |
| 782 for (int i = 1; i < sync_datatype_helper::test()->num_clients(); ++i) { | 774 for (int i = 1; i < sync_datatype_helper::test()->num_clients(); ++i) { |
| 783 if (!ModelsMatch(0, i)) { | 775 if (!ModelsMatch(0, i)) { |
| 784 LOG(ERROR) << "Model " << i << " does not match Model 0."; | 776 LOG(ERROR) << "Model " << i << " does not match Model 0."; |
| 785 return false; | 777 return false; |
| 786 } | 778 } |
| 787 } | 779 } |
| 788 return true; | 780 return true; |
| 789 } | 781 } |
| 790 | 782 |
| 791 namespace { | |
| 792 | |
| 793 // Helper class used in the implementation of AwaitAllModelsMatch. | |
| 794 class AllModelsMatchChecker : public MultiClientStatusChangeChecker { | |
| 795 public: | |
| 796 AllModelsMatchChecker(); | |
| 797 ~AllModelsMatchChecker() override; | |
| 798 | |
| 799 bool IsExitConditionSatisfied() override; | |
| 800 std::string GetDebugMessage() const override; | |
| 801 }; | |
| 802 | |
| 803 AllModelsMatchChecker::AllModelsMatchChecker() | |
| 804 : MultiClientStatusChangeChecker( | |
| 805 sync_datatype_helper::test()->GetSyncServices()) {} | |
| 806 | |
| 807 AllModelsMatchChecker::~AllModelsMatchChecker() {} | |
| 808 | |
| 809 bool AllModelsMatchChecker::IsExitConditionSatisfied() { | |
| 810 return AllModelsMatch(); | |
| 811 } | |
| 812 | |
| 813 std::string AllModelsMatchChecker::GetDebugMessage() const { | |
| 814 return "Waiting for matching models"; | |
| 815 } | |
| 816 | |
| 817 } // namespace | |
| 818 | |
| 819 bool AwaitAllModelsMatch() { | |
| 820 AllModelsMatchChecker checker; | |
| 821 checker.Wait(); | |
| 822 return !checker.TimedOut(); | |
| 823 } | |
| 824 | |
| 825 namespace { | |
| 826 | |
| 827 // TODO(pvalenzuela): Remove this class and instead use | |
| 828 // AwaitMatchStatusChangeChecker. | |
| 829 class CountBookmarksWithTitlesMatchingChecker | |
| 830 : public SingleClientStatusChangeChecker { | |
| 831 public: | |
| 832 CountBookmarksWithTitlesMatchingChecker( | |
| 833 browser_sync::ProfileSyncService* service, | |
| 834 int profile_index, | |
| 835 const std::string& title, | |
| 836 int expected_count) | |
| 837 : SingleClientStatusChangeChecker(service), | |
| 838 profile_index_(profile_index), | |
| 839 title_(title), | |
| 840 expected_count_(expected_count) { | |
| 841 DCHECK_GE(expected_count, 0) << "expected_count must be non-negative."; | |
| 842 } | |
| 843 | |
| 844 bool IsExitConditionSatisfied() override { | |
| 845 int actual_count = CountBookmarksWithTitlesMatching(profile_index_, title_); | |
| 846 return expected_count_ == actual_count; | |
| 847 } | |
| 848 | |
| 849 std::string GetDebugMessage() const override { | |
| 850 return "Waiting for bookmark count to match"; | |
| 851 } | |
| 852 | |
| 853 private: | |
| 854 const int profile_index_; | |
| 855 const std::string title_; | |
| 856 const int expected_count_; | |
| 857 }; | |
| 858 | |
| 859 } // namespace | |
| 860 | |
| 861 bool AwaitCountBookmarksWithTitlesMatching(int profile, | |
| 862 const std::string& title, | |
| 863 int expected_count) { | |
| 864 browser_sync::ProfileSyncService* service = | |
| 865 sync_datatype_helper::test()->GetSyncService(profile); | |
| 866 CountBookmarksWithTitlesMatchingChecker checker(service, | |
| 867 profile, | |
| 868 title, | |
| 869 expected_count); | |
| 870 checker.Wait(); | |
| 871 return !checker.TimedOut(); | |
| 872 } | |
| 873 | |
| 874 | |
| 875 bool BookmarkCountsByUrlMatch(int profile, | |
| 876 const GURL& url, | |
| 877 int expected_count) { | |
| 878 int actual_count = CountBookmarksWithUrlsMatching(profile, url); | |
| 879 if (expected_count != actual_count) { | |
| 880 DVLOG(1) << base::StringPrintf("Expected %d URL(s), but there were %d.", | |
| 881 expected_count, | |
| 882 actual_count); | |
| 883 return false; | |
| 884 } | |
| 885 return true; | |
| 886 } | |
| 887 | |
| 888 bool AwaitCountBookmarksWithUrlsMatching(int profile, | |
| 889 const GURL& url, | |
| 890 int expected_count) { | |
| 891 AwaitMatchStatusChangeChecker checker(base::Bind(BookmarkCountsByUrlMatch, | |
| 892 profile, | |
| 893 base::ConstRef(url), | |
| 894 expected_count), | |
| 895 "Bookmark URL counts match."); | |
| 896 checker.Wait(); | |
| 897 return !checker.TimedOut(); | |
| 898 } | |
| 899 | |
| 900 bool ContainsDuplicateBookmarks(int profile) { | 783 bool ContainsDuplicateBookmarks(int profile) { |
| 901 ui::TreeNodeIterator<const BookmarkNode> iterator( | 784 ui::TreeNodeIterator<const BookmarkNode> iterator( |
| 902 GetBookmarkModel(profile)->root_node()); | 785 GetBookmarkModel(profile)->root_node()); |
| 903 while (iterator.has_next()) { | 786 while (iterator.has_next()) { |
| 904 const BookmarkNode* node = iterator.Next(); | 787 const BookmarkNode* node = iterator.Next(); |
| 905 if (node->is_folder()) | 788 if (node->is_folder()) |
| 906 continue; | 789 continue; |
| 907 std::vector<const BookmarkNode*> nodes; | 790 std::vector<const BookmarkNode*> nodes; |
| 908 GetBookmarkModel(profile)->GetNodesByURL(node->url(), &nodes); | 791 GetBookmarkModel(profile)->GetNodesByURL(node->url(), &nodes); |
| 909 EXPECT_GE(nodes.size(), 1U); | 792 EXPECT_GE(nodes.size(), 1U); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1004 | 887 |
| 1005 std::string IndexedSubfolderName(int i) { | 888 std::string IndexedSubfolderName(int i) { |
| 1006 return base::StringPrintf("Subfolder Name %d", i); | 889 return base::StringPrintf("Subfolder Name %d", i); |
| 1007 } | 890 } |
| 1008 | 891 |
| 1009 std::string IndexedSubsubfolderName(int i) { | 892 std::string IndexedSubsubfolderName(int i) { |
| 1010 return base::StringPrintf("Subsubfolder Name %d", i); | 893 return base::StringPrintf("Subsubfolder Name %d", i); |
| 1011 } | 894 } |
| 1012 | 895 |
| 1013 } // namespace bookmarks_helper | 896 } // namespace bookmarks_helper |
| 897 |
| 898 BookmarksMatchChecker::BookmarksMatchChecker() |
| 899 : MultiClientStatusChangeChecker( |
| 900 sync_datatype_helper::test()->GetSyncServices()) {} |
| 901 |
| 902 bool BookmarksMatchChecker::IsExitConditionSatisfied() { |
| 903 return bookmarks_helper::AllModelsMatch(); |
| 904 } |
| 905 |
| 906 std::string BookmarksMatchChecker::GetDebugMessage() const { |
| 907 return "Waiting for matching models"; |
| 908 } |
| 909 |
| 910 BookmarksTitleChecker::BookmarksTitleChecker(int profile_index, |
| 911 const std::string& title, |
| 912 int expected_count) |
| 913 : SingleClientStatusChangeChecker( |
| 914 sync_datatype_helper::test()->GetSyncService(profile_index)), |
| 915 profile_index_(profile_index), |
| 916 title_(title), |
| 917 expected_count_(expected_count) { |
| 918 DCHECK_GE(expected_count, 0) << "expected_count must be non-negative."; |
| 919 } |
| 920 |
| 921 bool BookmarksTitleChecker::IsExitConditionSatisfied() { |
| 922 int actual_count = bookmarks_helper::CountBookmarksWithTitlesMatching( |
| 923 profile_index_, title_); |
| 924 return expected_count_ == actual_count; |
| 925 } |
| 926 |
| 927 std::string BookmarksTitleChecker::GetDebugMessage() const { |
| 928 return "Waiting for bookmark count to match"; |
| 929 } |
| 930 |
| 931 namespace { |
| 932 |
| 933 bool BookmarkCountsByUrlMatch(int profile, |
| 934 const GURL& url, |
| 935 int expected_count) { |
| 936 int actual_count = |
| 937 bookmarks_helper::CountBookmarksWithUrlsMatching(profile, url); |
| 938 if (expected_count != actual_count) { |
| 939 DVLOG(1) << base::StringPrintf("Expected %d URL(s), but there were %d.", |
| 940 expected_count, actual_count); |
| 941 return false; |
| 942 } |
| 943 return true; |
| 944 } |
| 945 |
| 946 } // namespace |
| 947 |
| 948 BookmarksUrlChecker::BookmarksUrlChecker(int profile, |
| 949 const GURL& url, |
| 950 int expected_count) |
| 951 : AwaitMatchStatusChangeChecker(base::Bind(BookmarkCountsByUrlMatch, |
| 952 profile, |
| 953 base::ConstRef(url), |
| 954 expected_count), |
| 955 "Bookmark URL counts match.") {} |
| OLD | NEW |