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.
5 #include "chrome/browser/history/android/android_history_provider_service.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/task/cancelable_task_tracker.h"
9 #include "base/time/time.h"
10 #include "chrome/browser/bookmarks/bookmark_model_factory.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 "components/bookmarks/test/bookmark_test_helpers.h"
16 #include "components/history/core/android/android_history_types.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/test/test_browser_thread.h"
19 #include "testing/gtest/include/gtest/gtest.h"
25 using content::BrowserThread;
26 using history::AndroidStatement;
27 using history::HistoryAndBookmarkRow;
28 using history::SearchRow;
30 // The test cases in this file don't intent to test the detail features of
31 // Android content provider which have been covered by
32 // android_provider_backend_unittest.cc, instead, they verify the code path to
33 // AndroidProviderBackend working fine.
35 class AndroidHistoryProviderServiceTest : public testing::Test {
37 AndroidHistoryProviderServiceTest()
39 TestingBrowserProcess::GetGlobal()),
40 ui_thread_(BrowserThread::UI, &message_loop_),
41 file_thread_(BrowserThread::FILE, &message_loop_) {
43 virtual ~AndroidHistoryProviderServiceTest() {
47 virtual void SetUp() OVERRIDE {
48 // Setup the testing profile, so the bookmark_model_sql_handler could
49 // get the bookmark model from it.
50 ASSERT_TRUE(profile_manager_.SetUp());
51 // It seems that the name has to be chrome::kInitialProfile, so it
52 // could be found by ProfileManager::GetLastUsedProfile().
53 testing_profile_ = profile_manager_.CreateTestingProfile(
54 chrome::kInitialProfile);
56 testing_profile_->CreateBookmarkModel(true);
57 test::WaitForBookmarkModelToLoad(
58 BookmarkModelFactory::GetForProfile(testing_profile_));
59 ASSERT_TRUE(testing_profile_->CreateHistoryService(true, false));
60 service_.reset(new AndroidHistoryProviderService(testing_profile_));
63 virtual void TearDown() OVERRIDE {
64 testing_profile_->DestroyHistoryService();
65 profile_manager_.DeleteTestingProfile(chrome::kInitialProfile);
66 testing_profile_=NULL;
70 TestingProfileManager profile_manager_;
71 base::MessageLoop message_loop_;
72 content::TestBrowserThread ui_thread_;
73 content::TestBrowserThread file_thread_;
74 scoped_ptr<AndroidHistoryProviderService> service_;
75 base::CancelableTaskTracker cancelable_tracker_;
76 TestingProfile* testing_profile_;
79 DISALLOW_COPY_AND_ASSIGN(AndroidHistoryProviderServiceTest);
82 class CallbackHelper : public base::RefCountedThreadSafe<CallbackHelper> {
91 bool success() const {
95 AndroidStatement* statement() const {
99 int cursor_position() const {
100 return cursor_position_;
107 void OnInserted(int64 id) {
109 base::MessageLoop::current()->Quit();
112 void OnQueryResult(AndroidStatement* statement) {
113 success_ = statement != NULL;
114 statement_ = statement;
115 base::MessageLoop::current()->Quit();
118 void OnUpdated(int count) {
119 success_ = count != 0;
121 base::MessageLoop::current()->Quit();
124 void OnDeleted(int count) {
125 success_ = count != 0;
127 base::MessageLoop::current()->Quit();
130 void OnStatementMoved(int cursor_position) {
131 cursor_position_ = cursor_position;
132 base::MessageLoop::current()->Quit();
136 friend class base::RefCountedThreadSafe<CallbackHelper>;
141 AndroidStatement* statement_;
142 int cursor_position_;
145 DISALLOW_COPY_AND_ASSIGN(CallbackHelper);
148 TEST_F(AndroidHistoryProviderServiceTest, TestHistoryAndBookmark) {
149 HistoryAndBookmarkRow row;
150 row.set_raw_url("http://www.google.com");
151 row.set_url(GURL("http://www.google.com"));
153 scoped_refptr<CallbackHelper> callback(new CallbackHelper());
155 // Insert a row and verify it succeeded.
156 service_->InsertHistoryAndBookmark(
158 Bind(&CallbackHelper::OnInserted, callback.get()),
159 &cancelable_tracker_);
161 base::MessageLoop::current()->Run();
162 EXPECT_TRUE(callback->success());
164 std::vector<HistoryAndBookmarkRow::ColumnID> projections;
165 projections.push_back(HistoryAndBookmarkRow::ID);
167 // Query the inserted row.
168 service_->QueryHistoryAndBookmarks(
171 std::vector<base::string16>(),
173 Bind(&CallbackHelper::OnQueryResult, callback.get()),
174 &cancelable_tracker_);
175 base::MessageLoop::current()->Run();
176 ASSERT_TRUE(callback->success());
178 // Move the cursor to the begining and verify whether we could get
180 AndroidStatement* statement = callback->statement();
181 service_->MoveStatement(
185 Bind(&CallbackHelper::OnStatementMoved, callback.get()),
186 &cancelable_tracker_);
187 base::MessageLoop::current()->Run();
188 EXPECT_EQ(-1, callback->cursor_position());
189 EXPECT_TRUE(callback->statement()->statement()->Step());
190 EXPECT_FALSE(callback->statement()->statement()->Step());
191 service_->CloseStatement(statement);
194 HistoryAndBookmarkRow update_row;
195 update_row.set_visit_count(3);
196 service_->UpdateHistoryAndBookmarks(
199 std::vector<base::string16>(),
200 Bind(&CallbackHelper::OnUpdated, callback.get()),
201 &cancelable_tracker_);
202 base::MessageLoop::current()->Run();
203 EXPECT_TRUE(callback->success());
204 EXPECT_EQ(1, callback->count());
207 service_->DeleteHistoryAndBookmarks(
209 std::vector<base::string16>(),
210 Bind(&CallbackHelper::OnDeleted, callback.get()),
211 &cancelable_tracker_);
212 base::MessageLoop::current()->Run();
213 EXPECT_TRUE(callback->success());
214 EXPECT_EQ(1, callback->count());
217 TEST_F(AndroidHistoryProviderServiceTest, TestSearchTerm) {
218 SearchRow search_row;
219 search_row.set_search_term(base::UTF8ToUTF16("google"));
220 search_row.set_url(GURL("http://google.com"));
221 search_row.set_keyword_id(1);
222 search_row.set_search_time(Time::Now());
224 scoped_refptr<CallbackHelper> callback(new CallbackHelper());
226 // Insert a row and verify it succeeded.
227 service_->InsertSearchTerm(search_row,
228 Bind(&CallbackHelper::OnInserted, callback.get()),
229 &cancelable_tracker_);
231 base::MessageLoop::current()->Run();
232 EXPECT_TRUE(callback->success());
234 std::vector<SearchRow::ColumnID> projections;
235 projections.push_back(SearchRow::ID);
237 // Query the inserted row.
238 service_->QuerySearchTerms(
241 std::vector<base::string16>(),
243 Bind(&CallbackHelper::OnQueryResult, callback.get()),
244 &cancelable_tracker_);
245 base::MessageLoop::current()->Run();
246 ASSERT_TRUE(callback->success());
248 // Move the cursor to the begining and verify whether we could get
250 AndroidStatement* statement = callback->statement();
251 service_->MoveStatement(
255 Bind(&CallbackHelper::OnStatementMoved, callback.get()),
256 &cancelable_tracker_);
257 base::MessageLoop::current()->Run();
258 EXPECT_EQ(-1, callback->cursor_position());
259 EXPECT_TRUE(callback->statement()->statement()->Step());
260 EXPECT_FALSE(callback->statement()->statement()->Step());
261 service_->CloseStatement(statement);
264 SearchRow update_row;
265 update_row.set_search_time(Time::Now());
266 service_->UpdateSearchTerms(update_row,
268 std::vector<base::string16>(),
269 Bind(&CallbackHelper::OnUpdated, callback.get()),
270 &cancelable_tracker_);
271 base::MessageLoop::current()->Run();
272 EXPECT_TRUE(callback->success());
273 EXPECT_EQ(1, callback->count());
276 service_->DeleteSearchTerms(std::string(),
277 std::vector<base::string16>(),
278 Bind(&CallbackHelper::OnDeleted, callback.get()),
279 &cancelable_tracker_);
280 base::MessageLoop::current()->Run();
281 EXPECT_TRUE(callback->success());
282 EXPECT_EQ(1, callback->count());