| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/files/scoped_temp_dir.h" | 5 #include "base/files/scoped_temp_dir.h" |
| 6 #include "base/message_loop/message_loop.h" |
| 6 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 7 #include "base/run_loop.h" | |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "chrome/browser/history/shortcuts_backend.h" | 11 #include "chrome/browser/history/shortcuts_backend.h" |
| 12 #include "chrome/browser/history/shortcuts_backend_factory.h" | 12 #include "chrome/browser/history/shortcuts_backend_factory.h" |
| 13 #include "chrome/browser/history/shortcuts_database.h" | 13 #include "chrome/browser/history/shortcuts_database.h" |
| 14 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread.h" |
| 16 #include "sql/statement.h" | 16 #include "sql/statement.h" |
| 17 | 17 |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 using content::BrowserThread; | 20 using content::BrowserThread; |
| 21 | 21 |
| 22 namespace history { | 22 namespace history { |
| 23 | 23 |
| 24 const base::TimeDelta kMaxRequestWaitTimeout = base::TimeDelta::FromSeconds(1); | 24 const base::TimeDelta kMaxRequestWaitTimeout = base::TimeDelta::FromSeconds(1); |
| 25 | 25 |
| 26 class ShortcutsBackendTest : public testing::Test, | 26 class ShortcutsBackendTest : public testing::Test, |
| 27 public ShortcutsBackend::ShortcutsBackendObserver { | 27 public ShortcutsBackend::ShortcutsBackendObserver { |
| 28 public: | 28 public: |
| 29 ShortcutsBackendTest() | 29 ShortcutsBackendTest() |
| 30 : load_notified_(false), | 30 : ui_thread_(BrowserThread::UI, &ui_message_loop_), |
| 31 db_thread_(BrowserThread::DB), |
| 32 load_notified_(false), |
| 31 changed_notified_(false) {} | 33 changed_notified_(false) {} |
| 32 | 34 |
| 33 virtual void SetUp(); | 35 virtual void SetUp(); |
| 34 virtual void TearDown(); | 36 virtual void TearDown(); |
| 35 | 37 |
| 36 virtual void OnShortcutsLoaded() OVERRIDE; | 38 virtual void OnShortcutsLoaded() OVERRIDE; |
| 37 virtual void OnShortcutsChanged() OVERRIDE; | 39 virtual void OnShortcutsChanged() OVERRIDE; |
| 38 | 40 |
| 39 void InitBackend(); | 41 void InitBackend(); |
| 40 | 42 |
| 41 content::TestBrowserThreadBundle thread_bundle_; | |
| 42 TestingProfile profile_; | 43 TestingProfile profile_; |
| 43 scoped_refptr<ShortcutsBackend> backend_; | 44 scoped_refptr<ShortcutsBackend> backend_; |
| 45 base::MessageLoopForUI ui_message_loop_; |
| 46 content::TestBrowserThread ui_thread_; |
| 47 content::TestBrowserThread db_thread_; |
| 44 | 48 |
| 45 bool load_notified_; | 49 bool load_notified_; |
| 46 bool changed_notified_; | 50 bool changed_notified_; |
| 47 base::RunLoop on_loaded_loop_; | |
| 48 }; | 51 }; |
| 49 | 52 |
| 50 void ShortcutsBackendTest::SetUp() { | 53 void ShortcutsBackendTest::SetUp() { |
| 54 db_thread_.Start(); |
| 51 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( | 55 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( |
| 52 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting); | 56 &profile_, &ShortcutsBackendFactory::BuildProfileForTesting); |
| 53 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); | 57 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); |
| 54 ASSERT_TRUE(backend_.get()); | 58 ASSERT_TRUE(backend_.get()); |
| 55 backend_->AddObserver(this); | 59 backend_->AddObserver(this); |
| 56 } | 60 } |
| 57 | 61 |
| 58 void ShortcutsBackendTest::TearDown() { | 62 void ShortcutsBackendTest::TearDown() { |
| 59 backend_->RemoveObserver(this); | 63 backend_->RemoveObserver(this); |
| 64 db_thread_.Stop(); |
| 60 } | 65 } |
| 61 | 66 |
| 62 void ShortcutsBackendTest::OnShortcutsLoaded() { | 67 void ShortcutsBackendTest::OnShortcutsLoaded() { |
| 63 load_notified_ = true; | 68 load_notified_ = true; |
| 64 on_loaded_loop_.Quit(); | 69 base::MessageLoop::current()->Quit(); |
| 65 } | 70 } |
| 66 | 71 |
| 67 void ShortcutsBackendTest::OnShortcutsChanged() { | 72 void ShortcutsBackendTest::OnShortcutsChanged() { |
| 68 changed_notified_ = true; | 73 changed_notified_ = true; |
| 69 } | 74 } |
| 70 | 75 |
| 71 void ShortcutsBackendTest::InitBackend() { | 76 void ShortcutsBackendTest::InitBackend() { |
| 72 ShortcutsBackend* backend = | 77 ShortcutsBackend* backend = |
| 73 ShortcutsBackendFactory::GetForProfile(&profile_).get(); | 78 ShortcutsBackendFactory::GetForProfile(&profile_).get(); |
| 74 ASSERT_TRUE(backend); | 79 ASSERT_TRUE(backend); |
| 75 ASSERT_FALSE(load_notified_); | 80 ASSERT_FALSE(load_notified_); |
| 76 ASSERT_FALSE(backend_->initialized()); | 81 ASSERT_FALSE(backend_->initialized()); |
| 77 on_loaded_loop_.Run(); | 82 base::MessageLoop::current()->Run(); |
| 78 EXPECT_TRUE(load_notified_); | 83 EXPECT_TRUE(load_notified_); |
| 79 EXPECT_TRUE(backend_->initialized()); | 84 EXPECT_TRUE(backend_->initialized()); |
| 80 } | 85 } |
| 81 | 86 |
| 82 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) { | 87 TEST_F(ShortcutsBackendTest, AddAndUpdateShortcut) { |
| 83 InitBackend(); | 88 InitBackend(); |
| 84 EXPECT_FALSE(changed_notified_); | 89 EXPECT_FALSE(changed_notified_); |
| 85 ShortcutsBackend::Shortcut shortcut("BD85DBA2-8C29-49F9-84AE-48E1E90880DF", | 90 ShortcutsBackend::Shortcut shortcut("BD85DBA2-8C29-49F9-84AE-48E1E90880DF", |
| 86 ASCIIToUTF16("goog"), GURL("http://www.google.com"), | 91 ASCIIToUTF16("goog"), GURL("http://www.google.com"), |
| 87 ASCIIToUTF16("Google"), | 92 ASCIIToUTF16("Google"), |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 std::vector<std::string> deleted_ids; | 166 std::vector<std::string> deleted_ids; |
| 162 deleted_ids.push_back(shortcut3.id); | 167 deleted_ids.push_back(shortcut3.id); |
| 163 deleted_ids.push_back(shortcut4.id); | 168 deleted_ids.push_back(shortcut4.id); |
| 164 | 169 |
| 165 EXPECT_TRUE(backend_->DeleteShortcutsWithIds(deleted_ids)); | 170 EXPECT_TRUE(backend_->DeleteShortcutsWithIds(deleted_ids)); |
| 166 | 171 |
| 167 ASSERT_EQ(0U, shortcuts.size()); | 172 ASSERT_EQ(0U, shortcuts.size()); |
| 168 } | 173 } |
| 169 | 174 |
| 170 } // namespace history | 175 } // namespace history |
| OLD | NEW |