PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/amexsbs/railsapp/vendor/plugins/paperclip/test/thumbnail_test.rb

https://bitbucket.org/AcireStudios/social-app-demo
Ruby | 227 lines | 185 code | 42 blank | 0 comment | 1 complexity | aadef0022736640710dc72746e7ff512 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, MIT
  1. require 'test/helper'
  2. class ThumbnailTest < Test::Unit::TestCase
  3. context "A Paperclip Tempfile" do
  4. setup do
  5. @tempfile = Paperclip::Tempfile.new("file.jpg")
  6. end
  7. should "have its path contain a real extension" do
  8. assert_equal ".jpg", File.extname(@tempfile.path)
  9. end
  10. should "be a real Tempfile" do
  11. assert @tempfile.is_a?(::Tempfile)
  12. end
  13. end
  14. context "Another Paperclip Tempfile" do
  15. setup do
  16. @tempfile = Paperclip::Tempfile.new("file")
  17. end
  18. should "not have an extension if not given one" do
  19. assert_equal "", File.extname(@tempfile.path)
  20. end
  21. should "still be a real Tempfile" do
  22. assert @tempfile.is_a?(::Tempfile)
  23. end
  24. end
  25. context "An image" do
  26. setup do
  27. @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
  28. end
  29. teardown { @file.close }
  30. [["600x600>", "434x66"],
  31. ["400x400>", "400x61"],
  32. ["32x32<", "434x66"]
  33. ].each do |args|
  34. context "being thumbnailed with a geometry of #{args[0]}" do
  35. setup do
  36. @thumb = Paperclip::Thumbnail.new(@file, :geometry => args[0])
  37. end
  38. should "start with dimensions of 434x66" do
  39. cmd = %Q[identify -format "%wx%h" "#{@file.path}"]
  40. assert_equal "434x66", `#{cmd}`.chomp
  41. end
  42. should "report the correct target geometry" do
  43. assert_equal args[0], @thumb.target_geometry.to_s
  44. end
  45. context "when made" do
  46. setup do
  47. @thumb_result = @thumb.make
  48. end
  49. should "be the size we expect it to be" do
  50. cmd = %Q[identify -format "%wx%h" "#{@thumb_result.path}"]
  51. assert_equal args[1], `#{cmd}`.chomp
  52. end
  53. end
  54. end
  55. end
  56. context "being thumbnailed at 100x50 with cropping" do
  57. setup do
  58. @thumb = Paperclip::Thumbnail.new(@file, :geometry => "100x50#")
  59. end
  60. should "report its correct current and target geometries" do
  61. assert_equal "100x50#", @thumb.target_geometry.to_s
  62. assert_equal "434x66", @thumb.current_geometry.to_s
  63. end
  64. should "report its correct format" do
  65. assert_nil @thumb.format
  66. end
  67. should "have whiny turned on by default" do
  68. assert @thumb.whiny
  69. end
  70. should "have convert_options set to nil by default" do
  71. assert_equal nil, @thumb.convert_options
  72. end
  73. should "send the right command to convert when sent #make" do
  74. Paperclip::CommandLine.expects(:"`").with do |arg|
  75. arg.match %r{convert ["']#{File.expand_path(@thumb.file.path)}\[0\]["'] -resize ["']x50["'] -crop ["']100x50\+114\+0["'] \+repage ["'].*?["']}
  76. end
  77. @thumb.make
  78. end
  79. should "create the thumbnail when sent #make" do
  80. dst = @thumb.make
  81. assert_match /100x50/, `identify "#{dst.path}"`
  82. end
  83. end
  84. context "being thumbnailed with source file options set" do
  85. setup do
  86. @thumb = Paperclip::Thumbnail.new(@file,
  87. :geometry => "100x50#",
  88. :source_file_options => "-strip")
  89. end
  90. should "have source_file_options value set" do
  91. assert_equal ["-strip"], @thumb.source_file_options
  92. end
  93. should "send the right command to convert when sent #make" do
  94. Paperclip::CommandLine.expects(:"`").with do |arg|
  95. arg.match %r{convert -strip ["']#{File.expand_path(@thumb.file.path)}\[0\]["'] -resize ["']x50["'] -crop ["']100x50\+114\+0["'] \+repage ["'].*?["']}
  96. end
  97. @thumb.make
  98. end
  99. should "create the thumbnail when sent #make" do
  100. dst = @thumb.make
  101. assert_match /100x50/, `identify "#{dst.path}"`
  102. end
  103. context "redefined to have bad source_file_options setting" do
  104. setup do
  105. @thumb = Paperclip::Thumbnail.new(@file,
  106. :geometry => "100x50#",
  107. :source_file_options => "-this-aint-no-option")
  108. end
  109. should "error when trying to create the thumbnail" do
  110. assert_raises(Paperclip::PaperclipError) do
  111. @thumb.make
  112. end
  113. end
  114. end
  115. end
  116. context "being thumbnailed with convert options set" do
  117. setup do
  118. @thumb = Paperclip::Thumbnail.new(@file,
  119. :geometry => "100x50#",
  120. :convert_options => "-strip -depth 8")
  121. end
  122. should "have convert_options value set" do
  123. assert_equal %w"-strip -depth 8", @thumb.convert_options
  124. end
  125. should "send the right command to convert when sent #make" do
  126. Paperclip::CommandLine.expects(:"`").with do |arg|
  127. arg.match %r{convert ["']#{File.expand_path(@thumb.file.path)}\[0\]["'] -resize ["']x50["'] -crop ["']100x50\+114\+0["'] \+repage -strip -depth 8 ["'].*?["']}
  128. end
  129. @thumb.make
  130. end
  131. should "create the thumbnail when sent #make" do
  132. dst = @thumb.make
  133. assert_match /100x50/, `identify "#{dst.path}"`
  134. end
  135. context "redefined to have bad convert_options setting" do
  136. setup do
  137. @thumb = Paperclip::Thumbnail.new(@file,
  138. :geometry => "100x50#",
  139. :convert_options => "-this-aint-no-option")
  140. end
  141. should "error when trying to create the thumbnail" do
  142. assert_raises(Paperclip::PaperclipError) do
  143. @thumb.make
  144. end
  145. end
  146. end
  147. end
  148. context "being thumbnailed with a blank geometry string" do
  149. setup do
  150. @thumb = Paperclip::Thumbnail.new(@file,
  151. :geometry => "",
  152. :convert_options => "-gravity center -crop \"300x300+0-0\"")
  153. end
  154. should "not get resized by default" do
  155. assert !@thumb.transformation_command.include?("-resize")
  156. end
  157. end
  158. end
  159. context "A multipage PDF" do
  160. setup do
  161. @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "twopage.pdf"), 'rb')
  162. end
  163. teardown { @file.close }
  164. should "start with two pages with dimensions 612x792" do
  165. cmd = %Q[identify -format "%wx%h" "#{@file.path}"]
  166. assert_equal "612x792"*2, `#{cmd}`.chomp
  167. end
  168. context "being thumbnailed at 100x100 with cropping" do
  169. setup do
  170. @thumb = Paperclip::Thumbnail.new(@file, :geometry => "100x100#", :format => :png)
  171. end
  172. should "report its correct current and target geometries" do
  173. assert_equal "100x100#", @thumb.target_geometry.to_s
  174. assert_equal "612x792", @thumb.current_geometry.to_s
  175. end
  176. should "report its correct format" do
  177. assert_equal :png, @thumb.format
  178. end
  179. should "create the thumbnail when sent #make" do
  180. dst = @thumb.make
  181. assert_match /100x100/, `identify "#{dst.path}"`
  182. end
  183. end
  184. end
  185. end