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 "base/files/scoped_temp_dir.h" 6 #include "base/message_loop/message_loop.h" 7 #include "base/path_service.h" 8 #include "base/strings/stringprintf.h" 9 #include "base/strings/utf_string_conversions.h" 10 #include "base/time/time.h" 11 #include "chrome/browser/history/shortcuts_backend.h" 12 #include "chrome/browser/history/shortcuts_backend_factory.h" 13 #include "chrome/browser/history/shortcuts_database.h" 14 #include "chrome/test/base/testing_profile.h" 15 #include "content/public/test/test_browser_thread.h" 16 #include "sql/statement.h" 17 18 #include "testing/gtest/include/gtest/gtest.h" 19 20 using content::BrowserThread; 21 22 namespace history { 23 24 const base::TimeDelta kMaxRequestWaitTimeout = base::TimeDelta::FromSeconds(1); 25 26 class ShortcutsBackendTest : public testing::Test, 27 public ShortcutsBackend::ShortcutsBackendObserver { 28 public: 29 ShortcutsBackendTest() 30 : ui_thread_(BrowserThread::UI, &ui_message_loop_), 31 db_thread_(BrowserThread::DB), 32 load_notified_(false), 33 changed_notified_(false) {} 34 35 virtual void SetUp(); 36 virtual void TearDown(); 37 38 virtual void OnShortcutsLoaded() OVERRIDE; 39 virtual void OnShortcutsChanged() OVERRIDE; 40 41 void InitBackend(); 42 43 TestingProfile profile_; 44 scoped_refptr<ShortcutsBackend> backend_; 45 base::MessageLoopForUI ui_message_loop_; 46 content::TestBrowserThread ui_thread_; 47 content::TestBrowserThread db_thread_; 48 49 bool load_notified_; 50 bool changed_notified_; 51 }; 52 53 void ShortcutsBackendTest::SetUp() { 54 db_thread_.Start(); 55 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( 56 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting); 57 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); 58 ASSERT_TRUE(backend_.get()); 59 backend_->AddObserver(this); 60 } 61 62 void ShortcutsBackendTest::TearDown() { 63 backend_->RemoveObserver(this); 64 db_thread_.Stop(); 65 } 66 67 void ShortcutsBackendTest::OnShortcutsLoaded() { 68 load_notified_ = true; 69 base::MessageLoop::current()->Quit(); 70 } 71 72 void ShortcutsBackendTest::OnShortcutsChanged() { 73 changed_notified_ = true; 74 } 75 76 void ShortcutsBackendTest::InitBackend() { 77 ShortcutsBackend* backend = 78 ShortcutsBackendFactory::GetForProfile(&profile_).get(); 79 ASSERT_TRUE(backend); 80 ASSERT_FALSE(load_notified_); 81 ASSERT_FALSE(backend_->initialized()); 82 base::MessageLoop::current()->Run(); 83 EXPECT_TRUE(load_notified_); 84 EXPECT_TRUE(backend_->initialized()); 85 } 86 87 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) { 88 InitBackend(); 89 EXPECT_FALSE(changed_notified_); 90 ShortcutsBackend::Shortcut shortcut("BD85DBA2-8C29-49F9-84AE-48E1E90880DF", 91 ASCIIToUTF16("goog"), GURL("http://www.google.com"), 92 ASCIIToUTF16("Google"), 93 AutocompleteMatch::ClassificationsFromString("0,1"), 94 ASCIIToUTF16("Google"), 95 AutocompleteMatch::ClassificationsFromString("0,1"), base::Time::Now(), 96 100); 97 EXPECT_TRUE(backend_->AddShortcut(shortcut)); 98 EXPECT_TRUE(changed_notified_); 99 changed_notified_ = false; 100 101 const ShortcutsBackend::ShortcutMap& shortcuts = backend_->shortcuts_map(); 102 ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut.text)); 103 EXPECT_EQ(shortcut.id, shortcuts.find(shortcut.text)->second.id); 104 EXPECT_EQ(shortcut.contents, shortcuts.find(shortcut.text)->second.contents); 105 shortcut.contents = ASCIIToUTF16("Google Web Search"); 106 EXPECT_TRUE(backend_->UpdateShortcut(shortcut)); 107 EXPECT_TRUE(changed_notified_); 108 EXPECT_EQ(shortcut.id, shortcuts.find(shortcut.text)->second.id); 109 EXPECT_EQ(shortcut.contents, shortcuts.find(shortcut.text)->second.contents); 110 } 111 112 TEST_F(ShortcutsBackendTest, DeleteShortcuts) { 113 InitBackend(); 114 ShortcutsBackend::Shortcut shortcut1("BD85DBA2-8C29-49F9-84AE-48E1E90880DF", 115 ASCIIToUTF16("goog"), GURL("http://www.google.com"), 116 ASCIIToUTF16("Google"), 117 AutocompleteMatch::ClassificationsFromString("0,1,4,0"), 118 ASCIIToUTF16("Google"), 119 AutocompleteMatch::ClassificationsFromString("0,3,4,1"), 120 base::Time::Now(), 100); 121 EXPECT_TRUE(backend_->AddShortcut(shortcut1)); 122 123 ShortcutsBackend::Shortcut shortcut2("BD85DBA2-8C29-49F9-84AE-48E1E90880E0", 124 ASCIIToUTF16("gle"), GURL("http://www.google.com"), 125 ASCIIToUTF16("Google"), 126 AutocompleteMatch::ClassificationsFromString("0,1"), 127 ASCIIToUTF16("Google"), 128 AutocompleteMatch::ClassificationsFromString("0,1"), base::Time::Now(), 129 100); 130 EXPECT_TRUE(backend_->AddShortcut(shortcut2)); 131 132 ShortcutsBackend::Shortcut shortcut3("BD85DBA2-8C29-49F9-84AE-48E1E90880E1", 133 ASCIIToUTF16("sp"), GURL("http://www.sport.com"), ASCIIToUTF16("Sports"), 134 AutocompleteMatch::ClassificationsFromString("0,1"), 135 ASCIIToUTF16("Sport news"), 136 AutocompleteMatch::ClassificationsFromString("0,1"), base::Time::Now(), 137 10); 138 EXPECT_TRUE(backend_->AddShortcut(shortcut3)); 139 140 ShortcutsBackend::Shortcut shortcut4("BD85DBA2-8C29-49F9-84AE-48E1E90880E2", 141 ASCIIToUTF16("mov"), GURL("http://www.film.com"), ASCIIToUTF16("Movies"), 142 AutocompleteMatch::ClassificationsFromString("0,1"), 143 ASCIIToUTF16("Movie news"), 144 AutocompleteMatch::ClassificationsFromString("0,1"), base::Time::Now(), 145 10); 146 EXPECT_TRUE(backend_->AddShortcut(shortcut4)); 147 148 const ShortcutsBackend::ShortcutMap& shortcuts = backend_->shortcuts_map(); 149 150 ASSERT_EQ(4U, shortcuts.size()); 151 EXPECT_EQ(shortcut1.id, shortcuts.find(shortcut1.text)->second.id); 152 EXPECT_EQ(shortcut2.id, shortcuts.find(shortcut2.text)->second.id); 153 EXPECT_EQ(shortcut3.id, shortcuts.find(shortcut3.text)->second.id); 154 EXPECT_EQ(shortcut4.id, shortcuts.find(shortcut4.text)->second.id); 155 156 EXPECT_TRUE(backend_->DeleteShortcutsWithUrl(shortcut1.url)); 157 158 ASSERT_EQ(2U, shortcuts.size()); 159 EXPECT_TRUE(shortcuts.end() == shortcuts.find(shortcut1.text)); 160 EXPECT_TRUE(shortcuts.end() == shortcuts.find(shortcut2.text)); 161 ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut3.text)); 162 ASSERT_TRUE(shortcuts.end() != shortcuts.find(shortcut4.text)); 163 EXPECT_EQ(shortcut3.id, shortcuts.find(shortcut3.text)->second.id); 164 EXPECT_EQ(shortcut4.id, shortcuts.find(shortcut4.text)->second.id); 165 166 std::vector<std::string> deleted_ids; 167 deleted_ids.push_back(shortcut3.id); 168 deleted_ids.push_back(shortcut4.id); 169 170 EXPECT_TRUE(backend_->DeleteShortcutsWithIds(deleted_ids)); 171 172 ASSERT_EQ(0U, shortcuts.size()); 173 } 174 175 } // namespace history 176