Home | History | Annotate | Download | only in android
      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 <vector>
      6 
      7 #include "chrome/browser/history/android/urls_sql_handler.h"
      8 
      9 #include "base/files/file_path.h"
     10 #include "base/files/scoped_temp_dir.h"
     11 #include "base/strings/stringprintf.h"
     12 #include "base/strings/utf_string_conversions.h"
     13 #include "chrome/browser/history/android/visit_sql_handler.h"
     14 #include "chrome/browser/history/history_database.h"
     15 #include "chrome/common/chrome_constants.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 
     18 using base::Time;
     19 using base::TimeDelta;
     20 
     21 namespace history {
     22 
     23 class UrlsSQLHandlerTest : public testing::Test {
     24  public:
     25   UrlsSQLHandlerTest()
     26       : urls_sql_handler_(&history_db_),
     27         visit_sql_handler_(&history_db_) {
     28   }
     29   virtual ~UrlsSQLHandlerTest() {}
     30 
     31  protected:
     32   virtual void SetUp() {
     33     // Get a temporary directory for the test DB files.
     34     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
     35     base::FilePath history_db_name = temp_dir_.path().AppendASCII(
     36         chrome::kHistoryFilename);
     37     ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name));
     38   }
     39 
     40   virtual void TearDown() {
     41   }
     42 
     43   HistoryDatabase history_db_;
     44   base::ScopedTempDir temp_dir_;
     45   UrlsSQLHandler urls_sql_handler_;
     46   VisitSQLHandler visit_sql_handler_;
     47 
     48  private:
     49   DISALLOW_COPY_AND_ASSIGN(UrlsSQLHandlerTest);
     50 };
     51 
     52 // Insert a row only has URL to verify the visit count and last visit time
     53 // are also set by UrlsSQLHandler.
     54 TEST_F(UrlsSQLHandlerTest, InsertURL) {
     55   HistoryAndBookmarkRow row;
     56   row.set_raw_url("http://google.com");
     57   row.set_url(GURL("http://google.com"));
     58 
     59   ASSERT_TRUE(urls_sql_handler_.Insert(&row));
     60   URLRow url_row;
     61   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
     62   EXPECT_EQ(row.url(), url_row.url());
     63   // Both visit count and last visit time are default value.
     64   EXPECT_EQ(0, url_row.visit_count());
     65   EXPECT_EQ(Time(), url_row.last_visit());
     66   // The new row's id was set in url_row correctly.
     67   EXPECT_EQ(row.url_id(), url_row.id());
     68 }
     69 
     70 // Insert a row with last visit time to verify the visit count is set to 1 by
     71 // the UrlsSQLHandler.
     72 TEST_F(UrlsSQLHandlerTest, InsertURLWithLastVisitTime) {
     73   HistoryAndBookmarkRow row;
     74   row.set_raw_url("http://google.com");
     75   row.set_url(GURL("http://google.com"));
     76   row.set_last_visit_time(Time::Now());
     77 
     78   ASSERT_TRUE(urls_sql_handler_.Insert(&row));
     79   URLRow url_row;
     80   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
     81   EXPECT_EQ(row.url_id(), url_row.id());
     82   EXPECT_EQ(row.url(), url_row.url());
     83   // Visit count should be set to 1 automatically.
     84   EXPECT_EQ(1, url_row.visit_count());
     85   EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
     86 }
     87 
     88 // Insert a row with different last visit time and created time to verify the
     89 // visit count is set to 2 by the UrlsSQLHandler.
     90 TEST_F(UrlsSQLHandlerTest, InsertURLWithBothTime) {
     91   HistoryAndBookmarkRow row;
     92   row.set_raw_url("http://google.com");
     93   row.set_url(GURL("http://google.com"));
     94   row.set_last_visit_time(Time::Now());
     95   row.set_created(Time::Now() - TimeDelta::FromDays(1));
     96 
     97   ASSERT_TRUE(urls_sql_handler_.Insert(&row));
     98   URLRow url_row;
     99   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    100   EXPECT_EQ(row.url_id(), url_row.id());
    101   EXPECT_EQ(row.url(), url_row.url());
    102   // Visit count should be set to 2 automatically.
    103   EXPECT_EQ(2, url_row.visit_count());
    104   EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
    105 }
    106 
    107 // Insert a row with created time to verify the visit count is also set to 1
    108 // and last visit time is set to created time by the UrlsSQLHanlder.
    109 TEST_F(UrlsSQLHandlerTest, InsertURLWithCreatedTime) {
    110   HistoryAndBookmarkRow row;
    111   row.set_raw_url("http://google.com");
    112   row.set_url(GURL("http://google.com"));
    113   row.set_title(UTF8ToUTF16("Google"));
    114   row.set_created(Time::Now());
    115 
    116   ASSERT_TRUE(urls_sql_handler_.Insert(&row));
    117   URLRow url_row;
    118   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    119   EXPECT_EQ(row.url_id(), url_row.id());
    120   EXPECT_EQ(row.url(), url_row.url());
    121   // Visit count should be set to 1 automatically.
    122   EXPECT_EQ(1, url_row.visit_count());
    123   // Last visit time should be set as created time.
    124   EXPECT_EQ(row.created(), url_row.last_visit());
    125   EXPECT_EQ(row.title(), url_row.title());
    126 }
    127 
    128 // Insert a row with the visit count as 1 to verify the last visit
    129 // time is set by the UrlsSQLHandler.
    130 TEST_F(UrlsSQLHandlerTest, InsertURLWithVisitCount) {
    131   HistoryAndBookmarkRow row;
    132   row.set_raw_url("http://google.com");
    133   row.set_url(GURL("http://google.com"));
    134   row.set_visit_count(1);
    135 
    136   ASSERT_TRUE(urls_sql_handler_.Insert(&row));
    137   URLRow url_row;
    138   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    139   EXPECT_EQ(row.url_id(), url_row.id());
    140   EXPECT_EQ(row.url(), url_row.url());
    141   EXPECT_EQ(1, url_row.visit_count());
    142   // Last visit time should be set to the time when it inserted.
    143   EXPECT_NE(Time(), url_row.last_visit());
    144 }
    145 
    146 // Insert a row with all columns set.
    147 TEST_F(UrlsSQLHandlerTest, Insert) {
    148   HistoryAndBookmarkRow row;
    149   row.set_raw_url("http://google.com");
    150   row.set_url(GURL("http://google.com"));
    151   row.set_visit_count(10);
    152   row.set_last_visit_time(Time::Now());
    153   row.set_title(UTF8ToUTF16("Google"));
    154 
    155   ASSERT_TRUE(urls_sql_handler_.Insert(&row));
    156   URLRow url_row;
    157   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    158   EXPECT_EQ(row.url_id(), url_row.id());
    159   EXPECT_EQ(row.url(), url_row.url());
    160   EXPECT_EQ(10, url_row.visit_count());
    161   EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
    162   EXPECT_EQ(row.title(), url_row.title());
    163 }
    164 
    165 // Update all columns except URL which can not be updated.
    166 TEST_F(UrlsSQLHandlerTest, Update) {
    167   HistoryAndBookmarkRow row;
    168   row.set_raw_url("http://google.com");
    169   row.set_url(GURL("http://google.com"));
    170   row.set_title(UTF8ToUTF16("Google"));
    171   row.set_visit_count(10);
    172   row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
    173 
    174   ASSERT_TRUE(urls_sql_handler_.Insert(&row));
    175   ASSERT_TRUE(visit_sql_handler_.Insert(&row));
    176   URLRow url_row;
    177   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    178   EXPECT_EQ(row.url(), url_row.url());
    179   EXPECT_EQ(10, url_row.visit_count());
    180   EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
    181 
    182   HistoryAndBookmarkRow update_row;
    183   update_row.set_last_visit_time(Time::Now());
    184   update_row.set_visit_count(1);
    185   update_row.set_title(UTF8ToUTF16("Google Inc"));
    186   TableIDRow id;
    187   id.url_id = url_row.id();
    188   TableIDRows ids;
    189   ids.push_back(id);
    190   ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
    191   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    192   EXPECT_EQ(1, url_row.visit_count());
    193   EXPECT_EQ(update_row.last_visit_time(), url_row.last_visit());
    194   EXPECT_EQ(update_row.title(), url_row.title());
    195 }
    196 
    197 // Update the both time to verify the created time is not impact to visit_count
    198 // as the history will be rebuild.
    199 TEST_F(UrlsSQLHandlerTest, UpdateLastBothTime) {
    200   HistoryAndBookmarkRow row;
    201   row.set_raw_url("http://google.com");
    202   row.set_url(GURL("http://google.com"));
    203   row.set_title(UTF8ToUTF16("Google"));
    204   row.set_visit_count(10);
    205   row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
    206 
    207   ASSERT_TRUE(urls_sql_handler_.Insert(&row));
    208   ASSERT_TRUE(visit_sql_handler_.Insert(&row));
    209   URLRow url_row;
    210   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    211   EXPECT_EQ(row.url(), url_row.url());
    212   EXPECT_EQ(10, url_row.visit_count());
    213   EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
    214 
    215   HistoryAndBookmarkRow update_row1;
    216   update_row1.set_created(url_row.last_visit());
    217   update_row1.set_last_visit_time(Time::Now());
    218 
    219   TableIDRow id;
    220   id.url_id = url_row.id();
    221   TableIDRows ids;
    222   ids.push_back(id);
    223   ASSERT_TRUE(urls_sql_handler_.Update(update_row1, ids));
    224   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    225   EXPECT_EQ(11, url_row.visit_count());
    226   EXPECT_EQ(update_row1.last_visit_time(), url_row.last_visit());
    227 
    228   HistoryAndBookmarkRow update_row;
    229   update_row.set_created(Time::Now());
    230   ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
    231   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    232   // Visit count will not changed.
    233   EXPECT_EQ(11, url_row.visit_count());
    234   EXPECT_EQ(update_row1.last_visit_time(), url_row.last_visit());
    235 }
    236 
    237 // Update the visit count be zero to verify last visit time also set to zero.
    238 TEST_F(UrlsSQLHandlerTest, UpdateVisitCountZero) {
    239   HistoryAndBookmarkRow row;
    240   row.set_raw_url("http://google.com");
    241   row.set_url(GURL("http://google.com"));
    242   row.set_visit_count(100);
    243   row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
    244 
    245   ASSERT_TRUE(urls_sql_handler_.Insert(&row));
    246   ASSERT_TRUE(visit_sql_handler_.Insert(&row));
    247   URLRow url_row;
    248   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    249   EXPECT_EQ(row.url(), url_row.url());
    250   EXPECT_EQ(100, url_row.visit_count());
    251   EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
    252 
    253   HistoryAndBookmarkRow update_row;
    254   update_row.set_visit_count(0);
    255   TableIDRow id;
    256   id.url_id = url_row.id();
    257   TableIDRows ids;
    258   ids.push_back(id);
    259   ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
    260   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    261   EXPECT_EQ(0, url_row.visit_count());
    262   // Last visit is reset.
    263   EXPECT_EQ(Time(), url_row.last_visit());
    264 }
    265 
    266 // Update the last visit time be a time earlier than current one to verify
    267 // update failed.
    268 TEST_F(UrlsSQLHandlerTest, UpdateEarlyLastVisit) {
    269   HistoryAndBookmarkRow row;
    270   row.set_raw_url("http://google.com");
    271   row.set_url(GURL("http://google.com"));
    272   row.set_visit_count(100);
    273   row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
    274 
    275   ASSERT_TRUE(urls_sql_handler_.Insert(&row));
    276   ASSERT_TRUE(visit_sql_handler_.Insert(&row));
    277   URLRow url_row;
    278   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    279   EXPECT_EQ(row.url(), url_row.url());
    280   EXPECT_EQ(100, url_row.visit_count());
    281   EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
    282 
    283   HistoryAndBookmarkRow update_row;
    284   update_row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(11));
    285   TableIDRow id;
    286   id.url_id = url_row.id();
    287   TableIDRows ids;
    288   ids.push_back(id);
    289   EXPECT_FALSE(urls_sql_handler_.Update(update_row, ids));
    290 }
    291 
    292 // Increase the visit count to verify the last visit time is also update.
    293 TEST_F(UrlsSQLHandlerTest, UpdateVisitCountIncreased) {
    294   HistoryAndBookmarkRow row;
    295   row.set_raw_url("http://google.com");
    296   row.set_url(GURL("http://google.com"));
    297   row.set_visit_count(10);
    298   row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
    299 
    300   ASSERT_TRUE(urls_sql_handler_.Insert(&row));
    301   ASSERT_TRUE(visit_sql_handler_.Insert(&row));
    302   URLRow url_row;
    303   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    304   EXPECT_EQ(row.url(), url_row.url());
    305   EXPECT_EQ(10, url_row.visit_count());
    306   EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
    307 
    308   HistoryAndBookmarkRow update_row;
    309   update_row.set_visit_count(11);
    310   TableIDRow id;
    311   id.url_id = url_row.id();
    312   TableIDRows ids;
    313   ids.push_back(id);
    314   ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
    315   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    316   EXPECT_EQ(row.url(), url_row.url());
    317   EXPECT_EQ(11, url_row.visit_count());
    318   EXPECT_LT(row.last_visit_time(), url_row.last_visit());
    319 }
    320 
    321 TEST_F(UrlsSQLHandlerTest, Delete) {
    322   HistoryAndBookmarkRow row;
    323   row.set_raw_url("http://google.com");
    324   row.set_url(GURL("http://google.com"));
    325   row.set_visit_count(10);
    326   row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
    327 
    328   ASSERT_TRUE(urls_sql_handler_.Insert(&row));
    329   URLRow url_row;
    330   ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
    331   EXPECT_EQ(row.url(), url_row.url());
    332   EXPECT_EQ(10, url_row.visit_count());
    333   EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
    334 
    335   TableIDRow id;
    336   id.url_id = url_row.id();
    337   TableIDRows ids;
    338   ids.push_back(id);
    339   ASSERT_TRUE(urls_sql_handler_.Delete(ids));
    340   EXPECT_FALSE(history_db_.GetURLRow(row.url_id(), &url_row));
    341 }
    342 
    343 }  // namespace history
    344