Skip to content

Commit

Permalink
Add visual tests for PNG and SVG images.
Browse files Browse the repository at this point in the history
Both tests generate an html page that displays the QR codes with
different options specified.
  • Loading branch information
abousquet committed Dec 14, 2019
1 parent dcc7486 commit f2a1ba1
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ erl_crash.dump
eqrcode-*.tar

**/.DS_Store
.elixir_ls
.elixir_ls

/test/images/
/test/html/
71 changes: 71 additions & 0 deletions test/png_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
defmodule EQRCode.PNGTest do
use ExUnit.Case

defp content, do: "www.google.com"
defp html_path, do: Path.expand("./test/html")
defp image_path, do: Path.expand("./test/images")

setup do
html_path() |> File.mkdir_p!()
image_path() |> File.mkdir_p!()
qr = content() |> EQRCode.encode()
[qr: qr]
end

test "Generate an html page with different types of PNG images", %{qr: qr} do
color = <<0, 0, 255>>
background_color = <<255, 0, 0>>

pngs =
[]
## Test Default
|> build_png(qr, "Default", [])
## Test Foreground Color png
|> build_png(qr, "Foreground", color: color)
## Test Background Color png
|> build_png(qr, "Background", background_color: background_color)
## Test Foreground and Background Color
|> build_png(qr, "Both Colors", background_color: background_color, color: color)
## Test Background Transparency
|> build_png(qr, "Transparent", background_color: :transparent, color: color)

html = gen_html(pngs)

html_path()
|> Path.join("png_test.html")
|> File.write(html)
end

defp write_png_to_file(png_binary, path), do: File.write!(path, png_binary, [:binary])

defp build_png(png_list, qr, label, opts) do
png_path = image_path() |> Path.join(Macro.underscore(label) <> ".png")

EQRCode.png(qr, opts)
|> write_png_to_file(png_path)

png_list ++ [{label, png_path}]
end

defp gen_html(kw_png) when is_list(kw_png) do
png =
Enum.map(kw_png, fn {key, value} ->
~s{<p><strong>#{key}:</strong></p><image src="#{value}">}
end)

"""
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<head>
<title>EQRCode PNG Image Tests</title>
<style>
body { background: rgba(0,255,0); }
</style>
</head>
#{png}
</body>
<html>
"""
end
end
69 changes: 69 additions & 0 deletions test/svg_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
defmodule EQRCode.SVGTest do
use ExUnit.Case

defp content, do: "www.google.com"
defp html_path, do: Path.expand("./test/html")

setup do
html_path() |> File.mkdir_p!()
qr = content() |> EQRCode.encode()
[qr: qr]
end

test "Generate an html page with different types of SVG images", %{qr: qr} do
color = "blue"
background_color = "red"

svgs =
[]
## Test default option
|> build_svgs(qr, "Default", [])
## Test foreground color being set
|> build_svgs(qr, "Foreground", color: color)
## Test background being set
|> build_svgs(qr, "Background", background_color: background_color)
## Test both background and foreground color being set
|> build_svgs(qr, "Both Colors", background_color: background_color, color: color)
## Test transparency in HTML
|> build_svgs(qr, "Transparent", background_color: :transparent, color: color)
## Test ViewBox
|> build_svgs(qr, "ViewBox", background_color: background_color, color: color, viewbox: true)
## Test transparent viewbox in HTML
|> build_svgs(qr, "Transparent ViewBox",
background_color: :transparent,
color: color,
viewbox: true
)

html = gen_html(svgs)

html_path()
|> Path.join("svg_test.html")
|> File.write(html)
end

defp build_svgs(svgs, qr, label, opts) do
svg = EQRCode.svg(qr, opts)
svg_c = EQRCode.svg(qr, [shape: "circle"] ++ opts)
svgs ++ [{label, svg <> svg_c}]
end

defp gen_html(kw_svg) when is_list(kw_svg) do
svg = Enum.map(kw_svg, fn {key, value} -> "<p><strong>#{key}:</strong></p>" <> value end)

"""
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<head>
<title>EQRCode PNG Image Tests</title>
<style>
body { background: rgba(0,255,0); }
</style>
</head>
#{svg}
</body>
<html>
"""
end
end

0 comments on commit f2a1ba1

Please sign in to comment.