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