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/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"
23 using content::BrowserThread;
24 using history::AndroidStatement;
25 using history::HistoryAndBookmarkRow;
26 using history::SearchRow;
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.
33 class AndroidHistoryProviderServiceTest : public testing::Test {
35 AndroidHistoryProviderServiceTest()
37 TestingBrowserProcess::GetGlobal()),
38 ui_thread_(BrowserThread::UI, &message_loop_),
39 file_thread_(BrowserThread::FILE, &message_loop_) {
41 virtual ~AndroidHistoryProviderServiceTest() {
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);
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_));
60 virtual void TearDown() OVERRIDE {
61 testing_profile_->DestroyHistoryService();
62 profile_manager_.DeleteTestingProfile(chrome::kInitialProfile);
63 testing_profile_=NULL;
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_;
76 DISALLOW_COPY_AND_ASSIGN(AndroidHistoryProviderServiceTest);
79 class CallbackHelper : public base::RefCountedThreadSafe<CallbackHelper> {
88 bool success() const {
92 AndroidStatement* statement() const {
96 int cursor_position() const {
97 return cursor_position_;
104 void OnInserted(AndroidHistoryProviderService::Handle handle,
108 base::MessageLoop::current()->Quit();
111 void OnQueryResult(AndroidHistoryProviderService::Handle handle,
113 AndroidStatement* statement) {
115 statement_ = statement;
116 base::MessageLoop::current()->Quit();
119 void OnUpdated(AndroidHistoryProviderService::Handle handle,
124 base::MessageLoop::current()->Quit();
127 void OnDeleted(AndroidHistoryProviderService::Handle handle,
132 base::MessageLoop::current()->Quit();
135 void OnStatementMoved(AndroidHistoryProviderService::Handle handle,
136 int cursor_position) {
137 cursor_position_ = cursor_position;
138 base::MessageLoop::current()->Quit();
142 friend class base::RefCountedThreadSafe<CallbackHelper>;
147 AndroidStatement* statement_;
148 int cursor_position_;
151 DISALLOW_COPY_AND_ASSIGN(CallbackHelper);
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"));
159 scoped_refptr<CallbackHelper> callback(new CallbackHelper());
161 // Insert a row and verify it succeeded.
162 service_->InsertHistoryAndBookmark(row, &cancelable_consumer_,
163 Bind(&CallbackHelper::OnInserted, callback.get()));
165 base::MessageLoop::current()->Run();
166 EXPECT_TRUE(callback->success());
168 std::vector<HistoryAndBookmarkRow::ColumnID> projections;
169 projections.push_back(HistoryAndBookmarkRow::ID);
171 // Query the inserted row.
172 service_->QueryHistoryAndBookmarks(projections, std::string(),
173 std::vector<string16>(), std::string(), &cancelable_consumer_,
174 Bind(&CallbackHelper::OnQueryResult, callback.get()));
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(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);
190 HistoryAndBookmarkRow update_row;
191 update_row.set_visit_count(3);
192 service_->UpdateHistoryAndBookmarks(update_row, std::string(),
193 std::vector<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());
200 service_->DeleteHistoryAndBookmarks(std::string(), std::vector<string16>(),
201 &cancelable_consumer_, Bind(&CallbackHelper::OnDeleted, callback.get()));
202 base::MessageLoop::current()->Run();
203 EXPECT_TRUE(callback->success());
204 EXPECT_EQ(1, callback->count());
207 TEST_F(AndroidHistoryProviderServiceTest, TestSearchTerm) {
208 SearchRow search_row;
209 search_row.set_search_term(UTF8ToUTF16("google"));
210 search_row.set_url(GURL("http://google.com"));
211 search_row.set_template_url_id(1);
212 search_row.set_search_time(Time::Now());
214 scoped_refptr<CallbackHelper> callback(new CallbackHelper());
216 // Insert a row and verify it succeeded.
217 service_->InsertSearchTerm(search_row, &cancelable_consumer_,
218 Bind(&CallbackHelper::OnInserted, callback.get()));
220 base::MessageLoop::current()->Run();
221 EXPECT_TRUE(callback->success());
223 std::vector<SearchRow::ColumnID> projections;
224 projections.push_back(SearchRow::ID);
226 // Query the inserted row.
227 service_->QuerySearchTerms(projections, std::string(),
228 std::vector<string16>(), std::string(), &cancelable_consumer_,
229 Bind(&CallbackHelper::OnQueryResult, callback.get()));
230 base::MessageLoop::current()->Run();
231 ASSERT_TRUE(callback->success());
233 // Move the cursor to the begining and verify whether we could get
235 AndroidStatement* statement = callback->statement();
236 service_->MoveStatement(statement, 0, -1, &cancelable_consumer_,
237 Bind(&CallbackHelper::OnStatementMoved, callback.get()));
238 base::MessageLoop::current()->Run();
239 EXPECT_EQ(-1, callback->cursor_position());
240 EXPECT_TRUE(callback->statement()->statement()->Step());
241 EXPECT_FALSE(callback->statement()->statement()->Step());
242 service_->CloseStatement(statement);
245 SearchRow update_row;
246 update_row.set_search_time(Time::Now());
247 service_->UpdateSearchTerms(update_row, std::string(),
248 std::vector<string16>(), &cancelable_consumer_,
249 Bind(&CallbackHelper::OnUpdated, callback.get()));
250 base::MessageLoop::current()->Run();
251 EXPECT_TRUE(callback->success());
252 EXPECT_EQ(1, callback->count());
255 service_->DeleteSearchTerms(std::string(), std::vector<string16>(),
256 &cancelable_consumer_, Bind(&CallbackHelper::OnDeleted, callback.get()));
257 base::MessageLoop::current()->Run();
258 EXPECT_TRUE(callback->success());
259 EXPECT_EQ(1, callback->count());