1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/scoped_temp_dir.h"
#include "base/stringprintf.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/autocomplete/shortcuts_provider_shortcut.h"
#include "chrome/browser/history/shortcuts_backend.h"
#include "chrome/browser/history/shortcuts_database.h"
#include "chrome/common/guid.h"
#include "content/browser/browser_thread.h"
#include "sql/statement.h"
#include "testing/gtest/include/gtest/gtest.h"
using shortcuts_provider::Shortcut;
using shortcuts_provider::ShortcutMap;
namespace history {
const base::TimeDelta kMaxRequestWaitTimeout = base::TimeDelta::FromSeconds(1);
class ShortcutsBackendTest : public testing::Test,
public ShortcutsBackend::ShortcutsBackendObserver {
public:
ShortcutsBackendTest()
: ui_thread_(BrowserThread::UI, &ui_message_loop_),
db_thread_(BrowserThread::DB),
load_notified_(false) {}
void SetUp();
void TearDown();
virtual void OnShortcutsLoaded() OVERRIDE;
virtual void OnShortcutsChanged() OVERRIDE;
void InitBackend();
ScopedTempDir temp_dir_;
scoped_refptr<ShortcutsBackend> backend_;
MessageLoopForUI ui_message_loop_;
BrowserThread ui_thread_;
BrowserThread db_thread_;
bool load_notified_;
};
void ShortcutsBackendTest::SetUp() {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
backend_ = new ShortcutsBackend(temp_dir_.path(), NULL);
backend_->AddObserver(this);
db_thread_.Start();
}
void ShortcutsBackendTest::TearDown() {
backend_->RemoveObserver(this);
db_thread_.Stop();
}
void ShortcutsBackendTest::OnShortcutsLoaded() {
load_notified_ = true;
MessageLoop::current()->Quit();
}
void ShortcutsBackendTest::OnShortcutsChanged() {
}
void ShortcutsBackendTest::InitBackend() {
EXPECT_FALSE(load_notified_);
EXPECT_FALSE(backend_->initialized());
EXPECT_TRUE(backend_->Init());
MessageLoop::current()->Run();
EXPECT_TRUE(load_notified_);
EXPECT_TRUE(backend_->initialized());
}
TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) {
InitBackend();
Shortcut shortcut("BD85DBA2-8C29-49F9-84AE-48E1E90880DF",
ASCIIToUTF16("goog"), ASCIIToUTF16("http://www.google.com"),
ASCIIToUTF16("Google"), ASCIIToUTF16("0,1"),
ASCIIToUTF16("Google"), ASCIIToUTF16("0,1"),
base::Time::Now().ToInternalValue(),
100);
EXPECT_TRUE(backend_->AddShortcut(shortcut));
const ShortcutMap& shortcuts = backend_->shortcuts_map();
ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut.text));
EXPECT_EQ(shortcut.id, shortcuts.find(shortcut.text)->second.id);
EXPECT_EQ(shortcut.contents, shortcuts.find(shortcut.text)->second.contents);
shortcut.contents = ASCIIToUTF16("Google Web Search");
EXPECT_TRUE(backend_->UpdateShortcut(shortcut));
EXPECT_EQ(shortcut.id, shortcuts.find(shortcut.text)->second.id);
EXPECT_EQ(shortcut.contents, shortcuts.find(shortcut.text)->second.contents);
}
TEST_F(ShortcutsBackendTest, DeleteShortcuts) {
InitBackend();
Shortcut shortcut1("BD85DBA2-8C29-49F9-84AE-48E1E90880DF",
ASCIIToUTF16("goog"),
ASCIIToUTF16("http://www.google.com"),
ASCIIToUTF16("Google"), ASCIIToUTF16("0,1,4,0"),
ASCIIToUTF16("Google"), ASCIIToUTF16("0,3,4,1"),
base::Time::Now().ToInternalValue(),
100);
EXPECT_TRUE(backend_->AddShortcut(shortcut1));
Shortcut shortcut2("BD85DBA2-8C29-49F9-84AE-48E1E90880E0",
ASCIIToUTF16("gle"), ASCIIToUTF16("http://www.google.com"),
ASCIIToUTF16("Google"), ASCIIToUTF16("0,1"),
ASCIIToUTF16("Google"), ASCIIToUTF16("0,1"),
base::Time::Now().ToInternalValue(),
100);
EXPECT_TRUE(backend_->AddShortcut(shortcut2));
Shortcut shortcut3("BD85DBA2-8C29-49F9-84AE-48E1E90880E1",
ASCIIToUTF16("sp"), ASCIIToUTF16("http://www.sport.com"),
ASCIIToUTF16("Sports"), ASCIIToUTF16("0,1"),
ASCIIToUTF16("Sport news"), ASCIIToUTF16("0,1"),
base::Time::Now().ToInternalValue(),
10);
EXPECT_TRUE(backend_->AddShortcut(shortcut3));
Shortcut shortcut4("BD85DBA2-8C29-49F9-84AE-48E1E90880E2",
ASCIIToUTF16("mov"), ASCIIToUTF16("http://www.film.com"),
ASCIIToUTF16("Movies"), ASCIIToUTF16("0,1"),
ASCIIToUTF16("Movie news"), ASCIIToUTF16("0,1"),
base::Time::Now().ToInternalValue(),
10);
EXPECT_TRUE(backend_->AddShortcut(shortcut4));
const ShortcutMap& shortcuts = backend_->shortcuts_map();
ASSERT_EQ(4U, shortcuts.size());
EXPECT_EQ(shortcut1.id, shortcuts.find(shortcut1.text)->second.id);
EXPECT_EQ(shortcut2.id, shortcuts.find(shortcut2.text)->second.id);
EXPECT_EQ(shortcut3.id, shortcuts.find(shortcut3.text)->second.id);
EXPECT_EQ(shortcut4.id, shortcuts.find(shortcut4.text)->second.id);
EXPECT_TRUE(backend_->DeleteShortcutsWithUrl(shortcut1.url));
ASSERT_EQ(2U, shortcuts.size());
EXPECT_TRUE(shortcuts.end() == shortcuts.find(shortcut1.text));
EXPECT_TRUE(shortcuts.end() == shortcuts.find(shortcut2.text));
ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut3.text));
ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut4.text));
EXPECT_EQ(shortcut3.id, shortcuts.find(shortcut3.text)->second.id);
EXPECT_EQ(shortcut4.id, shortcuts.find(shortcut4.text)->second.id);
std::vector<std::string> deleted_ids;
deleted_ids.push_back(shortcut3.id);
deleted_ids.push_back(shortcut4.id);
EXPECT_TRUE(backend_->DeleteShortcutsWithIds(deleted_ids));
ASSERT_EQ(0U, shortcuts.size());
}
} // namespace history
|