Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: content/shell/renderer/test_runner/test_runner.cc

Issue 301243022: Add testRunner.capturePixelsThen() method (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/shell/renderer/test_runner/test_runner.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "content/shell/renderer/test_runner/test_runner.h" 5 #include "content/shell/renderer/test_runner/test_runner.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "content/shell/common/test_runner/test_preferences.h" 10 #include "content/shell/common/test_runner/test_preferences.h"
11 #include "content/shell/renderer/test_runner/MockWebSpeechRecognizer.h" 11 #include "content/shell/renderer/test_runner/MockWebSpeechRecognizer.h"
12 #include "content/shell/renderer/test_runner/TestInterfaces.h" 12 #include "content/shell/renderer/test_runner/TestInterfaces.h"
13 #include "content/shell/renderer/test_runner/WebPermissions.h" 13 #include "content/shell/renderer/test_runner/WebPermissions.h"
14 #include "content/shell/renderer/test_runner/WebTestDelegate.h" 14 #include "content/shell/renderer/test_runner/WebTestDelegate.h"
15 #include "content/shell/renderer/test_runner/mock_web_push_client.h" 15 #include "content/shell/renderer/test_runner/mock_web_push_client.h"
16 #include "content/shell/renderer/test_runner/notification_presenter.h" 16 #include "content/shell/renderer/test_runner/notification_presenter.h"
17 #include "content/shell/renderer/test_runner/web_test_proxy.h" 17 #include "content/shell/renderer/test_runner/web_test_proxy.h"
18 #include "gin/arguments.h" 18 #include "gin/arguments.h"
19 #include "gin/array_buffer.h" 19 #include "gin/array_buffer.h"
20 #include "gin/handle.h" 20 #include "gin/handle.h"
21 #include "gin/object_template_builder.h" 21 #include "gin/object_template_builder.h"
22 #include "gin/wrappable.h" 22 #include "gin/wrappable.h"
23 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
23 #include "third_party/WebKit/public/platform/WebBatteryStatus.h" 24 #include "third_party/WebKit/public/platform/WebBatteryStatus.h"
24 #include "third_party/WebKit/public/platform/WebCanvas.h" 25 #include "third_party/WebKit/public/platform/WebCanvas.h"
25 #include "third_party/WebKit/public/platform/WebData.h" 26 #include "third_party/WebKit/public/platform/WebData.h"
26 #include "third_party/WebKit/public/platform/WebDeviceMotionData.h" 27 #include "third_party/WebKit/public/platform/WebDeviceMotionData.h"
27 #include "third_party/WebKit/public/platform/WebDeviceOrientationData.h" 28 #include "third_party/WebKit/public/platform/WebDeviceOrientationData.h"
28 #include "third_party/WebKit/public/platform/WebPoint.h" 29 #include "third_party/WebKit/public/platform/WebPoint.h"
29 #include "third_party/WebKit/public/platform/WebURLResponse.h" 30 #include "third_party/WebKit/public/platform/WebURLResponse.h"
31 #include "third_party/WebKit/public/web/WebArrayBufferConverter.h"
30 #include "third_party/WebKit/public/web/WebBindings.h" 32 #include "third_party/WebKit/public/web/WebBindings.h"
31 #include "third_party/WebKit/public/web/WebDataSource.h" 33 #include "third_party/WebKit/public/web/WebDataSource.h"
32 #include "third_party/WebKit/public/web/WebDocument.h" 34 #include "third_party/WebKit/public/web/WebDocument.h"
33 #include "third_party/WebKit/public/web/WebFindOptions.h" 35 #include "third_party/WebKit/public/web/WebFindOptions.h"
34 #include "third_party/WebKit/public/web/WebFrame.h" 36 #include "third_party/WebKit/public/web/WebFrame.h"
35 #include "third_party/WebKit/public/web/WebInputElement.h" 37 #include "third_party/WebKit/public/web/WebInputElement.h"
36 #include "third_party/WebKit/public/web/WebKit.h" 38 #include "third_party/WebKit/public/web/WebKit.h"
37 #include "third_party/WebKit/public/web/WebMIDIClientMock.h" 39 #include "third_party/WebKit/public/web/WebMIDIClientMock.h"
38 #include "third_party/WebKit/public/web/WebPageOverlay.h" 40 #include "third_party/WebKit/public/web/WebPageOverlay.h"
39 #include "third_party/WebKit/public/web/WebScriptSource.h" 41 #include "third_party/WebKit/public/web/WebScriptSource.h"
40 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" 42 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
41 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" 43 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
42 #include "third_party/WebKit/public/web/WebSettings.h" 44 #include "third_party/WebKit/public/web/WebSettings.h"
43 #include "third_party/WebKit/public/web/WebSurroundingText.h" 45 #include "third_party/WebKit/public/web/WebSurroundingText.h"
44 #include "third_party/WebKit/public/web/WebView.h" 46 #include "third_party/WebKit/public/web/WebView.h"
47 #include "third_party/skia/include/core/SkBitmap.h"
45 #include "third_party/skia/include/core/SkCanvas.h" 48 #include "third_party/skia/include/core/SkCanvas.h"
46 49
47 #if defined(__linux__) || defined(ANDROID) 50 #if defined(__linux__) || defined(ANDROID)
48 #include "third_party/WebKit/public/web/linux/WebFontRendering.h" 51 #include "third_party/WebKit/public/web/linux/WebFontRendering.h"
49 #endif 52 #endif
50 53
51 using namespace blink; 54 using namespace blink;
52 55
53 namespace content { 56 namespace content {
54 57
(...skipping 19 matching lines...) Expand all
74 private: 77 private:
75 CallbackMethodType callback_; 78 CallbackMethodType callback_;
76 }; 79 };
77 80
78 } // namespace 81 } // namespace
79 82
80 class InvokeCallbackTask : public WebMethodTask<TestRunner> { 83 class InvokeCallbackTask : public WebMethodTask<TestRunner> {
81 public: 84 public:
82 InvokeCallbackTask(TestRunner* object, v8::Handle<v8::Function> callback) 85 InvokeCallbackTask(TestRunner* object, v8::Handle<v8::Function> callback)
83 : WebMethodTask<TestRunner>(object), 86 : WebMethodTask<TestRunner>(object),
84 callback_(blink::mainThreadIsolate(), callback) {} 87 callback_(blink::mainThreadIsolate(), callback),
88 argc_(0) {}
85 89
86 virtual void runIfValid() OVERRIDE { 90 virtual void runIfValid() OVERRIDE {
87 v8::Isolate* isolate = blink::mainThreadIsolate(); 91 v8::Isolate* isolate = blink::mainThreadIsolate();
88 v8::HandleScope handle_scope(isolate); 92 v8::HandleScope handle_scope(isolate);
89 WebFrame* frame = m_object->web_view_->mainFrame(); 93 WebFrame* frame = m_object->web_view_->mainFrame();
90 94
91 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); 95 v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
92 if (context.IsEmpty()) 96 if (context.IsEmpty())
93 return; 97 return;
94 98
95 v8::Context::Scope context_scope(context); 99 v8::Context::Scope context_scope(context);
96 100
101 scoped_ptr<v8::Handle<v8::Value>[]> local_argv;
102 if (argc_) {
103 local_argv.reset(new v8::Handle<v8::Value>[argc_]);
104 for (int i = 0; i < argc_; ++i)
105 local_argv[i] = v8::Local<v8::Value>::New(isolate, argv_[i]);
106 }
107
97 frame->callFunctionEvenIfScriptDisabled( 108 frame->callFunctionEvenIfScriptDisabled(
98 v8::Local<v8::Function>::New(isolate, callback_), 109 v8::Local<v8::Function>::New(isolate, callback_),
99 context->Global(), 110 context->Global(),
100 0, 111 argc_,
101 NULL); 112 local_argv.get());
113 }
114
115 void SetArguments(int argc, v8::Handle<v8::Value> argv[]) {
116 v8::Isolate* isolate = blink::mainThreadIsolate();
117 argc_ = argc;
118 argv_.reset(new v8::UniquePersistent<v8::Value>[argc]);
119 for (int i = 0; i < argc; ++i)
120 argv_[i] = v8::UniquePersistent<v8::Value>(isolate, argv[i]);
102 } 121 }
103 122
104 private: 123 private:
105 v8::UniquePersistent<v8::Function> callback_; 124 v8::UniquePersistent<v8::Function> callback_;
125 int argc_;
126 scoped_ptr<v8::UniquePersistent<v8::Value>[]> argv_;
106 }; 127 };
107 128
108 class TestRunnerBindings : public gin::Wrappable<TestRunnerBindings> { 129 class TestRunnerBindings : public gin::Wrappable<TestRunnerBindings> {
109 public: 130 public:
110 static gin::WrapperInfo kWrapperInfo; 131 static gin::WrapperInfo kWrapperInfo;
111 132
112 static void Install(base::WeakPtr<TestRunner> controller, 133 static void Install(base::WeakPtr<TestRunner> controller,
113 WebFrame* frame); 134 WebFrame* frame);
114 135
115 private: 136 private:
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 bool SimulateWebNotificationClick(const std::string& value); 270 bool SimulateWebNotificationClick(const std::string& value);
250 void AddMockSpeechRecognitionResult(const std::string& transcript, 271 void AddMockSpeechRecognitionResult(const std::string& transcript,
251 double confidence); 272 double confidence);
252 void SetMockSpeechRecognitionError(const std::string& error, 273 void SetMockSpeechRecognitionError(const std::string& error,
253 const std::string& message); 274 const std::string& message);
254 bool WasMockSpeechRecognitionAborted(); 275 bool WasMockSpeechRecognitionAborted();
255 void AddWebPageOverlay(); 276 void AddWebPageOverlay();
256 void RemoveWebPageOverlay(); 277 void RemoveWebPageOverlay();
257 void DisplayAsync(); 278 void DisplayAsync();
258 void DisplayAsyncThen(v8::Handle<v8::Function> callback); 279 void DisplayAsyncThen(v8::Handle<v8::Function> callback);
280 void CapturePixelsAsyncThen(v8::Handle<v8::Function> callback);
259 void SetCustomTextOutput(std::string output); 281 void SetCustomTextOutput(std::string output);
260 void SetViewSourceForFrame(const std::string& name, bool enabled); 282 void SetViewSourceForFrame(const std::string& name, bool enabled);
261 void setMockPushClientSuccess(const std::string& end_point, 283 void setMockPushClientSuccess(const std::string& end_point,
262 const std::string& registration_id); 284 const std::string& registration_id);
263 void setMockPushClientError(const std::string& message); 285 void setMockPushClientError(const std::string& message);
264 286
265 bool GlobalFlag(); 287 bool GlobalFlag();
266 void SetGlobalFlag(bool value); 288 void SetGlobalFlag(bool value);
267 std::string PlatformName(); 289 std::string PlatformName();
268 std::string TooltipText(); 290 std::string TooltipText();
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 &TestRunnerBindings::AddMockSpeechRecognitionResult) 515 &TestRunnerBindings::AddMockSpeechRecognitionResult)
494 .SetMethod("setMockSpeechRecognitionError", 516 .SetMethod("setMockSpeechRecognitionError",
495 &TestRunnerBindings::SetMockSpeechRecognitionError) 517 &TestRunnerBindings::SetMockSpeechRecognitionError)
496 .SetMethod("wasMockSpeechRecognitionAborted", 518 .SetMethod("wasMockSpeechRecognitionAborted",
497 &TestRunnerBindings::WasMockSpeechRecognitionAborted) 519 &TestRunnerBindings::WasMockSpeechRecognitionAborted)
498 .SetMethod("addWebPageOverlay", &TestRunnerBindings::AddWebPageOverlay) 520 .SetMethod("addWebPageOverlay", &TestRunnerBindings::AddWebPageOverlay)
499 .SetMethod("removeWebPageOverlay", 521 .SetMethod("removeWebPageOverlay",
500 &TestRunnerBindings::RemoveWebPageOverlay) 522 &TestRunnerBindings::RemoveWebPageOverlay)
501 .SetMethod("displayAsync", &TestRunnerBindings::DisplayAsync) 523 .SetMethod("displayAsync", &TestRunnerBindings::DisplayAsync)
502 .SetMethod("displayAsyncThen", &TestRunnerBindings::DisplayAsyncThen) 524 .SetMethod("displayAsyncThen", &TestRunnerBindings::DisplayAsyncThen)
525 .SetMethod("capturePixelsAsyncThen", &TestRunnerBindings::CapturePixelsAsy ncThen)
503 .SetMethod("setCustomTextOutput", 526 .SetMethod("setCustomTextOutput",
504 &TestRunnerBindings::SetCustomTextOutput) 527 &TestRunnerBindings::SetCustomTextOutput)
505 .SetMethod("setViewSourceForFrame", 528 .SetMethod("setViewSourceForFrame",
506 &TestRunnerBindings::SetViewSourceForFrame) 529 &TestRunnerBindings::SetViewSourceForFrame)
507 .SetMethod("setMockPushClientSuccess", 530 .SetMethod("setMockPushClientSuccess",
508 &TestRunnerBindings::setMockPushClientSuccess) 531 &TestRunnerBindings::setMockPushClientSuccess)
509 .SetMethod("setMockPushClientError", 532 .SetMethod("setMockPushClientError",
510 &TestRunnerBindings::setMockPushClientError) 533 &TestRunnerBindings::setMockPushClientError)
511 534
512 // Properties. 535 // Properties.
(...skipping 780 matching lines...) Expand 10 before | Expand all | Expand 10 after
1293 void TestRunnerBindings::DisplayAsync() { 1316 void TestRunnerBindings::DisplayAsync() {
1294 if (runner_) 1317 if (runner_)
1295 runner_->DisplayAsync(); 1318 runner_->DisplayAsync();
1296 } 1319 }
1297 1320
1298 void TestRunnerBindings::DisplayAsyncThen(v8::Handle<v8::Function> callback) { 1321 void TestRunnerBindings::DisplayAsyncThen(v8::Handle<v8::Function> callback) {
1299 if (runner_) 1322 if (runner_)
1300 runner_->DisplayAsyncThen(callback); 1323 runner_->DisplayAsyncThen(callback);
1301 } 1324 }
1302 1325
1326 void TestRunnerBindings::CapturePixelsAsyncThen(
1327 v8::Handle<v8::Function> callback) {
1328 if (runner_)
1329 runner_->CapturePixelsAsyncThen(callback);
1330 }
1331
1303 void TestRunnerBindings::SetCustomTextOutput(std::string output) { 1332 void TestRunnerBindings::SetCustomTextOutput(std::string output) {
1304 runner_->setCustomTextOutput(output); 1333 runner_->setCustomTextOutput(output);
1305 } 1334 }
1306 1335
1307 void TestRunnerBindings::SetViewSourceForFrame(const std::string& name, 1336 void TestRunnerBindings::SetViewSourceForFrame(const std::string& name,
1308 bool enabled) { 1337 bool enabled) {
1309 if (runner_ && runner_->web_view_) { 1338 if (runner_ && runner_->web_view_) {
1310 WebFrame* target_frame = 1339 WebFrame* target_frame =
1311 runner_->web_view_->findFrameByName(WebString::fromUTF8(name)); 1340 runner_->web_view_->findFrameByName(WebString::fromUTF8(name));
1312 if (target_frame) 1341 if (target_frame)
(...skipping 1397 matching lines...) Expand 10 before | Expand all | Expand 10 after
2710 } 2739 }
2711 2740
2712 void TestRunner::DisplayAsyncThen(v8::Handle<v8::Function> callback) { 2741 void TestRunner::DisplayAsyncThen(v8::Handle<v8::Function> callback) {
2713 scoped_ptr<InvokeCallbackTask> task( 2742 scoped_ptr<InvokeCallbackTask> task(
2714 new InvokeCallbackTask(this, callback)); 2743 new InvokeCallbackTask(this, callback));
2715 proxy_->DisplayAsyncThen(base::Bind(&TestRunner::InvokeCallback, 2744 proxy_->DisplayAsyncThen(base::Bind(&TestRunner::InvokeCallback,
2716 base::Unretained(this), 2745 base::Unretained(this),
2717 base::Passed(&task))); 2746 base::Passed(&task)));
2718 } 2747 }
2719 2748
2749 void TestRunner::CapturePixelsAsyncThen(v8::Handle<v8::Function> callback) {
2750 scoped_ptr<InvokeCallbackTask> task(
2751 new InvokeCallbackTask(this, callback));
2752 proxy_->CapturePixelsAsync(base::Bind(&TestRunner::CapturePixelsCallback,
2753 base::Unretained(this),
2754 base::Passed(&task)));
2755 }
2756
2757 void TestRunner::CapturePixelsCallback(scoped_ptr<InvokeCallbackTask> task,
2758 const SkBitmap& snapshot) {
2759 v8::Isolate* isolate = blink::mainThreadIsolate();
2760 v8::HandleScope handle_scope(isolate);
2761
2762 v8::Handle<v8::Context> context =
2763 web_view_->mainFrame()->mainWorldScriptContext();
2764 if (context.IsEmpty())
2765 return;
2766
2767 v8::Context::Scope context_scope(context);
2768 v8::Handle<v8::Value> argv[3];
2769 SkAutoLockPixels snapshot_lock(snapshot);
2770
2771 int width = snapshot.info().fWidth;
2772 DCHECK_NE(0, width);
2773 argv[0] = v8::Number::New(isolate, width);
2774
2775 int height = snapshot.info().fHeight;
2776 DCHECK_NE(0, height);
2777 argv[1] = v8::Number::New(isolate, height);
2778
2779 blink::WebArrayBuffer buffer =
2780 blink::WebArrayBuffer::create(snapshot.getSize(), 1);
2781 memcpy(buffer.data(), snapshot.getPixels(), buffer.byteLength());
2782 argv[2] = blink::WebArrayBufferConverter::toV8Value(
2783 &buffer, context->Global(), isolate);
2784
2785 task->SetArguments(3, argv);
2786 InvokeCallback(task.Pass());
2787 }
2788
2720 void TestRunner::SetMockPushClientSuccess( 2789 void TestRunner::SetMockPushClientSuccess(
2721 const std::string& end_point, const std::string& registration_id) { 2790 const std::string& end_point, const std::string& registration_id) {
2722 proxy_->GetPushClientMock()->SetMockSuccessValues(end_point, registration_id); 2791 proxy_->GetPushClientMock()->SetMockSuccessValues(end_point, registration_id);
2723 } 2792 }
2724 2793
2725 void TestRunner::SetMockPushClientError(const std::string& message) { 2794 void TestRunner::SetMockPushClientError(const std::string& message) {
2726 proxy_->GetPushClientMock()->SetMockErrorValues(message); 2795 proxy_->GetPushClientMock()->SetMockErrorValues(message);
2727 } 2796 }
2728 2797
2729 void TestRunner::LocationChangeDone() { 2798 void TestRunner::LocationChangeDone() {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
2773 } 2842 }
2774 2843
2775 void TestRunner::DidLosePointerLockInternal() { 2844 void TestRunner::DidLosePointerLockInternal() {
2776 bool was_locked = pointer_locked_; 2845 bool was_locked = pointer_locked_;
2777 pointer_locked_ = false; 2846 pointer_locked_ = false;
2778 if (was_locked) 2847 if (was_locked)
2779 web_view_->didLosePointerLock(); 2848 web_view_->didLosePointerLock();
2780 } 2849 }
2781 2850
2782 } // namespace content 2851 } // namespace content
OLDNEW
« no previous file with comments | « content/shell/renderer/test_runner/test_runner.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698