aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/template/upload_progress_helper_testx.rb
blob: 84ee607cbddd9590194abdb4f08b650da5c0244d (plain) (blame)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
require File.dirname(__FILE__) + '/../abstract_unit'

require File.dirname(__FILE__) + '/../../lib/action_view/helpers/date_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/number_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/asset_tag_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_tag_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/tag_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/javascript_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/upload_progress_helper'
require File.dirname(__FILE__) + '/../../../activesupport/lib/active_support/core_ext/hash' #for stringify keys

class MockProgress
  def initialize(started, finished)
    @started, @finished = [started, finished]
  end

  def started?
    @started
  end

  def finished?
    @finished
  end

  def message
    "A message"
  end

  def method_missing(meth, *args)
    # Just return some consitant number
    meth.to_s.hash.to_i.abs + args.hash.to_i.abs
  end
end

class UploadProgressHelperTest < Test::Unit::TestCase
  include ActionView::Helpers::DateHelper
  include ActionView::Helpers::NumberHelper
  include ActionView::Helpers::AssetTagHelper
  include ActionView::Helpers::FormTagHelper
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::UrlHelper
  include ActionView::Helpers::JavaScriptHelper
  include ActionView::Helpers::UploadProgressHelper

  def next_upload_id; @upload_id = last_upload_id.succ; end
  def last_upload_id; @upload_id ||= 0; end
  def current_upload_id; last_upload_id; end
  def upload_progress(upload_id = nil); @upload_progress or MockProgress.new(false, true); end

  def setup
    @controller = Class.new do
      def url_for(options, *parameters_for_method_reference)
        "http://www.example.com"
      end
    end
    @controller = @controller.new
  end

  def test_upload_status_tag
    assert_equal(
      '<div class="progressBar" id="UploadProgressBar0"><div class="border"><div class="background"><div class="foreground"></div></div></div></div><div class="uploadStatus" id="UploadStatus0"></div>',
      upload_status_tag
    )
  end

  def test_upload_status_text_tag
    assert_equal(
      '<div class="my-upload" id="my-id">Starting</div>',
      upload_status_text_tag('Starting', :class => 'my-upload', :id => 'my-id')
    )
  end


  def test_upload_progress_text
    @upload_progress = MockProgress.new(false, false)
    assert_equal(
      "Upload starting...",
      upload_progress_text
    )

    @upload_progress = MockProgress.new(true, false)
    assert_equal(
      "828.7 MB of 456.2 MB at 990.1 MB/s; 10227 days remaining",
      upload_progress_text
    )

    @upload_progress = MockProgress.new(true, true)
    assert_equal(
      "Upload finished. A message",
      upload_progress_text
    )
  end

  def test_upload_progress_update_bar_js
    assert_equal(
      "$('UploadProgressBar0').firstChild.firstChild.style.width='0%';",
      upload_progress_update_bar_js
    )

    assert_equal(
      "$('UploadProgressBar0').firstChild.firstChild.style.width='50%';",
      upload_progress_update_bar_js(50)
    )
  end

  def test_finish_upload_status
    assert_equal(
      "<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>",
      finish_upload_status
    )

    assert_equal(
      "<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>",
      finish_upload_status(:client_js_argument => 123)
    )

    assert_equal(
      "<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>",
      finish_upload_status(:redirect_to => '/redirected/')
    )
  end

  def test_form_tag_with_upload_progress
    assert_equal(
      "<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';$('UploadProgressBar1').firstChild.firstChild.style.width='0%';; document.uploadStatus1 = new Ajax.PeriodicalUpdater('UploadStatus1','http://www.example.com',{script:true, onComplete:function(request){$('UploadProgressBar1').firstChild.firstChild.style.width='100%';; }, asynchronous:true}.extend({decay:1.8,frequency:2.0})); return true\"><iframe id=\"UploadTarget1\" name=\"UploadTarget1\" src=\"\" style=\"width:0px;height:0px;border:0\"></iframe>",
      form_tag_with_upload_progress
    )
  end

  def test_form_tag_with_upload_progress_custom
    assert_equal(
      "<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';$('UploadProgressBar0').firstChild.firstChild.style.width='0%';; alert('foo'); document.uploadStatus0 = new Ajax.PeriodicalUpdater('UploadStatus0','http://www.example.com',{script:true, onComplete:function(request){$('UploadProgressBar0').firstChild.firstChild.style.width='100%';; alert('bar'); alert('bar')}, asynchronous:true}.extend({decay:7,frequency:6})); return true\" target=\"awindow\">",
      form_tag_with_upload_progress({:upload_id => 5}, {:begin => "alert('foo')", :finish => "alert('bar')", :frequency => 6, :decay => 7, :target => 'awindow'}) 
    )
  end
end
require File.dirname(__FILE__) + '/../abstract_unit'

require File.dirname(__FILE__) + '/../../lib/action_view/helpers/date_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/number_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/asset_tag_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_tag_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/tag_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/javascript_helper'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/upload_progress_helper'
require File.dirname(__FILE__) + '/../../../activesupport/lib/active_support/core_ext/hash' #for stringify keys

class MockProgress
  def initialize(started, finished)
    @started, @finished = [started, finished]
  end

  def started?
    @started
  end

  def finished?
    @finished
  end

  def message
    "A message"
  end

  def method_missing(meth, *args)
    # Just return some consitant number
    meth.to_s.hash.to_i.abs + args.hash.to_i.abs
  end
end

class UploadProgressHelperTest < Test::Unit::TestCase
  include ActionView::Helpers::DateHelper
  include ActionView::Helpers::NumberHelper
  include ActionView::Helpers::AssetTagHelper
  include ActionView::Helpers::FormTagHelper
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::UrlHelper
  include ActionView::Helpers::JavaScriptHelper
  include ActionView::Helpers::UploadProgressHelper

  def next_upload_id; @upload_id = last_upload_id.succ; end
  def last_upload_id; @upload_id ||= 0; end
  def current_upload_id; last_upload_id; end
  def upload_progress(upload_id = nil); @upload_progress ||= MockProgress.new(false, true); end

  def setup
    @controller = Class.new do
      def url_for(options, *parameters_for_method_reference)
        "http://www.example.com"
      end
    end
    @controller = @controller.new
  end

  def test_upload_status_tag
    assert_equal(
      '<div class="progressBar" id="UploadProgressBar0"><div class="border"><div class="background"><div class="foreground"></div></div></div></div><div class="uploadStatus" id="UploadStatus0"></div>',
      upload_status_tag
    )
  end

  def test_upload_status_text_tag
    assert_equal(
      '<div class="my-upload" id="my-id">Starting</div>',
      upload_status_text_tag('Starting', :class => 'my-upload', :id => 'my-id')
    )
  end


  def test_upload_progress_text
    @upload_progress = MockProgress.new(false, false)
    assert_equal(
      "Upload starting...",
      upload_progress_text
    )

    @upload_progress = MockProgress.new(true, false)
    assert_equal(
      "828.7 MB of 456.2 MB at 990.1 MB/s; 10227 days remaining",
      upload_progress_text
    )

    @upload_progress = MockProgress.new(true, true)
    assert_equal(
      "A message",
      upload_progress_text
    )
  end

  def test_upload_progress_update_bar_js
    assert_equal(
      "$('UploadProgressBar0').firstChild.firstChild.style.width='0%'",
      upload_progress_update_bar_js
    )

    assert_equal(
      "$('UploadProgressBar0').firstChild.firstChild.style.width='50%'",
      upload_progress_update_bar_js(50)
    )
  end

  def test_finish_upload_status
    assert_equal(
      "<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>",
      finish_upload_status
    )

    assert_equal(
      "<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>",
      finish_upload_status(:client_js_argument => 123)
    )

    assert_equal(
      "<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>",
      finish_upload_status(:redirect_to => '/redirected/')
    )
  end

  def test_form_tag_with_upload_progress
    assert_equal(
      "<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...'; $('UploadProgressBar1').firstChild.firstChild.style.width='0%'; if (document.uploadStatus1) { document.uploadStatus1.stop(); }document.uploadStatus1 = new Ajax.PeriodicalUpdater('UploadStatus1','http://www.example.com', {onComplete:function(request){$('UploadStatus1').innerHTML='A message';$('UploadProgressBar1').firstChild.firstChild.style.width='100%';document.uploadStatus1 = null}, evalScripts:true, asynchronous:true}.extend({decay:1.8,frequency:2.0})); return true\"><iframe id=\"UploadTarget1\" name=\"UploadTarget1\" src=\"\" style=\"width:0px;height:0px;border:0\"></iframe>",
      form_tag_with_upload_progress
    )
  end

  def test_form_tag_with_upload_progress_custom
    assert_equal(
      "<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...'; $('UploadProgressBar0').firstChild.firstChild.style.width='0%'; alert('foo'); if (document.uploadStatus0) { document.uploadStatus0.stop(); }document.uploadStatus0 = new Ajax.PeriodicalUpdater('UploadStatus0','http://www.example.com', {onComplete:function(request){$('UploadStatus0').innerHTML='A message';$('UploadProgressBar0').firstChild.firstChild.style.width='100%';document.uploadStatus0 = null; alert('bar')}, evalScripts:true, asynchronous:true}.extend({decay:7,frequency:6})); return true\" target=\"awindow\">",
      form_tag_with_upload_progress({:upload_id => 5}, {:begin => "alert('foo')", :finish => "alert('bar')", :frequency => 6, :decay => 7, :target => 'awindow'}) 
    )
  end
end