1 // Copyright 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 #ifndef CHROME_BROWSER_EXTENSIONS_TEST_BLOCKLIST_H_ 6 #define CHROME_BROWSER_EXTENSIONS_TEST_BLOCKLIST_H_ 7 8 #include <map> 9 #include <string> 10 11 #include "base/macros.h" 12 #include "chrome/browser/extensions/blocklist.h" 13 #include "chrome/browser/extensions/blocklist_state_fetcher.h" 14 15 namespace extensions { 16 17 class FakeSafeBrowsingDatabaseManager; 18 19 // Replace BlocklistStateFetcher for testing of the Boacklist class. 20 class BlocklistStateFetcherMock : public BlocklistStateFetcher { 21 public: 22 BlocklistStateFetcherMock(); 23 24 ~BlocklistStateFetcherMock() override; 25 26 void Request(const std::string& id, const RequestCallback& callback) override; 27 28 void SetState(const std::string& id, BlocklistState state); 29 30 void Clear(); 31 request_count()32 int request_count() const { return request_count_; } 33 34 private: 35 std::map<std::string, BlocklistState> states_; 36 int request_count_; 37 }; 38 39 // A wrapper for an extensions::Blocklist that provides functionality for 40 // testing. It sets up mocks for SafeBrowsing database and BlocklistFetcher, 41 // that are used by blocklist to retrieve respectively the set of blocklisted 42 // extensions and their blocklist states. 43 class TestBlocklist { 44 public: 45 // Use this if the SafeBrowsing and/or StateFetcher mocks should be created 46 // before initializing the Blocklist. 47 TestBlocklist(); 48 49 explicit TestBlocklist(Blocklist* blocklist); 50 51 ~TestBlocklist(); 52 53 void Attach(Blocklist* blocklist); 54 55 // Only call this if Blocklist is destroyed before TestBlocklist, otherwise 56 // it will be performed from the destructor. 57 void Detach(); 58 blocklist()59 Blocklist* blocklist() { return blocklist_; } 60 61 // Set the extension state in SafeBrowsingDatabaseManager and 62 // BlocklistFetcher. 63 void SetBlocklistState(const std::string& extension_id, 64 BlocklistState state, 65 bool notify); 66 67 BlocklistState GetBlocklistState(const std::string& extension_id); 68 69 void Clear(bool notify); 70 71 void DisableSafeBrowsing(); 72 73 void EnableSafeBrowsing(); 74 75 void NotifyUpdate(); 76 fetcher()77 const BlocklistStateFetcherMock* fetcher() { return &state_fetcher_mock_; } 78 79 private: 80 Blocklist* blocklist_; 81 82 // The BlocklistStateFetcher object is normally managed by Blocklist. Because 83 // of this, we need to prevent this object from being deleted with Blocklist. 84 // For this, Detach() should be called before blocklist_ is deleted. 85 BlocklistStateFetcherMock state_fetcher_mock_; 86 87 scoped_refptr<FakeSafeBrowsingDatabaseManager> blocklist_db_; 88 89 Blocklist::ScopedDatabaseManagerForTest scoped_blocklist_db_; 90 91 DISALLOW_COPY_AND_ASSIGN(TestBlocklist); 92 }; 93 94 } // namespace extensions 95 96 #endif // CHROME_BROWSER_EXTENSIONS_TEST_BLOCKLIST_H_ 97