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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/history/android/android_history_provider_service.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/time/time.h"
9 #include "chrome/browser/bookmarks/bookmark_test_helpers.h"
10 #include "chrome/browser/history/android/android_history_types.h"
11 #include "chrome/common/chrome_constants.h"
12 #include "chrome/test/base/testing_browser_process.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "chrome/test/base/testing_profile_manager.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/test/test_browser_thread.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace {
20
21 using base::Bind;
22 using base::Time;
23 using content::BrowserThread;
24 using history::AndroidStatement;
25 using history::HistoryAndBookmarkRow;
26 using history::SearchRow;
27
28 // The test cases in this file don't intent to test the detail features of
29 // Android content provider which have been covered by
30 // android_provider_backend_unittest.cc, instead, they verify the code path to
31 // AndroidProviderBackend working fine.
32
33 class AndroidHistoryProviderServiceTest : public testing::Test {
34 public:
AndroidHistoryProviderServiceTest()35 AndroidHistoryProviderServiceTest()
36 : profile_manager_(
37 TestingBrowserProcess::GetGlobal()),
38 ui_thread_(BrowserThread::UI, &message_loop_),
39 file_thread_(BrowserThread::FILE, &message_loop_) {
40 }
~AndroidHistoryProviderServiceTest()41 virtual ~AndroidHistoryProviderServiceTest() {
42 }
43
44 protected:
SetUp()45 virtual void SetUp() OVERRIDE {
46 // Setup the testing profile, so the bookmark_model_sql_handler could
47 // get the bookmark model from it.
48 ASSERT_TRUE(profile_manager_.SetUp());
49 // It seems that the name has to be chrome::kInitialProfile, so it
50 // could be found by ProfileManager::GetLastUsedProfile().
51 testing_profile_ = profile_manager_.CreateTestingProfile(
52 chrome::kInitialProfile);
53
54 testing_profile_->CreateBookmarkModel(true);
55 test::WaitForBookmarkModelToLoad(testing_profile_);
56 ASSERT_TRUE(testing_profile_->CreateHistoryService(true, false));
57 service_.reset(new AndroidHistoryProviderService(testing_profile_));
58 }
59
TearDown()60 virtual void TearDown() OVERRIDE {
61 testing_profile_->DestroyHistoryService();
62 profile_manager_.DeleteTestingProfile(chrome::kInitialProfile);
63 testing_profile_=NULL;
64 }
65
66 protected:
67 TestingProfileManager profile_manager_;
68 base::MessageLoop message_loop_;
69 content::TestBrowserThread ui_thread_;
70 content::TestBrowserThread file_thread_;
71 scoped_ptr<AndroidHistoryProviderService> service_;
72 CancelableRequestConsumer cancelable_consumer_;
73 TestingProfile* testing_profile_;
74
75 private:
76 DISALLOW_COPY_AND_ASSIGN(AndroidHistoryProviderServiceTest);
77 };
78
79 class CallbackHelper : public base::RefCountedThreadSafe<CallbackHelper> {
80 public:
CallbackHelper()81 CallbackHelper()
82 : success_(false),
83 statement_(NULL),
84 cursor_position_(0),
85 count_(0) {
86 }
87
success() const88 bool success() const {
89 return success_;
90 }
91
statement() const92 AndroidStatement* statement() const {
93 return statement_;
94 }
95
cursor_position() const96 int cursor_position() const {
97 return cursor_position_;
98 }
99
count() const100 int count() const {
101 return count_;
102 }
103
OnInserted(AndroidHistoryProviderService::Handle handle,bool success,int64 id)104 void OnInserted(AndroidHistoryProviderService::Handle handle,
105 bool success,
106 int64 id) {
107 success_ = success;
108 base::MessageLoop::current()->Quit();
109 }
110
OnQueryResult(AndroidHistoryProviderService::Handle handle,bool success,AndroidStatement * statement)111 void OnQueryResult(AndroidHistoryProviderService::Handle handle,
112 bool success,
113 AndroidStatement* statement) {
114 success_ = success;
115 statement_ = statement;
116 base::MessageLoop::current()->Quit();
117 }
118
OnUpdated(AndroidHistoryProviderService::Handle handle,bool success,int count)119 void OnUpdated(AndroidHistoryProviderService::Handle handle,
120 bool success,
121 int count) {
122 success_ = success;
123 count_ = count;
124 base::MessageLoop::current()->Quit();
125 }
126
OnDeleted(AndroidHistoryProviderService::Handle handle,bool success,int count)127 void OnDeleted(AndroidHistoryProviderService::Handle handle,
128 bool success,
129 int count) {
130 success_ = success;
131 count_ = count;
132 base::MessageLoop::current()->Quit();
133 }
134
OnStatementMoved(AndroidHistoryProviderService::Handle handle,int cursor_position)135 void OnStatementMoved(AndroidHistoryProviderService::Handle handle,
136 int cursor_position) {
137 cursor_position_ = cursor_position;
138 base::MessageLoop::current()->Quit();
139 }
140
141 private:
142 friend class base::RefCountedThreadSafe<CallbackHelper>;
~CallbackHelper()143 ~CallbackHelper() {
144 }
145
146 bool success_;
147 AndroidStatement* statement_;
148 int cursor_position_;
149 int count_;
150
151 DISALLOW_COPY_AND_ASSIGN(CallbackHelper);
152 };
153
TEST_F(AndroidHistoryProviderServiceTest,TestHistoryAndBookmark)154 TEST_F(AndroidHistoryProviderServiceTest, TestHistoryAndBookmark) {
155 HistoryAndBookmarkRow row;
156 row.set_raw_url("http://www.google.com");
157 row.set_url(GURL("http://www.google.com"));
158
159 scoped_refptr<CallbackHelper> callback(new CallbackHelper());
160
161 // Insert a row and verify it succeeded.
162 service_->InsertHistoryAndBookmark(row, &cancelable_consumer_,
163 Bind(&CallbackHelper::OnInserted, callback.get()));
164
165 base::MessageLoop::current()->Run();
166 EXPECT_TRUE(callback->success());
167
168 std::vector<HistoryAndBookmarkRow::ColumnID> projections;
169 projections.push_back(HistoryAndBookmarkRow::ID);
170
171 // Query the inserted row.
172 service_->QueryHistoryAndBookmarks(projections, std::string(),
173 std::vector<base::string16>(), std::string(), &cancelable_consumer_,
174 Bind(&CallbackHelper::OnQueryResult, callback.get()));
175 base::MessageLoop::current()->Run();
176 ASSERT_TRUE(callback->success());
177
178 // Move the cursor to the begining and verify whether we could get
179 // the same result.
180 AndroidStatement* statement = callback->statement();
181 service_->MoveStatement(statement, 0, -1, &cancelable_consumer_,
182 Bind(&CallbackHelper::OnStatementMoved, callback.get()));
183 base::MessageLoop::current()->Run();
184 EXPECT_EQ(-1, callback->cursor_position());
185 EXPECT_TRUE(callback->statement()->statement()->Step());
186 EXPECT_FALSE(callback->statement()->statement()->Step());
187 service_->CloseStatement(statement);
188
189 // Update the row.
190 HistoryAndBookmarkRow update_row;
191 update_row.set_visit_count(3);
192 service_->UpdateHistoryAndBookmarks(update_row, std::string(),
193 std::vector<base::string16>(), &cancelable_consumer_,
194 Bind(&CallbackHelper::OnUpdated, callback.get()));
195 base::MessageLoop::current()->Run();
196 EXPECT_TRUE(callback->success());
197 EXPECT_EQ(1, callback->count());
198
199 // Delete the row.
200 service_->DeleteHistoryAndBookmarks(std::string(),
201 std::vector<base::string16>(),
202 &cancelable_consumer_, Bind(&CallbackHelper::OnDeleted, callback.get()));
203 base::MessageLoop::current()->Run();
204 EXPECT_TRUE(callback->success());
205 EXPECT_EQ(1, callback->count());
206 }
207
TEST_F(AndroidHistoryProviderServiceTest,TestSearchTerm)208 TEST_F(AndroidHistoryProviderServiceTest, TestSearchTerm) {
209 SearchRow search_row;
210 search_row.set_search_term(UTF8ToUTF16("google"));
211 search_row.set_url(GURL("http://google.com"));
212 search_row.set_template_url_id(1);
213 search_row.set_search_time(Time::Now());
214
215 scoped_refptr<CallbackHelper> callback(new CallbackHelper());
216
217 // Insert a row and verify it succeeded.
218 service_->InsertSearchTerm(search_row, &cancelable_consumer_,
219 Bind(&CallbackHelper::OnInserted, callback.get()));
220
221 base::MessageLoop::current()->Run();
222 EXPECT_TRUE(callback->success());
223
224 std::vector<SearchRow::ColumnID> projections;
225 projections.push_back(SearchRow::ID);
226
227 // Query the inserted row.
228 service_->QuerySearchTerms(projections, std::string(),
229 std::vector<base::string16>(), std::string(), &cancelable_consumer_,
230 Bind(&CallbackHelper::OnQueryResult, callback.get()));
231 base::MessageLoop::current()->Run();
232 ASSERT_TRUE(callback->success());
233
234 // Move the cursor to the begining and verify whether we could get
235 // the same result.
236 AndroidStatement* statement = callback->statement();
237 service_->MoveStatement(statement, 0, -1, &cancelable_consumer_,
238 Bind(&CallbackHelper::OnStatementMoved, callback.get()));
239 base::MessageLoop::current()->Run();
240 EXPECT_EQ(-1, callback->cursor_position());
241 EXPECT_TRUE(callback->statement()->statement()->Step());
242 EXPECT_FALSE(callback->statement()->statement()->Step());
243 service_->CloseStatement(statement);
244
245 // Update the row.
246 SearchRow update_row;
247 update_row.set_search_time(Time::Now());
248 service_->UpdateSearchTerms(update_row, std::string(),
249 std::vector<base::string16>(), &cancelable_consumer_,
250 Bind(&CallbackHelper::OnUpdated, callback.get()));
251 base::MessageLoop::current()->Run();
252 EXPECT_TRUE(callback->success());
253 EXPECT_EQ(1, callback->count());
254
255 // Delete the row.
256 service_->DeleteSearchTerms(std::string(), std::vector<base::string16>(),
257 &cancelable_consumer_, Bind(&CallbackHelper::OnDeleted, callback.get()));
258 base::MessageLoop::current()->Run();
259 EXPECT_TRUE(callback->success());
260 EXPECT_EQ(1, callback->count());
261 }
262
263 } // namespace
264