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