| OLD | NEW |
| (Empty) | |
| 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 "content/public/browser/browser_thread.h" |
| 15 #include "content/test/test_browser_thread.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 using base::Bind; |
| 21 using base::Time; |
| 22 using content::BrowserThread; |
| 23 using history::AndroidStatement; |
| 24 using history::HistoryAndBookmarkRow; |
| 25 using history::SearchRow; |
| 26 |
| 27 // The test cases in this file don't intent to test the detail features of |
| 28 // Android content provider which have been covered by |
| 29 // android_provider_backend_unittest.cc, instead, they verify the code path to |
| 30 // AndroidProviderBackend working fine. |
| 31 |
| 32 class AndroidHistoryProviderServiceTest : public testing::Test { |
| 33 public: |
| 34 AndroidHistoryProviderServiceTest() |
| 35 : profile_manager_( |
| 36 static_cast<TestingBrowserProcess*>(g_browser_process)), |
| 37 ui_thread_(BrowserThread::UI, &message_loop_), |
| 38 file_thread_(BrowserThread::FILE, &message_loop_) { |
| 39 } |
| 40 virtual ~AndroidHistoryProviderServiceTest() { |
| 41 } |
| 42 |
| 43 protected: |
| 44 virtual void SetUp() OVERRIDE { |
| 45 // Setup the testing profile, so the bookmark_model_sql_handler could |
| 46 // get the bookmark model from it. |
| 47 ASSERT_TRUE(profile_manager_.SetUp()); |
| 48 // It seems that the name has to be chrome::kInitialProfile, so it |
| 49 // could be found by ProfileManager::GetLastUsedProfile(). |
| 50 testing_profile_ = profile_manager_.CreateTestingProfile( |
| 51 chrome::kInitialProfile); |
| 52 |
| 53 testing_profile_->CreateBookmarkModel(true); |
| 54 testing_profile_->BlockUntilBookmarkModelLoaded(); |
| 55 testing_profile_->CreateHistoryService(true, false); |
| 56 service_.reset(new AndroidHistoryProviderService(testing_profile_)); |
| 57 } |
| 58 |
| 59 virtual void TearDown() OVERRIDE { |
| 60 testing_profile_->DestroyHistoryService(); |
| 61 profile_manager_.DeleteTestingProfile(chrome::kInitialProfile); |
| 62 testing_profile_=NULL; |
| 63 } |
| 64 |
| 65 protected: |
| 66 TestingProfileManager profile_manager_; |
| 67 MessageLoop message_loop_; |
| 68 content::TestBrowserThread ui_thread_; |
| 69 content::TestBrowserThread file_thread_; |
| 70 scoped_ptr<AndroidHistoryProviderService> service_; |
| 71 CancelableRequestConsumer cancelable_consumer_; |
| 72 TestingProfile* testing_profile_; |
| 73 |
| 74 private: |
| 75 DISALLOW_COPY_AND_ASSIGN(AndroidHistoryProviderServiceTest); |
| 76 }; |
| 77 |
| 78 class CallbackHelper : public base::RefCountedThreadSafe<CallbackHelper> { |
| 79 public: |
| 80 CallbackHelper() |
| 81 : success_(false), |
| 82 statement_(NULL), |
| 83 cursor_position_(0), |
| 84 count_(0) { |
| 85 } |
| 86 |
| 87 bool success() const { |
| 88 return success_; |
| 89 } |
| 90 |
| 91 AndroidStatement* statement() const { |
| 92 return statement_; |
| 93 } |
| 94 |
| 95 int cursor_position() const { |
| 96 return cursor_position_; |
| 97 } |
| 98 |
| 99 int count() const { |
| 100 return count_; |
| 101 } |
| 102 |
| 103 void OnInserted(AndroidHistoryProviderService::Handle handle, |
| 104 bool success, |
| 105 int64 id) { |
| 106 success_ = success; |
| 107 MessageLoop::current()->Quit(); |
| 108 } |
| 109 |
| 110 void OnQueryResult(AndroidHistoryProviderService::Handle handle, |
| 111 bool success, |
| 112 AndroidStatement* statement) { |
| 113 success_ = success; |
| 114 statement_ = statement; |
| 115 MessageLoop::current()->Quit(); |
| 116 } |
| 117 |
| 118 void OnUpdated(AndroidHistoryProviderService::Handle handle, |
| 119 bool success, |
| 120 int count) { |
| 121 success_ = success; |
| 122 count_ = count; |
| 123 MessageLoop::current()->Quit(); |
| 124 } |
| 125 |
| 126 void OnDeleted(AndroidHistoryProviderService::Handle handle, |
| 127 bool success, |
| 128 int count) { |
| 129 success_ = success; |
| 130 count_ = count; |
| 131 MessageLoop::current()->Quit(); |
| 132 } |
| 133 |
| 134 void OnStatementMoved(AndroidHistoryProviderService::Handle handle, |
| 135 int cursor_position) { |
| 136 cursor_position_ = cursor_position; |
| 137 MessageLoop::current()->Quit(); |
| 138 } |
| 139 |
| 140 private: |
| 141 friend class base::RefCountedThreadSafe<CallbackHelper>; |
| 142 ~CallbackHelper() { |
| 143 } |
| 144 |
| 145 bool success_; |
| 146 AndroidStatement* statement_; |
| 147 int cursor_position_; |
| 148 int count_; |
| 149 |
| 150 DISALLOW_COPY_AND_ASSIGN(CallbackHelper); |
| 151 }; |
| 152 |
| 153 TEST_F(AndroidHistoryProviderServiceTest, TestHistoryAndBookmark) { |
| 154 HistoryAndBookmarkRow row; |
| 155 row.set_raw_url("http://www.google.com"); |
| 156 row.set_url(GURL("http://www.google.com")); |
| 157 |
| 158 scoped_refptr<CallbackHelper> callback(new CallbackHelper()); |
| 159 |
| 160 // Insert a row and verify it succeeded. |
| 161 service_->InsertHistoryAndBookmark(row, &cancelable_consumer_, |
| 162 Bind(&CallbackHelper::OnInserted, callback.get())); |
| 163 |
| 164 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(projections, std::string(), |
| 172 std::vector<string16>(), std::string(), &cancelable_consumer_, |
| 173 Bind(&CallbackHelper::OnQueryResult, callback.get())); |
| 174 MessageLoop::current()->Run(); |
| 175 ASSERT_TRUE(callback->success()); |
| 176 |
| 177 // Move the cursor to the begining and verify whether we could get |
| 178 // the same result. |
| 179 AndroidStatement* statement = callback->statement(); |
| 180 service_->MoveStatement(statement, 0, -1, &cancelable_consumer_, |
| 181 Bind(&CallbackHelper::OnStatementMoved, callback.get())); |
| 182 MessageLoop::current()->Run(); |
| 183 EXPECT_EQ(-1, callback->cursor_position()); |
| 184 EXPECT_TRUE(callback->statement()->statement()->Step()); |
| 185 EXPECT_FALSE(callback->statement()->statement()->Step()); |
| 186 service_->CloseStatement(statement); |
| 187 |
| 188 // Update the row. |
| 189 HistoryAndBookmarkRow update_row; |
| 190 update_row.set_visit_count(3); |
| 191 service_->UpdateHistoryAndBookmarks(update_row, std::string(), |
| 192 std::vector<string16>(), &cancelable_consumer_, |
| 193 Bind(&CallbackHelper::OnUpdated, callback.get())); |
| 194 MessageLoop::current()->Run(); |
| 195 EXPECT_TRUE(callback->success()); |
| 196 EXPECT_EQ(1, callback->count()); |
| 197 |
| 198 // Delete the row. |
| 199 service_->DeleteHistoryAndBookmarks(std::string(), std::vector<string16>(), |
| 200 &cancelable_consumer_, Bind(&CallbackHelper::OnDeleted, callback.get())); |
| 201 MessageLoop::current()->Run(); |
| 202 EXPECT_TRUE(callback->success()); |
| 203 EXPECT_EQ(1, callback->count()); |
| 204 } |
| 205 |
| 206 TEST_F(AndroidHistoryProviderServiceTest, TestSearchTerm) { |
| 207 SearchRow search_row; |
| 208 search_row.set_search_term(UTF8ToUTF16("google")); |
| 209 search_row.set_url(GURL("http://google.com")); |
| 210 search_row.set_template_url_id(1); |
| 211 search_row.set_search_time(Time::Now()); |
| 212 |
| 213 scoped_refptr<CallbackHelper> callback(new CallbackHelper()); |
| 214 |
| 215 // Insert a row and verify it succeeded. |
| 216 service_->InsertSearchTerm(search_row, &cancelable_consumer_, |
| 217 Bind(&CallbackHelper::OnInserted, callback.get())); |
| 218 |
| 219 MessageLoop::current()->Run(); |
| 220 EXPECT_TRUE(callback->success()); |
| 221 |
| 222 std::vector<SearchRow::ColumnID> projections; |
| 223 projections.push_back(SearchRow::ID); |
| 224 |
| 225 // Query the inserted row. |
| 226 service_->QuerySearchTerms(projections, std::string(), |
| 227 std::vector<string16>(), std::string(), &cancelable_consumer_, |
| 228 Bind(&CallbackHelper::OnQueryResult, callback.get())); |
| 229 MessageLoop::current()->Run(); |
| 230 ASSERT_TRUE(callback->success()); |
| 231 |
| 232 // Move the cursor to the begining and verify whether we could get |
| 233 // the same result. |
| 234 AndroidStatement* statement = callback->statement(); |
| 235 service_->MoveStatement(statement, 0, -1, &cancelable_consumer_, |
| 236 Bind(&CallbackHelper::OnStatementMoved, callback.get())); |
| 237 MessageLoop::current()->Run(); |
| 238 EXPECT_EQ(-1, callback->cursor_position()); |
| 239 EXPECT_TRUE(callback->statement()->statement()->Step()); |
| 240 EXPECT_FALSE(callback->statement()->statement()->Step()); |
| 241 service_->CloseStatement(statement); |
| 242 |
| 243 // Update the row. |
| 244 SearchRow update_row; |
| 245 update_row.set_search_time(Time::Now()); |
| 246 service_->UpdateSearchTerms(update_row, std::string(), |
| 247 std::vector<string16>(), &cancelable_consumer_, |
| 248 Bind(&CallbackHelper::OnUpdated, callback.get())); |
| 249 MessageLoop::current()->Run(); |
| 250 EXPECT_TRUE(callback->success()); |
| 251 EXPECT_EQ(1, callback->count()); |
| 252 |
| 253 // Delete the row. |
| 254 service_->DeleteSearchTerms(std::string(), std::vector<string16>(), |
| 255 &cancelable_consumer_, Bind(&CallbackHelper::OnDeleted, callback.get())); |
| 256 MessageLoop::current()->Run(); |
| 257 EXPECT_TRUE(callback->success()); |
| 258 EXPECT_EQ(1, callback->count()); |
| 259 } |
| 260 |
| 261 } // namespace |
| OLD | NEW |