upload_progress_helper_testx.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:6k
源码类别:

Ajax

开发平台:

Others

  1. require File.dirname(__FILE__) + 'action_view/helpers/date_helper'
  2. require File.dirname(__FILE__) + 'action_view/helpers/number_helper'
  3. require File.dirname(__FILE__) + 'action_view/helpers/asset_tag_helper'
  4. require File.dirname(__FILE__) + 'action_view/helpers/form_tag_helper'
  5. require File.dirname(__FILE__) + 'action_view/helpers/tag_helper'
  6. require File.dirname(__FILE__) + 'action_view/helpers/javascript_helper'
  7. require File.dirname(__FILE__) + 'action_view/helpers/upload_progress_helper'
  8. require File.dirname(__FILE__) + 'activesupport/lib/active_support/core_ext/hash' #for stringify keys
  9. class MockProgress
  10.   def initialize(started, finished)
  11.     @started, @finished = [started, finished]
  12.   end
  13.   def started?
  14.     @started
  15.   end
  16.   def finished?
  17.     @finished
  18.   end
  19.   def message
  20.     "A message"
  21.   end
  22.   def method_missing(meth, *args)
  23.     # Just return some consitant number
  24.     meth.to_s.hash.to_i.abs + args.hash.to_i.abs
  25.   end
  26. end
  27. class UploadProgressHelperTest < Test::Unit::TestCase
  28.   include ActionView::Helpers::DateHelper
  29.   include ActionView::Helpers::NumberHelper
  30.   include ActionView::Helpers::AssetTagHelper
  31.   include ActionView::Helpers::FormTagHelper
  32.   include ActionView::Helpers::TagHelper
  33.   include ActionView::Helpers::UrlHelper
  34.   include ActionView::Helpers::JavaScriptHelper
  35.   include ActionView::Helpers::UploadProgressHelper
  36.   def next_upload_id; @upload_id = last_upload_id.succ; end
  37.   def last_upload_id; @upload_id ||= 0; end
  38.   def current_upload_id; last_upload_id; end
  39.   def upload_progress(upload_id = nil); @upload_progress ||= MockProgress.new(false, true); end
  40.   def setup
  41.     @controller = Class.new do
  42.       def url_for(options, *parameters_for_method_reference)
  43.         "http://www.example.com"
  44.       end
  45.     end
  46.     @controller = @controller.new
  47.   end
  48.   def test_upload_status_tag
  49.     assert_dom_equal(
  50.       '<div class="progressBar" id="UploadProgressBar0"><div class="border"><div class="background"><div class="foreground"></div></div></div></div><div class="uploadStatus" id="UploadStatus0"></div>',
  51.       upload_status_tag
  52.     )
  53.   end
  54.   def test_upload_status_text_tag
  55.     assert_dom_equal(
  56.       '<div class="my-upload" id="my-id">Starting</div>',
  57.       upload_status_text_tag('Starting', :class => 'my-upload', :id => 'my-id')
  58.     )
  59.   end
  60.   def test_upload_progress_text
  61.     @upload_progress = MockProgress.new(false, false)
  62.     assert_equal(
  63.       "Upload starting...",
  64.       upload_progress_text
  65.     )
  66.     @upload_progress = MockProgress.new(true, false)
  67.     assert_equal(
  68.       "828.7 MB of 456.2 MB at 990.1 MB/s; 10227 days remaining",
  69.       upload_progress_text
  70.     )
  71.     @upload_progress = MockProgress.new(true, true)
  72.     assert_equal(
  73.       "A message",
  74.       upload_progress_text
  75.     )
  76.   end
  77.   def test_upload_progress_update_bar_js
  78.     assert_equal(
  79.       "if($('UploadProgressBar0')){$('UploadProgressBar0').firstChild.firstChild.style.width='0%'}",
  80.       upload_progress_update_bar_js
  81.     )
  82.     assert_equal(
  83.       "if($('UploadProgressBar0')){$('UploadProgressBar0').firstChild.firstChild.style.width='50%'}",
  84.       upload_progress_update_bar_js(50)
  85.     )
  86.   end
  87.   def test_finish_upload_status
  88.     assert_dom_equal(
  89.       "<html><head><script language="javascript" type="text/javascript">function finish() { if (parent.document.uploadStatus0) { parent.document.uploadStatus0.stop();n }n }</script></head><body onload="finish()"></body></html>",
  90.       finish_upload_status
  91.     )
  92.     assert_dom_equal(
  93.       "<html><head><script language="javascript" type="text/javascript">function finish() { if (parent.document.uploadStatus0) { parent.document.uploadStatus0.stop(123);n }n }</script></head><body onload="finish()"></body></html>",
  94.       finish_upload_status(:client_js_argument => 123)
  95.     )
  96.     assert_dom_equal(
  97.       "<html><head><script language="javascript" type="text/javascript">function finish() { if (parent.document.uploadStatus0) { parent.document.uploadStatus0.stop();nparent.location.replace('/redirected/');n }n }</script></head><body onload="finish()"></body></html>",
  98.       finish_upload_status(:redirect_to => '/redirected/')
  99.     )
  100.   end
  101.   def test_form_tag_with_upload_progress
  102.     assert_dom_equal(
  103.       "<form action="http://www.example.com" enctype="multipart/form-data" method="post" onsubmit="if (this.action.indexOf('upload_id') &lt; 0){ this.action += '?upload_id=1'; }this.target = 'UploadTarget1';$('UploadStatus1').innerHTML='Upload starting...'; if($('UploadProgressBar1')){$('UploadProgressBar1').firstChild.firstChild.style.width='0%'}; if (document.uploadStatus1) { document.uploadStatus1.stop(); }document.uploadStatus1 = new Ajax.PeriodicalUpdater('UploadStatus1','http://www.example.com', Object.extend({asynchronous:true, evalScripts:true, onComplete:function(request){$('UploadStatus1').innerHTML='A message';if($('UploadProgressBar1')){$('UploadProgressBar1').firstChild.firstChild.style.width='100%'};document.uploadStatus1 = null}},{decay:1.8,frequency:2.0})); return true"><iframe id="UploadTarget1" name="UploadTarget1" src="" style="width:0px;height:0px;border:0"></iframe>",
  104.       form_tag_with_upload_progress
  105.     )
  106.   end
  107.   def test_form_tag_with_upload_progress_custom
  108.     assert_dom_equal(
  109.       "<form action="http://www.example.com" enctype="multipart/form-data" method="post" onsubmit="if (this.action.indexOf('upload_id') &lt; 0){ this.action += '?upload_id=5'; }this.target = 'awindow';$('UploadStatus0').innerHTML='Upload starting...'; if($('UploadProgressBar0')){$('UploadProgressBar0').firstChild.firstChild.style.width='0%'}; alert('foo'); if (document.uploadStatus0) { document.uploadStatus0.stop(); }document.uploadStatus0 = new Ajax.PeriodicalUpdater('UploadStatus0','http://www.example.com', Object.extend({asynchronous:true, evalScripts:true, onComplete:function(request){$('UploadStatus0').innerHTML='A message';if($('UploadProgressBar0')){$('UploadProgressBar0').firstChild.firstChild.style.width='100%'};document.uploadStatus0 = null; alert('bar')}},{decay:7,frequency:6})); return true" target="awindow">",
  110.       form_tag_with_upload_progress({:upload_id => 5}, {:begin => "alert('foo')", :finish => "alert('bar')", :frequency => 6, :decay => 7, :target => 'awindow'}) 
  111.     )
  112.   end
  113. end