Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: components/bookmarks/core/browser/bookmark_model_unittest.cc

Issue 291503007: bookmarks: Componentize bookmark_model_unittest.cc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rm it from chrome_tests_unit.gypi Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/chrome_tests_unit.gypi ('k') | components/components_tests.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "components/bookmarks/core/browser/bookmark_model.h" 5 #include "components/bookmarks/core/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/path_service.h" 15 #include "base/path_service.h"
16 #include "base/strings/string16.h" 16 #include "base/strings/string16.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_split.h" 18 #include "base/strings/string_split.h"
19 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
20 #include "base/strings/utf_string_conversions.h" 20 #include "base/strings/utf_string_conversions.h"
21 #include "base/time/time.h" 21 #include "base/time/time.h"
22 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
23 #include "chrome/test/base/testing_profile.h"
24 #include "components/bookmarks/core/browser/bookmark_model_observer.h" 22 #include "components/bookmarks/core/browser/bookmark_model_observer.h"
25 #include "components/bookmarks/core/browser/bookmark_utils.h" 23 #include "components/bookmarks/core/browser/bookmark_utils.h"
26 #include "components/bookmarks/core/test/bookmark_test_helpers.h" 24 #include "components/bookmarks/core/test/bookmark_test_helpers.h"
27 #include "components/bookmarks/core/test/test_bookmark_client.h" 25 #include "components/bookmarks/core/test/test_bookmark_client.h"
28 #include "content/public/test/test_browser_thread_bundle.h"
29 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
30 #include "ui/base/models/tree_node_iterator.h" 27 #include "ui/base/models/tree_node_iterator.h"
31 #include "ui/base/models/tree_node_model.h" 28 #include "ui/base/models/tree_node_model.h"
32 #include "url/gurl.h" 29 #include "url/gurl.h"
33 30
34 using base::ASCIIToUTF16; 31 using base::ASCIIToUTF16;
35 using base::Time; 32 using base::Time;
36 using base::TimeDelta; 33 using base::TimeDelta;
37 34
38 namespace { 35 namespace {
(...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 } else { 889 } else {
893 model->AddURL(bb_node, i, child->GetTitle(), 890 model->AddURL(bb_node, i, child->GetTitle(),
894 GURL("http://" + base::UTF16ToASCII(child->GetTitle()))); 891 GURL("http://" + base::UTF16ToASCII(child->GetTitle())));
895 } 892 }
896 } 893 }
897 } 894 }
898 895
899 // Test class that creates a BookmarkModel with a real history backend. 896 // Test class that creates a BookmarkModel with a real history backend.
900 class BookmarkModelTestWithProfile : public testing::Test { 897 class BookmarkModelTestWithProfile : public testing::Test {
901 public: 898 public:
902 BookmarkModelTestWithProfile() 899 BookmarkModelTestWithProfile() {}
903 : bb_model_(NULL) {}
904
905 // testing::Test:
906 virtual void TearDown() OVERRIDE {
907 profile_.reset(NULL);
908 }
909 900
910 protected: 901 protected:
911 // Verifies the contents of the bookmark bar node match the contents of the 902 // Verifies the contents of the bookmark bar node match the contents of the
912 // TestNode. 903 // TestNode.
913 void VerifyModelMatchesNode(TestNode* expected, const BookmarkNode* actual) { 904 void VerifyModelMatchesNode(TestNode* expected, const BookmarkNode* actual) {
914 ASSERT_EQ(expected->child_count(), actual->child_count()); 905 ASSERT_EQ(expected->child_count(), actual->child_count());
915 for (int i = 0; i < expected->child_count(); ++i) { 906 for (int i = 0; i < expected->child_count(); ++i) {
916 TestNode* expected_child = expected->GetChild(i); 907 TestNode* expected_child = expected->GetChild(i);
917 const BookmarkNode* actual_child = actual->GetChild(i); 908 const BookmarkNode* actual_child = actual->GetChild(i);
918 ASSERT_EQ(expected_child->GetTitle(), actual_child->GetTitle()); 909 ASSERT_EQ(expected_child->GetTitle(), actual_child->GetTitle());
(...skipping 10 matching lines...) Expand all
929 } 920 }
930 } 921 }
931 922
932 void VerifyNoDuplicateIDs(BookmarkModel* model) { 923 void VerifyNoDuplicateIDs(BookmarkModel* model) {
933 ui::TreeNodeIterator<const BookmarkNode> it(model->root_node()); 924 ui::TreeNodeIterator<const BookmarkNode> it(model->root_node());
934 base::hash_set<int64> ids; 925 base::hash_set<int64> ids;
935 while (it.has_next()) 926 while (it.has_next())
936 ASSERT_TRUE(ids.insert(it.Next()->id()).second); 927 ASSERT_TRUE(ids.insert(it.Next()->id()).second);
937 } 928 }
938 929
939 void BlockTillBookmarkModelLoaded() { 930 test::TestBookmarkClient client_;
940 bb_model_ = BookmarkModelFactory::GetForProfile(profile_.get()); 931 scoped_ptr<BookmarkModel> model_;
941 test::WaitForBookmarkModelToLoad(bb_model_);
942 }
943
944 // The profile.
945 scoped_ptr<TestingProfile> profile_;
946 BookmarkModel* bb_model_;
947
948 private:
949 content::TestBrowserThreadBundle thread_bundle_;
950 }; 932 };
951 933
952 // Creates a set of nodes in the bookmark bar model, then recreates the 934 // Creates a set of nodes in the bookmark bar model, then recreates the
953 // bookmark bar model which triggers loading from the db and checks the loaded 935 // bookmark bar model which triggers loading from the db and checks the loaded
954 // structure to make sure it is what we first created. 936 // structure to make sure it is what we first created.
955 TEST_F(BookmarkModelTestWithProfile, CreateAndRestore) { 937 TEST_F(BookmarkModelTestWithProfile, CreateAndRestore) {
956 struct TestData { 938 struct TestData {
957 // Structure of the children of the bookmark bar model node. 939 // Structure of the children of the bookmark bar model node.
958 const std::string bbn_contents; 940 const std::string bbn_contents;
959 // Structure of the children of the other node. 941 // Structure of the children of the other node.
960 const std::string other_contents; 942 const std::string other_contents;
961 // Structure of the children of the synced node. 943 // Structure of the children of the synced node.
962 const std::string mobile_contents; 944 const std::string mobile_contents;
963 } data[] = { 945 } data[] = {
964 // See PopulateNodeFromString for a description of these strings. 946 // See PopulateNodeFromString for a description of these strings.
965 { "", "" }, 947 { "", "" },
966 { "a", "b" }, 948 { "a", "b" },
967 { "a [ b ]", "" }, 949 { "a [ b ]", "" },
968 { "", "[ b ] a [ c [ d e [ f ] ] ]" }, 950 { "", "[ b ] a [ c [ d e [ f ] ] ]" },
969 { "a [ b ]", "" }, 951 { "a [ b ]", "" },
970 { "a b c [ d e [ f ] ]", "g h i [ j k [ l ] ]"}, 952 { "a b c [ d e [ f ] ]", "g h i [ j k [ l ] ]"},
971 }; 953 };
972 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { 954 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
973 // Recreate the profile. We need to reset with NULL first so that the last 955 model_ = client_.CreateModel(false);
974 // HistoryService releases the locks on the files it creates and we can
975 // delete them.
976 profile_.reset(NULL);
977 profile_.reset(new TestingProfile());
978 profile_->CreateBookmarkModel(true);
979 ASSERT_TRUE(profile_->CreateHistoryService(true, false));
980 BlockTillBookmarkModelLoaded();
981 956
982 TestNode bbn; 957 TestNode bbn;
983 PopulateNodeFromString(data[i].bbn_contents, &bbn); 958 PopulateNodeFromString(data[i].bbn_contents, &bbn);
984 PopulateBookmarkNode(&bbn, bb_model_, bb_model_->bookmark_bar_node()); 959 PopulateBookmarkNode(&bbn, model_.get(), model_->bookmark_bar_node());
985 960
986 TestNode other; 961 TestNode other;
987 PopulateNodeFromString(data[i].other_contents, &other); 962 PopulateNodeFromString(data[i].other_contents, &other);
988 PopulateBookmarkNode(&other, bb_model_, bb_model_->other_node()); 963 PopulateBookmarkNode(&other, model_.get(), model_->other_node());
989 964
990 TestNode mobile; 965 TestNode mobile;
991 PopulateNodeFromString(data[i].mobile_contents, &mobile); 966 PopulateNodeFromString(data[i].mobile_contents, &mobile);
992 PopulateBookmarkNode(&mobile, bb_model_, bb_model_->mobile_node()); 967 PopulateBookmarkNode(&mobile, model_.get(), model_->mobile_node());
993 968
994 profile_->CreateBookmarkModel(false); 969 VerifyModelMatchesNode(&bbn, model_->bookmark_bar_node());
995 BlockTillBookmarkModelLoaded(); 970 VerifyModelMatchesNode(&other, model_->other_node());
996 971 VerifyModelMatchesNode(&mobile, model_->mobile_node());
997 VerifyModelMatchesNode(&bbn, bb_model_->bookmark_bar_node()); 972 VerifyNoDuplicateIDs(model_.get());
998 VerifyModelMatchesNode(&other, bb_model_->other_node());
999 VerifyModelMatchesNode(&mobile, bb_model_->mobile_node());
1000 VerifyNoDuplicateIDs(bb_model_);
1001 } 973 }
1002 } 974 }
1003 975
1004 TEST_F(BookmarkModelTest, Sort) { 976 TEST_F(BookmarkModelTest, Sort) {
1005 // Populate the bookmark bar node with nodes for 'B', 'a', 'd' and 'C'. 977 // Populate the bookmark bar node with nodes for 'B', 'a', 'd' and 'C'.
1006 // 'C' and 'a' are folders. 978 // 'C' and 'a' are folders.
1007 TestNode bbn; 979 TestNode bbn;
1008 PopulateNodeFromString("B [ a ] d [ a ]", &bbn); 980 PopulateNodeFromString("B [ a ] d [ a ]", &bbn);
1009 const BookmarkNode* parent = model_->bookmark_bar_node(); 981 const BookmarkNode* parent = model_->bookmark_bar_node();
1010 PopulateBookmarkNode(&bbn, model_.get(), parent); 982 PopulateBookmarkNode(&bbn, model_.get(), parent);
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
1151 EXPECT_TRUE(node.DeleteMetaInfo("key2.subkey2.leaf")); 1123 EXPECT_TRUE(node.DeleteMetaInfo("key2.subkey2.leaf"));
1152 EXPECT_FALSE(node.DeleteMetaInfo("key3")); 1124 EXPECT_FALSE(node.DeleteMetaInfo("key3"));
1153 EXPECT_FALSE(node.GetMetaInfo("key1", &out_value)); 1125 EXPECT_FALSE(node.GetMetaInfo("key1", &out_value));
1154 EXPECT_FALSE(node.GetMetaInfo("key2.subkey1", &out_value)); 1126 EXPECT_FALSE(node.GetMetaInfo("key2.subkey1", &out_value));
1155 EXPECT_FALSE(node.GetMetaInfo("key2.subkey2", &out_value)); 1127 EXPECT_FALSE(node.GetMetaInfo("key2.subkey2", &out_value));
1156 EXPECT_FALSE(node.GetMetaInfo("key2.subkey2.leaf", &out_value)); 1128 EXPECT_FALSE(node.GetMetaInfo("key2.subkey2.leaf", &out_value));
1157 EXPECT_FALSE(node.GetMetaInfoMap()); 1129 EXPECT_FALSE(node.GetMetaInfoMap());
1158 } 1130 }
1159 1131
1160 } // namespace 1132 } // namespace
OLDNEW
« no previous file with comments | « chrome/chrome_tests_unit.gypi ('k') | components/components_tests.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698