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