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