cirandas.net

ref: master

plugins/html5_video/test/unit/video_presenter_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
require 'test_helper'
require_relative '../download_fixture'

class VideoPresenterTest < ActiveSupport::TestCase

  #include Html5VideoPlugin::Ffmpeg

  def create_video(file, mime)
    file = UploadedFile.create!(
      :uploaded_data => fixture_file_upload('/videos/'+file, mime),
      :profile => fast_create(Person))
  end

  def process_video(file)
    process_delayed_job_queue
    file.reload
    FilePresenter::Video.new file
  end

  def create_and_proc_video(file, mime)
    process_video(create_video(file, mime))
  end

  def setup
    Environment.default.enable_plugin Html5VideoPlugin
  end

  should 'accept to encapsulate a video file' do
    file = create_video 'old-movie.mpg', 'video/mpeg'
    assert_equal 10, FilePresenter::Video.accepts?(file)
  end

  should 'retrieve meta-data' do
    video = create_and_proc_video('old-movie.mpg', 'video/mpeg')
    assert_equal 'Video (MPEG)', video.short_description, 'describe the file type'
    assert_equivalent video.meta_data.settings.keys,
      [:image_previews, :original_video, :web_versions]
    assert_equivalent video.original_video.keys,
      [:metadata, :type, :streams, :global_bitrate, :error, :duration, :parameters]
    assert_equal h2s([:OGV, :WEBM]), h2s(video.web_versions.keys)
  end

  should 'retrieve all web versions' do
    video1 = create_and_proc_video('old-movie.mpg', 'video/mpeg')
    video2 = create_and_proc_video('atropelamento.ogv', 'video/ogg')
    # make video2 as fake valid web video:
    audio = video2.original_video[:streams].find{|s| s[:type] == 'audio' }
    audio = audio[:codec] = 'vorbis'
    assert_equal video1.web_versions, video1.web_versions!, 'all web versions (1)'
    assert_equal h2s(video2.web_versions[:OGV].merge(:orig => {
      :type => :OGV,
      :status => "done",
      :vbrate => 334,
      :size_name => "orig",
      :file_name => "atropelamento.ogv",
      :size => {:h=>130, :w=>208},
      :original => true,
      :path => video2.public_filename,
      :abrate => 0 })),
      h2s(video2.web_versions![:OGV]), 'all web versions (2)'
    # test get the tiniest web version:
    data = video1.tiniest_web_version(:OGV)
    assert_equal h2s(data), h2s(
      :type=>:OGV, :size_name=>"tiny", :status=>"done",
      :fps=>12, :abrate=>64, :vbrate=>250, :size=>{:h=>212, :w=>320},
      :path=>File.join(Rails.root,"/test/tmp/0000/#{'%04d'%video1.id}/web/tiny.ogv"),
      :file_name=>"tiny.ogv" )
  end

  should 'know if it has ready_web_versions' do
    file = create_video 'old-movie.mpg', 'video/mpeg'
    video = FilePresenter::Video.new file
    assert_equal h2s(video.ready_web_versions), h2s({})
    video = process_video file
    web_versions = video.ready_web_versions
    assert_equal 'nice.ogv',  web_versions[:OGV][:nice][:file_name]
    assert_equal 'tiny.ogv',  web_versions[:OGV][:tiny][:file_name]
    assert_equal 'nice.webm', web_versions[:WEBM][:nice][:file_name]
    assert_equal 'tiny.webm', web_versions[:WEBM][:tiny][:file_name]
  end

  should 'know its tiniest_web_version' do
    video = create_and_proc_video 'atropelamento.ogv', 'video/ogg'
    tiniestOGV = video.tiniest_web_version :OGV
    tiniestWEBM = video.tiniest_web_version :WEBM
    assert_equal h2s({w:208,h:130}), h2s(tiniestOGV[:size])
    assert_equal :OGV, tiniestOGV[:type]
    assert_equal h2s({w:208,h:130}), h2s(tiniestWEBM[:size])
    assert_equal :WEBM, tiniestWEBM[:type]
    assert_equal nil, video.tiniest_web_version(:MP4)
  end

  should 'know if it has_ogv_version' do
    video = create_and_proc_video 'old-movie.mpg', 'video/mpeg'
    assert video.has_ogv_version
  end

  should 'know if it has_mp4_version' do
    video = create_and_proc_video 'old-movie.mpg', 'video/mpeg'
    assert not(video.has_mp4_version), 'must NOT to list MP4'
    video.web_versions[:MP4] = {SIZE: {file_name:'sized.mp4', status:'done'} }
    assert video.has_mp4_version, 'must to list MP4'
  end

  should 'know if it has_webm_version' do
    video = create_and_proc_video 'old-movie.mpg', 'video/mpeg'
    assert video.has_webm_version
  end

  should 'list its web_version_jobs' do
    videoA = FilePresenter::Video.new create_video 'old-movie.mpg', 'video/mpeg'
    videoB = FilePresenter::Video.new create_video 'atropelamento.ogv', 'video/ogg'
    jobA = videoA.web_version_jobs.map &:payload_object
    jobB = videoB.web_version_jobs.map &:payload_object
    # TODO: jobA.length must be 4.
    # `Html5VideoPlugin::uploaded_file_after_create_callback` is been called two times
    #assert_equal 4, jobA.length
    assert_equal Html5VideoPlugin::CreateVideoForWebJob, jobA[0].class
    assert_equal :OGV, jobA[0].format
    assert_equal :tiny, jobA[0].size
    assert_match /.*\/old-movie.mpg$/, jobA[0].full_filename
    assert_equal Html5VideoPlugin::CreateVideoForWebJob, jobA[1].class
    assert_equal :WEBM, jobA[1].format
    assert_equal :tiny, jobA[1].size
    assert_match /.*\/old-movie.mpg$/, jobA[1].full_filename
    assert_equal Html5VideoPlugin::CreateVideoForWebJob, jobA[2].class
    assert_equal :OGV, jobA[2].format
    assert_equal :nice, jobA[2].size
    assert_match /.*\/old-movie.mpg$/, jobA[1].full_filename
    assert_equal Html5VideoPlugin::CreateVideoForWebJob, jobA[3].class
    assert_equal :WEBM, jobA[3].format
    assert_equal :nice, jobA[3].size
    assert_match /.*\/old-movie.mpg$/, jobA[1].full_filename
    assert_equal Html5VideoPlugin::CreateVideoForWebJob, jobB[0].class
    assert_equal :OGV, jobB[0].format
    assert_equal :tiny, jobB[0].size
    assert_match /.*\/atropelamento.ogv$/, jobB[0].full_filename
    assert_equal Html5VideoPlugin::CreateVideoForWebJob, jobB[1].class
    assert_equal :WEBM, jobB[1].format
    assert_equal :tiny, jobB[1].size
    assert_match /.*\/atropelamento.ogv$/, jobB[1].full_filename
  end

  should 'list its web_preview_jobs' do
    videoA = FilePresenter::Video.new create_video 'old-movie.mpg', 'video/mpeg'
    videoB = FilePresenter::Video.new create_video 'atropelamento.ogv', 'video/ogg'
    jobA = videoA.web_preview_jobs.map &:payload_object
    jobB = videoB.web_preview_jobs.map &:payload_object
    # TODO: jobA.length must be 1.
    # `Html5VideoPlugin::uploaded_file_after_create_callback` is been called two times
    #assert_equal 1, jobA.length
    assert_equal Html5VideoPlugin::CreateVideoPreviewJob, jobA[0].class
    assert_match /.*\/old-movie.mpg$/, jobA[0].full_filename
    assert_equal Html5VideoPlugin::CreateVideoPreviewJob, jobB[0].class
    assert_match /.*\/atropelamento.ogv$/, jobB[0].full_filename
  end

  should 'know if it has_previews' do
    video = create_and_proc_video 'old-movie.mpg', 'video/mpeg'
    assert video.has_previews?
  end

  should 'list its image previews' do
    video = create_and_proc_video 'old-movie.mpg', 'video/mpeg'
    assert_equal h2s(big:'/web/preview_160x120.jpg', thumb:'/web/preview_107x80.jpg'), h2s(video.previews)
  end

  should 'set its image previews' do
    video = FilePresenter::Video.new create_video 'old-movie.mpg', 'video/mpeg'
    assert_equal nil, video.previews
    video.previews = {big:'big.jpg', thumb:'thumb.jpg'}
    assert_equal h2s(big:'big.jpg', thumb:'thumb.jpg'), h2s(video.previews)
  end

  should 'get image_preview for a processed video' do
    video = create_and_proc_video 'old-movie.mpg', 'video/mpeg'
    assert_match /\/[0-9]+\/web\/preview_160x120\.jpg/, video.image_preview(:big)
  end

  should 'get default image_preview for non processed video' do
    video = FilePresenter::Video.new create_video 'old-movie.mpg', 'video/mpeg'
    assert_match /\/html5_video\/images\/video-preview-big\.png/, video.image_preview(:big)
  end

  should 'list its conversion_errors' do
    video = FilePresenter::Video.new create_video 'old-movie.mpg', 'video/mpeg'
    video.web_versions[:MP4] = {nice: {status:'error converting', error:{code:-99,message:'some error',output:'abcde'}} }
    assert_equal h2s(MP4:{nice:{code:-99,message:'some error',output:'abcde'}}), h2s(video.conversion_errors)
  end

end