OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
7 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 7 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
8 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/browser/ui/browser_commands.h" | 9 #include "chrome/browser/ui/browser_commands.h" |
10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
11 #include "chrome/common/print_messages.h" | 11 #include "chrome/common/print_messages.h" |
12 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
13 #include "chrome/test/base/in_process_browser_test.h" | 13 #include "chrome/test/base/in_process_browser_test.h" |
14 #include "chrome/test/base/ui_test_utils.h" | 14 #include "chrome/test/base/ui_test_utils.h" |
15 #include "content/public/browser/web_contents_observer.h" | 15 #include "content/public/browser/web_contents_observer.h" |
16 #include "content/public/test/browser_test_utils.h" | 16 #include "content/public/test/browser_test_utils.h" |
17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
18 #include "ipc/ipc_message_macros.h" | 18 #include "ipc/ipc_message_macros.h" |
19 | 19 |
20 using content::WebContents; | 20 using content::WebContents; |
21 using content::WebContentsObserver; | 21 using content::WebContentsObserver; |
22 | 22 |
23 class RequestPrintPreviewObserver : public WebContentsObserver { | 23 class RequestPrintPreviewObserver : public WebContentsObserver { |
24 public: | 24 public: |
25 explicit RequestPrintPreviewObserver(WebContents* dialog) | 25 explicit RequestPrintPreviewObserver(WebContents* dialog) |
26 : WebContentsObserver(dialog) { | 26 : WebContentsObserver(dialog) { |
27 } | 27 } |
28 virtual ~RequestPrintPreviewObserver() {} | 28 ~RequestPrintPreviewObserver() override {} |
29 | 29 |
30 void set_quit_closure(const base::Closure& quit_closure) { | 30 void set_quit_closure(const base::Closure& quit_closure) { |
31 quit_closure_ = quit_closure; | 31 quit_closure_ = quit_closure; |
32 } | 32 } |
33 | 33 |
34 private: | 34 private: |
35 // content::WebContentsObserver implementation. | 35 // content::WebContentsObserver implementation. |
36 virtual bool OnMessageReceived(const IPC::Message& message) override { | 36 bool OnMessageReceived(const IPC::Message& message) override { |
37 IPC_BEGIN_MESSAGE_MAP(RequestPrintPreviewObserver, message) | 37 IPC_BEGIN_MESSAGE_MAP(RequestPrintPreviewObserver, message) |
38 IPC_MESSAGE_HANDLER(PrintHostMsg_RequestPrintPreview, | 38 IPC_MESSAGE_HANDLER(PrintHostMsg_RequestPrintPreview, |
39 OnRequestPrintPreview) | 39 OnRequestPrintPreview) |
40 IPC_MESSAGE_UNHANDLED(break;) | 40 IPC_MESSAGE_UNHANDLED(break;) |
41 IPC_END_MESSAGE_MAP(); | 41 IPC_END_MESSAGE_MAP(); |
42 return false; // Report not handled so the real handler receives it. | 42 return false; // Report not handled so the real handler receives it. |
43 } | 43 } |
44 | 44 |
45 void OnRequestPrintPreview( | 45 void OnRequestPrintPreview( |
46 const PrintHostMsg_RequestPrintPreview_Params& /* params */) { | 46 const PrintHostMsg_RequestPrintPreview_Params& /* params */) { |
47 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_); | 47 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_); |
48 } | 48 } |
49 | 49 |
50 base::Closure quit_closure_; | 50 base::Closure quit_closure_; |
51 | 51 |
52 DISALLOW_COPY_AND_ASSIGN(RequestPrintPreviewObserver); | 52 DISALLOW_COPY_AND_ASSIGN(RequestPrintPreviewObserver); |
53 }; | 53 }; |
54 | 54 |
55 class PrintPreviewDialogClonedObserver : public WebContentsObserver { | 55 class PrintPreviewDialogClonedObserver : public WebContentsObserver { |
56 public: | 56 public: |
57 explicit PrintPreviewDialogClonedObserver(WebContents* dialog) | 57 explicit PrintPreviewDialogClonedObserver(WebContents* dialog) |
58 : WebContentsObserver(dialog) { | 58 : WebContentsObserver(dialog) { |
59 } | 59 } |
60 virtual ~PrintPreviewDialogClonedObserver() {} | 60 ~PrintPreviewDialogClonedObserver() override {} |
61 | 61 |
62 RequestPrintPreviewObserver* request_preview_dialog_observer() { | 62 RequestPrintPreviewObserver* request_preview_dialog_observer() { |
63 return request_preview_dialog_observer_.get(); | 63 return request_preview_dialog_observer_.get(); |
64 } | 64 } |
65 | 65 |
66 private: | 66 private: |
67 // content::WebContentsObserver implementation. | 67 // content::WebContentsObserver implementation. |
68 virtual void DidCloneToNewWebContents( | 68 void DidCloneToNewWebContents(WebContents* old_web_contents, |
69 WebContents* old_web_contents, | 69 WebContents* new_web_contents) override { |
70 WebContents* new_web_contents) override { | |
71 request_preview_dialog_observer_.reset( | 70 request_preview_dialog_observer_.reset( |
72 new RequestPrintPreviewObserver(new_web_contents)); | 71 new RequestPrintPreviewObserver(new_web_contents)); |
73 } | 72 } |
74 | 73 |
75 scoped_ptr<RequestPrintPreviewObserver> request_preview_dialog_observer_; | 74 scoped_ptr<RequestPrintPreviewObserver> request_preview_dialog_observer_; |
76 | 75 |
77 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogClonedObserver); | 76 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogClonedObserver); |
78 }; | 77 }; |
79 | 78 |
80 class PrintPreviewDialogDestroyedObserver : public WebContentsObserver { | 79 class PrintPreviewDialogDestroyedObserver : public WebContentsObserver { |
81 public: | 80 public: |
82 explicit PrintPreviewDialogDestroyedObserver(WebContents* dialog) | 81 explicit PrintPreviewDialogDestroyedObserver(WebContents* dialog) |
83 : WebContentsObserver(dialog), | 82 : WebContentsObserver(dialog), |
84 dialog_destroyed_(false) { | 83 dialog_destroyed_(false) { |
85 } | 84 } |
86 virtual ~PrintPreviewDialogDestroyedObserver() {} | 85 ~PrintPreviewDialogDestroyedObserver() override {} |
87 | 86 |
88 bool dialog_destroyed() const { return dialog_destroyed_; } | 87 bool dialog_destroyed() const { return dialog_destroyed_; } |
89 | 88 |
90 private: | 89 private: |
91 // content::WebContentsObserver implementation. | 90 // content::WebContentsObserver implementation. |
92 virtual void WebContentsDestroyed() override { | 91 void WebContentsDestroyed() override { dialog_destroyed_ = true; } |
93 dialog_destroyed_ = true; | |
94 } | |
95 | 92 |
96 bool dialog_destroyed_; | 93 bool dialog_destroyed_; |
97 | 94 |
98 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDestroyedObserver); | 95 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDestroyedObserver); |
99 }; | 96 }; |
100 | 97 |
101 class PrintPreviewDialogControllerBrowserTest : public InProcessBrowserTest { | 98 class PrintPreviewDialogControllerBrowserTest : public InProcessBrowserTest { |
102 public: | 99 public: |
103 PrintPreviewDialogControllerBrowserTest() : initiator_(NULL) {} | 100 PrintPreviewDialogControllerBrowserTest() : initiator_(NULL) {} |
104 virtual ~PrintPreviewDialogControllerBrowserTest() {} | 101 virtual ~PrintPreviewDialogControllerBrowserTest() {} |
105 | 102 |
106 WebContents* initiator() { | 103 WebContents* initiator() { |
107 return initiator_; | 104 return initiator_; |
108 } | 105 } |
109 | 106 |
110 void PrintPreview() { | 107 void PrintPreview() { |
111 base::RunLoop run_loop; | 108 base::RunLoop run_loop; |
112 request_preview_dialog_observer()->set_quit_closure(run_loop.QuitClosure()); | 109 request_preview_dialog_observer()->set_quit_closure(run_loop.QuitClosure()); |
113 chrome::Print(browser()); | 110 chrome::Print(browser()); |
114 run_loop.Run(); | 111 run_loop.Run(); |
115 } | 112 } |
116 | 113 |
117 WebContents* GetPrintPreviewDialog() { | 114 WebContents* GetPrintPreviewDialog() { |
118 printing::PrintPreviewDialogController* dialog_controller = | 115 printing::PrintPreviewDialogController* dialog_controller = |
119 printing::PrintPreviewDialogController::GetInstance(); | 116 printing::PrintPreviewDialogController::GetInstance(); |
120 return dialog_controller->GetPrintPreviewForContents(initiator_); | 117 return dialog_controller->GetPrintPreviewForContents(initiator_); |
121 } | 118 } |
122 | 119 |
123 private: | 120 private: |
124 virtual void SetUpOnMainThread() override { | 121 void SetUpOnMainThread() override { |
125 WebContents* first_tab = | 122 WebContents* first_tab = |
126 browser()->tab_strip_model()->GetActiveWebContents(); | 123 browser()->tab_strip_model()->GetActiveWebContents(); |
127 ASSERT_TRUE(first_tab); | 124 ASSERT_TRUE(first_tab); |
128 | 125 |
129 // Open a new tab so |cloned_tab_observer_| can see it first and attach a | 126 // Open a new tab so |cloned_tab_observer_| can see it first and attach a |
130 // RequestPrintPreviewObserver to it before the real | 127 // RequestPrintPreviewObserver to it before the real |
131 // PrintPreviewMessageHandler gets created. Thus enabling | 128 // PrintPreviewMessageHandler gets created. Thus enabling |
132 // RequestPrintPreviewObserver to get messages first for the purposes of | 129 // RequestPrintPreviewObserver to get messages first for the purposes of |
133 // this test. | 130 // this test. |
134 cloned_tab_observer_.reset(new PrintPreviewDialogClonedObserver(first_tab)); | 131 cloned_tab_observer_.reset(new PrintPreviewDialogClonedObserver(first_tab)); |
135 chrome::DuplicateTab(browser()); | 132 chrome::DuplicateTab(browser()); |
136 | 133 |
137 initiator_ = browser()->tab_strip_model()->GetActiveWebContents(); | 134 initiator_ = browser()->tab_strip_model()->GetActiveWebContents(); |
138 ASSERT_TRUE(initiator_); | 135 ASSERT_TRUE(initiator_); |
139 ASSERT_NE(first_tab, initiator_); | 136 ASSERT_NE(first_tab, initiator_); |
140 } | 137 } |
141 | 138 |
142 virtual void TearDownOnMainThread() override { | 139 void TearDownOnMainThread() override { |
143 cloned_tab_observer_.reset(); | 140 cloned_tab_observer_.reset(); |
144 initiator_ = NULL; | 141 initiator_ = NULL; |
145 } | 142 } |
146 | 143 |
147 RequestPrintPreviewObserver* request_preview_dialog_observer() { | 144 RequestPrintPreviewObserver* request_preview_dialog_observer() { |
148 return cloned_tab_observer_->request_preview_dialog_observer(); | 145 return cloned_tab_observer_->request_preview_dialog_observer(); |
149 } | 146 } |
150 | 147 |
151 scoped_ptr<PrintPreviewDialogClonedObserver> cloned_tab_observer_; | 148 scoped_ptr<PrintPreviewDialogClonedObserver> cloned_tab_observer_; |
152 WebContents* initiator_; | 149 WebContents* initiator_; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 browser()->tab_strip_model()->GetActiveWebContents()); | 214 browser()->tab_strip_model()->GetActiveWebContents()); |
218 ASSERT_TRUE(dialog_destroyed_observer.dialog_destroyed()); | 215 ASSERT_TRUE(dialog_destroyed_observer.dialog_destroyed()); |
219 | 216 |
220 // Try printing again. | 217 // Try printing again. |
221 PrintPreview(); | 218 PrintPreview(); |
222 | 219 |
223 // Create a preview dialog for the initiator tab. | 220 // Create a preview dialog for the initiator tab. |
224 WebContents* new_preview_dialog = GetPrintPreviewDialog(); | 221 WebContents* new_preview_dialog = GetPrintPreviewDialog(); |
225 EXPECT_TRUE(new_preview_dialog); | 222 EXPECT_TRUE(new_preview_dialog); |
226 } | 223 } |
OLD | NEW |