#include "chrome/browser/enterprise/data_protection/print_utils.h"
#include "base/containers/span.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/enterprise/connectors/reporting/realtime_reporting_client.h"
#include "chrome/browser/enterprise/connectors/reporting/realtime_reporting_client_factory.h"
#include "chrome/browser/enterprise/connectors/test/deep_scanning_test_utils.h"
#include "chrome/browser/extensions/api/safe_browsing_private/safe_browsing_private_event_router.h"
#include "chrome/browser/extensions/api/safe_browsing_private/safe_browsing_private_event_router_factory.h"
#include "chrome/browser/policy/dm_token_utils.h"
#include "chrome/browser/printing/print_preview_test.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/enterprise/buildflags/buildflags.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "content/public/test/browser_task_environment.h"
#include "printing/printing_features.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(ENTERPRISE_LOCAL_CONTENT_ANALYSIS)
#include "chrome/browser/enterprise/connectors/test/fake_content_analysis_sdk_manager.h"
#endif
namespace enterprise_data_protection {
namespace {
ContentAnalysisResponse;
ContentAnalysisDelegate;
RealtimeReportingClientFactory;
ContentAnalysisDelegate* test_delegate_ = …;
constexpr char kDmToken[] = …;
constexpr char kLocalPolicy[] = …;
constexpr char kCloudPolicy[] = …;
constexpr char kScanId[] = …;
constexpr char kTestData[] = …;
constexpr char kPrinterName[] = …;
constexpr char kUserName[] = …;
constexpr char16_t kUserJustification[] = …;
scoped_refptr<base::RefCountedMemory> CreateData() { … }
const std::set<std::string>* PrintMimeTypes() { … }
ContentAnalysisResponse::Result CreateResult(
ContentAnalysisResponse::Result::TriggeredRule::Action action) { … }
ContentAnalysisResponse CreateResponse(
ContentAnalysisResponse::Result::TriggeredRule::Action action) { … }
class PrintTestContentAnalysisDelegate : public ContentAnalysisDelegate { … };
class PrintContentAnalysisUtilsTest
: public PrintPreviewTest,
public testing::WithParamInterface<const char*> { … };
}
TEST_P(PrintContentAnalysisUtilsTest, GetPrintAnalysisData_BeforeSystemDialog) { … }
TEST_P(PrintContentAnalysisUtilsTest,
GetPrintAnalysisData_SystemPrintAfterPreview) { … }
TEST_P(PrintContentAnalysisUtilsTest,
GetPrintAnalysisData_NormalPrintAfterPreview) { … }
TEST_P(PrintContentAnalysisUtilsTest,
GetPrintAnalysisData_NormalPrintBeforePrintDocument) { … }
TEST_P(PrintContentAnalysisUtilsTest,
GetPrintAnalysisData_SystemPrintBeforePrintDocument) { … }
#if BUILDFLAG(IS_MAC)
TEST_P(PrintContentAnalysisUtilsTest,
GetPrintAnalysisData_MacOpenPdfInPreview) {
auto data =
GetPrintAnalysisData(contents(), PrintScanningContext::kOpenPdfInPreview);
ASSERT_TRUE(data);
ASSERT_EQ(data->settings.block_until_verdict,
enterprise_connectors::BlockUntilVerdict::kBlock);
ASSERT_EQ(data->reason,
enterprise_connectors::ContentAnalysisRequest::PRINT_PREVIEW_PRINT);
ASSERT_EQ(policy_value() == kLocalPolicy,
data->settings.cloud_or_local_settings.is_local_analysis());
ASSERT_EQ(policy_value() == kCloudPolicy,
data->settings.cloud_or_local_settings.is_cloud_analysis());
if (policy_value() == kLocalPolicy) {
histogram_tester().ExpectTotalCount("Enterprise.OnPrint.Local.PrintType",
1);
histogram_tester().ExpectUniqueSample(
"Enterprise.OnPrint.Local.PrintType",
PrintScanningContext::kOpenPdfInPreview, 1);
histogram_tester().ExpectTotalCount("Enterprise.OnPrint.Cloud.PrintType",
0);
} else {
histogram_tester().ExpectTotalCount("Enterprise.OnPrint.Cloud.PrintType",
1);
histogram_tester().ExpectUniqueSample(
"Enterprise.OnPrint.Cloud.PrintType",
PrintScanningContext::kOpenPdfInPreview, 1);
histogram_tester().ExpectTotalCount("Enterprise.OnPrint.Local.PrintType",
0);
}
}
#endif
TEST_P(PrintContentAnalysisUtilsTest, PrintIfAllowedByPolicyAllowed) { … }
TEST_P(PrintContentAnalysisUtilsTest, PrintIfAllowedByPolicyReportOnly) { … }
TEST_P(PrintContentAnalysisUtilsTest, PrintIfAllowedByPolicyWarnThenCancel) { … }
TEST_P(PrintContentAnalysisUtilsTest, PrintIfAllowedByPolicyWarnedThenBypass) { … }
TEST_P(PrintContentAnalysisUtilsTest, PrintIfAllowedByPolicyBlocked) { … }
TEST_P(PrintContentAnalysisUtilsTest, PrintIfAllowedByPolicyNullInitiator) { … }
INSTANTIATE_TEST_SUITE_P(…);
}