helper.coffee 2.39 KB
Newer Older
1
jasmine.stubbedMetadata =
2 3 4 5 6
  slowerSpeedYoutubeId:
    id: 'slowerSpeedYoutubeId'
    duration: 300
  normalSpeedYoutubeId:
    id: 'normalSpeedYoutubeId'
7 8
    duration: 200
  bogus:
9
    duration: 100
10 11

jasmine.stubbedCaption =
12 13
  start: [0, 10000, 20000, 30000]
  text: ['Caption at 0', 'Caption at 10000', 'Caption at 20000', 'Caption at 30000']
14 15 16 17 18 19 20

jasmine.stubRequests = ->
  spyOn($, 'ajax').andCallFake (settings) ->
    if match = settings.url.match /youtube\.com\/.+\/videos\/(.+)\?v=2&alt=jsonc/
      settings.success data: jasmine.stubbedMetadata[match[1]]
    else if match = settings.url.match /static\/subs\/(.+)\.srt\.sjson/
      settings.success jasmine.stubbedCaption
21
    else if settings.url.match /modx\/.+\/problem_get$/
22
      settings.success html: readFixtures('problem_content.html')
23
    else if settings.url == '/calculate' ||
24
      settings.url.match(/modx\/.+\/goto_position$/) ||
25
      settings.url.match(/event$/) ||
26
      settings.url.match(/modx\/.+\/problem_(check|reset|show|save)$/)
27 28 29 30 31 32
      # do nothing
    else
      throw "External request attempted for #{settings.url}, which is not defined."

jasmine.stubYoutubePlayer = ->
  YT.Player = -> jasmine.createSpyObj 'YT.Player', ['cueVideoById', 'getVideoEmbedCode',
33 34
    'getCurrentTime', 'getPlayerState', 'getVolume', 'setVolume', 'loadVideoById',
    'playVideo', 'pauseVideo', 'seekTo']
35

36
jasmine.stubVideoPlayer = (context, enableParts, createPlayer=true) ->
37 38 39 40 41 42
  enableParts = [enableParts] unless $.isArray(enableParts)

  suite = context.suite
  currentPartName = suite.description while suite = suite.parentSuite
  enableParts.push currentPartName

43
  for part in ['VideoCaption', 'VideoSpeedControl', 'VideoVolumeControl', 'VideoProgressSlider']
44 45 46 47 48 49
    unless $.inArray(part, enableParts) >= 0
      spyOn window, part

  loadFixtures 'video.html'
  jasmine.stubRequests()
  YT.Player = undefined
50
  context.video = new Video 'example', '.75:slowerSpeedYoutubeId,1.0:normalSpeedYoutubeId'
51
  jasmine.stubYoutubePlayer()
52
  if createPlayer
53
    return new VideoPlayer(video: context.video)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

spyOn(window, 'onunload')

# Stub Youtube API
window.YT =
  PlayerState:
    UNSTARTED: -1
    ENDED: 0
    PLAYING: 1
    PAUSED: 2
    BUFFERING: 3
    CUED: 5

# Stub jQuery.cookie
$.cookie = jasmine.createSpy('jQuery.cookie').andReturn '1.0'

# Stub jQuery.qtip
$.fn.qtip = jasmine.createSpy 'jQuery.qtip'

# Stub jQuery.scrollTo
$.fn.scrollTo = jasmine.createSpy 'jQuery.scrollTo'