OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/printing/print_dialog_cloud.h" | 5 #include "chrome/browser/printing/print_dialog_cloud.h" |
6 #include "chrome/browser/printing/print_dialog_cloud_internal.h" | 6 #include "chrome/browser/printing/print_dialog_cloud_internal.h" |
7 | 7 |
8 #include <functional> | 8 #include <functional> |
9 | 9 |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "chrome/test/in_process_browser_test.h" | 24 #include "chrome/test/in_process_browser_test.h" |
25 #include "chrome/test/ui_test_utils.h" | 25 #include "chrome/test/ui_test_utils.h" |
26 #include "net/url_request/url_request_filter.h" | 26 #include "net/url_request/url_request_filter.h" |
27 #include "net/url_request/url_request_test_job.h" | 27 #include "net/url_request/url_request_test_job.h" |
28 #include "net/url_request/url_request_unittest.h" | 28 #include "net/url_request/url_request_unittest.h" |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 class TestData { | 32 class TestData { |
33 public: | 33 public: |
34 TestData() {} | 34 static TestData* GetInstance() { |
| 35 return Singleton<TestData>::get(); |
| 36 } |
35 | 37 |
36 const char* GetTestData() { | 38 const char* GetTestData() { |
37 // Fetching this data blocks the IO thread, but we don't really care because | 39 // Fetching this data blocks the IO thread, but we don't really care because |
38 // this is a test. | 40 // this is a test. |
39 base::ThreadRestrictions::ScopedAllowIO allow_io; | 41 base::ThreadRestrictions::ScopedAllowIO allow_io; |
40 | 42 |
41 if (test_data_.empty()) { | 43 if (test_data_.empty()) { |
42 FilePath test_data_directory; | 44 FilePath test_data_directory; |
43 PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory); | 45 PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory); |
44 FilePath test_file = | 46 FilePath test_file = |
45 test_data_directory.AppendASCII("printing/cloud_print_uitest.html"); | 47 test_data_directory.AppendASCII("printing/cloud_print_uitest.html"); |
46 file_util::ReadFileToString(test_file, &test_data_); | 48 file_util::ReadFileToString(test_file, &test_data_); |
47 } | 49 } |
48 return test_data_.c_str(); | 50 return test_data_.c_str(); |
49 } | 51 } |
50 private: | 52 private: |
| 53 TestData() {} |
| 54 |
51 std::string test_data_; | 55 std::string test_data_; |
| 56 |
| 57 friend struct DefaultSingletonTraits<TestData>; |
52 }; | 58 }; |
53 | 59 |
54 // A simple test URLRequestJob. We don't care what it does, only that | 60 // A simple test URLRequestJob. We don't care what it does, only that |
55 // whether it starts and finishes. | 61 // whether it starts and finishes. |
56 class SimpleTestJob : public URLRequestTestJob { | 62 class SimpleTestJob : public URLRequestTestJob { |
57 public: | 63 public: |
58 explicit SimpleTestJob(net::URLRequest* request) | 64 explicit SimpleTestJob(net::URLRequest* request) |
59 : URLRequestTestJob(request, test_headers(), | 65 : URLRequestTestJob(request, test_headers(), |
60 Singleton<TestData>()->GetTestData(), true) {} | 66 TestData::GetInstance()->GetTestData(), true) {} |
61 | 67 |
62 virtual void GetResponseInfo(net::HttpResponseInfo* info) { | 68 virtual void GetResponseInfo(net::HttpResponseInfo* info) { |
63 URLRequestTestJob::GetResponseInfo(info); | 69 URLRequestTestJob::GetResponseInfo(info); |
64 if (request_->url().SchemeIsSecure()) { | 70 if (request_->url().SchemeIsSecure()) { |
65 // Make up a fake certificate for this response since we don't have | 71 // Make up a fake certificate for this response since we don't have |
66 // access to the real SSL info. | 72 // access to the real SSL info. |
67 const char* kCertIssuer = "Chrome Internal"; | 73 const char* kCertIssuer = "Chrome Internal"; |
68 const int kLifetimeDays = 100; | 74 const int kLifetimeDays = 100; |
69 | 75 |
70 info->ssl_info.cert = | 76 info->ssl_info.cert = |
71 new net::X509Certificate(request_->url().GetWithEmptyPath().spec(), | 77 new net::X509Certificate(request_->url().GetWithEmptyPath().spec(), |
72 kCertIssuer, | 78 kCertIssuer, |
73 base::Time::Now(), | 79 base::Time::Now(), |
74 base::Time::Now() + | 80 base::Time::Now() + |
75 base::TimeDelta::FromDays(kLifetimeDays)); | 81 base::TimeDelta::FromDays(kLifetimeDays)); |
76 info->ssl_info.cert_status = 0; | 82 info->ssl_info.cert_status = 0; |
77 info->ssl_info.security_bits = -1; | 83 info->ssl_info.security_bits = -1; |
78 } | 84 } |
79 } | 85 } |
80 | 86 |
81 private: | 87 private: |
82 ~SimpleTestJob() {} | 88 ~SimpleTestJob() {} |
83 }; | 89 }; |
84 | 90 |
85 class TestController { | 91 class TestController { |
86 public: | 92 public: |
87 TestController() | 93 static TestController* GetInstance() { |
88 : result_(false), | 94 return Singleton<TestController>::get(); |
89 use_delegate_(false), | 95 } |
90 delegate_(NULL) {} | |
91 void set_result(bool value) { | 96 void set_result(bool value) { |
92 result_ = value; | 97 result_ = value; |
93 } | 98 } |
94 bool result() { | 99 bool result() { |
95 return result_; | 100 return result_; |
96 } | 101 } |
97 void set_expected_url(const GURL& url) { | 102 void set_expected_url(const GURL& url) { |
98 expected_url_ = url; | 103 expected_url_ = url; |
99 } | 104 } |
100 const GURL expected_url() { | 105 const GURL expected_url() { |
101 return expected_url_; | 106 return expected_url_; |
102 } | 107 } |
103 void set_delegate(TestDelegate* delegate) { | 108 void set_delegate(TestDelegate* delegate) { |
104 delegate_ = delegate; | 109 delegate_ = delegate; |
105 } | 110 } |
106 TestDelegate* delegate() { | 111 TestDelegate* delegate() { |
107 return delegate_; | 112 return delegate_; |
108 } | 113 } |
109 void set_use_delegate(bool value) { | 114 void set_use_delegate(bool value) { |
110 use_delegate_ = value; | 115 use_delegate_ = value; |
111 } | 116 } |
112 bool use_delegate() { | 117 bool use_delegate() { |
113 return use_delegate_; | 118 return use_delegate_; |
114 } | 119 } |
115 private: | 120 private: |
| 121 TestController() |
| 122 : result_(false), |
| 123 use_delegate_(false), |
| 124 delegate_(NULL) {} |
| 125 |
116 bool result_; | 126 bool result_; |
117 bool use_delegate_; | 127 bool use_delegate_; |
118 GURL expected_url_; | 128 GURL expected_url_; |
119 TestDelegate* delegate_; | 129 TestDelegate* delegate_; |
| 130 |
| 131 friend struct DefaultSingletonTraits<TestController>; |
120 }; | 132 }; |
121 | 133 |
122 } // namespace | 134 } // namespace |
123 | 135 |
124 class PrintDialogCloudTest : public InProcessBrowserTest { | 136 class PrintDialogCloudTest : public InProcessBrowserTest { |
125 public: | 137 public: |
126 PrintDialogCloudTest() : handler_added_(false) { | 138 PrintDialogCloudTest() : handler_added_(false) { |
127 PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_); | 139 PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_); |
128 } | 140 } |
129 | 141 |
130 // Must be static for handing into AddHostnameHandler. | 142 // Must be static for handing into AddHostnameHandler. |
131 static net::URLRequest::ProtocolFactory Factory; | 143 static net::URLRequest::ProtocolFactory Factory; |
132 | 144 |
133 class AutoQuitDelegate : public TestDelegate { | 145 class AutoQuitDelegate : public TestDelegate { |
134 public: | 146 public: |
135 AutoQuitDelegate() {} | 147 AutoQuitDelegate() {} |
136 | 148 |
137 virtual void OnResponseCompleted(net::URLRequest* request) { | 149 virtual void OnResponseCompleted(net::URLRequest* request) { |
138 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 150 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
139 new MessageLoop::QuitTask()); | 151 new MessageLoop::QuitTask()); |
140 } | 152 } |
141 }; | 153 }; |
142 | 154 |
143 virtual void SetUp() { | 155 virtual void SetUp() { |
144 Singleton<TestController>()->set_result(false); | 156 TestController::GetInstance()->set_result(false); |
145 InProcessBrowserTest::SetUp(); | 157 InProcessBrowserTest::SetUp(); |
146 } | 158 } |
147 | 159 |
148 virtual void TearDown() { | 160 virtual void TearDown() { |
149 if (handler_added_) { | 161 if (handler_added_) { |
150 URLRequestFilter* filter = URLRequestFilter::GetInstance(); | 162 URLRequestFilter* filter = URLRequestFilter::GetInstance(); |
151 filter->RemoveHostnameHandler(scheme_, host_name_); | 163 filter->RemoveHostnameHandler(scheme_, host_name_); |
152 handler_added_ = false; | 164 handler_added_ = false; |
153 Singleton<TestController>()->set_delegate(NULL); | 165 TestController::GetInstance()->set_delegate(NULL); |
154 } | 166 } |
155 InProcessBrowserTest::TearDown(); | 167 InProcessBrowserTest::TearDown(); |
156 } | 168 } |
157 | 169 |
158 // Normally this is something I would expect could go into SetUp(), | 170 // Normally this is something I would expect could go into SetUp(), |
159 // but there seems to be some timing or ordering related issue with | 171 // but there seems to be some timing or ordering related issue with |
160 // the test harness that made that flaky. Calling this from the | 172 // the test harness that made that flaky. Calling this from the |
161 // individual test functions seems to fix that. | 173 // individual test functions seems to fix that. |
162 void AddTestHandlers() { | 174 void AddTestHandlers() { |
163 if (!handler_added_) { | 175 if (!handler_added_) { |
164 URLRequestFilter* filter = URLRequestFilter::GetInstance(); | 176 URLRequestFilter* filter = URLRequestFilter::GetInstance(); |
165 GURL cloud_print_service_url = | 177 GURL cloud_print_service_url = |
166 CloudPrintURL(browser()->profile()). | 178 CloudPrintURL(browser()->profile()). |
167 GetCloudPrintServiceURL(); | 179 GetCloudPrintServiceURL(); |
168 scheme_ = cloud_print_service_url.scheme(); | 180 scheme_ = cloud_print_service_url.scheme(); |
169 host_name_ = cloud_print_service_url.host(); | 181 host_name_ = cloud_print_service_url.host(); |
170 filter->AddHostnameHandler(scheme_, host_name_, | 182 filter->AddHostnameHandler(scheme_, host_name_, |
171 &PrintDialogCloudTest::Factory); | 183 &PrintDialogCloudTest::Factory); |
172 handler_added_ = true; | 184 handler_added_ = true; |
173 | 185 |
174 GURL cloud_print_dialog_url = | 186 GURL cloud_print_dialog_url = |
175 CloudPrintURL(browser()->profile()). | 187 CloudPrintURL(browser()->profile()). |
176 GetCloudPrintServiceDialogURL(); | 188 GetCloudPrintServiceDialogURL(); |
177 Singleton<TestController>()->set_expected_url(cloud_print_dialog_url); | 189 TestController::GetInstance()->set_expected_url(cloud_print_dialog_url); |
178 Singleton<TestController>()->set_delegate(&delegate_); | 190 TestController::GetInstance()->set_delegate(&delegate_); |
179 } | 191 } |
180 | 192 |
181 CreateDialogForTest(); | 193 CreateDialogForTest(); |
182 } | 194 } |
183 | 195 |
184 void CreateDialogForTest() { | 196 void CreateDialogForTest() { |
185 FilePath path_to_pdf = | 197 FilePath path_to_pdf = |
186 test_data_directory_.AppendASCII("printing/cloud_print_uitest.pdf"); | 198 test_data_directory_.AppendASCII("printing/cloud_print_uitest.pdf"); |
187 BrowserThread::PostTask( | 199 BrowserThread::PostTask( |
188 BrowserThread::UI, FROM_HERE, | 200 BrowserThread::UI, FROM_HERE, |
189 NewRunnableFunction(&PrintDialogCloud::CreateDialogImpl, path_to_pdf)); | 201 NewRunnableFunction(&PrintDialogCloud::CreateDialogImpl, path_to_pdf)); |
190 } | 202 } |
191 | 203 |
192 bool handler_added_; | 204 bool handler_added_; |
193 std::string scheme_; | 205 std::string scheme_; |
194 std::string host_name_; | 206 std::string host_name_; |
195 FilePath test_data_directory_; | 207 FilePath test_data_directory_; |
196 AutoQuitDelegate delegate_; | 208 AutoQuitDelegate delegate_; |
197 }; | 209 }; |
198 | 210 |
199 URLRequestJob* PrintDialogCloudTest::Factory(net::URLRequest* request, | 211 URLRequestJob* PrintDialogCloudTest::Factory(net::URLRequest* request, |
200 const std::string& scheme) { | 212 const std::string& scheme) { |
201 if (Singleton<TestController>()->use_delegate()) | 213 if (TestController::GetInstance()->use_delegate()) |
202 request->set_delegate(Singleton<TestController>()->delegate()); | 214 request->set_delegate(TestController::GetInstance()->delegate()); |
203 if (request && | 215 if (request && |
204 (request->url() == Singleton<TestController>()->expected_url())) { | 216 (request->url() == TestController::GetInstance()->expected_url())) { |
205 Singleton<TestController>()->set_result(true); | 217 TestController::GetInstance()->set_result(true); |
206 } | 218 } |
207 return new SimpleTestJob(request); | 219 return new SimpleTestJob(request); |
208 } | 220 } |
209 | 221 |
210 IN_PROC_BROWSER_TEST_F(PrintDialogCloudTest, HandlersRegistered) { | 222 IN_PROC_BROWSER_TEST_F(PrintDialogCloudTest, HandlersRegistered) { |
211 BrowserList::SetLastActive(browser()); | 223 BrowserList::SetLastActive(browser()); |
212 ASSERT_TRUE(BrowserList::GetLastActive()); | 224 ASSERT_TRUE(BrowserList::GetLastActive()); |
213 | 225 |
214 AddTestHandlers(); | 226 AddTestHandlers(); |
215 | 227 |
216 Singleton<TestController>()->set_use_delegate(true); | 228 TestController::GetInstance()->set_use_delegate(true); |
217 | 229 |
218 ui_test_utils::RunMessageLoop(); | 230 ui_test_utils::RunMessageLoop(); |
219 | 231 |
220 ASSERT_TRUE(Singleton<TestController>()->result()); | 232 ASSERT_TRUE(TestController::GetInstance()->result()); |
221 } | 233 } |
222 | 234 |
223 #if defined(OS_CHROMEOS) | 235 #if defined(OS_CHROMEOS) |
224 // Disabled until the extern URL is live so that the Print menu item | 236 // Disabled until the extern URL is live so that the Print menu item |
225 // can be enabled for Chromium OS. | 237 // can be enabled for Chromium OS. |
226 IN_PROC_BROWSER_TEST_F(PrintDialogCloudTest, DISABLED_DialogGrabbed) { | 238 IN_PROC_BROWSER_TEST_F(PrintDialogCloudTest, DISABLED_DialogGrabbed) { |
227 BrowserList::SetLastActive(browser()); | 239 BrowserList::SetLastActive(browser()); |
228 ASSERT_TRUE(BrowserList::GetLastActive()); | 240 ASSERT_TRUE(BrowserList::GetLastActive()); |
229 | 241 |
230 AddTestHandlers(); | 242 AddTestHandlers(); |
231 | 243 |
232 // This goes back one step further for the Chrome OS case, to making | 244 // This goes back one step further for the Chrome OS case, to making |
233 // sure 'window.print()' gets to the right place. | 245 // sure 'window.print()' gets to the right place. |
234 ASSERT_TRUE(browser()->GetSelectedTabContents()); | 246 ASSERT_TRUE(browser()->GetSelectedTabContents()); |
235 ASSERT_TRUE(browser()->GetSelectedTabContents()->render_view_host()); | 247 ASSERT_TRUE(browser()->GetSelectedTabContents()->render_view_host()); |
236 | 248 |
237 std::wstring window_print(L"window.print()"); | 249 std::wstring window_print(L"window.print()"); |
238 browser()->GetSelectedTabContents()->render_view_host()-> | 250 browser()->GetSelectedTabContents()->render_view_host()-> |
239 ExecuteJavascriptInWebFrame(std::wstring(), window_print); | 251 ExecuteJavascriptInWebFrame(std::wstring(), window_print); |
240 | 252 |
241 ui_test_utils::RunMessageLoop(); | 253 ui_test_utils::RunMessageLoop(); |
242 | 254 |
243 ASSERT_TRUE(Singleton<TestController>()->result()); | 255 ASSERT_TRUE(TestController::GetInstance()->result()); |
244 } | 256 } |
245 #endif | 257 #endif |
OLD | NEW |