Bug 893309 - Implement TrackEvent. r=cpearce

This commit is contained in:
Andrew Quartey 2013-10-04 14:28:25 -04:00
parent dca1c06e52
commit 3a03fec19c
6 changed files with 140 additions and 5 deletions

View File

@ -5,6 +5,8 @@
#include "mozilla/dom/TextTrackList.h"
#include "mozilla/dom/TextTrackListBinding.h"
#include "mozilla/dom/TrackEvent.h"
#include "nsThreadUtils.h"
namespace mozilla {
namespace dom {
@ -54,8 +56,10 @@ TextTrackList::AddTextTrack(HTMLMediaElement* aMediaElement,
{
nsRefPtr<TextTrack> track = new TextTrack(mGlobal, aMediaElement, aKind,
aLabel, aLanguage);
mTextTracks.AppendElement(track);
// TODO: dispatch addtrack event
if (mTextTracks.AppendElement(track)) {
CreateAndDispatchTrackEventRunner(track, NS_LITERAL_STRING("addtrack"));
}
return track.forget();
}
@ -73,9 +77,11 @@ TextTrackList::GetTrackById(const nsAString& aId)
}
void
TextTrackList::RemoveTextTrack(const TextTrack& aTrack)
TextTrackList::RemoveTextTrack(TextTrack& aTrack)
{
mTextTracks.RemoveElement(&aTrack);
if (mTextTracks.RemoveElement(&aTrack)) {
CreateAndDispatchTrackEventRunner(&aTrack, NS_LITERAL_STRING("removetrack"));
}
}
void
@ -86,5 +92,45 @@ TextTrackList::DidSeek()
}
}
class TrackEventRunner MOZ_FINAL: public nsRunnable
{
public:
TrackEventRunner(TextTrackList* aList, TrackEvent* aEvent)
: mList(aList)
, mEvent(aEvent)
{}
NS_IMETHOD Run() MOZ_OVERRIDE
{
return mList->DispatchTrackEvent(mEvent);
}
private:
nsRefPtr<TextTrackList> mList;
nsRefPtr<TrackEvent> mEvent;
};
nsresult
TextTrackList::DispatchTrackEvent(TrackEvent* aEvent)
{
return DispatchTrustedEvent(aEvent);
}
void
TextTrackList::CreateAndDispatchTrackEventRunner(TextTrack* aTrack,
const nsAString& aEventName)
{
TrackEventInitInitializer eventInit;
eventInit.mBubbles = false;
eventInit.mCancelable = false;
eventInit.mTrack = aTrack;
nsRefPtr<TrackEvent> trackEvent =
TrackEvent::Constructor(this, aEventName, eventInit);
// Dispatch the TrackEvent asynchronously.
nsCOMPtr<nsIRunnable> event = new TrackEventRunner(this, trackEvent);
NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
}
} // namespace dom
} // namespace mozilla

View File

@ -14,6 +14,9 @@
namespace mozilla {
namespace dom {
class TrackEvent;
class TrackEventRunner;
class TextTrackList MOZ_FINAL : public nsDOMEventTargetHelper
{
public:
@ -50,15 +53,20 @@ public:
mTextTracks.AppendElement(aTextTrack);
}
void RemoveTextTrack(const TextTrack& aTrack);
void RemoveTextTrack(TextTrack& aTrack);
void DidSeek();
nsresult DispatchTrackEvent(TrackEvent* aEvent);
IMPL_EVENT_HANDLER(addtrack)
IMPL_EVENT_HANDLER(removetrack)
private:
nsCOMPtr<nsISupports> mGlobal;
nsTArray< nsRefPtr<TextTrack> > mTextTracks;
void CreateAndDispatchTrackEventRunner(TextTrack* aTrack,
const nsAString& aEventName);
};
} // namespace dom

View File

@ -145,6 +145,7 @@ MOCHITEST_FILES = \
$(filter disabled-for-intermittent-failures--bug-608634, test_error_in_video_document.html) \
test_texttrack.html \
test_texttrackcue.html \
test_trackevent.html \
test_texttrackregion.html \
test_timeupdate_small_files.html \
test_unseekable.html \

View File

@ -0,0 +1,62 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for Bug 893309 - Implement TrackEvent</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [["media.webvtt.enabled", true]]},
function() {
var video = document.createElement("video");
isnot(video.textTracks, undefined, "HTMLMediaElement::TextTrack() property should be available.")
ok(typeof video.addTextTrack == "function", "HTMLMediaElement::AddTextTrack() function should be available.")
var trackList = video.textTracks;
is(trackList.length, 0, "Length should be 0.");
var evtTextTrack, numOfCalls = 0;
trackList.onaddtrack = function(event) {
ok(event instanceof TrackEvent, "Fired event from onaddtrack should be a TrackEvent");
is(event.type, "addtrack", "Event type should be addtrack");
ok(event.isTrusted, "Event should be trusted!");
ok(!event.bubbles, "Event shouldn't bubble!");
ok(!event.cancelable, "Event shouldn't be cancelable!");
evtTextTrack = event.track;
ok(textTrack[numOfCalls] === evtTextTrack, "Text tracks should be the same");
is(evtTextTrack.label, label[numOfCalls], "Label should be set to "+ label[numOfCalls]);
is(evtTextTrack.language, language[numOfCalls], "Language should be " + language[numOfCalls]);
is(evtTextTrack.kind, kind[numOfCalls], "Kind should be " + kind[numOfCalls]);
if (++numOfCalls == 3) {
SimpleTest.finish();
}
};
var label = ["Oasis", "Coldplay", "t.A.T.u"];
language = ["en-CA", "en-GB", "ru" ];
kind = ["subtitles", "captions", "chapters"];
var textTrack = new Array(3);
for (var i = 0; i < 3; ++i) {
textTrack[i] = video.addTextTrack(kind[i], label[i], language[i]);
is(trackList.length, i + 1, "Length should be " + (i+1));
}
//TODO: Tests for removetrack event to be added along with bug 882677
}
);
</script>
</pre>
</body>
</html>

View File

@ -0,0 +1,17 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
[Constructor(DOMString type, optional TrackEventInit eventInitDict)]
interface TrackEvent : Event
{
readonly attribute TextTrack? track;
};
dictionary TrackEventInit : EventInit
{
TextTrack? track = null;
};

View File

@ -548,6 +548,7 @@ GENERATED_EVENTS_WEBIDL_FILES = [
'MozInterAppMessageEvent.webidl',
'RTCDataChannelEvent.webidl',
'RTCPeerConnectionIceEvent.webidl',
'TrackEvent.webidl',
'UserProximityEvent.webidl',
]