-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relocate test PrintPreviewObserver into separate file
Extract the PrintPreviewObserver class from print_browsertest.cc and put it in its own file. This is so that it can be shared by multiple test files, in preparation of splitting the tests in print_browsertest.cc into multiple files. Rename the class to have a "Test" prefix, to clearly indicate that this class is intended only for testing. Bug: 1418833 Change-Id: I5075a1e3db0a1e0a2280ea405ff59cd6af1370bb Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4326976 Reviewed-by: Andy Phan <andyphan@chromium.org> Commit-Queue: Alan Screen <awscreen@chromium.org> Cr-Commit-Position: refs/heads/main@{#1115991}
- Loading branch information
Alan Screen
authored and
Chromium LUCI CQ
committed
Mar 11, 2023
1 parent
19431b4
commit c000f43
Showing
4 changed files
with
171 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// 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. | ||
|
||
#include "chrome/browser/printing/test_print_preview_observer.h" | ||
|
||
#include <tuple> | ||
|
||
#include "base/check_op.h" | ||
#include "base/logging.h" | ||
#include "base/run_loop.h" | ||
#include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | ||
#include "content/public/browser/web_contents.h" | ||
#include "content/public/test/browser_test_utils.h" | ||
|
||
namespace printing { | ||
|
||
TestPrintPreviewObserver::TestPrintPreviewObserver(bool wait_for_loaded) | ||
: TestPrintPreviewObserver(wait_for_loaded, /*pages_per_sheet=*/1) {} | ||
|
||
TestPrintPreviewObserver::TestPrintPreviewObserver(bool wait_for_loaded, | ||
int pages_per_sheet) | ||
: pages_per_sheet_(pages_per_sheet), wait_for_loaded_(wait_for_loaded) { | ||
PrintPreviewUI::SetDelegateForTesting(this); | ||
} | ||
|
||
TestPrintPreviewObserver::~TestPrintPreviewObserver() { | ||
PrintPreviewUI::SetDelegateForTesting(nullptr); | ||
} | ||
|
||
[[nodiscard]] content::WebContents* | ||
TestPrintPreviewObserver::WaitUntilPreviewIsReadyAndReturnPreviewDialog() { | ||
if (rendered_page_count_ < expected_rendered_page_count_) { | ||
base::RunLoop run_loop; | ||
base::AutoReset<base::RunLoop*> auto_reset(&run_loop_, &run_loop); | ||
run_loop.Run(); | ||
|
||
if (queue_.has_value()) { | ||
std::string message; | ||
EXPECT_TRUE(queue_->WaitForMessage(&message)); | ||
EXPECT_EQ("\"success\"", message); | ||
} | ||
} | ||
|
||
// Grab and reset `preview_dialog_` to avoid potential dangling pointers. | ||
content::WebContents* dialog = preview_dialog_; | ||
preview_dialog_ = nullptr; | ||
return dialog; | ||
} | ||
|
||
// Wrapper for WaitUntilPreviewIsReadyAndReturnPreviewDialog() provided for | ||
// convenience for callers that do not need the returned result. | ||
void TestPrintPreviewObserver::WaitUntilPreviewIsReady() { | ||
std::ignore = WaitUntilPreviewIsReadyAndReturnPreviewDialog(); | ||
} | ||
|
||
void TestPrintPreviewObserver::DidGetPreviewPageCount(uint32_t page_count) { | ||
// `page_count` is the number of pages to be generated but doesn't take | ||
// N-up into consideration. Since `DidRenderPreviewPage()` is called after | ||
// any N-up processing is performed, determine the number of times that | ||
// function is expected to be called. | ||
expected_rendered_page_count_ = | ||
(page_count + pages_per_sheet_ - 1) / pages_per_sheet_; | ||
} | ||
|
||
void TestPrintPreviewObserver::DidRenderPreviewPage( | ||
content::WebContents* preview_dialog) { | ||
++rendered_page_count_; | ||
DVLOG(2) << "Rendered preview page " << rendered_page_count_ | ||
<< " of a total expected " << expected_rendered_page_count_; | ||
CHECK_LE(rendered_page_count_, expected_rendered_page_count_); | ||
if (rendered_page_count_ == expected_rendered_page_count_ && run_loop_) { | ||
run_loop_->Quit(); | ||
preview_dialog_ = preview_dialog; | ||
|
||
if (wait_for_loaded_) { | ||
// Instantiate `queue_` to listen for messages in `preview_dialog_`. | ||
queue_.emplace(preview_dialog_); | ||
content::ExecuteScriptAsync( | ||
preview_dialog_.get(), | ||
"window.addEventListener('message', event => {" | ||
" if (event.data.type === 'documentLoaded') {" | ||
" domAutomationController.send(event.data.load_state);" | ||
" }" | ||
"});"); | ||
} | ||
} | ||
} | ||
|
||
} // namespace printing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 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_PRINTING_TEST_PRINT_PREVIEW_OBSERVER_H_ | ||
#define CHROME_BROWSER_PRINTING_TEST_PRINT_PREVIEW_OBSERVER_H_ | ||
|
||
#include "base/memory/raw_ptr.h" | ||
#include "base/memory/raw_ptr_exclusion.h" | ||
#include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | ||
#include "content/public/test/browser_test_utils.h" | ||
#include "third_party/abseil-cpp/absl/types/optional.h" | ||
|
||
namespace base { | ||
class RunLoop; | ||
} | ||
namespace content { | ||
class WebContents; | ||
} | ||
|
||
namespace printing { | ||
|
||
class TestPrintPreviewObserver : PrintPreviewUI::TestDelegate { | ||
public: | ||
explicit TestPrintPreviewObserver(bool wait_for_loaded); | ||
TestPrintPreviewObserver(bool wait_for_loaded, int pages_per_sheet); | ||
TestPrintPreviewObserver(const TestPrintPreviewObserver&) = delete; | ||
TestPrintPreviewObserver& operator=(const TestPrintPreviewObserver&) = delete; | ||
~TestPrintPreviewObserver() override; | ||
|
||
// Tests that use PrintPreviewObserver must call | ||
// WaitUntilPreviewIsReady*() exactly once. | ||
[[nodiscard]] content::WebContents* | ||
WaitUntilPreviewIsReadyAndReturnPreviewDialog(); | ||
|
||
// Wrapper for WaitUntilPreviewIsReadyAndReturnPreviewDialog() provided for | ||
// convenience for callers that do not need the returned result. | ||
void WaitUntilPreviewIsReady(); | ||
|
||
uint32_t rendered_page_count() const { return rendered_page_count_; } | ||
|
||
private: | ||
// PrintPreviewUI::TestDelegate: | ||
void DidGetPreviewPageCount(uint32_t page_count) override; | ||
|
||
// PrintPreviewUI::TestDelegate: | ||
void DidRenderPreviewPage(content::WebContents* preview_dialog) override; | ||
|
||
absl::optional<content::DOMMessageQueue> queue_; | ||
|
||
// Rendered pages are provided after N-up processing, which will be different | ||
// from the count provided to `DidGetPreviewPageCount()` when | ||
// `pages_per_sheet_` is larger than one. | ||
const int pages_per_sheet_; | ||
uint32_t expected_rendered_page_count_ = 1; | ||
uint32_t rendered_page_count_ = 0; | ||
|
||
const bool wait_for_loaded_; | ||
raw_ptr<content::WebContents> preview_dialog_ = nullptr; | ||
// This field is not a raw_ptr<> because it was filtered by the rewriter for: | ||
// #addr-of | ||
RAW_PTR_EXCLUSION base::RunLoop* run_loop_ = nullptr; | ||
}; | ||
|
||
} // namespace printing | ||
|
||
#endif // CHROME_BROWSER_PRINTING_TEST_PRINT_PREVIEW_OBSERVER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters