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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <atomic>
#include "base/command_line.h"
#include "base/memory/weak_ptr.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/api/page_capture/page_capture_api.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "content/public/test/browser_test.h"
#include "net/dns/mock_host_resolver.h"
#include "third_party/blink/public/common/switches.h"
namespace extensions {
using ContextType = extensions::browser_test_util::ContextType;
class PageCaptureSaveAsMHTMLDelegate
: public PageCaptureSaveAsMHTMLFunction::TestDelegate {
public:
PageCaptureSaveAsMHTMLDelegate() {
PageCaptureSaveAsMHTMLFunction::SetTestDelegate(this);
}
virtual ~PageCaptureSaveAsMHTMLDelegate() {
PageCaptureSaveAsMHTMLFunction::SetTestDelegate(nullptr);
}
void OnTemporaryFileCreated(
scoped_refptr<storage::ShareableFileReference> file) override {
file->AddFinalReleaseCallback(
base::BindOnce(&PageCaptureSaveAsMHTMLDelegate::OnReleaseCallback,
weak_factory_.GetWeakPtr()));
++temp_file_count_;
}
void WaitForFinalRelease() {
if (temp_file_count_ > 0)
run_loop_.Run();
}
int temp_file_count() const { return temp_file_count_; }
private:
void OnReleaseCallback(const base::FilePath& path) {
if (--temp_file_count_ == 0)
release_closure_.Run();
}
base::RunLoop run_loop_;
base::RepeatingClosure release_closure_ = run_loop_.QuitClosure();
std::atomic<int> temp_file_count_{0};
base::WeakPtrFactory<PageCaptureSaveAsMHTMLDelegate> weak_factory_{this};
};
class ExtensionPageCaptureApiTest
: public ExtensionApiTest,
public testing::WithParamInterface<ContextType> {
protected:
void SetUpCommandLine(base::CommandLine* command_line) override {
ExtensionApiTest::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII(blink::switches::kJavaScriptFlags,
"--expose-gc");
}
void SetUpOnMainThread() override {
ExtensionApiTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
}
bool RunTest(const char* extension_name,
const char* custom_arg = nullptr,
bool allow_file_access = false) {
return RunExtensionTest(extension_name, {.custom_arg = custom_arg},
{.allow_file_access = allow_file_access});
}
void WaitForFileCleanup(PageCaptureSaveAsMHTMLDelegate* delegate) {
// Garbage collection in SW-based extensions doesn't clean up the temp
// file.
if (GetParam() != ContextType::kServiceWorker)
delegate->WaitForFinalRelease();
}
};
INSTANTIATE_TEST_SUITE_P(PersistentBackground,
ExtensionPageCaptureApiTest,
::testing::Values(ContextType::kPersistentBackground));
INSTANTIATE_TEST_SUITE_P(ServiceWorker,
ExtensionPageCaptureApiTest,
::testing::Values(ContextType::kServiceWorker));
// TODO(crbug.com/326868086, crbug.com/342254075, crbug.com/374409662,
// crbug.com/406917890): Test is flaky across all platforms.
IN_PROC_BROWSER_TEST_P(ExtensionPageCaptureApiTest, DISABLED_SaveAsMHTMLWithoutFileAccess) {
ASSERT_TRUE(StartEmbeddedTestServer());
PageCaptureSaveAsMHTMLDelegate delegate;
ASSERT_TRUE(RunTest("page_capture", "ONLY_PAGE_CAPTURE_PERMISSION"))
<< message_;
WaitForFileCleanup(&delegate);
}
// TODO(crbug.com/326868086): Test is flaky.
IN_PROC_BROWSER_TEST_P(ExtensionPageCaptureApiTest,
DISABLED_SaveAsMHTMLWithFileAccess) {
ASSERT_TRUE(StartEmbeddedTestServer());
PageCaptureSaveAsMHTMLDelegate delegate;
ASSERT_TRUE(RunTest("page_capture", /*custom_arg=*/nullptr,
/*allow_file_access=*/true))
<< message_;
WaitForFileCleanup(&delegate);
}
} // namespace extensions
|