android_history_provider_service_unittest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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/time.h" 8#include "base/utf_string_conversions.h" 9#include "chrome/browser/history/android/android_history_types.h" 10#include "chrome/common/chrome_constants.h" 11#include "chrome/test/base/testing_browser_process.h" 12#include "chrome/test/base/testing_profile_manager.h" 13#include "chrome/test/base/testing_profile.h" 14#include "chrome/test/base/ui_test_utils.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 19namespace { 20 21using base::Bind; 22using base::Time; 23using content::BrowserThread; 24using history::AndroidStatement; 25using history::HistoryAndBookmarkRow; 26using 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 33class AndroidHistoryProviderServiceTest : public testing::Test { 34 public: 35 AndroidHistoryProviderServiceTest() 36 : profile_manager_( 37 TestingBrowserProcess::GetGlobal()), 38 ui_thread_(BrowserThread::UI, &message_loop_), 39 file_thread_(BrowserThread::FILE, &message_loop_) { 40 } 41 virtual ~AndroidHistoryProviderServiceTest() { 42 } 43 44 protected: 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 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 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 MessageLoop::current()->Quit(); 109 } 110 111 void OnQueryResult(AndroidHistoryProviderService::Handle handle, 112 bool success, 113 AndroidStatement* statement) { 114 success_ = success; 115 statement_ = statement; 116 MessageLoop::current()->Quit(); 117 } 118 119 void OnUpdated(AndroidHistoryProviderService::Handle handle, 120 bool success, 121 int count) { 122 success_ = success; 123 count_ = count; 124 MessageLoop::current()->Quit(); 125 } 126 127 void OnDeleted(AndroidHistoryProviderService::Handle handle, 128 bool success, 129 int count) { 130 success_ = success; 131 count_ = count; 132 MessageLoop::current()->Quit(); 133 } 134 135 void OnStatementMoved(AndroidHistoryProviderService::Handle handle, 136 int cursor_position) { 137 cursor_position_ = cursor_position; 138 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 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 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 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 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 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 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 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 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 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 MessageLoop::current()->Run(); 258 EXPECT_TRUE(callback->success()); 259 EXPECT_EQ(1, callback->count()); 260} 261 262} // namespace 263