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

Side by Side Diff: chrome/browser/history/shortcuts_backend_unittest.cc

Issue 10701043: Make ShortcutsBackend a ProfileKeyedService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 5 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
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 "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/message_loop.h" 6 #include "base/message_loop.h"
7 #include "base/path_service.h" 7 #include "base/path_service.h"
8 #include "base/scoped_temp_dir.h" 8 #include "base/scoped_temp_dir.h"
9 #include "base/stringprintf.h" 9 #include "base/stringprintf.h"
10 #include "base/time.h" 10 #include "base/time.h"
11 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/history/shortcuts_backend.h" 12 #include "chrome/browser/history/shortcuts_backend.h"
13 #include "chrome/browser/history/shortcuts_backend_factory.h"
13 #include "chrome/browser/history/shortcuts_database.h" 14 #include "chrome/browser/history/shortcuts_database.h"
15 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/test/test_browser_thread.h" 16 #include "content/public/test/test_browser_thread.h"
15 #include "sql/statement.h" 17 #include "sql/statement.h"
16 18
17 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
18 20
19 using content::BrowserThread; 21 using content::BrowserThread;
20 22
21 namespace history { 23 namespace history {
22 24
23 const base::TimeDelta kMaxRequestWaitTimeout = base::TimeDelta::FromSeconds(1); 25 const base::TimeDelta kMaxRequestWaitTimeout = base::TimeDelta::FromSeconds(1);
24 26
25 class ShortcutsBackendTest : public testing::Test, 27 class ShortcutsBackendTest : public testing::Test,
26 public ShortcutsBackend::ShortcutsBackendObserver { 28 public ShortcutsBackend::ShortcutsBackendObserver {
27 public: 29 public:
28 ShortcutsBackendTest() 30 ShortcutsBackendTest()
29 : ui_thread_(BrowserThread::UI, &ui_message_loop_), 31 : ui_thread_(BrowserThread::UI, &ui_message_loop_),
30 db_thread_(BrowserThread::DB), 32 db_thread_(BrowserThread::DB),
31 load_notified_(false) {} 33 load_notified_(false),
34 changed_notified_(false) {}
32 35
33 void SetUp(); 36 void SetUp();
34 void TearDown(); 37 void TearDown();
35 38
36 virtual void OnShortcutsLoaded() OVERRIDE; 39 virtual void OnShortcutsLoaded() OVERRIDE;
37 virtual void OnShortcutsChanged() OVERRIDE; 40 virtual void OnShortcutsChanged() OVERRIDE;
38 41
39 void InitBackend(); 42 void InitBackend();
40 43
41 ScopedTempDir temp_dir_; 44 TestingProfile profile_;
42 scoped_refptr<ShortcutsBackend> backend_; 45 scoped_refptr<ShortcutsBackend> backend_;
43 MessageLoopForUI ui_message_loop_; 46 MessageLoopForUI ui_message_loop_;
44 content::TestBrowserThread ui_thread_; 47 content::TestBrowserThread ui_thread_;
45 content::TestBrowserThread db_thread_; 48 content::TestBrowserThread db_thread_;
46 49
47 bool load_notified_; 50 bool load_notified_;
51 bool changed_notified_;
48 }; 52 };
49 53
50 void ShortcutsBackendTest::SetUp() { 54 void ShortcutsBackendTest::SetUp() {
51 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 55 db_thread_.Start();
52 backend_ = new ShortcutsBackend(temp_dir_.path(), NULL); 56 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse(
57 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting);
58 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_);
59 ASSERT_TRUE(backend_.get());
53 backend_->AddObserver(this); 60 backend_->AddObserver(this);
54 db_thread_.Start();
55 } 61 }
56 62
57 void ShortcutsBackendTest::TearDown() { 63 void ShortcutsBackendTest::TearDown() {
58 backend_->RemoveObserver(this); 64 backend_->RemoveObserver(this);
59 db_thread_.Stop(); 65 db_thread_.Stop();
60 } 66 }
61 67
62 void ShortcutsBackendTest::OnShortcutsLoaded() { 68 void ShortcutsBackendTest::OnShortcutsLoaded() {
63 load_notified_ = true; 69 load_notified_ = true;
64 MessageLoop::current()->Quit(); 70 MessageLoop::current()->Quit();
65 } 71 }
66 72
67 void ShortcutsBackendTest::OnShortcutsChanged() { 73 void ShortcutsBackendTest::OnShortcutsChanged() {
74 changed_notified_ = true;
68 } 75 }
69 76
70 void ShortcutsBackendTest::InitBackend() { 77 void ShortcutsBackendTest::InitBackend() {
71 EXPECT_FALSE(load_notified_); 78 ShortcutsBackend* backend = ShortcutsBackendFactory::GetForProfile(&profile_);
72 EXPECT_FALSE(backend_->initialized()); 79 ASSERT_TRUE(backend);
73 EXPECT_TRUE(backend_->Init()); 80 ASSERT_FALSE(load_notified_);
81 ASSERT_FALSE(backend_->initialized());
74 MessageLoop::current()->Run(); 82 MessageLoop::current()->Run();
75 EXPECT_TRUE(load_notified_); 83 EXPECT_TRUE(load_notified_);
76 EXPECT_TRUE(backend_->initialized()); 84 EXPECT_TRUE(backend_->initialized());
77 } 85 }
78 86
79 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) { 87 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) {
80 InitBackend(); 88 InitBackend();
89 EXPECT_FALSE(changed_notified_);
81 ShortcutsBackend::Shortcut shortcut("BD85DBA2-8C29-49F9-84AE-48E1E90880DF", 90 ShortcutsBackend::Shortcut shortcut("BD85DBA2-8C29-49F9-84AE-48E1E90880DF",
82 ASCIIToUTF16("goog"), GURL("http://www.google.com"), 91 ASCIIToUTF16("goog"), GURL("http://www.google.com"),
83 ASCIIToUTF16("Google"), 92 ASCIIToUTF16("Google"),
84 AutocompleteMatch::ClassificationsFromString("0,1"), 93 AutocompleteMatch::ClassificationsFromString("0,1"),
85 ASCIIToUTF16("Google"), 94 ASCIIToUTF16("Google"),
86 AutocompleteMatch::ClassificationsFromString("0,1"), base::Time::Now(), 95 AutocompleteMatch::ClassificationsFromString("0,1"), base::Time::Now(),
87 100); 96 100);
88 EXPECT_TRUE(backend_->AddShortcut(shortcut)); 97 EXPECT_TRUE(backend_->AddShortcut(shortcut));
98 EXPECT_TRUE(changed_notified_);
99 changed_notified_ = false;
89 100
90 const ShortcutsBackend::ShortcutMap& shortcuts = backend_->shortcuts_map(); 101 const ShortcutsBackend::ShortcutMap& shortcuts = backend_->shortcuts_map();
91 ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut.text)); 102 ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut.text));
92 EXPECT_EQ(shortcut.id, shortcuts.find(shortcut.text)->second.id); 103 EXPECT_EQ(shortcut.id, shortcuts.find(shortcut.text)->second.id);
93 EXPECT_EQ(shortcut.contents, shortcuts.find(shortcut.text)->second.contents); 104 EXPECT_EQ(shortcut.contents, shortcuts.find(shortcut.text)->second.contents);
94 shortcut.contents = ASCIIToUTF16("Google Web Search"); 105 shortcut.contents = ASCIIToUTF16("Google Web Search");
95 EXPECT_TRUE(backend_->UpdateShortcut(shortcut)); 106 EXPECT_TRUE(backend_->UpdateShortcut(shortcut));
107 EXPECT_TRUE(changed_notified_);
96 EXPECT_EQ(shortcut.id, shortcuts.find(shortcut.text)->second.id); 108 EXPECT_EQ(shortcut.id, shortcuts.find(shortcut.text)->second.id);
97 EXPECT_EQ(shortcut.contents, shortcuts.find(shortcut.text)->second.contents); 109 EXPECT_EQ(shortcut.contents, shortcuts.find(shortcut.text)->second.contents);
98 } 110 }
99 111
100 TEST_F(ShortcutsBackendTest, DeleteShortcuts) { 112 TEST_F(ShortcutsBackendTest, DeleteShortcuts) {
101 InitBackend(); 113 InitBackend();
102 ShortcutsBackend::Shortcut shortcut1("BD85DBA2-8C29-49F9-84AE-48E1E90880DF", 114 ShortcutsBackend::Shortcut shortcut1("BD85DBA2-8C29-49F9-84AE-48E1E90880DF",
103 ASCIIToUTF16("goog"), GURL("http://www.google.com"), 115 ASCIIToUTF16("goog"), GURL("http://www.google.com"),
104 ASCIIToUTF16("Google"), 116 ASCIIToUTF16("Google"),
105 AutocompleteMatch::ClassificationsFromString("0,1,4,0"), 117 AutocompleteMatch::ClassificationsFromString("0,1,4,0"),
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 std::vector<std::string> deleted_ids; 166 std::vector<std::string> deleted_ids;
155 deleted_ids.push_back(shortcut3.id); 167 deleted_ids.push_back(shortcut3.id);
156 deleted_ids.push_back(shortcut4.id); 168 deleted_ids.push_back(shortcut4.id);
157 169
158 EXPECT_TRUE(backend_->DeleteShortcutsWithIds(deleted_ids)); 170 EXPECT_TRUE(backend_->DeleteShortcutsWithIds(deleted_ids));
159 171
160 ASSERT_EQ(0U, shortcuts.size()); 172 ASSERT_EQ(0U, shortcuts.size());
161 } 173 }
162 174
163 } // namespace history 175 } // namespace history
OLDNEW
« no previous file with comments | « chrome/browser/history/shortcuts_backend_factory.cc ('k') | chrome/browser/history/shortcuts_database.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698