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