1 // Copyright (c) 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.
5 // Tests PPB_VideoSource_Private interface.
7 #include "ppapi/tests/test_video_source.h"
13 #include "ppapi/c/dev/ppb_testing_dev.h"
14 #include "ppapi/cpp/completion_callback.h"
15 #include "ppapi/cpp/instance.h"
16 #include "ppapi/cpp/private/video_frame_private.h"
17 #include "ppapi/cpp/private/video_source_private.h"
18 #include "ppapi/cpp/var.h"
19 #include "ppapi/tests/test_utils.h"
20 #include "ppapi/tests/testing_instance.h"
22 REGISTER_TEST_CASE(VideoSource
);
26 const PP_Resource kInvalidResource
= 0;
27 const PP_Instance kInvalidInstance
= 0;
31 TestVideoSource::TestVideoSource(TestingInstance
* instance
)
33 ppb_video_source_private_interface_(NULL
),
34 ppb_core_interface_(NULL
),
35 event_(instance_
->pp_instance()) {
38 bool TestVideoSource::Init() {
39 ppb_video_source_private_interface_
=
40 static_cast<const PPB_VideoSource_Private
*>(
41 pp::Module::Get()->GetBrowserInterface(
42 PPB_VIDEOSOURCE_PRIVATE_INTERFACE
));
43 if (!ppb_video_source_private_interface_
)
44 instance_
->AppendError("PPB_VideoSource_Private interface not available");
46 ppb_core_interface_
= static_cast<const PPB_Core
*>(
47 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE
));
48 if (!ppb_core_interface_
)
49 instance_
->AppendError("PPB_Core interface not available");
51 return ppb_video_source_private_interface_
&& ppb_core_interface_
;
54 TestVideoSource::~TestVideoSource() {
57 void TestVideoSource::RunTests(const std::string
& filter
) {
58 RUN_TEST(Create
, filter
);
59 RUN_TEST(GetFrame
, filter
);
62 void TestVideoSource::HandleMessage(const pp::Var
& message_data
) {
63 if (message_data
.AsString().find("blob:") == 0) {
64 stream_url_
= message_data
.AsString();
69 std::string
TestVideoSource::TestCreate() {
70 PP_Resource video_source
;
71 // Creating a source from an invalid instance returns an invalid resource.
72 video_source
= ppb_video_source_private_interface_
->Create(kInvalidInstance
);
73 ASSERT_EQ(kInvalidResource
, video_source
);
75 ppb_video_source_private_interface_
->IsVideoSource(video_source
));
77 // Creating a source from a valid instance returns a valid resource.
79 ppb_video_source_private_interface_
->Create(instance_
->pp_instance());
80 ASSERT_NE(kInvalidResource
, video_source
);
82 ppb_video_source_private_interface_
->IsVideoSource(video_source
));
84 ppb_core_interface_
->ReleaseResource(video_source
);
85 // Once released, the resource shouldn't be a video source.
87 ppb_video_source_private_interface_
->IsVideoSource(video_source
));
92 std::string
TestVideoSource::TestGetFrame() {
94 js_code
+= "var test_stream;"
95 "function gotStream(stream){"
96 " test_stream = stream;"
97 " var url = webkitURL.createObjectURL(test_stream);"
98 " var plugin = document.getElementById('plugin');"
99 " plugin.postMessage(url);"
101 "navigator.webkitGetUserMedia("
102 "{audio:false, video:true}, gotStream, function() {});";
103 instance_
->EvalScript(js_code
);
106 pp::VideoSource_Private
video_source(instance_
);
107 TestCompletionCallback
cc1(instance_
->pp_instance(), false);
108 cc1
.WaitForResult(video_source
.Open(stream_url_
, cc1
.GetCallback()));
109 ASSERT_EQ(PP_OK
, cc1
.result());
110 TestCompletionCallbackWithOutput
<pp::VideoFrame_Private
> cc2(
111 instance_
->pp_instance(), false);
112 cc2
.WaitForResult(video_source
.GetFrame(cc2
.GetCallback()));
113 ASSERT_EQ(PP_OK
, cc2
.result());
114 const pp::VideoFrame_Private video_frame
= cc2
.output();
115 ASSERT_FALSE(video_frame
.image_data().is_null());
117 video_source
.Close();