Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / history / shortcuts_database_unittest.cc
index c95847f..52324c3 100644 (file)
 #include "chrome/common/chrome_constants.h"
 #include "chrome/common/chrome_paths.h"
 #include "chrome/test/base/testing_profile.h"
+#include "components/omnibox/autocomplete_match_type.h"
 #include "sql/statement.h"
 #include "sql/test/test_helpers.h"
-
 #include "testing/gtest/include/gtest/gtest.h"
+#include "ui/base/page_transition_types.h"
 
+using base::ASCIIToUTF16;
 
 // Helpers --------------------------------------------------------------------
 
@@ -29,29 +31,27 @@ struct ShortcutsDatabaseTestInfo {
   std::string destination_url;
   std::string contents;
   std::string contents_class;
-  std::string stripped_contents_class;  // |contents_class| with matches removed
   std::string description;
   std::string description_class;
-  std::string stripped_description_class;
-  content::PageTransition transition;
-  AutocompleteMatch::Type type;
+  ui::PageTransition transition;
+  AutocompleteMatchType::Type type;
   std::string keyword;
   int days_from_now;
   int number_of_hits;
 } shortcut_test_db[] = {
   { "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", "goog", "www.google.com",
-    "http://www.google.com/", "Google", "0,1,4,0", "0,1,4,0", "Google",
-    "0,3,4,1", "0,1", content::PAGE_TRANSITION_GENERATED,
-    AutocompleteMatchType::SEARCH_HISTORY, "google.com", 1, 100, },
+    "http://www.google.com/", "Google", "0,1,4,0", "Google", "0,1",
+    ui::PAGE_TRANSITION_GENERATED, AutocompleteMatchType::SEARCH_HISTORY,
+    "google.com", 1, 100, },
   { "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", "slash", "slashdot.org",
-    "http://slashdot.org/", "slashdot.org", "0,3,5,1", "0,1",
-    "Slashdot - News for nerds, stuff that matters", "0,2,5,0", "0,0",
-    content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 0,
+    "http://slashdot.org/", "slashdot.org", "0,1",
+    "Slashdot - News for nerds, stuff that matters", "0,0",
+    ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, "", 0,
     100},
   { "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", "news", "slashdot.org",
-    "http://slashdot.org/", "slashdot.org", "0,1", "0,1",
-    "Slashdot - News for nerds, stuff that matters", "0,0,11,2,15,0", "0,0",
-    content::PAGE_TRANSITION_LINK, AutocompleteMatchType::HISTORY_TITLE, "", 0,
+    "http://slashdot.org/", "slashdot.org", "0,1",
+    "Slashdot - News for nerds, stuff that matters", "0,0",
+    ui::PAGE_TRANSITION_LINK, AutocompleteMatchType::HISTORY_TITLE, "", 0,
     5},
 };
 
@@ -84,7 +84,7 @@ class ShortcutsDatabaseTest : public testing::Test {
   void ClearDB();
   size_t CountRecords() const;
 
-  ShortcutsBackend::Shortcut ShortcutFromTestInfo(
+  ShortcutsDatabase::Shortcut ShortcutFromTestInfo(
       const ShortcutsDatabaseTestInfo& info);
 
   void AddAll();
@@ -118,16 +118,14 @@ size_t ShortcutsDatabaseTest::CountRecords() const {
   return static_cast<size_t>(s.ColumnInt(0));
 }
 
-ShortcutsBackend::Shortcut ShortcutsDatabaseTest::ShortcutFromTestInfo(
+ShortcutsDatabase::Shortcut ShortcutsDatabaseTest::ShortcutFromTestInfo(
     const ShortcutsDatabaseTestInfo& info) {
-  return ShortcutsBackend::Shortcut(
+  return ShortcutsDatabase::Shortcut(
       info.guid, ASCIIToUTF16(info.text),
-      ShortcutsBackend::Shortcut::MatchCore(
+      ShortcutsDatabase::Shortcut::MatchCore(
           ASCIIToUTF16(info.fill_into_edit), GURL(info.destination_url),
-          ASCIIToUTF16(info.contents),
-          AutocompleteMatch::ClassificationsFromString(info.contents_class),
-          ASCIIToUTF16(info.description),
-          AutocompleteMatch::ClassificationsFromString(info.description_class),
+          ASCIIToUTF16(info.contents), info.contents_class,
+          ASCIIToUTF16(info.description), info.description_class,
           info.transition, info.type, ASCIIToUTF16(info.keyword)),
       base::Time::Now() - base::TimeDelta::FromDays(info.days_from_now),
       info.number_of_hits);
@@ -156,7 +154,7 @@ TEST_F(ShortcutsDatabaseTest, AddShortcut) {
 
 TEST_F(ShortcutsDatabaseTest, UpdateShortcut) {
   AddAll();
-  ShortcutsBackend::Shortcut shortcut(
+  ShortcutsDatabase::Shortcut shortcut(
       ShortcutFromTestInfo(shortcut_test_db[1]));
   shortcut.match_core.contents = ASCIIToUTF16("gro.todhsals");
   EXPECT_TRUE(db_->UpdateShortcut(shortcut));
@@ -173,7 +171,7 @@ TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithIds) {
   std::vector<std::string> shortcut_ids;
   shortcut_ids.push_back(shortcut_test_db[0].guid);
   shortcut_ids.push_back(shortcut_test_db[2].guid);
-  EXPECT_TRUE(db_->DeleteShortcutsWithIds(shortcut_ids));
+  EXPECT_TRUE(db_->DeleteShortcutsWithIDs(shortcut_ids));
   EXPECT_EQ(arraysize(shortcut_test_db) - 2, CountRecords());
 
   ShortcutsDatabase::GuidToShortcutMap shortcuts;
@@ -190,10 +188,10 @@ TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithIds) {
   EXPECT_TRUE(it == shortcuts.end());
 }
 
-TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithUrl) {
+TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithURL) {
   AddAll();
 
-  EXPECT_TRUE(db_->DeleteShortcutsWithUrl("http://slashdot.org/"));
+  EXPECT_TRUE(db_->DeleteShortcutsWithURL("http://slashdot.org/"));
   EXPECT_EQ(arraysize(shortcut_test_db) - 2, CountRecords());
 
   ShortcutsDatabase::GuidToShortcutMap shortcuts;
@@ -210,41 +208,6 @@ TEST_F(ShortcutsDatabaseTest, DeleteShortcutsWithUrl) {
   EXPECT_TRUE(it == shortcuts.end());
 }
 
-TEST_F(ShortcutsDatabaseTest, LoadShortcuts) {
-  AddAll();
-  ShortcutsDatabase::GuidToShortcutMap shortcuts;
-  db_->LoadShortcuts(&shortcuts);
-
-  for (size_t i = 0; i < arraysize(shortcut_test_db); ++i) {
-    SCOPED_TRACE(base::StringPrintf("Comparing shortcut #%" PRIuS, i));
-    ShortcutsDatabase::GuidToShortcutMap::const_iterator it(
-        shortcuts.find(shortcut_test_db[i].guid));
-    ASSERT_TRUE(it != shortcuts.end());
-    const ShortcutsBackend::Shortcut& shortcut = it->second;
-    EXPECT_EQ(ASCIIToUTF16(shortcut_test_db[i].text), shortcut.text);
-    EXPECT_EQ(ASCIIToUTF16(shortcut_test_db[i].fill_into_edit),
-              shortcut.match_core.fill_into_edit);
-    EXPECT_EQ(shortcut_test_db[i].destination_url,
-              shortcut.match_core.destination_url.spec());
-    EXPECT_EQ(ASCIIToUTF16(shortcut_test_db[i].contents),
-              shortcut.match_core.contents);
-    EXPECT_EQ(shortcut_test_db[i].stripped_contents_class,
-              AutocompleteMatch::ClassificationsToString(
-                  shortcut.match_core.contents_class));
-    EXPECT_EQ(ASCIIToUTF16(shortcut_test_db[i].description),
-              shortcut.match_core.description);
-    EXPECT_EQ(shortcut_test_db[i].stripped_description_class,
-              AutocompleteMatch::ClassificationsToString(
-                  shortcut.match_core.description_class));
-    EXPECT_EQ(shortcut_test_db[i].transition, shortcut.match_core.transition);
-    EXPECT_EQ(shortcut_test_db[i].type, shortcut.match_core.type);
-    EXPECT_EQ(ASCIIToUTF16(shortcut_test_db[i].keyword),
-              shortcut.match_core.keyword);
-    EXPECT_EQ(shortcut_test_db[i].number_of_hits, shortcut.number_of_hits);
-    // We don't bother trying to check the |days_from_now| field, since dealing
-    // with times in unittests is annoying.
-  }
-}
 
 TEST_F(ShortcutsDatabaseTest, DeleteAllShortcuts) {
   AddAll();
@@ -256,14 +219,19 @@ TEST_F(ShortcutsDatabaseTest, DeleteAllShortcuts) {
   EXPECT_EQ(0U, shortcuts.size());
 }
 
-TEST(ShortcutsDatabaseMigrationTest, MigrateV1ToV2) {
-  // Open the v1 test file and use it to create a test database in a temp dir.
+TEST(ShortcutsDatabaseMigrationTest, MigrateTableAddFillIntoEdit) {
+  // Use the pre-v0 test file to create a test database in a temp dir.
   base::FilePath sql_path;
   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &sql_path));
-  sql_path = sql_path.AppendASCII("History").AppendASCII("Shortcuts.v1.sql");
+  sql_path = sql_path.AppendASCII("History").AppendASCII(
+#if defined(OS_ANDROID)
+      "Shortcuts.v1.sql");
+#else
+      "Shortcuts.no_fill_into_edit.sql");
+#endif
   base::ScopedTempDir temp_dir;
   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
-  base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts.db"));
+  base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts1.db"));
   ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path));
 
   CheckV2ColumnExistence(db_path, false);
@@ -289,13 +257,47 @@ TEST(ShortcutsDatabaseMigrationTest, MigrateV1ToV2) {
     EXPECT_EQ(statement.ColumnString(1), statement.ColumnString(0));
 
     // The other three columns have default values.
-    EXPECT_EQ(content::PAGE_TRANSITION_TYPED,
-              static_cast<content::PageTransition>(statement.ColumnInt(2)));
+    EXPECT_EQ(ui::PAGE_TRANSITION_TYPED,
+              ui::PageTransitionFromInt(statement.ColumnInt(2)));
     EXPECT_EQ(AutocompleteMatchType::HISTORY_TITLE,
-              static_cast<AutocompleteMatch::Type>(statement.ColumnInt(3)));
+              static_cast<AutocompleteMatchType::Type>(statement.ColumnInt(3)));
     EXPECT_TRUE(statement.ColumnString(4).empty());
   }
   EXPECT_TRUE(statement.Succeeded());
+#if !defined(OS_WIN)
+  EXPECT_TRUE(temp_dir.Delete());
+#endif
+}
+
+TEST(ShortcutsDatabaseMigrationTest, MigrateV0ToV1) {
+  // Use the v0 test file to create a test database in a temp dir.
+  base::FilePath sql_path;
+  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &sql_path));
+  sql_path = sql_path.AppendASCII("History").AppendASCII("Shortcuts.v0.sql");
+  base::ScopedTempDir temp_dir;
+  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+  base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts2.db"));
+  ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path));
+
+  // Create a ShortcutsDatabase from the test database, which will migrate the
+  // test database to the current version.
+  {
+    scoped_refptr<ShortcutsDatabase> db(new ShortcutsDatabase(db_path));
+    db->Init();
+  }
+
+  // Check that all the old type values got converted to new values.
+  sql::Connection connection;
+  ASSERT_TRUE(connection.Open(db_path));
+  sql::Statement statement(connection.GetUniqueStatement(
+      "SELECT count(1) FROM omni_box_shortcuts WHERE type in (9, 10, 11, 12)"));
+  ASSERT_TRUE(statement.is_valid());
+  while (statement.Step())
+    EXPECT_EQ(0, statement.ColumnInt(0));
+  EXPECT_TRUE(statement.Succeeded());
+#if !defined(OS_WIN)
+  EXPECT_TRUE(temp_dir.Delete());
+#endif
 }
 
 }  // namespace history