1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_SAFETY_HUB_SAFETY_HUB_TEST_UTIL_H_
#define CHROME_BROWSER_UI_SAFETY_HUB_SAFETY_HUB_TEST_UTIL_H_
#include <memory>
#include <string_view>
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/safety_hub/notification_permission_review_service.h"
#include "chrome/browser/ui/safety_hub/revoked_permissions_service.h"
#include "chrome/browser/ui/safety_hub/safety_hub_service.h"
#include "components/password_manager/core/browser/leak_detection/bulk_leak_check_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/extensions/cws_info_service.h"
#include "chrome/browser/ui/safety_hub/password_status_check_service.h"
#include "chrome/browser/ui/safety_hub/safety_hub_hats_service.h"
#include "chrome/test/base/testing_profile.h"
#include "extensions/common/extension_urls.h"
#include "extensions/common/manifest.h"
#endif // BUILDFLAG(IS_ANDROID)
namespace safety_hub_test_util {
#if !BUILDFLAG(IS_ANDROID)
// Mock CWS info service for extensions.
class MockCWSInfoService : public extensions::CWSInfoService {
public:
explicit MockCWSInfoService(Profile* profile);
~MockCWSInfoService() override;
MOCK_METHOD(std::optional<CWSInfoServiceInterface::CWSInfo>,
GetCWSInfo,
(const extensions::Extension&),
(const, override));
};
// This will run UpdateInsecureCredentialCountAsync on
// PasswordStatusCheckService and return when the check is completed.
void UpdatePasswordCheckServiceAsync(
PasswordStatusCheckService* password_service);
// Creates a new instance of the PasswordStatusService and associates it with
// `context`. Immediately returns the created service.
PasswordStatusCheckService* CreateAndUsePasswordStatusService(
content::BrowserContext* context);
// This will run until ongoing checks in PasswordStatusCheckService to be
// completed.
void RunUntilPasswordCheckCompleted(Profile* profile);
// Creates a mock service that returns mock results for the CWS info service.
// If |with_calls| is true, total six extensions with different properties are
// mocked: malware, policy violation, unpublished, combination of malware and
// unpublished, no violation, and an extension that is not present.
std::unique_ptr<testing::NiceMock<MockCWSInfoService>> GetMockCWSInfoService(
Profile* profile,
bool with_calls = true);
// Creates a mock service that returns mock results for the CWS info service.
// Each call will respond with a extension having no triggers.
std::unique_ptr<testing::NiceMock<MockCWSInfoService>>
GetMockCWSInfoServiceNoTriggers(Profile* profile);
// Adds a testing extension with |name| and |location| to |profile|.
void AddExtension(
const std::string& name,
extensions::mojom::ManifestLocation location,
Profile* profile,
std::string update_url = extension_urls::kChromeWebstoreUpdateURL);
// Adds seven extensions, of which one is installed by an external policy.
void CreateMockExtensions(TestingProfile* profile);
// Deletes all mock extensions that are added by CreateMockExtensions.
void CleanAllMockExtensions(Profile* profile);
// Creates and returns a CWSInfo without any triggers.
extensions::CWSInfoService::CWSInfo GetCWSInfoNoTrigger();
// Removes an extension from the Chrome registry and extension prefs.
void RemoveExtension(const std::string& name,
extensions::mojom::ManifestLocation location,
Profile* profile);
// Add the `ack_safety_check_warning_reason` pref to an extension. This
// will remove it from the Safety Hub.
void AcknowledgeSafetyCheckExtensions(const std::string& name,
Profile* profile);
// Creates the service used for bulk leak checks.
password_manager::BulkLeakCheckService* CreateAndUseBulkLeakCheckService(
signin::IdentityManager* identity_manager,
Profile* profile);
// Creates a form for the password manager with a given username, password and
// origin. If |is_leaked| is set to |true|, the password will be considered a
// leaked password.
password_manager::PasswordForm MakeForm(std::u16string_view username,
std::u16string_view password,
std::string origin,
bool is_leaked = false);
#endif // BUILDFLAG(IS_ANDROID)
// Creates a new instance of the RevokedPermissionsService and associates it
// with |context|.
void CreateRevokedPermissionsService(content::BrowserContext* context);
// Creates a new instance of the NotificationPermissionsReviewService and
// associates it with |context|.
void CreateNotificationPermissionsReviewService(
content::BrowserContext* context);
// This will run the UpdateAsync function on the provided SafetyHubService and
// return when both the background task and UI task are completed. It will
// temporary add an observer to the service, which will be removed again before
// the function returns.
void UpdateSafetyHubServiceAsync(SafetyHubService* service);
// This will run the UpdateAsync function on the RevokedPermissionsService
// and return when both the background task and UI task are completed. This
// separate helper is needed because abusive notification revocation is
// asynchronous, so this method should run until idle.
void UpdateRevokedPermissionsServiceAsync(RevokedPermissionsService* service);
// Returns true if the provided list of content settings has a setting with the
// provided url.
bool IsUrlInSettingsList(ContentSettingsForOneType content_settings, GURL url);
// Create a series of notifications on a site that has not been interacted with
// that will result in a Safety Hub menu notification being shown.
void GenerateSafetyHubMenuNotification(Profile* profile);
} // namespace safety_hub_test_util
#endif // CHROME_BROWSER_UI_SAFETY_HUB_SAFETY_HUB_TEST_UTIL_H_
|