Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(811)

Side by Side Diff: chrome/browser/safe_browsing/test_safe_browsing_service.h

Issue 1943993006: Create test fixture for SafeBrowsingService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 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 #ifndef CHROME_BROWSER_SAFE_BROWSING_TEST_SAFE_BROWSING_SERVICE_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_TEST_SAFE_BROWSING_SERVICE_H_
7
8 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
9
10 #include "chrome/browser/safe_browsing/protocol_manager.h"
11 #include "chrome/browser/safe_browsing/protocol_manager_helper.h"
12 #include "chrome/browser/safe_browsing/ui_manager.h"
13 #include "components/safe_browsing_db/v4_protocol_manager_util.h"
14
15 namespace safe_browsing {
16 struct SafeBrowsingProtocolConfig;
17 class SafeBrowsingDatabaseManager;
18 class SafeBrowsingPingManager;
19 class SafeBrowsingProtocolManager;
20 struct V4ProtocolConfig;
21 class TestSafeBrowsingDatabaseManager;
22 class TestSafeBrowsingUIManager;
23
24 // TestSafeBrowsingService and its factory provides a flexible way to configure
25 // customized safe browsing UI manager, database manager, protocol manager,
26 // and etc without the need of override SafeBrowsingService in tests over and
27 // over again.
28 //
29 // How to configure TestSafeBrowsingService in browser tests set up?
30 // * When overriding SetUp():
31 // (1) create an instance of TestSafeBrowsingServiceFactory (
32 // e.g. test_sb_factory_),
33 // (2) Set up necessary test components by calling
34 // test_sb_factory_->SetTest[DatabaseManager/UIManager/...](...),
35 // (3) Register TestSafeBrowsingServiceFactory
36 // SafeBrowsingService::RegisterFactory(test_sb_factory_);
37 // (4) InProcessBrowserTest::SetUp() or other base class SetUp() function must
38 // be called at last.
39 // * When overriding TearDown():
40 // Call base class TearDown() first then call
41 // SafeBrowsingService::RegisterFactory(nullptr) to unregister
42 // test_sb_factory_.
43 class TestSafeBrowsingService : public SafeBrowsingService {
44 public:
45 TestSafeBrowsingService();
46 // SafeBrowsingService overrides
47 SafeBrowsingProtocolConfig GetProtocolConfig() const override;
48 V4ProtocolConfig GetV4ProtocolConfig() const override;
49
50 std::string serilized_download_report();
51
52 // In browser tests, the following setters must be called before
53 // SafeBrowsingService::Initialize().
54 // The preferable way to use these setters is by calling corresponding
55 // TestSafeBrowsingServiceFactory::SetTest[DatabaseManager/UIManager/
56 // ProtocolConfig]() before InProcessBrowserTest::SetUp() is called. Then
57 // inside TestSafeBrowsingServiceFactory::CreateSafeBrowsingService(),
58 // TestSafeBrowsingService instance is created, customised(by using the
59 // following setters), and then initialized.
60 void SetUIManager(TestSafeBrowsingUIManager* ui_manager);
61 void SetDatabaseManager(TestSafeBrowsingDatabaseManager* database_manager);
62 void SetProtocolConfig(SafeBrowsingProtocolConfig* protocol_config);
63 void SetV4ProtocolConfig(V4ProtocolConfig* v4_protocol_config);
64
65 protected:
66 // SafeBrowsingService overrides
67 ~TestSafeBrowsingService() override;
68 SafeBrowsingDatabaseManager* CreateDatabaseManager() override;
69 SafeBrowsingUIManager* CreateUIManager() override;
70 SafeBrowsingProtocolManagerDelegate* GetProtocolManagerDelegate() override;
71 void SendSerializedDownloadReport(const std::string& report) override;
72
73 private:
74 bool protocol_manager_delegate_disabled_;
75 std::unique_ptr<SafeBrowsingProtocolConfig> protocol_config_;
76 std::unique_ptr<V4ProtocolConfig> v4_protocol_config_;
77 std::string serialized_download_report_;
78
79 DISALLOW_COPY_AND_ASSIGN(TestSafeBrowsingService);
80 };
81
82 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory {
83 public:
84 TestSafeBrowsingServiceFactory();
85 ~TestSafeBrowsingServiceFactory() override;
86
87 // Creates test safe browsing service, and configures test UI manager,
88 // database manager and so on.
89 SafeBrowsingService* CreateSafeBrowsingService() override;
90
91 TestSafeBrowsingService* test_safe_browsing_service();
92
93 // Test UI manager, database manager and protocol config need to be set before
94 // SafeBrowsingService::Initialize() is called.
95 void SetTestUIManager(TestSafeBrowsingUIManager* ui_manager);
96 void SetTestDatabaseManager(
97 TestSafeBrowsingDatabaseManager* database_manager);
98 void SetTestProtocolConfig(const SafeBrowsingProtocolConfig& protocol_config);
99
100 private:
101 TestSafeBrowsingService* test_safe_browsing_service_;
102 scoped_refptr<TestSafeBrowsingDatabaseManager> test_database_manager_;
103 scoped_refptr<TestSafeBrowsingUIManager> test_ui_manager_;
104 SafeBrowsingProtocolConfig* test_protocol_config_;
105
106 DISALLOW_COPY_AND_ASSIGN(TestSafeBrowsingServiceFactory);
107 };
108
109 // This is an implmentation of SafeBrowsingUIManager without actually
Nathan Parker 2016/05/07 00:05:32 nit: implementation
Jialiu Lin 2016/05/12 21:53:58 done.
110 // sending report to safe browsing backend. Safe browsing reports are
111 // stored in strings for easy verification.
112 class TestSafeBrowsingUIManager : public SafeBrowsingUIManager {
113 public:
114 TestSafeBrowsingUIManager();
115 explicit TestSafeBrowsingUIManager(
116 const scoped_refptr<SafeBrowsingService>& service);
117 void SendSerializedThreatDetails(const std::string& serialized) override;
118 void SetSafeBrowsingService(SafeBrowsingService* sb_service);
119 std::list<std::string>* GetThreatDetails();
120
121 protected:
122 ~TestSafeBrowsingUIManager() override;
123 std::list<std::string> details_;
124
125 DISALLOW_COPY_AND_ASSIGN(TestSafeBrowsingUIManager);
126 };
127
128 } // namespace safe_browsing
129
130 #endif // CHROME_BROWSER_SAFE_BROWSING_TEST_SAFE_BROWSING_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698