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