OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/extensions/api/downloads/downloads_api.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "chrome/browser/download/download_history.h" |
| 10 #include "chrome/browser/download/download_service_factory.h" |
| 11 #include "chrome/browser/download/download_service_impl.h" |
| 12 #include "chrome/browser/extensions/extension_api_unittest.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "content/public/test/mock_download_manager.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 using HistoryService = history::HistoryService; |
| 19 using MockDownloadManager = content::MockDownloadManager; |
| 20 |
| 21 namespace extensions { |
| 22 |
| 23 namespace { |
| 24 |
| 25 // A DownloadService that returns a custom DownloadHistory. |
| 26 class TestDownloadService : public DownloadServiceImpl { |
| 27 public: |
| 28 explicit TestDownloadService(Profile* profile) |
| 29 : DownloadServiceImpl(profile), profile_(profile) {} |
| 30 ~TestDownloadService() override {} |
| 31 |
| 32 void set_download_history(std::unique_ptr<DownloadHistory> download_history) { |
| 33 download_history_.swap(download_history); |
| 34 } |
| 35 |
| 36 DownloadHistory* GetDownloadHistory() override { |
| 37 return download_history_.get(); |
| 38 } |
| 39 |
| 40 ExtensionDownloadsEventRouter* GetExtensionEventRouter() override { |
| 41 if (!router_.get()) { |
| 42 router_.reset(new ExtensionDownloadsEventRouter( |
| 43 profile_, content::BrowserContext::GetDownloadManager(profile_))); |
| 44 } |
| 45 return router_.get(); |
| 46 } |
| 47 |
| 48 private: |
| 49 std::unique_ptr<DownloadHistory> download_history_; |
| 50 std::unique_ptr<ExtensionDownloadsEventRouter> router_; |
| 51 Profile* profile_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(TestDownloadService); |
| 54 }; |
| 55 |
| 56 } // namespace |
| 57 |
| 58 class DownloadsApiUnitTest : public ExtensionApiUnittest { |
| 59 public: |
| 60 DownloadsApiUnitTest() {} |
| 61 ~DownloadsApiUnitTest() override {} |
| 62 void SetUp() override { |
| 63 ExtensionApiUnittest::SetUp(); |
| 64 |
| 65 manager_.reset(new testing::StrictMock<MockDownloadManager>()); |
| 66 EXPECT_CALL(*manager_, AddObserver(testing::_)) |
| 67 .WillOnce(testing::SaveArg<0>(&download_history_manager_observer_)); |
| 68 EXPECT_CALL(*manager_, RemoveObserver(testing::Eq(testing::ByRef( |
| 69 download_history_manager_observer_)))) |
| 70 .WillOnce(testing::Assign( |
| 71 &download_history_manager_observer_, |
| 72 static_cast<content::DownloadManager::Observer*>(nullptr))); |
| 73 EXPECT_CALL(*manager_, GetAllDownloads(testing::_)) |
| 74 .Times(testing::AnyNumber()); |
| 75 |
| 76 std::unique_ptr<HistoryAdapter> history_adapter(new HistoryAdapter); |
| 77 std::unique_ptr<DownloadHistory> download_history( |
| 78 new DownloadHistory(manager_.get(), std::move(history_adapter))); |
| 79 TestDownloadService* download_service = static_cast<TestDownloadService*>( |
| 80 DownloadServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 81 profile(), &TestingDownloadServiceFactory)); |
| 82 ASSERT_TRUE(download_service); |
| 83 download_service->set_download_history(std::move(download_history)); |
| 84 } |
| 85 void TearDown() override { ExtensionApiUnittest::TearDown(); } |
| 86 |
| 87 private: |
| 88 // A private empty history adapter that does nothing on QueryDownloads(). |
| 89 class HistoryAdapter : public DownloadHistory::HistoryAdapter { |
| 90 public: |
| 91 HistoryAdapter() : DownloadHistory::HistoryAdapter(nullptr) {} |
| 92 |
| 93 private: |
| 94 void QueryDownloads( |
| 95 const HistoryService::DownloadQueryCallback& callback) override {} |
| 96 }; |
| 97 |
| 98 // Constructs and returns a TestDownloadService. |
| 99 static std::unique_ptr<KeyedService> TestingDownloadServiceFactory( |
| 100 content::BrowserContext* browser_context); |
| 101 |
| 102 std::unique_ptr<MockDownloadManager> manager_; |
| 103 content::DownloadManager::Observer* download_history_manager_observer_; |
| 104 |
| 105 DISALLOW_COPY_AND_ASSIGN(DownloadsApiUnitTest); |
| 106 }; |
| 107 |
| 108 // static |
| 109 std::unique_ptr<KeyedService> |
| 110 DownloadsApiUnitTest::TestingDownloadServiceFactory( |
| 111 content::BrowserContext* browser_context) { |
| 112 return base::WrapUnique( |
| 113 new TestDownloadService(Profile::FromBrowserContext(browser_context))); |
| 114 } |
| 115 |
| 116 // Tests that Number/double properties in query are parsed correctly. |
| 117 // Regression test for https://crbug.com/617435. |
| 118 TEST_F(DownloadsApiUnitTest, ParseSearchQuery) { |
| 119 RunFunction(new DownloadsSearchFunction, "[{\"totalBytesLess\":1}]"); |
| 120 RunFunction(new DownloadsSearchFunction, "[{\"totalBytesGreater\":2}]"); |
| 121 } |
| 122 |
| 123 } // namespace extensions |
OLD | NEW |