1context("ggsave") 2 3test_that("ggsave creates file", { 4 path <- tempfile() 5 on.exit(unlink(path)) 6 7 p <- ggplot(mpg, aes(displ, hwy)) + geom_point() 8 9 expect_false(file.exists(path)) 10 ggsave(path, p, device = "pdf", width = 5, height = 5) 11 expect_true(file.exists(path)) 12}) 13 14test_that("ggsave restores previous graphics device", { 15 # When multiple devices are open, dev.off() restores the next one in the list, 16 # not the previously-active one. (#2363) 17 path <- tempfile() 18 on.exit(unlink(path)) 19 20 png(tempfile()) 21 png(tempfile()) 22 on.exit(graphics.off(), add = TRUE) 23 24 old_dev <- dev.cur() 25 p <- ggplot(mpg, aes(displ, hwy)) + geom_point() 26 ggsave(path, p, device = "png", width = 5, height = 5) 27 28 expect_identical(old_dev, dev.cur()) 29}) 30 31test_that("ggsave uses theme background as image background", { 32 skip_if_not_installed("xml2") 33 34 path <- tempfile() 35 on.exit(unlink(path)) 36 p <- ggplot(mtcars, aes(disp, mpg)) + 37 geom_point() + 38 coord_fixed() + 39 theme(plot.background = element_rect(fill = "#00CCCC")) 40 ggsave(path, p, device = "svg", width = 5, height = 5) 41 img <- xml2::read_xml(path) 42 # Find background rect in svg 43 bg <- as.character(xml2::xml_find_first(img, xpath = "d1:rect/@style")) 44 expect_true(grepl("fill: #00CCCC", bg)) 45}) 46 47test_that("ggsave can handle blank background", { 48 skip_if_not_installed("xml2") 49 50 path <- tempfile() 51 on.exit(unlink(path)) 52 p <- ggplot(mtcars, aes(disp, mpg)) + 53 geom_point() + 54 theme(plot.background = element_blank()) 55 ggsave(path, p, device = "svg", width = 5, height = 5) 56 img <- xml2::read_xml(path) 57 bg <- as.character(xml2::xml_find_first(img, xpath = "d1:rect/@style")) 58 expect_true(grepl("fill: none", bg)) 59}) 60 61 62# plot_dim --------------------------------------------------------------- 63 64test_that("guesses and informs if dim not specified", { 65 png(width = 10, height = 10, units = "in", res = 300) 66 on.exit(capture.output(dev.off())) 67 68 expect_message(out <- plot_dim(), "10 x 10") 69 expect_equal(out, c(10, 10)) 70}) 71 72test_that("uses 7x7 if no graphics device open", { 73 expect_equal(plot_dim(), c(7, 7)) 74}) 75 76test_that("warned about large plot unless limitsize = FALSE", { 77 expect_error(plot_dim(c(50, 50)), "exceed 50 inches") 78 expect_equal(plot_dim(c(50, 50), limitsize = FALSE), c(50, 50)) 79}) 80 81test_that("scale multiplies height & width", { 82 expect_equal(plot_dim(c(10, 10), scale = 1), c(10, 10)) 83 expect_equal(plot_dim(c(5, 5), scale = 2), c(10, 10)) 84}) 85 86# plot_dev --------------------------------------------------------------------- 87 88test_that("unknown device triggers error", { 89 expect_error(plot_dev("xyz"), "Unknown graphics device") 90 expect_error(plot_dev(NULL, "test.xyz"), "Unknown graphics device") 91}) 92 93 94test_that("text converted to function", { 95 expect_identical(body(plot_dev("png"))[[1]], quote(png_dev)) 96 expect_identical(body(plot_dev("pdf"))[[1]], quote(grDevices::pdf)) 97}) 98 99test_that("if device is NULL, guess from extension", { 100 expect_identical(body(plot_dev(NULL, "test.png"))[[1]], quote(png_dev)) 101}) 102 103# parse_dpi --------------------------------------------------------------- 104 105test_that("DPI string values are parsed correctly", { 106 expect_type(parse_dpi("print"), "double") 107 expect_type(parse_dpi("screen"), "double") 108 expect_type(parse_dpi("retina"), "double") 109 expect_type(parse_dpi(100), "double") 110 expect_type(parse_dpi(300L), "integer") 111}) 112 113test_that("invalid single-string DPI values throw an error", { 114 expect_error(parse_dpi("abc"), "Unknown DPI string") 115}) 116 117test_that("invalid non-single-string DPI values throw an error", { 118 expect_error(parse_dpi(factor(100)), "DPI must be a single number or string") 119 expect_error(parse_dpi(c("print", "screen")), "DPI must be a single number or string") 120 expect_error(parse_dpi(c(150, 300)), "DPI must be a single number or string") 121 expect_error(parse_dpi(list(150)), "DPI must be a single number or string") 122}) 123