|
| 1 | +safe_device_ids <- function() { |
| 2 | + devices <- grDevices::dev.list() |
| 3 | + if (is.null(devices)) integer() else unname(devices) |
| 4 | +} |
| 5 | + |
| 6 | +close_new_devices <- function(before_ids) { |
| 7 | + after_ids <- safe_device_ids() |
| 8 | + new_ids <- setdiff(after_ids, before_ids) |
| 9 | + if (length(new_ids) > 0) { |
| 10 | + for (id in new_ids) { |
| 11 | + grDevices::dev.off(id) |
| 12 | + } |
| 13 | + } |
| 14 | + invisible(new_ids) |
| 15 | +} |
| 16 | + |
| 17 | +test_that("file_null returns the platform null device", { |
| 18 | + expected <- if (.Platform$OS.type == "windows") "nul:" else "/dev/null" |
| 19 | + expect_identical(r2rtf:::file_null(), expected) |
| 20 | +}) |
| 21 | + |
| 22 | +test_that("close_device ignores invalid identifiers", { |
| 23 | + before <- safe_device_ids() |
| 24 | + r2rtf:::close_device(NA_real_) |
| 25 | + r2rtf:::close_device("not-a-device") |
| 26 | + r2rtf:::close_device(1) |
| 27 | + r2rtf:::close_device(9999) |
| 28 | + expect_identical(safe_device_ids(), before) |
| 29 | +}) |
| 30 | + |
| 31 | +test_that("close_device closes a live device", { |
| 32 | + before <- safe_device_ids() |
| 33 | + on.exit(close_new_devices(before), add = TRUE) |
| 34 | + |
| 35 | + grDevices::pdf(file = r2rtf:::file_null()) |
| 36 | + new_ids <- setdiff(safe_device_ids(), before) |
| 37 | + |
| 38 | + expect_true(length(new_ids) >= 1) |
| 39 | + |
| 40 | + r2rtf:::close_device(new_ids[1]) |
| 41 | + |
| 42 | + expect_identical(safe_device_ids(), before) |
| 43 | +}) |
| 44 | + |
| 45 | +test_that("with_graphics_device restores device state", { |
| 46 | + before <- safe_device_ids() |
| 47 | + on.exit(close_new_devices(before), add = TRUE) |
| 48 | + |
| 49 | + width <- r2rtf:::with_graphics_device({ |
| 50 | + graphics::plot.new() |
| 51 | + graphics::strwidth("x") |
| 52 | + }) |
| 53 | + |
| 54 | + expect_true(is.numeric(width)) |
| 55 | + expect_identical(safe_device_ids(), before) |
| 56 | +}) |
| 57 | + |
| 58 | +test_that("with_graphics_device does not add devices when one is active", { |
| 59 | + before <- safe_device_ids() |
| 60 | + on.exit(close_new_devices(before), add = TRUE) |
| 61 | + |
| 62 | + grDevices::pdf(file = r2rtf:::file_null()) |
| 63 | + open_devices <- safe_device_ids() |
| 64 | + |
| 65 | + width <- r2rtf:::with_graphics_device({ |
| 66 | + graphics::plot.new() |
| 67 | + graphics::strwidth("x") |
| 68 | + }) |
| 69 | + |
| 70 | + expect_true(is.numeric(width)) |
| 71 | + expect_identical(safe_device_ids(), open_devices) |
| 72 | +}) |
| 73 | + |
| 74 | +test_that("open_null_device honors device option with file", { |
| 75 | + before <- safe_device_ids() |
| 76 | + on.exit(close_new_devices(before), add = TRUE) |
| 77 | + |
| 78 | + old_device <- getOption("device") |
| 79 | + on.exit(options(device = old_device), add = TRUE) |
| 80 | + |
| 81 | + called <- FALSE |
| 82 | + file_arg <- NULL |
| 83 | + device_fun <- function(file, ...) { |
| 84 | + called <<- TRUE |
| 85 | + file_arg <<- file |
| 86 | + grDevices::pdf(file = file, ...) |
| 87 | + } |
| 88 | + options(device = device_fun) |
| 89 | + |
| 90 | + opened <- r2rtf:::open_null_device() |
| 91 | + |
| 92 | + expect_true(called) |
| 93 | + expect_identical(file_arg, r2rtf:::file_null()) |
| 94 | + expect_true(opened %in% safe_device_ids()) |
| 95 | +}) |
| 96 | + |
| 97 | +test_that("open_null_device honors device option with filename", { |
| 98 | + before <- safe_device_ids() |
| 99 | + on.exit(close_new_devices(before), add = TRUE) |
| 100 | + |
| 101 | + old_device <- getOption("device") |
| 102 | + on.exit(options(device = old_device), add = TRUE) |
| 103 | + |
| 104 | + called <- FALSE |
| 105 | + filename_arg <- NULL |
| 106 | + device_fun <- function(filename, ...) { |
| 107 | + called <<- TRUE |
| 108 | + filename_arg <<- filename |
| 109 | + grDevices::pdf(file = filename, ...) |
| 110 | + } |
| 111 | + options(device = device_fun) |
| 112 | + |
| 113 | + opened <- r2rtf:::open_null_device() |
| 114 | + |
| 115 | + expect_true(called) |
| 116 | + expect_identical(filename_arg, r2rtf:::file_null()) |
| 117 | + expect_true(opened %in% safe_device_ids()) |
| 118 | +}) |
| 119 | + |
| 120 | +test_that("open_null_device falls back when option cannot accept a file", { |
| 121 | + before <- safe_device_ids() |
| 122 | + if (length(before) > 0) { |
| 123 | + skip("requires no active devices for fallback path") |
| 124 | + } |
| 125 | + on.exit(close_new_devices(before), add = TRUE) |
| 126 | + |
| 127 | + old_device <- getOption("device") |
| 128 | + on.exit(options(device = old_device), add = TRUE) |
| 129 | + |
| 130 | + options(device = function(...) NULL) |
| 131 | + |
| 132 | + opened <- r2rtf:::open_null_device() |
| 133 | + |
| 134 | + after <- safe_device_ids() |
| 135 | + expect_true(length(after) >= 1) |
| 136 | + expect_true(opened %in% after) |
| 137 | +}) |
0 commit comments