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