android_history_provider_service_unittest.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file. 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/history/android/android_history_provider_service.h" 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/utf_string_conversions.h" 8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/time/time.h" 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/history/android/android_history_types.h" 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/chrome_constants.h" 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/test/base/testing_browser_process.h" 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/test/base/testing_profile.h" 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/test/base/testing_profile_manager.h" 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/test/base/ui_test_utils.h" 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/browser_thread.h" 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/test/test_browser_thread.h" 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace { 2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using base::Bind; 2268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)using base::Time; 2368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)using content::BrowserThread; 2468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)using history::AndroidStatement; 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using history::HistoryAndBookmarkRow; 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using history::SearchRow; 27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The test cases in this file don't intent to test the detail features of 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Android content provider which have been covered by 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// android_provider_backend_unittest.cc, instead, they verify the code path to 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// AndroidProviderBackend working fine. 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class AndroidHistoryProviderServiceTest : public testing::Test { 342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public: 352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) AndroidHistoryProviderServiceTest() 362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : profile_manager_( 372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) TestingBrowserProcess::GetGlobal()), 382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ui_thread_(BrowserThread::UI, &message_loop_), 392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) file_thread_(BrowserThread::FILE, &message_loop_) { 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual ~AndroidHistoryProviderServiceTest() { 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) protected: 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void SetUp() OVERRIDE { 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Setup the testing profile, so the bookmark_model_sql_handler could 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // get the bookmark model from it. 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ASSERT_TRUE(profile_manager_.SetUp()); 492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // It seems that the name has to be chrome::kInitialProfile, so it 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // could be found by ProfileManager::GetLastUsedProfile(). 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) testing_profile_ = profile_manager_.CreateTestingProfile( 52 chrome::kInitialProfile); 53 54 testing_profile_->CreateBookmarkModel(true); 55 ui_test_utils::WaitForBookmarkModelToLoad(testing_profile_); 56 testing_profile_->CreateHistoryService(true, false); 57 service_.reset(new AndroidHistoryProviderService(testing_profile_)); 58 } 59 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 79class CallbackHelper : public base::RefCountedThreadSafe<CallbackHelper> { 80 public: 81 CallbackHelper() 82 : success_(false), 83 statement_(NULL), 84 cursor_position_(0), 85 count_(0) { 86 } 87 88 bool success() const { 89 return success_; 90 } 91 92 AndroidStatement* statement() const { 93 return statement_; 94 } 95 96 int cursor_position() const { 97 return cursor_position_; 98 } 99 100 int count() const { 101 return count_; 102 } 103 104 void OnInserted(AndroidHistoryProviderService::Handle handle, 105 bool success, 106 int64 id) { 107 success_ = success; 108 base::MessageLoop::current()->Quit(); 109 } 110 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 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 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 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>; 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 154TEST_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<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<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(), 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()); 205} 206 207TEST_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()); 213 214 scoped_refptr<CallbackHelper> callback(new CallbackHelper()); 215 216 // Insert a row and verify it succeeded. 217 service_->InsertSearchTerm(search_row, &cancelable_consumer_, 218 Bind(&CallbackHelper::OnInserted, callback.get())); 219 220 base::MessageLoop::current()->Run(); 221 EXPECT_TRUE(callback->success()); 222 223 std::vector<SearchRow::ColumnID> projections; 224 projections.push_back(SearchRow::ID); 225 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()); 232 233 // Move the cursor to the begining and verify whether we could get 234 // the same result. 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); 243 244 // Update the row. 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()); 253 254 // Delete the row. 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()); 260} 261 262} // namespace 263