| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 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 /* |
| 6 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 7 * |
| 8 * Redistribution and use in source and binary forms, with or without |
| 9 * modification, are permitted provided that the following conditions are |
| 10 * met: |
| 11 * |
| 12 * * Redistributions of source code must retain the above copyright |
| 13 * notice, this list of conditions and the following disclaimer. |
| 14 * * Redistributions in binary form must reproduce the above |
| 15 * copyright notice, this list of conditions and the following disclaimer |
| 16 * in the documentation and/or other materials provided with the |
| 17 * distribution. |
| 18 * * Neither the name of Google Inc. nor the names of its |
| 19 * contributors may be used to endorse or promote products derived from |
| 20 * this software without specific prior written permission. |
| 21 * |
| 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 */ |
| 34 |
| 35 #include "content/test/layout_tests/runner/WebUserMediaClientMock.h" |
| 36 |
| 37 #include "content/public/test/layout_tests/WebTestDelegate.h" |
| 38 #include "content/test/layout_tests/runner/MockConstraints.h" |
| 39 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" |
| 40 #include "third_party/WebKit/public/platform/WebMediaStream.h" |
| 41 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
| 42 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
| 43 #include "third_party/WebKit/public/platform/WebVector.h" |
| 44 #include "third_party/WebKit/public/web/WebDocument.h" |
| 45 #include "third_party/WebKit/public/web/WebMediaStreamRegistry.h" |
| 46 #include "third_party/WebKit/public/web/WebUserMediaRequest.h" |
| 47 |
| 48 using namespace blink; |
| 49 |
| 50 namespace WebTestRunner { |
| 51 |
| 52 class UserMediaRequestTask : public WebMethodTask<WebUserMediaClientMock> { |
| 53 public: |
| 54 UserMediaRequestTask(WebUserMediaClientMock* object, const WebUserMediaReque
st& request, const WebMediaStream result) |
| 55 : WebMethodTask<WebUserMediaClientMock>(object) |
| 56 , m_request(request) |
| 57 , m_result(result) |
| 58 { |
| 59 BLINK_ASSERT(!m_result.isNull()); |
| 60 } |
| 61 |
| 62 virtual void runIfValid() OVERRIDE |
| 63 { |
| 64 m_request.requestSucceeded(m_result); |
| 65 } |
| 66 |
| 67 private: |
| 68 WebUserMediaRequest m_request; |
| 69 WebMediaStream m_result; |
| 70 }; |
| 71 |
| 72 class UserMediaRequestConstraintFailedTask : public WebMethodTask<WebUserMediaCl
ientMock> { |
| 73 public: |
| 74 UserMediaRequestConstraintFailedTask(WebUserMediaClientMock* object, const W
ebUserMediaRequest& request, const WebString& constraint) |
| 75 : WebMethodTask<WebUserMediaClientMock>(object) |
| 76 , m_request(request) |
| 77 , m_constraint(constraint) |
| 78 { |
| 79 } |
| 80 |
| 81 virtual void runIfValid() OVERRIDE |
| 82 { |
| 83 m_request.requestFailedConstraint(m_constraint); |
| 84 } |
| 85 |
| 86 private: |
| 87 WebUserMediaRequest m_request; |
| 88 WebString m_constraint; |
| 89 }; |
| 90 |
| 91 class UserMediaRequestPermissionDeniedTask : public WebMethodTask<WebUserMediaCl
ientMock> { |
| 92 public: |
| 93 UserMediaRequestPermissionDeniedTask(WebUserMediaClientMock* object, const W
ebUserMediaRequest& request) |
| 94 : WebMethodTask<WebUserMediaClientMock>(object) |
| 95 , m_request(request) |
| 96 { |
| 97 } |
| 98 |
| 99 virtual void runIfValid() OVERRIDE |
| 100 { |
| 101 m_request.requestFailed(); |
| 102 } |
| 103 |
| 104 private: |
| 105 WebUserMediaRequest m_request; |
| 106 }; |
| 107 |
| 108 //////////////////////////////// |
| 109 |
| 110 class MockExtraData : public WebMediaStream::ExtraData { |
| 111 public: |
| 112 int foo; |
| 113 }; |
| 114 |
| 115 WebUserMediaClientMock::WebUserMediaClientMock(WebTestDelegate* delegate) |
| 116 : m_delegate(delegate) |
| 117 { |
| 118 } |
| 119 |
| 120 void WebUserMediaClientMock::requestUserMedia(const WebUserMediaRequest& streamR
equest) |
| 121 { |
| 122 BLINK_ASSERT(!streamRequest.isNull()); |
| 123 WebUserMediaRequest request = streamRequest; |
| 124 |
| 125 if (request.ownerDocument().isNull() || !request.ownerDocument().frame()) { |
| 126 m_delegate->postTask(new UserMediaRequestPermissionDeniedTask(this, requ
est)); |
| 127 return; |
| 128 } |
| 129 |
| 130 WebMediaConstraints constraints = request.audioConstraints(); |
| 131 WebString failedConstraint; |
| 132 if (!constraints.isNull() && !MockConstraints::verifyConstraints(constraints
, &failedConstraint)) { |
| 133 m_delegate->postTask(new UserMediaRequestConstraintFailedTask(this, requ
est, failedConstraint)); |
| 134 return; |
| 135 } |
| 136 constraints = request.videoConstraints(); |
| 137 if (!constraints.isNull() && !MockConstraints::verifyConstraints(constraints
, &failedConstraint)) { |
| 138 m_delegate->postTask(new UserMediaRequestConstraintFailedTask(this, requ
est, failedConstraint)); |
| 139 return; |
| 140 } |
| 141 |
| 142 const size_t zero = 0; |
| 143 const size_t one = 1; |
| 144 WebVector<WebMediaStreamTrack> audioTracks(request.audio() ? one : zero); |
| 145 WebVector<WebMediaStreamTrack> videoTracks(request.video() ? one : zero); |
| 146 |
| 147 if (request.audio()) { |
| 148 WebMediaStreamSource source; |
| 149 source.initialize("MockAudioDevice#1", WebMediaStreamSource::TypeAudio,
"Mock audio device"); |
| 150 audioTracks[0].initialize(source); |
| 151 } |
| 152 |
| 153 if (request.video()) { |
| 154 WebMediaStreamSource source; |
| 155 source.initialize("MockVideoDevice#1", WebMediaStreamSource::TypeVideo,
"Mock video device"); |
| 156 videoTracks[0].initialize(source); |
| 157 } |
| 158 |
| 159 WebMediaStream stream; |
| 160 stream.initialize(audioTracks, videoTracks); |
| 161 |
| 162 stream.setExtraData(new MockExtraData()); |
| 163 |
| 164 m_delegate->postTask(new UserMediaRequestTask(this, request, stream)); |
| 165 } |
| 166 |
| 167 void WebUserMediaClientMock::cancelUserMediaRequest(const WebUserMediaRequest&) |
| 168 { |
| 169 } |
| 170 |
| 171 } |
| OLD | NEW |