1 // Copyright 2019 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 #ifndef EXTENSIONS_BROWSER_UPDATER_EXTENSION_DOWNLOADER_TEST_HELPER_H_ 6 #define EXTENSIONS_BROWSER_UPDATER_EXTENSION_DOWNLOADER_TEST_HELPER_H_ 7 8 #include <memory> 9 #include <set> 10 #include <string> 11 12 #include "extensions/browser/updater/extension_downloader.h" 13 #include "extensions/browser/updater/extension_downloader_delegate.h" 14 #include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h" 15 #include "services/network/public/cpp/shared_url_loader_factory.h" 16 #include "services/network/test/test_url_loader_factory.h" 17 #include "testing/gmock/include/gmock/gmock.h" 18 19 namespace extensions { 20 21 class MockExtensionDownloaderDelegate : public ExtensionDownloaderDelegate { 22 public: 23 MockExtensionDownloaderDelegate(); 24 25 ~MockExtensionDownloaderDelegate(); 26 27 MOCK_METHOD5(OnExtensionDownloadFailed, 28 void(const ExtensionId&, 29 Error, 30 const PingResult&, 31 const std::set<int>&, 32 const FailureData&)); 33 MOCK_METHOD2(OnExtensionDownloadStageChanged, 34 void(const ExtensionId&, Stage)); 35 MOCK_METHOD2(OnExtensionDownloadCacheStatusRetrieved, 36 void(const ExtensionId&, CacheStatus)); 37 // Gmock doesn't have good support for move-only types like 38 // base::OnceCallback, so we have to do this hack. OnExtensionDownloadFinished(const CRXFileInfo & file,bool file_ownership_passed,const GURL & download_url,const PingResult & ping_result,const std::set<int> & request_ids,InstallCallback callback)39 void OnExtensionDownloadFinished(const CRXFileInfo& file, 40 bool file_ownership_passed, 41 const GURL& download_url, 42 const PingResult& ping_result, 43 const std::set<int>& request_ids, 44 InstallCallback callback) override { 45 OnExtensionDownloadFinished_(file, file_ownership_passed, download_url, 46 ping_result, request_ids, callback); 47 } 48 MOCK_METHOD6(OnExtensionDownloadFinished_, 49 void(const extensions::CRXFileInfo&, 50 bool, 51 const GURL&, 52 const PingResult&, 53 const std::set<int>&, 54 InstallCallback&)); 55 MOCK_METHOD0(OnExtensionDownloadRetryForTests, void()); 56 MOCK_METHOD2(GetPingDataForExtension, 57 bool(const ExtensionId&, ManifestFetchData::PingData*)); 58 MOCK_METHOD1(GetUpdateUrlData, std::string(const ExtensionId&)); 59 MOCK_METHOD1(IsExtensionPending, bool(const ExtensionId&)); 60 MOCK_METHOD2(GetExtensionExistingVersion, 61 bool(const ExtensionId&, std::string*)); 62 63 void Wait(); 64 65 void Quit(); 66 67 void DelegateTo(ExtensionDownloaderDelegate* delegate); 68 69 private: 70 base::RepeatingClosure quit_closure_; 71 }; 72 73 // Creates ExtensionDownloader for tests, with mocked delegate and 74 // TestURLLoaderFactory as a URL factory. 75 class ExtensionDownloaderTestHelper { 76 public: 77 ExtensionDownloaderTestHelper(); 78 79 ~ExtensionDownloaderTestHelper(); 80 delegate()81 MockExtensionDownloaderDelegate& delegate() { return delegate_; } downloader()82 ExtensionDownloader& downloader() { return downloader_; } url_loader_factory()83 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory() { 84 return test_shared_url_loader_factory_; 85 } test_url_loader_factory()86 network::TestURLLoaderFactory& test_url_loader_factory() { 87 return test_url_loader_factory_; 88 } 89 90 // Returns a request that URL loader factory has received (or nullptr if it 91 // didn't receive enough requests). 92 network::TestURLLoaderFactory::PendingRequest* GetPendingRequest( 93 size_t index = 0); 94 95 // Clears previously added responses from URL loader factory. 96 void ClearURLLoaderFactoryResponses(); 97 98 // Create a downloader in a separate pointer. Could be used by, for example, 99 // ExtensionUpdater. 100 std::unique_ptr<ExtensionDownloader> CreateDownloader(); 101 102 private: 103 network::TestURLLoaderFactory test_url_loader_factory_; 104 scoped_refptr<network::SharedURLLoaderFactory> 105 test_shared_url_loader_factory_; 106 data_decoder::test::InProcessDataDecoder in_process_data_decoder_; 107 MockExtensionDownloaderDelegate delegate_; 108 ExtensionDownloader downloader_; 109 110 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloaderTestHelper); 111 }; 112 113 } // namespace extensions 114 115 #endif // EXTENSIONS_BROWSER_UPDATER_EXTENSION_DOWNLOADER_TEST_HELPER_H_ 116