Skip to content

Commit f458bf6

Browse files
committed
[test] print API footer
1 parent 4bb90c1 commit f458bf6

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

src/nwjs_browsertest.cc

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "components/guest_view/browser/guest_view_manager_factory.h"
5555
#include "components/guest_view/browser/test_guest_view_manager.h"
5656
#include "content/public/browser/ax_event_notification_details.h"
57+
#include "content/public/browser/browser_accessibility_state.h"
5758
#include "content/public/browser/browser_child_process_host_iterator.h"
5859
#include "content/public/browser/child_process_data.h"
5960
#include "content/public/common/process_type.h"
@@ -97,6 +98,9 @@
9798
#include "ui/views/view.h"
9899
#include "ui/views/widget/widget.h"
99100

101+
#include "chrome/browser/printing/print_preview_dialog_controller.h"
102+
#include "content/browser/frame_host/render_frame_host_impl.h"
103+
100104
#if defined(ENABLE_PLUGINS)
101105
#include "content/public/browser/plugin_service.h"
102106
#include "content/public/common/webplugininfo.h"
@@ -122,6 +126,7 @@ using task_manager::browsertest_util::MatchWebView;
122126
using task_manager::browsertest_util::WaitForTaskManagerRows;
123127
using ui::MenuModel;
124128
using content::BrowserThread;
129+
using content::WebContents;
125130

126131
namespace {
127132
const char kEmptyResponsePath[] = "/close-socket";
@@ -899,6 +904,8 @@ class NWAppTest : public extensions::PlatformAppBrowserTest {
899904

900905
};
901906

907+
class NWJSAppTest : public NWAppTest {};
908+
902909
void NWTimeoutCallback(const std::string& timeout_message) {
903910
base::MessageLoop::current()->QuitWhenIdle();
904911
}
@@ -923,6 +930,121 @@ IN_PROC_BROWSER_TEST_F(NWAppTest, LocalFlash) {
923930
EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
924931
}
925932

933+
namespace {
934+
class PrintDialogWaiter {
935+
public:
936+
PrintDialogWaiter(content::WebContents* web_contents)
937+
: web_contents_(web_contents) {}
938+
939+
WebContents* Wait() {
940+
WebContents* ret;
941+
if ((ret = CheckDlg()))
942+
return ret;
943+
944+
base::RepeatingTimer check_timer;
945+
check_timer.Start(
946+
FROM_HERE,
947+
base::TimeDelta::FromMilliseconds(200),
948+
this,
949+
&PrintDialogWaiter::OnTimer);
950+
951+
runner_ = new content::MessageLoopRunner;
952+
runner_->Run();
953+
return CheckDlg();
954+
}
955+
956+
private:
957+
WebContents* CheckDlg() {
958+
printing::PrintPreviewDialogController* dialog_controller =
959+
printing::PrintPreviewDialogController::GetInstance();
960+
return dialog_controller->GetPrintPreviewForContents(web_contents_);
961+
}
962+
963+
void OnTimer() {
964+
DCHECK(runner_.get());
965+
if (CheckDlg())
966+
runner_->Quit();
967+
}
968+
content::WebContents* web_contents_;
969+
scoped_refptr<content::MessageLoopRunner> runner_;
970+
971+
DISALLOW_COPY_AND_ASSIGN(PrintDialogWaiter);
972+
};
973+
974+
static std::string DumpPdfAccessibilityTree(const ui::AXTreeUpdate& ax_tree) {
975+
// Create a string representation of the tree starting with the embedded
976+
// object.
977+
std::string ax_tree_dump;
978+
base::hash_map<int32_t, int> id_to_indentation;
979+
//bool found_embedded_object = false;
980+
for (auto& node : ax_tree.nodes) {
981+
#if 0
982+
if (node.role == ui::AX_ROLE_EMBEDDED_OBJECT)
983+
found_embedded_object = true;
984+
if (!found_embedded_object)
985+
continue;
986+
#endif
987+
int indent = id_to_indentation[node.id];
988+
ax_tree_dump += std::string(2 * indent, ' ');
989+
ax_tree_dump += ui::ToString(node.role);
990+
991+
std::string name = node.GetStringAttribute(ui::AX_ATTR_NAME);
992+
base::ReplaceChars(name, "\r", "\\r", &name);
993+
base::ReplaceChars(name, "\n", "\\n", &name);
994+
if (!name.empty())
995+
ax_tree_dump += " '" + name + "'";
996+
ax_tree_dump += "\n";
997+
for (size_t j = 0; j < node.child_ids.size(); ++j)
998+
id_to_indentation[node.child_ids[j]] = indent + 1;
999+
}
1000+
1001+
return ax_tree_dump;
1002+
}
1003+
1004+
void CountFrames(int* frame_count,
1005+
content::RenderFrameHost* frame) {
1006+
++(*frame_count);
1007+
}
1008+
1009+
std::string g_tree_dump;
1010+
void CheckPdfPluginForRenderFrame(content::RenderFrameHost* frame) {
1011+
content::RenderFrameHostImpl* f = static_cast<content::RenderFrameHostImpl*>(frame);
1012+
content::BrowserAccessibilityManager* manager = f->GetOrCreateBrowserAccessibilityManager();
1013+
ui::AXTreeUpdate ax_tree = manager->SnapshotAXTreeForTesting();
1014+
std::string ax_tree_dump = DumpPdfAccessibilityTree(ax_tree);
1015+
g_tree_dump += ax_tree_dump;
1016+
}
1017+
1018+
} //namespace
1019+
1020+
IN_PROC_BROWSER_TEST_F(NWJSAppTest, PrintChangeFooter) {
1021+
content::BrowserAccessibilityState::GetInstance()->EnableAccessibility();
1022+
LoadAndLaunchPlatformApp("print_test", "Launched");
1023+
content::WebContents* web_contents = GetFirstAppWindowWebContents();
1024+
ASSERT_TRUE(web_contents);
1025+
ASSERT_TRUE(content::ExecuteScript(web_contents, "document.getElementById('testbtn').click()"));
1026+
LOG(INFO) << "waiting for print dialog";
1027+
WebContents* preview_dialog = PrintDialogWaiter(web_contents).Wait();
1028+
LOG(INFO) << "done";
1029+
const int kExpectedFrameCount = 2;
1030+
int frame_count;
1031+
do {
1032+
base::RunLoop run_loop;
1033+
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
1034+
FROM_HERE, run_loop.QuitClosure(), base::TimeDelta::FromSeconds(1));
1035+
run_loop.Run();
1036+
1037+
frame_count = 0;
1038+
preview_dialog->ForEachFrame(
1039+
base::Bind(&CountFrames, base::Unretained(&frame_count)));
1040+
} while (frame_count < kExpectedFrameCount);
1041+
ASSERT_EQ(kExpectedFrameCount, frame_count);
1042+
WaitForAccessibilityTreeToContainNodeWithName(preview_dialog, "hello world\r\n");
1043+
// Make sure all the frames in the dialog has access to the PDF plugin.
1044+
preview_dialog->ForEachFrame(base::Bind(&CheckPdfPluginForRenderFrame));
1045+
EXPECT_TRUE(g_tree_dump.find("nwtestfooter") != std::string::npos);
1046+
}
1047+
9261048
IN_PROC_BROWSER_TEST_P(NWJSWebViewTest, LocalPDF) {
9271049
std::string contents;
9281050
base::FilePath test_dir = test_data_dir_.Append(FILE_PATH_LITERAL("platform_apps")).Append(FILE_PATH_LITERAL("local_pdf"));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<html>
2+
<head>
3+
</head>
4+
<body>
5+
<script>
6+
chrome.test.sendMessage("Launched");
7+
function test0() {
8+
nw.Window.get().print({autoprint: false, footerString: 'nwtestfooter'});
9+
}
10+
function test() {
11+
setTimeout(test0, 1000);
12+
}
13+
</script>
14+
<p>hello world</p>
15+
<button id="testbtn" onclick="test()">test</button>
16+
</body>
17+
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "nw-demo",
3+
"main": "index.html"
4+
}

0 commit comments

Comments
 (0)