OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <set> | |
6 #include <vector> | |
7 | |
8 #include "base/memory/ptr_util.h" | |
9 #include "base/path_service.h" | |
10 #include "base/stl_util.h" | |
11 #include "base/strings/stringprintf.h" | |
12 #include "chrome/browser/printing/print_view_manager.h" | |
13 #include "chrome/browser/ui/browser.h" | |
14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
15 #include "chrome/common/chrome_paths.h" | |
16 #include "chrome/test/base/in_process_browser_test.h" | |
17 #include "chrome/test/base/ui_test_utils.h" | |
18 #include "content/public/browser/render_frame_host.h" | |
19 #include "content/public/browser/web_contents_delegate.h" | |
20 #include "content/public/test/browser_test_utils.h" | |
21 #include "net/dns/mock_host_resolver.h" | |
22 #include "net/test/embedded_test_server/embedded_test_server.h" | |
23 #include "url/gurl.h" | |
24 | |
25 using content::RenderFrameHost; | |
26 | |
27 namespace printing { | |
28 | |
29 namespace { | |
30 | |
31 struct ScriptedPrintRequest { | |
32 const RenderFrameHost* render_frame_host; | |
33 GURL url; | |
34 }; | |
35 | |
36 class PrintViewManagerTestDelegate : public PrintViewManager::TestDelegate { | |
37 public: | |
38 PrintViewManagerTestDelegate() {} | |
39 ~PrintViewManagerTestDelegate() override {} | |
40 | |
41 // PrintViewManager::TestDelegate: | |
42 bool ScriptedPrint(const RenderFrameHost* render_frame_host) override { | |
43 scripted_print_requests_.push_back( | |
44 {render_frame_host, render_frame_host->GetLastCommittedURL()}); | |
45 return false; | |
46 } | |
47 | |
48 const std::vector<ScriptedPrintRequest>& scripted_print_requests() const { | |
49 return scripted_print_requests_; | |
50 } | |
51 | |
52 private: | |
53 std::vector<ScriptedPrintRequest> scripted_print_requests_; | |
54 | |
55 DISALLOW_COPY_AND_ASSIGN(PrintViewManagerTestDelegate); | |
56 }; | |
57 | |
58 class WindowDotPrintObserver : public content::WebContentsObserver { | |
59 public: | |
60 explicit WindowDotPrintObserver(content::WebContents* contents) | |
61 : WebContentsObserver(contents) { | |
62 PrintViewManager::FromWebContents(web_contents()) | |
63 ->SetTestDelegate(&test_delegate_); | |
64 } | |
65 | |
66 ~WindowDotPrintObserver() override { | |
67 PrintViewManager::FromWebContents(web_contents())->SetTestDelegate(nullptr); | |
68 } | |
69 | |
70 const std::vector<ScriptedPrintRequest>& scripted_print_requests() const { | |
71 return test_delegate_.scripted_print_requests(); | |
72 } | |
73 | |
74 const std::set<const RenderFrameHost*>& render_frame_hosts() const { | |
75 return rfhs_; | |
76 } | |
77 | |
78 private: | |
79 // WebContentsObserver: | |
80 void RenderFrameCreated(RenderFrameHost* render_frame_host) override { | |
81 rfhs_.insert(render_frame_host); | |
82 } | |
83 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override { | |
84 rfhs_.erase(render_frame_host); | |
85 } | |
86 | |
87 std::set<const RenderFrameHost*> rfhs_; | |
88 PrintViewManagerTestDelegate test_delegate_; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(WindowDotPrintObserver); | |
91 }; | |
92 | |
93 } // namespace | |
94 | |
95 class PrintViewManagerTest : public InProcessBrowserTest { | |
96 public: | |
97 void SetUpOnMainThread() override { | |
98 host_resolver()->AddRule("*", "127.0.0.1"); | |
99 ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); | |
100 content::SetupCrossSiteRedirector(embedded_test_server()); | |
101 | |
102 base::FilePath test_data_dir; | |
103 CHECK(base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)); | |
104 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); | |
105 embedded_test_server()->StartAcceptingConnections(); | |
106 } | |
107 }; | |
108 | |
109 IN_PROC_BROWSER_TEST_F(PrintViewManagerTest, SubframeWindowDotPrint) { | |
nasko
2016/11/18 19:19:42
Put a small comment describing what this test is a
| |
110 { | |
nasko
2016/11/18 19:19:42
Why have this nested scope when it includes all th
| |
111 WindowDotPrintObserver observer( | |
112 browser()->tab_strip_model()->GetActiveWebContents()); | |
113 | |
114 GURL main_url(embedded_test_server()->GetURL( | |
115 "/cross-site/a.com/iframe_cross_site_print.html")); | |
116 ui_test_utils::NavigateToURL(browser(), main_url); | |
117 | |
118 const auto& requests = observer.scripted_print_requests(); | |
119 ASSERT_EQ(1U, requests.size()); | |
120 | |
121 std::string expected_subframe_url = | |
122 base::StringPrintf("http://c.com:%d/printing/window_print.html", | |
123 embedded_test_server()->port()); | |
nasko
2016/11/18 19:19:42
You can just call embedded_test_server()->GetURL("
| |
124 EXPECT_EQ(GURL(expected_subframe_url), requests[0].url); | |
125 EXPECT_TRUE(base::ContainsKey(observer.render_frame_hosts(), | |
126 requests[0].render_frame_host)); | |
nasko
2016/11/18 19:19:42
Why not check that the specific RenderFrameHost is
| |
127 } | |
128 } | |
129 | |
130 } // namespace printing | |
OLD | NEW |