Commit 6e5249f6 authored by Dominique Fauteux-Chapleau's avatar Dominique Fauteux-Chapleau Committed by Commit Bot

Add test coverage to "Cancel" button of upload Deep Scanning UI

Also fix an ASAN issue triggered by this test in the fake delegate.

Bug: 2078973
Change-Id: I2d98178b4c640b6509a999ef1b910d5490763f6c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2079470Reviewed-by: default avatarRoger Tawa <rogerta@chromium.org>
Commit-Queue: Dominique Fauteux-Chapleau <domfc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#747236}
parent 98a32fc7
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_browsertest_base.h" #include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_browsertest_base.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_dialog_views.h" #include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_dialog_views.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/fake_deep_scanning_dialog_delegate.h" #include "chrome/browser/safe_browsing/cloud_content_scanning/fake_deep_scanning_dialog_delegate.h"
...@@ -170,6 +171,48 @@ class DeepScanningDialogViewsBehaviorBrowserTest ...@@ -170,6 +171,48 @@ class DeepScanningDialogViewsBehaviorBrowserTest
bool expected_scan_result_; bool expected_scan_result_;
}; };
// Tests the behavior of the dialog in the following ways:
// - It closes when the "Cancel" button is clicked.
// - It returns a negative verdict on the scanned content.
// - The "CancelledByUser" metrics are recorded.
class DeepScanningDialogViewsCancelPendingScanBrowserTest
: public DeepScanningBrowserTestBase,
public DeepScanningDialogViews::TestObserver {
public:
DeepScanningDialogViewsCancelPendingScanBrowserTest() {
DeepScanningDialogViews::SetObserverForTesting(this);
}
void ViewsFirstShown(DeepScanningDialogViews* views,
base::TimeTicks timestamp) override {
// Simulate the user clicking "Cancel" after the dialog is first shown.
views->CancelDialog();
}
void DestructorCalled(DeepScanningDialogViews* views) override {
// The test is over once the views are destroyed.
CallQuitClosure();
}
void ValidateMetrics() const {
ASSERT_EQ(
2u,
histograms_.GetTotalCountsForPrefix("SafeBrowsing.DeepScan.").size());
ASSERT_EQ(1u, histograms_
.GetTotalCountsForPrefix(
"SafeBrowsing.DeepScan.Upload.Duration")
.size());
ASSERT_EQ(1u,
histograms_
.GetTotalCountsForPrefix(
"SafeBrowsing.DeepScan.Upload.CancelledByUser.Duration")
.size());
}
private:
base::HistogramTester histograms_;
};
} // namespace } // namespace
IN_PROC_BROWSER_TEST_P(DeepScanningDialogViewsBehaviorBrowserTest, Test) { IN_PROC_BROWSER_TEST_P(DeepScanningDialogViewsBehaviorBrowserTest, Test) {
...@@ -250,4 +293,47 @@ INSTANTIATE_TEST_SUITE_P( ...@@ -250,4 +293,47 @@ INSTANTIATE_TEST_SUITE_P(
/*response_delay*/ /*response_delay*/
testing::Values(kNoDelay, kSmallDelay, kNormalDelay))); testing::Values(kNoDelay, kSmallDelay, kNormalDelay)));
IN_PROC_BROWSER_TEST_F(DeepScanningDialogViewsCancelPendingScanBrowserTest,
Test) {
// Setup policies to enable deep scanning, its UI and the responses to be
// simulated.
SetDlpPolicy(CHECK_UPLOADS);
SetStatusCallbackResponse(SimpleDeepScanningClientResponseForTesting(
/*dlp=*/true, /*malware=*/base::nullopt));
// Always set this policy so the UI is shown.
SetWaitPolicy(DELAY_UPLOADS);
// Set up delegate test values.
FakeDeepScanningDialogDelegate::SetResponseDelay(kSmallDelay);
SetUpDelegate();
bool called = false;
base::RunLoop run_loop;
SetQuitClosure(run_loop.QuitClosure());
DeepScanningDialogDelegate::Data data;
data.do_dlp_scan = true;
data.do_malware_scan = false;
data.paths.emplace_back(FILE_PATH_LITERAL("/tmp/foo.doc"));
data.paths.emplace_back(FILE_PATH_LITERAL("/tmp/bar.doc"));
data.paths.emplace_back(FILE_PATH_LITERAL("/tmp/baz.doc"));
DeepScanningDialogDelegate::ShowForWebContents(
browser()->tab_strip_model()->GetActiveWebContents(), std::move(data),
base::BindOnce(
[](bool* called, const DeepScanningDialogDelegate::Data& data,
const DeepScanningDialogDelegate::Result& result) {
for (bool result : result.paths_results)
ASSERT_FALSE(result);
*called = true;
},
&called),
DeepScanAccessPoint::UPLOAD);
run_loop.Run();
EXPECT_TRUE(called);
ValidateMetrics();
}
} // namespace safe_browsing } // namespace safe_browsing
...@@ -164,7 +164,7 @@ void FakeDeepScanningDialogDelegate::UploadTextForDeepScanning( ...@@ -164,7 +164,7 @@ void FakeDeepScanningDialogDelegate::UploadTextForDeepScanning(
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, FROM_HERE,
base::BindOnce(&FakeDeepScanningDialogDelegate::Response, base::BindOnce(&FakeDeepScanningDialogDelegate::Response,
base::Unretained(this), base::FilePath(), weakptr_factory_.GetWeakPtr(), base::FilePath(),
std::move(request)), std::move(request)),
response_delay); response_delay);
} }
...@@ -179,7 +179,7 @@ void FakeDeepScanningDialogDelegate::UploadFileForDeepScanning( ...@@ -179,7 +179,7 @@ void FakeDeepScanningDialogDelegate::UploadFileForDeepScanning(
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, FROM_HERE,
base::BindOnce(&FakeDeepScanningDialogDelegate::Response, base::BindOnce(&FakeDeepScanningDialogDelegate::Response,
base::Unretained(this), path, std::move(request)), weakptr_factory_.GetWeakPtr(), path, std::move(request)),
response_delay); response_delay);
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/callback_forward.h" #include "base/callback_forward.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/binary_upload_service.h" #include "chrome/browser/safe_browsing/cloud_content_scanning/binary_upload_service.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_dialog_delegate.h" #include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_dialog_delegate.h"
#include "components/safe_browsing/core/proto/webprotect.pb.h" #include "components/safe_browsing/core/proto/webprotect.pb.h"
...@@ -107,6 +108,8 @@ class FakeDeepScanningDialogDelegate : public DeepScanningDialogDelegate { ...@@ -107,6 +108,8 @@ class FakeDeepScanningDialogDelegate : public DeepScanningDialogDelegate {
StatusCallback status_callback_; StatusCallback status_callback_;
EncryptionStatusCallback encryption_callback_; EncryptionStatusCallback encryption_callback_;
std::string dm_token_; std::string dm_token_;
base::WeakPtrFactory<FakeDeepScanningDialogDelegate> weakptr_factory_{this};
}; };
} // namespace safe_browsing } // namespace safe_browsing
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment