From 1e7c8a9df3f6146746fb640349513005e9143276 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Sat, 4 May 2024 20:18:21 -0400 Subject: [PATCH 001/105] changed browser name to chromote --- tests/testthat/helper-HTML.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 3da3ad883..8c5a5c4b1 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -50,7 +50,7 @@ tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { if(OS == "Windows" || OS == "Darwin") { animint_server <- "host.docker.internal" } - if(browserName == "chrome"){ + if(browserName == "chromote"){ chrome.session <- chromote::ChromoteSession$new() chrome.session$view() chrome.session$refresh <- function()chrome.session$Page$reload() From 361265a275bf7335a62a34e0fdefb25c4cabf8a8 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Mon, 27 May 2024 16:29:21 -0400 Subject: [PATCH 002/105] Update helper-HTML.R --- tests/testthat/helper-HTML.R | 233 ++++++++++++++++++----------------- 1 file changed, 117 insertions(+), 116 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 8c5a5c4b1..8d4020278 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -1,116 +1,117 @@ -#' Apply `animint2dir` to a list ggplots and extract the (rendered) page source via RSelenium -#' -#' @param plotList A named list of ggplot2 objects -animint2HTML <- function(plotList) { - unlink("animint-htmltest", recursive=TRUE) - res <- animint2dir(plotList, out.dir = "animint-htmltest", - open.browser = FALSE) - remDr$refresh() - Sys.sleep(1) - res$html <- getHTML() - ## [ERROR - 2019-06-05T18:30:55.358Z] Session [e7c4e500-871e-11e9-a9b5-8dab1f486f7e] - page.onError - msg: TypeError: 'undefined' is not an object (evaluating 's_info.type') - ## [ERROR - 2019-06-05T18:30:55.360Z] Session [e7c4e500-871e-11e9-a9b5-8dab1f486f7e] - page.onError - stack: - ## (anonymous function) (http://localhost:4848/animint-htmltest/animint.js:2535) - ## print(sapply(res$selectors, "[[", "type")) - res -} -getHTML <- function(){ - XML::htmlParse(remDr$getPageSource(), asText = TRUE) -} -#' Initiate external processes necessary for running tests. -#' -#' Initiates a local file server and remote driver. -#' -#' @param browserName Name of the browser to use for testing. -#' See ?RSelenium::remoteDriver for details. -#' @param dir character string with the path to animint's source code. Defaults to current directory -#' @param port port portnumber used for local file server -#' @param ... list of additional options passed onto RSelenium::remoteDriver -#' @return invisible(TRUE) -#' @export -#' @seealso \link{tests_run} -#' -tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { - # try to exit out of previously initated processes - ex <- tests_exit() - # start a non-blocking local file server under path/to/animint/tests/testhat - testPath <- find_test_path(dir) - run_servr(port = port, directory = testPath) - # animint tests are performed in path/to/testthat/animint-htmltest/ - # note this path has to match the out.dir argument in animint2THML... - testDir <- file.path(testPath, "animint-htmltest") - # if the htmltest directory exists, wipe clean, then create an empty folder - unlink(testDir, recursive = TRUE) - # start-up remote driver - remotePort <- 4444L - OS <- Sys.info()[['sysname']] - if(OS == "Linux") { - animint_server <- "localhost" - } - if(OS == "Windows" || OS == "Darwin") { - animint_server <- "host.docker.internal" - } - if(browserName == "chromote"){ - chrome.session <- chromote::ChromoteSession$new() - chrome.session$view() - chrome.session$refresh <- function()chrome.session$Page$reload() - chrome.session$navigate <- function(u){ - chrome.session$Page$navigate(u) - } - chrome.session$getPageSource <- function(){ - doc <- chrome.session$DOM$getDocument() - chrome.session$DOM$getOuterHTML(doc$root$nodeId)$outerHTML - } - remDr <<- chrome.session - }else{ - if (browserName == "phantomjs") { - message("Starting phantomjs binary. To shut it down, run: \n pJS$stop()") - pJS <<- wdman::phantomjs( - port = remotePort, - phantomver = "latest" - ) - ## Give time for phantomjs binary to start - animint_server <- "localhost" - Sys.sleep(8) - } else if(browserName=="firefox"){ - ## If using firefox, you'll need to run selenium-firefox docker image in order to make it work correctly. - ## We're using docker to avoid version incompatibility issues. - message("You need to run selenium docker image(selenium/standalone-firefox:2.53.0) as specified in docs(https://github.com/tdhock/animint2/wiki/Testing). \nNote: Ignore if already running.") - }else stop("unrecognized browser name") - remDr <<- RSelenium::remoteDriver( - port = remotePort, - browser = browserName, - ) - ## wait for the remote driver to start-up - Sys.sleep(6) - remDr$open(silent = TRUE) - ## some tests don't run reliably with phantomjs (see tests-widerect.R) - Sys.setenv("ANIMINT_BROWSER" = browserName) - ## wait a maximum of 30 seconds when searchinsg for elements. - remDr$setTimeout(type = "implicit", milliseconds = 30000) - ## wait a maximum of 30 seconds for a particular type of operation to execute - remDr$setTimeout(type = "page load", milliseconds = 30000) - ## if we navigate to localhost:%s/htmltest directly, some browsers will - ## redirect to www.htmltest.com. A 'safer' approach is to navigate, then click. - } - remDr$navigate(sprintf("http://%s:%s/animint-htmltest/", animint_server, port)) - ## Why not just navigate to the right URL to begin with? - ## e <- remDr$findElement("xpath", "//a[@href='animint-htmltest/']") - ## e$clickElement() - invisible(TRUE) -} -## get both horizontal and vertical grid lines -get_grid_lines <- function(html, p_name, grid_class){ - path.i <- - '//svg[@id="plot_%s"]//g[@class="grid_%s"]//g[@class="%s"]//line' - path.hor <- sprintf(path.i, p_name, grid_class, "y") - path.vert <- sprintf(path.i, p_name, grid_class, "x") - nodes_h <- getNodeSet(html, path.hor) - nodes_v <- getNodeSet(html, path.vert) - # take x1, x2, y1, y2 values only - attr_h <- sapply(nodes_h, xmlAttrs)[1:4, ] - attr_v <- sapply(nodes_v, xmlAttrs)[1:4, ] - attr_h <- apply(attr_h, 2, as.numeric) - attr_v <- apply(attr_v, 2, as.numeric) - return(list(hor=attr_h, vert=attr_v)) -} +#' Apply `animint2dir` to a list ggplots and extract the (rendered) page source via RSelenium +#' +#' @param plotList A named list of ggplot2 objects +animint2HTML <- function(plotList) { + unlink("animint-htmltest", recursive=TRUE) + res <- animint2dir(plotList, out.dir = "animint-htmltest", + open.browser = FALSE) + remDr$refresh() + Sys.sleep(1) + res$html <- getHTML() + print(res) + ## [ERROR - 2019-06-05T18:30:55.358Z] Session [e7c4e500-871e-11e9-a9b5-8dab1f486f7e] - page.onError - msg: TypeError: 'undefined' is not an object (evaluating 's_info.type') + ## [ERROR - 2019-06-05T18:30:55.360Z] Session [e7c4e500-871e-11e9-a9b5-8dab1f486f7e] - page.onError - stack: + ## (anonymous function) (http://localhost:4848/animint-htmltest/animint.js:2535) + ## print(sapply(res$selectors, "[[", "type")) + res +} +getHTML <- function(){ + XML::htmlParse(remDr$getPageSource(), asText = TRUE) +} +#' Initiate external processes necessary for running tests. +#' +#' Initiates a local file server and remote driver. +#' +#' @param browserName Name of the browser to use for testing. +#' See ?RSelenium::remoteDriver for details. +#' @param dir character string with the path to animint's source code. Defaults to current directory +#' @param port port portnumber used for local file server +#' @param ... list of additional options passed onto RSelenium::remoteDriver +#' @return invisible(TRUE) +#' @export +#' @seealso \link{tests_run} +#' +tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { + # try to exit out of previously initated processes + ex <- tests_exit() + # start a non-blocking local file server under path/to/animint/tests/testhat + testPath <- find_test_path(dir) + run_servr(port = port, directory = testPath) + # animint tests are performed in path/to/testthat/animint-htmltest/ + # note this path has to match the out.dir argument in animint2THML... + testDir <- file.path(testPath, "animint-htmltest") + # if the htmltest directory exists, wipe clean, then create an empty folder + unlink(testDir, recursive = TRUE) + # start-up remote driver + remotePort <- 4444L + OS <- Sys.info()[['sysname']] + if(OS == "Linux") { + animint_server <- "localhost" + } + if(OS == "Windows" || OS == "Darwin") { + animint_server <- "host.docker.internal" + } + if(browserName == "chromote"){ + chrome.session <- chromote::ChromoteSession$new() + chrome.session$view() + chrome.session$refresh <- function()chrome.session$Page$reload() + chrome.session$navigate <- function(u){ + chrome.session$Page$navigate(u) + } + chrome.session$getPageSource <- function(){ + doc <- chrome.session$DOM$getDocument() + chrome.session$DOM$getOuterHTML(doc$root$nodeId)$outerHTML + } + remDr <<- chrome.session + }else{ + if (browserName == "phantomjs") { + message("Starting phantomjs binary. To shut it down, run: \n pJS$stop()") + pJS <<- wdman::phantomjs( + port = remotePort, + phantomver = "latest" + ) + ## Give time for phantomjs binary to start + animint_server <- "localhost" + Sys.sleep(8) + } else if(browserName=="firefox"){ + ## If using firefox, you'll need to run selenium-firefox docker image in order to make it work correctly. + ## We're using docker to avoid version incompatibility issues. + message("You need to run selenium docker image(selenium/standalone-firefox:2.53.0) as specified in docs(https://github.com/tdhock/animint2/wiki/Testing). \nNote: Ignore if already running.") + }else stop("unrecognized browser name") + remDr <<- RSelenium::remoteDriver( + port = remotePort, + browser = browserName, + ) + ## wait for the remote driver to start-up + Sys.sleep(6) + remDr$open(silent = TRUE) + ## some tests don't run reliably with phantomjs (see tests-widerect.R) + Sys.setenv("ANIMINT_BROWSER" = browserName) + ## wait a maximum of 30 seconds when searchinsg for elements. + remDr$setTimeout(type = "implicit", milliseconds = 30000) + ## wait a maximum of 30 seconds for a particular type of operation to execute + remDr$setTimeout(type = "page load", milliseconds = 30000) + ## if we navigate to localhost:%s/htmltest directly, some browsers will + ## redirect to www.htmltest.com. A 'safer' approach is to navigate, then click. + } + remDr$navigate(sprintf("http://%s:%s/animint-htmltest/", animint_server, port)) + ## Why not just navigate to the right URL to begin with? + ## e <- remDr$findElement("xpath", "//a[@href='animint-htmltest/']") + ## e$clickElement() + invisible(TRUE) +} +## get both horizontal and vertical grid lines +get_grid_lines <- function(html, p_name, grid_class){ + path.i <- + '//svg[@id="plot_%s"]//g[@class="grid_%s"]//g[@class="%s"]//line' + path.hor <- sprintf(path.i, p_name, grid_class, "y") + path.vert <- sprintf(path.i, p_name, grid_class, "x") + nodes_h <- getNodeSet(html, path.hor) + nodes_v <- getNodeSet(html, path.vert) + # take x1, x2, y1, y2 values only + attr_h <- sapply(nodes_h, xmlAttrs)[1:4, ] + attr_v <- sapply(nodes_v, xmlAttrs)[1:4, ] + attr_h <- apply(attr_h, 2, as.numeric) + attr_v <- apply(attr_v, 2, as.numeric) + return(list(hor=attr_h, vert=attr_v)) +} From 635e7a1741bd9ff15d4c1f4265012f12e6c1c061 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Tue, 28 May 2024 22:32:02 -0400 Subject: [PATCH 003/105] Update helper-HTML.R --- tests/testthat/helper-HTML.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 8d4020278..ba62f4822 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -60,6 +60,7 @@ tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { } chrome.session$getPageSource <- function(){ doc <- chrome.session$DOM$getDocument() + print(doc) chrome.session$DOM$getOuterHTML(doc$root$nodeId)$outerHTML } remDr <<- chrome.session From bbb45afa7b42bbddce1e60d696ab340bb700993f Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Wed, 29 May 2024 23:48:58 -0400 Subject: [PATCH 004/105] Update helper-HTML.R --- tests/testthat/helper-HTML.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index ba62f4822..0593f2981 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -63,6 +63,7 @@ tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { print(doc) chrome.session$DOM$getOuterHTML(doc$root$nodeId)$outerHTML } + Sys.sleep(3) remDr <<- chrome.session }else{ if (browserName == "phantomjs") { From 41b36cf9e5690d9e15d89c159032a6fe1e9ebb99 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Fri, 31 May 2024 09:35:39 -0400 Subject: [PATCH 005/105] Update helper-HTML.R --- tests/testthat/helper-HTML.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 0593f2981..db16eba2b 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -96,6 +96,8 @@ tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { ## if we navigate to localhost:%s/htmltest directly, some browsers will ## redirect to www.htmltest.com. A 'safer' approach is to navigate, then click. } + print(animint_server) + print(port) remDr$navigate(sprintf("http://%s:%s/animint-htmltest/", animint_server, port)) ## Why not just navigate to the right URL to begin with? ## e <- remDr$findElement("xpath", "//a[@href='animint-htmltest/']") From 58afca27a482eecf3a7878fd13c3d594cae8bf75 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Sat, 1 Jun 2024 15:43:01 -0400 Subject: [PATCH 006/105] Update helper-HTML.R --- tests/testthat/helper-HTML.R | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index db16eba2b..aea1355ed 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -65,6 +65,7 @@ tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { } Sys.sleep(3) remDr <<- chrome.session + remDr$navigate(sprintf("http://localhost:4848/animint-htmltest/")) }else{ if (browserName == "phantomjs") { message("Starting phantomjs binary. To shut it down, run: \n pJS$stop()") @@ -95,10 +96,11 @@ tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { remDr$setTimeout(type = "page load", milliseconds = 30000) ## if we navigate to localhost:%s/htmltest directly, some browsers will ## redirect to www.htmltest.com. A 'safer' approach is to navigate, then click. + remDr$navigate(sprintf("http://%s:%s/animint-htmltest/", animint_server, port)) } - print(animint_server) - print(port) - remDr$navigate(sprintf("http://%s:%s/animint-htmltest/", animint_server, port)) + #print(animint_server) + #print(port) + #remDr$navigate(sprintf("http://%s:%s/animint-htmltest/", animint_server, port)) ## Why not just navigate to the right URL to begin with? ## e <- remDr$findElement("xpath", "//a[@href='animint-htmltest/']") ## e$clickElement() From 03f362d8a450975ac4aef3878ce89be10138c869 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Sat, 1 Jun 2024 15:59:51 -0400 Subject: [PATCH 007/105] Update helper-HTML.R --- tests/testthat/helper-HTML.R | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index aea1355ed..aac59a2d0 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -8,7 +8,6 @@ animint2HTML <- function(plotList) { remDr$refresh() Sys.sleep(1) res$html <- getHTML() - print(res) ## [ERROR - 2019-06-05T18:30:55.358Z] Session [e7c4e500-871e-11e9-a9b5-8dab1f486f7e] - page.onError - msg: TypeError: 'undefined' is not an object (evaluating 's_info.type') ## [ERROR - 2019-06-05T18:30:55.360Z] Session [e7c4e500-871e-11e9-a9b5-8dab1f486f7e] - page.onError - stack: ## (anonymous function) (http://localhost:4848/animint-htmltest/animint.js:2535) @@ -60,10 +59,8 @@ tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { } chrome.session$getPageSource <- function(){ doc <- chrome.session$DOM$getDocument() - print(doc) chrome.session$DOM$getOuterHTML(doc$root$nodeId)$outerHTML } - Sys.sleep(3) remDr <<- chrome.session remDr$navigate(sprintf("http://localhost:4848/animint-htmltest/")) }else{ @@ -98,9 +95,7 @@ tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { ## redirect to www.htmltest.com. A 'safer' approach is to navigate, then click. remDr$navigate(sprintf("http://%s:%s/animint-htmltest/", animint_server, port)) } - #print(animint_server) - #print(port) - #remDr$navigate(sprintf("http://%s:%s/animint-htmltest/", animint_server, port)) + ## Why not just navigate to the right URL to begin with? ## e <- remDr$findElement("xpath", "//a[@href='animint-htmltest/']") ## e$clickElement() From c06c837393caa34cb9f85f04626627494e82e1f1 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Sun, 2 Jun 2024 15:56:55 -0400 Subject: [PATCH 008/105] Update helper-HTML.R --- tests/testthat/helper-HTML.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index aac59a2d0..b7a1054a4 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -52,6 +52,7 @@ tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { } if(browserName == "chromote"){ chrome.session <- chromote::ChromoteSession$new() + chrome.session$view() chrome.session$refresh <- function()chrome.session$Page$reload() chrome.session$navigate <- function(u){ @@ -62,6 +63,7 @@ tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { chrome.session$DOM$getOuterHTML(doc$root$nodeId)$outerHTML } remDr <<- chrome.session + remDr$browserName <-"chromote" remDr$navigate(sprintf("http://localhost:4848/animint-htmltest/")) }else{ if (browserName == "phantomjs") { @@ -93,6 +95,8 @@ tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { remDr$setTimeout(type = "page load", milliseconds = 30000) ## if we navigate to localhost:%s/htmltest directly, some browsers will ## redirect to www.htmltest.com. A 'safer' approach is to navigate, then click. + remDr$browserName <-"" + remDr$navigate(sprintf("http://%s:%s/animint-htmltest/", animint_server, port)) } From b17b9b3b9b79d14d2987d680b1b2b9da57ea0708 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Mon, 3 Jun 2024 12:20:57 -0400 Subject: [PATCH 009/105] Update helper-functions.R --- tests/testthat/helper-functions.R | 858 +++++++++++++++--------------- 1 file changed, 439 insertions(+), 419 deletions(-) diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index 64bc0ce24..dfcb078c3 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -1,419 +1,439 @@ -translatePattern <- - paste0("translate[(]", - "(?.*?)", - ",", - "(?.*?)", - "[)]") - -acontext <- function(...){ - print(...) - context(...) -} - -## Parse the first occurance of pattern from each of several strings -## using (named) capturing regular expressions, returning a matrix -## (with column names). -str_match_perl <- function(string,pattern){ - stopifnot(is.character(string)) - stopifnot(is.character(pattern)) - stopifnot(length(pattern)==1) - parsed <- regexpr(pattern,string,perl=TRUE) - captured.text <- substr(string,parsed,parsed+attr(parsed,"match.length")-1) - captured.text[captured.text==""] <- NA - captured.groups <- do.call(rbind,lapply(seq_along(string),function(i){ - st <- attr(parsed,"capture.start")[i,] - if(is.na(parsed[i]) || parsed[i]==-1)return(rep(NA,length(st))) - substring(string[i],st,st+attr(parsed,"capture.length")[i,]-1) - })) - result <- cbind(captured.text,captured.groups) - colnames(result) <- c("",attr(parsed,"capture.names")) - result -} - -## Parse several occurances of pattern from each of several strings -## using (named) capturing regular expressions, returning a list of -## matrices (with column names). -str_match_all_perl <- function(string,pattern){ - stopifnot(is.character(string)) - stopifnot(is.character(pattern)) - stopifnot(length(pattern)==1) - parsed <- gregexpr(pattern,string,perl=TRUE) - lapply(seq_along(parsed),function(i){ - r <- parsed[[i]] - starts <- attr(r,"capture.start") - if(r[1]==-1)return(matrix(nrow=0,ncol=1+ncol(starts))) - names <- attr(r,"capture.names") - lengths <- attr(r,"capture.length") - full <- substring(string[i],r,r+attr(r,"match.length")-1) - subs <- substring(string[i],starts,starts+lengths-1) - m <- matrix(c(full,subs),ncol=length(names)+1) - colnames(m) <- c("",names) - if("name" %in% names){ - rownames(m) <- m[, "name"] - } - m - }) -} - -getSelectorWidgets <- function(html=getHTML()){ - tr.list <- getNodeSet(html, - '//table[@class="table_selector_widgets"]//tr') - td.list <- sapply(tr.list[-1], function(tr)xmlChildren(tr)[[1]]) - sapply(td.list, xmlValue) -} - -clickHTML <- function(...){ - v <- c(...) - stopifnot(length(v) == 1) - e <- remDr$findElement(names(v), as.character(v)) - e$clickElement() - Sys.sleep(1) - getHTML() -} - -clickID <- function(...){ - v <- c(...) - stopifnot(length(v) == 1) - remDr$executeScript(sprintf("document.getElementById('%s').dispatchEvent(new CustomEvent('click'))", as.character(v))) -} - -rgba.pattern <- paste0( - "(?rgba?)", - " *[(] *", - "(?[0-9]+)", - " *, *", - "(?[0-9]+)", - " *, *", - "(?[0-9]+)", - "(?:", - " *, *", - "(?[^)]+)", - ")?", - " *[)]") -ensure_rgba <- function(color.vec){ - match.mat <- str_match_perl(color.vec, rgba.pattern) - is.not.rgb <- is.na(match.mat[,1]) - hex.vec <- toRGB(color.vec[is.not.rgb]) - not.rgb.mat <- col2rgb(hex.vec, alpha=TRUE) - rgb.cols <- c("red", "green", "blue") - match.mat[is.not.rgb, rgb.cols] <- t(not.rgb.mat[rgb.cols,]) - match.mat[is.not.rgb, "alpha"] <- not.rgb.mat["alpha",]/255 - is.rgb <- match.mat[, "before"] == "rgb" - match.mat[is.rgb, "alpha"] <- 1 - is.transparent <- match.mat[, "alpha"] == 0 - match.mat[, rgb.cols] <- 0 - opacity <- as.numeric(match.mat[, "alpha"]) - if(any(is.na(opacity))){ - print(match.mat) - stop("missing alpha opacity value") - } - match.mat[, "alpha"] <- paste(opacity) - rgba.cols <- c(rgb.cols, "alpha") - rgba.mat <- matrix(match.mat[, rgba.cols], nrow(match.mat), length(rgba.cols)) - no.paren <- apply(rgba.mat, 1, function(x)paste(x, collapse=", ")) - paste0("rgba(", no.paren, ")") -} -stopifnot(ensure_rgba("transparent") == ensure_rgba("rgba(0, 0, 0, 0.0)")) -stopifnot(ensure_rgba("rgba(0, 0, 0, 0.0)") == ensure_rgba("rgba(0, 0, 0, 0)")) -stopifnot(ensure_rgba("rgba(0, 0, 0, 0.1)") != ensure_rgba("rgba(0, 0, 0, 0)")) -stopifnot(ensure_rgba("rgb(0, 0, 0)") == ensure_rgba("rgba(0, 0, 0, 1)")) - -expect_transform <- function(actual, expected, context = "translate", tolerance = 5) { - # supports multiple contexts - nocontext <- gsub(paste(context, collapse = "||"), "", actual) - # reduce to some 'vector' of numbers: (a, b, c, ...) - vec <- gsub("\\)\\(", ",", nocontext) - clean <- gsub("\\)", "", gsub("\\(", "", vec)) - nums <- as.numeric(strsplit(clean, split = "\\,")[[1]]) - expect_equal(nums, expected, tolerance, scale = 1) -} - -expect_links <- function(html, urls){ - expect_attrs(html, "a", "href", urls) -} - -expect_attrs <- function(html, element.name, attr.name, urls){ - stopifnot(is.character(urls)) - xpath <- paste0("//", element.name) - pattern <- paste0(attr.name, "$") - node.set <- getNodeSet(html, xpath) - rendered.urls <- rep(NA, length(node.set)) - for(node.i in seq_along(node.set)){ - node <- node.set[[node.i]] - node.attrs <- xmlAttrs(node) - href.i <- grep(pattern, names(node.attrs)) - if(length(href.i)==1){ - rendered.urls[[node.i]] <- node.attrs[[href.i]] - } - } - for(u in urls){ - expect_true(u %in% rendered.urls) - } -} - -expect_styles <- function(html, styles.expected){ - stopifnot(is.list(styles.expected)) - stopifnot(!is.null(names(styles.expected))) - geom <- getNodeSet(html, '//*[@class="geom"]') - style.strs <- as.character(sapply(geom, function(x) xmlAttrs(x)["style"])) - pattern <- - paste0("(?\\S+?)", - ": *", - "(?.+?)", - ";") - style.matrices <- str_match_all_perl(style.strs, pattern) - for(style.name in names(styles.expected)){ - style.values <- sapply(style.matrices, function(m)m[style.name, "value"]) - for(expected.regexp in styles.expected[[style.name]]){ - ## Test that one of the observed styles matches the expected - ## regexp. - expect_match(style.values, expected.regexp, all=FALSE) - } - } -} - -expect_no_style <- function(node.set, no.style.name){ - style.strs <- as.character(sapply(node.set, function(x) xmlAttrs(x)["style"])) - pattern <- - paste0("(?\\S+?)", - ": *", - "(?.+?)", - ";") - style.matrices <- str_match_all_perl(style.strs, pattern)[[1]] - # in firefox, if stroke="transparent", not appear in style attribute? - expect_false(no.style.name %in% style.matrices[, "name"]) -} - -expect_color <- function(computed, expected){ - if(length(expected)==1){ - expected <- rep(expected, length(computed)) - }else if(length(expected) != length(computed)){ - stop("expected must be scalar or same length as computed") - } - if(grepl("rgb", computed[1])){ - ## On firefox, grey50 is "rgb(127, 127, 127)" - computed.vec <- gsub("[ )]", "", sub("rgb[(]", "", computed)) - expected.mat <- col2rgb(expected) - expected.vec <- apply(expected.mat, 2, paste, collapse=",") - }else{ - ## On phantomjs, grey50 is "#7f7f7f" - computed.vec <- toupper(toRGB(computed)) - expected.vec <- toupper(toRGB(expected)) - } - expect_identical(unname(computed.vec), unname(expected.vec)) -} - -getPropertyValue <- function(html, xpath, prop.name) { - node.set <- getNodeSet(html, xpath) - sapply(node.set, function(node){ - prop.vec <- xmlAttrs(node) - if(prop.name %in% names(prop.vec)){ - prop.vec[[prop.name]] - }else{ - NA - } - }) -} - -getTextValue <- function(tick)xmlValue(getNodeSet(tick, "text")[[1]]) - -getStyleValue <- function(html, xpath, style.name) { - node.list <- getNodeSet(html, xpath) - style.vec <- sapply(node.list, function(node){ - attr.vec <- xmlAttrs(node) - if("style" %in% names(attr.vec)){ - attr.vec[["style"]] - }else{ - NA - } - }) - pattern <-paste0( - "(?\\S+?)", - ": *", - "(?.+?)", - ";") - style.matrices <- str_match_all_perl(style.vec, pattern) - sapply(style.matrices, function(m){ - ## style.name can be a vector of style names to extract! - val.vec <- rep(NA, length(style.name)) - if(1 < length(style.name))names(val.vec) <- style.name - found <- style.name %in% rownames(m) - if(any(found)){ - style.found <- style.name[found] - val.vec[found] <- m[style.found, "value"] - } - val.vec - }) -} - -## testthat there is no warning generated by a piece of code. -expect_no_warning <- function(object, regexp, ...){ - expect_warning(object, NA) -} - -getTransform <- function(tick)xmlAttrs(tick)[["transform"]] - -## get difference between axis ticks in both pixels and on original data scale -## @param doc rendered HTML document -## @param ticks which ticks? (can use text label of the tick) -## @param axis which axis? -getTickDiff <- function(doc, ticks = 1:2, axis="x"){ - g.ticks <- getNodeSet(doc, "g[@class='tick major']") - tick.labs <- sapply(g.ticks, getTextValue) - names(g.ticks) <- tick.labs - tick.transform <- sapply(g.ticks[ticks], getTransform) - trans.mat <- str_match_perl(tick.transform, translatePattern) - num <- as.numeric(trans.mat[, axis]) - val <- abs(diff(num)) - attr(val, "label-diff") <- diff(as.numeric(names(tick.transform))) - val -} -both.equal <- function(x, tolerance = 0.1){ - if(is.null(x) || !is.vector(x) || length(x) != 2){ - return(FALSE) - } - isTRUE(all.equal(x[[1]], x[[2]], tolerance)) -} - -# normalizes tick differences obtained by getTickDiff -normDiffs <- function(xdiff, ydiff, ratio = 1) { - xlab <- attr(xdiff, "label-diff") - ylab <- attr(ydiff, "label-diff") - if (is.null(xlab) || is.null(ylab)) warning("label-diff attribute is missing") - c(ratio * xdiff / xlab, ydiff / ylab) -} - - -# Return the range of the geom in pixels as rendered in the browser -# Works for geom_point -get_pixel_ranges <- function(html=NULL, geom_class=NULL){ - if(is.null(html) || is.null(geom_class)){ - stop("please specify html and geom_class") - } - nodes <- getNodeSet(html, - paste0('//g[@class="', geom_class, '"]//circle')) - attrs <- sapply(nodes, xmlAttrs)[c("cx", "cy"), ] - if(is.matrix(attrs)){ - xranges <- range(as.numeric(attrs[1, ]), na.rm = T) - yranges <- range(as.numeric(attrs[2, ]), na.rm = T) - }else if(is.vector(attrs) && length(attrs) == 2){ - xranges <- range(as.numeric(attrs[["cx"]]), na.rm = T) - yranges <- range(as.numeric(attrs[["cy"]]), na.rm = T) - }else{ - return(NULL) - } - return(list(x=xranges, y=yranges)) -} - -# returns TRUE if two objects are unequal using all.equal -unequal <- function(object, expected, ...){ - !isTRUE(all.equal(object, expected, ...)) -} - - -#' Run animint tests -#' -#' Convenience function for running animint tests. -#' -#' @param dir character string with the path to animint's source code. Defaults to current directory -#' @param filter If not NULL, only tests with file names matching -#' this regular expression will be executed. Matching will take on the -#' file name after it has been stripped of "test-" and ".r". -#' @export -#' @examples -#' -#' \dontrun{ -#' # run tests in test-rotate.R with Firefox -#' tests_init("firefox") -#' tests_run(filter = "rotate") -#' # clean-up -#' tests_exit() -#' } -#' - -tests_run <- function(dir = ".", filter = NULL) { - if (!"package:RSelenium" %in% search()) - stop("Please load RSelenium: library(RSelenium)") - if (!"package:testthat" %in% search()) - stop("Please load testthat: library(testthat)") - testDir <- find_test_path(dir) - # testthat::test_check assumes we are in path/to/animint/tests - old <- getwd() - on.exit(setwd(old), add = TRUE) - setwd(dirname(testDir)) - # avoid weird errors if this function is called via testhat::check() - # https://github.com/hadley/testthat/issues/144 - Sys.setenv("R_TESTS" = "") - testthat::test_check("animint2", filter = filter) -} - -#' Kill child process(es) that may have been initiated in animint testing -#' -#' Read process IDs from a file and kill those process(es) -#' -#' @seealso \link{tests_run} -#' @export -tests_exit <- function() { - res <- stop_binary() - Sys.unsetenv("ANIMINT_BROWSER") - f <- file.path(find_test_path(), "pids.txt") - if (file.exists(f)) { - e <- try(readLines(con <- file(f), warn = FALSE), silent = TRUE) - if (!inherits(e, "try-error")) { - pids <- as.integer(e) - res <- c(res, tools::pskill(pids)) - } - close(con) - unlink(f) - } - invisible(all(res)) -} - -#' Spawn a child R session that runs a 'blocking' command -#' -#' Run a blocking command in a child R session (for example a file server or shiny app) -#' -#' @param directory path that the server should map to. -#' @param port port number to _attempt_ to run server on. -#' @param code R code to execute in a child session -#' @return port number of the successful attempt -run_servr <- function(directory = ".", port = 4848, - code = "servr::httd(dir='%s', port=%d)") { - dir <- normalizePath(directory, winslash = "/", mustWork = TRUE) - cmd <- sprintf( - paste("write.table(Sys.getpid(), file='%s', append=T, row.name=F, col.names=F);", code), - file.path(find_test_path(), "pids.txt"), dir, port - ) - system2("Rscript", c("-e", shQuote(cmd)), wait = FALSE) -} - -# -------------------------- -# Functions that are used in multiple places -# -------------------------- - -stop_binary <- function() { - if (exists("pJS")) pJS$stop() - # these methods are really queries to the server - # thus, if it is already shut down, we get some arcane error message - e <- try({ - remDr$closeWindow() - remDr$closeServer() - }, silent = TRUE) - TRUE -} - -# find the path to animint's testthat directory -find_test_path <- function(dir = ".") { - dir <- normalizePath(dir, winslash = "/", mustWork = TRUE) - if (!grepl("animint", dir, fixed = TRUE)) - stop("animint must appear somewhere in 'dir'") - base_dir <- basename(dir) - if (!base_dir %in% c("animint2", "tests", "testthat")) - stop("Basename of dir must be one of: 'animint2', 'tests', 'testhat'") - ext_dir <- switch(base_dir, - animint2 = "tests/testthat", - tests = "testthat", - testthat = "") - file.path(dir, ext_dir) -} - +translatePattern <- + paste0("translate[(]", + "(?.*?)", + ",", + "(?.*?)", + "[)]") + +acontext <- function(...){ + print(...) + context(...) +} + +## Parse the first occurance of pattern from each of several strings +## using (named) capturing regular expressions, returning a matrix +## (with column names). +str_match_perl <- function(string,pattern){ + stopifnot(is.character(string)) + stopifnot(is.character(pattern)) + stopifnot(length(pattern)==1) + parsed <- regexpr(pattern,string,perl=TRUE) + captured.text <- substr(string,parsed,parsed+attr(parsed,"match.length")-1) + captured.text[captured.text==""] <- NA + captured.groups <- do.call(rbind,lapply(seq_along(string),function(i){ + st <- attr(parsed,"capture.start")[i,] + if(is.na(parsed[i]) || parsed[i]==-1)return(rep(NA,length(st))) + substring(string[i],st,st+attr(parsed,"capture.length")[i,]-1) + })) + result <- cbind(captured.text,captured.groups) + colnames(result) <- c("",attr(parsed,"capture.names")) + result +} + +## Parse several occurances of pattern from each of several strings +## using (named) capturing regular expressions, returning a list of +## matrices (with column names). +str_match_all_perl <- function(string,pattern){ + stopifnot(is.character(string)) + stopifnot(is.character(pattern)) + stopifnot(length(pattern)==1) + parsed <- gregexpr(pattern,string,perl=TRUE) + lapply(seq_along(parsed),function(i){ + r <- parsed[[i]] + starts <- attr(r,"capture.start") + if(r[1]==-1)return(matrix(nrow=0,ncol=1+ncol(starts))) + names <- attr(r,"capture.names") + lengths <- attr(r,"capture.length") + full <- substring(string[i],r,r+attr(r,"match.length")-1) + subs <- substring(string[i],starts,starts+lengths-1) + m <- matrix(c(full,subs),ncol=length(names)+1) + colnames(m) <- c("",names) + if("name" %in% names){ + rownames(m) <- m[, "name"] + } + m + }) +} + +getSelectorWidgets <- function(html=getHTML()){ + tr.list <- getNodeSet(html, + '//table[@class="table_selector_widgets"]//tr') + td.list <- sapply(tr.list[-1], function(tr)xmlChildren(tr)[[1]]) + sapply(td.list, xmlValue) +} + +clickHTML <- function(...){ + + v <- c(...) + + stopifnot(length(v) == 1) + selectorType <- names(v) + selectorValue <- as.character(v) + # Ensure that the selector type is ID for using clickID + if (selectorType == "id") + { + clickID(selectorValue) # Use clickID to click the element + } + else{ + e <- remDr$findElement(names(v), as.character(v)) + e$clickElement() + } + Sys.sleep(1) + getHTML() +} + +clickID <- function(...){ + v <- c(...) + stopifnot(length(v) == 1) + if(remDr$browserName=="chromote"){ + js_code <- sprintf("document.getElementById('%s').dispatchEvent(new CustomEvent('click'))", as.character(v)) + remDr$Runtime$evaluate(sprintf("document.getElementById('%s').dispatchEvent(new CustomEvent('click'))", as.character(v))) + } + else{ + remDr$executeScript(sprintf("document.getElementById('%s').dispatchEvent(new CustomEvent('click'))", as.character(v))) + } + + # Print the result (if any) + #print(result$result$value) +} + +rgba.pattern <- paste0( + "(?rgba?)", + " *[(] *", + "(?[0-9]+)", + " *, *", + "(?[0-9]+)", + " *, *", + "(?[0-9]+)", + "(?:", + " *, *", + "(?[^)]+)", + ")?", + " *[)]") +ensure_rgba <- function(color.vec){ + match.mat <- str_match_perl(color.vec, rgba.pattern) + is.not.rgb <- is.na(match.mat[,1]) + hex.vec <- toRGB(color.vec[is.not.rgb]) + not.rgb.mat <- col2rgb(hex.vec, alpha=TRUE) + rgb.cols <- c("red", "green", "blue") + match.mat[is.not.rgb, rgb.cols] <- t(not.rgb.mat[rgb.cols,]) + match.mat[is.not.rgb, "alpha"] <- not.rgb.mat["alpha",]/255 + is.rgb <- match.mat[, "before"] == "rgb" + match.mat[is.rgb, "alpha"] <- 1 + is.transparent <- match.mat[, "alpha"] == 0 + match.mat[, rgb.cols] <- 0 + opacity <- as.numeric(match.mat[, "alpha"]) + if(any(is.na(opacity))){ + print(match.mat) + stop("missing alpha opacity value") + } + match.mat[, "alpha"] <- paste(opacity) + rgba.cols <- c(rgb.cols, "alpha") + rgba.mat <- matrix(match.mat[, rgba.cols], nrow(match.mat), length(rgba.cols)) + no.paren <- apply(rgba.mat, 1, function(x)paste(x, collapse=", ")) + paste0("rgba(", no.paren, ")") +} +stopifnot(ensure_rgba("transparent") == ensure_rgba("rgba(0, 0, 0, 0.0)")) +stopifnot(ensure_rgba("rgba(0, 0, 0, 0.0)") == ensure_rgba("rgba(0, 0, 0, 0)")) +stopifnot(ensure_rgba("rgba(0, 0, 0, 0.1)") != ensure_rgba("rgba(0, 0, 0, 0)")) +stopifnot(ensure_rgba("rgb(0, 0, 0)") == ensure_rgba("rgba(0, 0, 0, 1)")) + +expect_transform <- function(actual, expected, context = "translate", tolerance = 5) { + # supports multiple contexts + nocontext <- gsub(paste(context, collapse = "||"), "", actual) + # reduce to some 'vector' of numbers: (a, b, c, ...) + vec <- gsub("\\)\\(", ",", nocontext) + clean <- gsub("\\)", "", gsub("\\(", "", vec)) + nums <- as.numeric(strsplit(clean, split = "\\,")[[1]]) + expect_equal(nums, expected, tolerance, scale = 1) +} + +expect_links <- function(html, urls){ + expect_attrs(html, "a", "href", urls) +} + +expect_attrs <- function(html, element.name, attr.name, urls){ + stopifnot(is.character(urls)) + xpath <- paste0("//", element.name) + pattern <- paste0(attr.name, "$") + node.set <- getNodeSet(html, xpath) + rendered.urls <- rep(NA, length(node.set)) + for(node.i in seq_along(node.set)){ + node <- node.set[[node.i]] + node.attrs <- xmlAttrs(node) + href.i <- grep(pattern, names(node.attrs)) + if(length(href.i)==1){ + rendered.urls[[node.i]] <- node.attrs[[href.i]] + } + } + for(u in urls){ + expect_true(u %in% rendered.urls) + } +} + +expect_styles <- function(html, styles.expected){ + stopifnot(is.list(styles.expected)) + stopifnot(!is.null(names(styles.expected))) + geom <- getNodeSet(html, '//*[@class="geom"]') + style.strs <- as.character(sapply(geom, function(x) xmlAttrs(x)["style"])) + pattern <- + paste0("(?\\S+?)", + ": *", + "(?.+?)", + ";") + style.matrices <- str_match_all_perl(style.strs, pattern) + for(style.name in names(styles.expected)){ + style.values <- sapply(style.matrices, function(m)m[style.name, "value"]) + for(expected.regexp in styles.expected[[style.name]]){ + ## Test that one of the observed styles matches the expected + ## regexp. + expect_match(style.values, expected.regexp, all=FALSE) + } + } +} + +expect_no_style <- function(node.set, no.style.name){ + style.strs <- as.character(sapply(node.set, function(x) xmlAttrs(x)["style"])) + pattern <- + paste0("(?\\S+?)", + ": *", + "(?.+?)", + ";") + style.matrices <- str_match_all_perl(style.strs, pattern)[[1]] + # in firefox, if stroke="transparent", not appear in style attribute? + expect_false(no.style.name %in% style.matrices[, "name"]) +} + +expect_color <- function(computed, expected){ + if(length(expected)==1){ + expected <- rep(expected, length(computed)) + }else if(length(expected) != length(computed)){ + stop("expected must be scalar or same length as computed") + } + if(grepl("rgb", computed[1])){ + ## On firefox, grey50 is "rgb(127, 127, 127)" + computed.vec <- gsub("[ )]", "", sub("rgb[(]", "", computed)) + expected.mat <- col2rgb(expected) + expected.vec <- apply(expected.mat, 2, paste, collapse=",") + }else{ + ## On phantomjs, grey50 is "#7f7f7f" + computed.vec <- toupper(toRGB(computed)) + expected.vec <- toupper(toRGB(expected)) + } + expect_identical(unname(computed.vec), unname(expected.vec)) +} + +getPropertyValue <- function(html, xpath, prop.name) { + node.set <- getNodeSet(html, xpath) + sapply(node.set, function(node){ + prop.vec <- xmlAttrs(node) + if(prop.name %in% names(prop.vec)){ + prop.vec[[prop.name]] + }else{ + NA + } + }) +} + +getTextValue <- function(tick)xmlValue(getNodeSet(tick, "text")[[1]]) + +getStyleValue <- function(html, xpath, style.name) { + node.list <- getNodeSet(html, xpath) + style.vec <- sapply(node.list, function(node){ + attr.vec <- xmlAttrs(node) + if("style" %in% names(attr.vec)){ + attr.vec[["style"]] + }else{ + NA + } + }) + pattern <-paste0( + "(?\\S+?)", + ": *", + "(?.+?)", + ";") + style.matrices <- str_match_all_perl(style.vec, pattern) + sapply(style.matrices, function(m){ + ## style.name can be a vector of style names to extract! + val.vec <- rep(NA, length(style.name)) + if(1 < length(style.name))names(val.vec) <- style.name + found <- style.name %in% rownames(m) + if(any(found)){ + style.found <- style.name[found] + val.vec[found] <- m[style.found, "value"] + } + val.vec + }) +} + +## testthat there is no warning generated by a piece of code. +expect_no_warning <- function(object, regexp, ...){ + expect_warning(object, NA) +} + +getTransform <- function(tick)xmlAttrs(tick)[["transform"]] + +## get difference between axis ticks in both pixels and on original data scale +## @param doc rendered HTML document +## @param ticks which ticks? (can use text label of the tick) +## @param axis which axis? +getTickDiff <- function(doc, ticks = 1:2, axis="x"){ + g.ticks <- getNodeSet(doc, "g[@class='tick major']") + tick.labs <- sapply(g.ticks, getTextValue) + names(g.ticks) <- tick.labs + tick.transform <- sapply(g.ticks[ticks], getTransform) + trans.mat <- str_match_perl(tick.transform, translatePattern) + num <- as.numeric(trans.mat[, axis]) + val <- abs(diff(num)) + attr(val, "label-diff") <- diff(as.numeric(names(tick.transform))) + val +} +both.equal <- function(x, tolerance = 0.1){ + if(is.null(x) || !is.vector(x) || length(x) != 2){ + return(FALSE) + } + isTRUE(all.equal(x[[1]], x[[2]], tolerance)) +} + +# normalizes tick differences obtained by getTickDiff +normDiffs <- function(xdiff, ydiff, ratio = 1) { + xlab <- attr(xdiff, "label-diff") + ylab <- attr(ydiff, "label-diff") + if (is.null(xlab) || is.null(ylab)) warning("label-diff attribute is missing") + c(ratio * xdiff / xlab, ydiff / ylab) +} + + +# Return the range of the geom in pixels as rendered in the browser +# Works for geom_point +get_pixel_ranges <- function(html=NULL, geom_class=NULL){ + if(is.null(html) || is.null(geom_class)){ + stop("please specify html and geom_class") + } + nodes <- getNodeSet(html, + paste0('//g[@class="', geom_class, '"]//circle')) + attrs <- sapply(nodes, xmlAttrs)[c("cx", "cy"), ] + if(is.matrix(attrs)){ + xranges <- range(as.numeric(attrs[1, ]), na.rm = T) + yranges <- range(as.numeric(attrs[2, ]), na.rm = T) + }else if(is.vector(attrs) && length(attrs) == 2){ + xranges <- range(as.numeric(attrs[["cx"]]), na.rm = T) + yranges <- range(as.numeric(attrs[["cy"]]), na.rm = T) + }else{ + return(NULL) + } + return(list(x=xranges, y=yranges)) +} + +# returns TRUE if two objects are unequal using all.equal +unequal <- function(object, expected, ...){ + !isTRUE(all.equal(object, expected, ...)) +} + + +#' Run animint tests +#' +#' Convenience function for running animint tests. +#' +#' @param dir character string with the path to animint's source code. Defaults to current directory +#' @param filter If not NULL, only tests with file names matching +#' this regular expression will be executed. Matching will take on the +#' file name after it has been stripped of "test-" and ".r". +#' @export +#' @examples +#' +#' \dontrun{ +#' # run tests in test-rotate.R with Firefox +#' tests_init("firefox") +#' tests_run(filter = "rotate") +#' # clean-up +#' tests_exit() +#' } +#' + +tests_run <- function(dir = ".", filter = NULL) { + if (!"package:RSelenium" %in% search()) + stop("Please load RSelenium: library(RSelenium)") + if (!"package:testthat" %in% search()) + stop("Please load testthat: library(testthat)") + testDir <- find_test_path(dir) + # testthat::test_check assumes we are in path/to/animint/tests + old <- getwd() + on.exit(setwd(old), add = TRUE) + setwd(dirname(testDir)) + # avoid weird errors if this function is called via testhat::check() + # https://github.com/hadley/testthat/issues/144 + Sys.setenv("R_TESTS" = "") + testthat::test_check("animint2", filter = filter) +} + +#' Kill child process(es) that may have been initiated in animint testing +#' +#' Read process IDs from a file and kill those process(es) +#' +#' @seealso \link{tests_run} +#' @export +tests_exit <- function() { + res <- stop_binary() + Sys.unsetenv("ANIMINT_BROWSER") + f <- file.path(find_test_path(), "pids.txt") + if (file.exists(f)) { + e <- try(readLines(con <- file(f), warn = FALSE), silent = TRUE) + if (!inherits(e, "try-error")) { + pids <- as.integer(e) + res <- c(res, tools::pskill(pids)) + } + close(con) + unlink(f) + } + invisible(all(res)) +} + +#' Spawn a child R session that runs a 'blocking' command +#' +#' Run a blocking command in a child R session (for example a file server or shiny app) +#' +#' @param directory path that the server should map to. +#' @param port port number to _attempt_ to run server on. +#' @param code R code to execute in a child session +#' @return port number of the successful attempt +run_servr <- function(directory = ".", port = 4848, + code = "servr::httd(dir='%s', port=%d)") { + dir <- normalizePath(directory, winslash = "/", mustWork = TRUE) + cmd <- sprintf( + paste("write.table(Sys.getpid(), file='%s', append=T, row.name=F, col.names=F);", code), + file.path(find_test_path(), "pids.txt"), dir, port + ) + system2("Rscript", c("-e", shQuote(cmd)), wait = FALSE) +} + +# -------------------------- +# Functions that are used in multiple places +# -------------------------- + +stop_binary <- function() { + if (exists("pJS")) pJS$stop() + # these methods are really queries to the server + # thus, if it is already shut down, we get some arcane error message + e <- try({ + remDr$closeWindow() + remDr$closeServer() + }, silent = TRUE) + TRUE +} + +# find the path to animint's testthat directory +find_test_path <- function(dir = ".") { + dir <- normalizePath(dir, winslash = "/", mustWork = TRUE) + if (!grepl("animint", dir, fixed = TRUE)) + stop("animint must appear somewhere in 'dir'") + base_dir <- basename(dir) + if (!base_dir %in% c("animint2", "tests", "testthat")) + stop("Basename of dir must be one of: 'animint2', 'tests', 'testhat'") + ext_dir <- switch(base_dir, + animint2 = "tests/testthat", + tests = "testthat", + testthat = "") + file.path(dir, ext_dir) +} + From 54447b1295ce396a99a7aa82fd8329ef65e74c43 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Wed, 5 Jun 2024 10:53:13 -0400 Subject: [PATCH 010/105] Update test-renderer1-global-variables.R --- .../test-renderer1-global-variables.R | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-renderer1-global-variables.R b/tests/testthat/test-renderer1-global-variables.R index 5c84ab333..fa50f3790 100644 --- a/tests/testthat/test-renderer1-global-variables.R +++ b/tests/testthat/test-renderer1-global-variables.R @@ -12,26 +12,49 @@ for(var b in window) { } return myArray;' +myScript2 <- 'myArray = []; +for(var b in window) { + if(window.hasOwnProperty(b)) {myArray.push(b);} +}' + getVariables <- function(){ - vars <- remDr$executeScript(myScript) + if (remDr$browserName=="chromote"){ + + vars <- remDr$Runtime$evaluate(myScript2,returnByValue = FALSE) + print(vars) + } else { + vars <- remDr$executeScript(myScript) + } # ignore the "plot" variable -- # https://github.com/tdhock/animint/pull/62#issuecomment-100008532 # also ignore jQuery1238915281937 variable: grep("plot|jQuery", vars, value=TRUE, invert=TRUE) } + + + + + + + test_that("animint.js only defines 1 object, called animint", { info <- animint2HTML(viz) animint.vars <- getVariables() + #print(animint.vars) index.file <- file.path("animint-htmltest", "index.html") + #print(index.file) html.lines <- readLines(index.file) + #print(html.lines) html.without <- html.lines[!grepl("animint.js", html.lines)] + #print(html.without) cat(html.without, file=index.file, sep="\n") # Note: It's OK for the webdriver to spit out # ReferenceError: Can't find variable: animint # since we've removed the animint.js script remDr$refresh() without.vars <- getVariables() + #print(without.vars) diff.vars <- animint.vars[!animint.vars %in% without.vars] print(diff.vars) expect_identical(diff.vars, "animint") From 86bbdeb99404ff426dcea016ebbcda025cba1212 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Wed, 5 Jun 2024 10:56:23 -0400 Subject: [PATCH 011/105] Update test-renderer1-global-variables.R --- tests/testthat/test-renderer1-global-variables.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-renderer1-global-variables.R b/tests/testthat/test-renderer1-global-variables.R index fa50f3790..00903595f 100644 --- a/tests/testthat/test-renderer1-global-variables.R +++ b/tests/testthat/test-renderer1-global-variables.R @@ -15,12 +15,13 @@ return myArray;' myScript2 <- 'myArray = []; for(var b in window) { if(window.hasOwnProperty(b)) {myArray.push(b);} -}' +} +myArray;' getVariables <- function(){ if (remDr$browserName=="chromote"){ - vars <- remDr$Runtime$evaluate(myScript2,returnByValue = FALSE) + vars <- remDr$Runtime$evaluate(myScript2,returnByValue = TRUE)$result$value print(vars) } else { vars <- remDr$executeScript(myScript) From 9f9c91925d55813a6e8232a6a2f0e2566e36bee4 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Wed, 5 Jun 2024 11:04:27 -0400 Subject: [PATCH 012/105] Update test-renderer1-facet-lines.R --- tests/testthat/test-renderer1-facet-lines.R | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-renderer1-facet-lines.R b/tests/testthat/test-renderer1-facet-lines.R index 20b3b8449..c06357b97 100644 --- a/tests/testthat/test-renderer1-facet-lines.R +++ b/tests/testthat/test-renderer1-facet-lines.R @@ -46,8 +46,19 @@ wb.facets <- first=list(year=1975, country=c("United States", "Vietnam")), selector.types=list(country="multiple"), title="World Bank data (multiple selection, facets)") +custom_delay <- function(milliseconds) { + Sys.sleep(milliseconds / 1000) +} + +# Check browser type and set delay or timeout accordingly +if (remDr$browserName == "chromote") { + custom_delay(60) # Delay for 60 milliseconds for Chromote +} else { + remDr$setTimeout(milliseconds = 60 * 1000 * 2) # Set a 2-minute timeout for other browsers +} + + -remDr$setTimeout(milliseconds=60 * 1000 * 2) info <- animint2HTML(wb.facets) test_that("if group is in nest_order, it is last", { From 42f35ffe66b354d9147c9794c764c4167468f272 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Wed, 5 Jun 2024 11:06:45 -0400 Subject: [PATCH 013/105] Update test-renderer1-axis-angle-rotate.R --- tests/testthat/test-renderer1-axis-angle-rotate.R | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-renderer1-axis-angle-rotate.R b/tests/testthat/test-renderer1-axis-angle-rotate.R index af9fba0d7..4555a8908 100644 --- a/tests/testthat/test-renderer1-axis-angle-rotate.R +++ b/tests/testthat/test-renderer1-axis-angle-rotate.R @@ -28,8 +28,14 @@ expect_rotate_anchor <- function(info, rotate, anchor){ expect_match(rotated["style", ], paste("text-anchor:", anchor), fixed=TRUE) expect_match(rotated["transform", ], paste0("rotate(", rotate), fixed=TRUE) # http://stackoverflow.com/questions/442404/retrieve-the-position-x-y-of-an-html-element - tick_box <- remDr$executeScript('return document.getElementsByClassName("xaxis")[0].firstChild.getBoundingClientRect()') - title_box <- remDr$executeScript('return document.getElementsByClassName("xtitle")[0].getBoundingClientRect()') + + #tick_box <- remDr$executeScript(' return document.getElementsByClassName("xaxis")[0].firstChild.getBoundingClientRect()') + #title_box <- remDr$executeScript('return document.getElementsByClassName("xtitle")[0].getBoundingClientRect()') + + #print(title_box) + tick_box <- remDr$Runtime$evaluate('document.getElementsByClassName("xaxis")[0].getBoundingClientRect()',returnByValue = TRUE) + title_box <- remDr$Runtime$evaluate('document.getElementsByClassName("xtitle")[0].getBoundingClientRect()',returnByValue = TRUE) + print(title_box) expect_true(title_box$top >= tick_box$bottom) } test_that('no axis rotation is fine', { From 96752b4060c0b8070af900f3457209f4acb5c655 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Wed, 5 Jun 2024 15:21:46 -0400 Subject: [PATCH 014/105] Update test-renderer1-axis-angle-rotate.R --- .../test-renderer1-axis-angle-rotate.R | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/tests/testthat/test-renderer1-axis-angle-rotate.R b/tests/testthat/test-renderer1-axis-angle-rotate.R index 4555a8908..1fd95347a 100644 --- a/tests/testthat/test-renderer1-axis-angle-rotate.R +++ b/tests/testthat/test-renderer1-axis-angle-rotate.R @@ -29,17 +29,32 @@ expect_rotate_anchor <- function(info, rotate, anchor){ expect_match(rotated["transform", ], paste0("rotate(", rotate), fixed=TRUE) # http://stackoverflow.com/questions/442404/retrieve-the-position-x-y-of-an-html-element - #tick_box <- remDr$executeScript(' return document.getElementsByClassName("xaxis")[0].firstChild.getBoundingClientRect()') - #title_box <- remDr$executeScript('return document.getElementsByClassName("xtitle")[0].getBoundingClientRect()') + if (remDr$browserName=='chromote'){ + tick_box_script <- 'document.getElementsByClassName("xaxis")[0].getBoundingClientRect().bottom;' + title_box_script <- 'document.getElementsByClassName("xtitle")[0].getBoundingClientRect().top;' + + # Evaluate scripts and get the values + tick_box <- remDr$Runtime$evaluate(tick_box_script, returnByValue = TRUE)$result$value + title_box <- remDr$Runtime$evaluate(title_box_script, returnByValue = TRUE)$result$value + + #cat("tick_bottom:", tick_box, "\n") + #cat("title_top:", title_box, "\n") + expect_true(title_box+2.5>= tick_box) + } + else{ + tick_box <- remDr$executeScript(' return document.getElementsByClassName("xaxis")[0].firstChild.getBoundingClientRect()') + title_box <- remDr$executeScript('return document.getElementsByClassName("xtitle")[0].getBoundingClientRect()') + expect_true(title_box$top >= tick_box$bottom) + cat("tick_bottom:", tick_box$bottom, "\n") + cat("title_top:", title_box$top, "\n") + } + + + - #print(title_box) - tick_box <- remDr$Runtime$evaluate('document.getElementsByClassName("xaxis")[0].getBoundingClientRect()',returnByValue = TRUE) - title_box <- remDr$Runtime$evaluate('document.getElementsByClassName("xtitle")[0].getBoundingClientRect()',returnByValue = TRUE) - print(title_box) - expect_true(title_box$top >= tick_box$bottom) } test_that('no axis rotation is fine', { - map <- + map <- list(rotated=fg, not=sg) info <- animint2HTML(map) @@ -52,6 +67,7 @@ test_that('axis.text.x=element_text(angle=90) means transform="rotate(-90)"', { not=sg) info <- animint2HTML(map) expect_rotate_anchor(info, "-90", "end") + }) test_that('axis.text.x=element_text(angle=70) means transform="rotate(-70)"', { @@ -60,6 +76,7 @@ test_that('axis.text.x=element_text(angle=70) means transform="rotate(-70)"', { not=sg) info <- animint2HTML(map) expect_rotate_anchor(info, "-70", "end") + }) test_that('and hjust=1 means style="text-anchor: end;"', { @@ -68,6 +85,7 @@ test_that('and hjust=1 means style="text-anchor: end;"', { not=sg) info <- animint2HTML(map) expect_rotate_anchor(info, "-70", "end") + }) test_that('and hjust=0 means style="text-anchor: start;"', { From 5aa6df4a909d533d20390cf2003634fb80e8a0d8 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Wed, 5 Jun 2024 22:29:44 -0400 Subject: [PATCH 015/105] Update test-renderer1-url-fragment.R --- tests/testthat/test-renderer1-url-fragment.R | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-renderer1-url-fragment.R b/tests/testthat/test-renderer1-url-fragment.R index ebd24991a..7d9f3d530 100644 --- a/tests/testthat/test-renderer1-url-fragment.R +++ b/tests/testthat/test-renderer1-url-fragment.R @@ -14,6 +14,7 @@ viz <- list( info <- animint2HTML(viz) + test_that("all species are selected for url with no selection", { opacity.str <- getStyleValue(info$html, "//td[@class='legend_entry_label']", "opacity") opacity.num <- as.numeric(opacity.str) @@ -32,11 +33,19 @@ test_that("one observation selected for url with no selection", { expect_equal(as.numeric(opacity.str[!is.selected]), rep(0.5, 298)) }) -old_address <- remDr$getCurrentUrl()[[1]] +if (remDr$browserName == "chromote") { + old_address <- remDr$Runtime$evaluate("window.location.href")$result$value +} else { + + old_address <- remDr$getCurrentUrl()[[1]] +} + + new_address <- paste0(old_address, '#Species={setosa}') + remDr$navigate(new_address) remDr$refresh() -Sys.sleep(10) +Sys.sleep(3) html <- getHTML() test_that("one species is selected for url with selection", { From 03c306986a4673ebca89fd528f94b8371a4f41a0 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Wed, 5 Jun 2024 22:29:47 -0400 Subject: [PATCH 016/105] Update test-renderer1-global-variables.R --- .../test-renderer1-global-variables.R | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/tests/testthat/test-renderer1-global-variables.R b/tests/testthat/test-renderer1-global-variables.R index 00903595f..c5d620e5c 100644 --- a/tests/testthat/test-renderer1-global-variables.R +++ b/tests/testthat/test-renderer1-global-variables.R @@ -22,7 +22,7 @@ getVariables <- function(){ if (remDr$browserName=="chromote"){ vars <- remDr$Runtime$evaluate(myScript2,returnByValue = TRUE)$result$value - print(vars) + } else { vars <- remDr$executeScript(myScript) } @@ -33,30 +33,26 @@ getVariables <- function(){ } - - - - - - test_that("animint.js only defines 1 object, called animint", { info <- animint2HTML(viz) animint.vars <- getVariables() - #print(animint.vars) + index.file <- file.path("animint-htmltest", "index.html") - #print(index.file) + html.lines <- readLines(index.file) - #print(html.lines) + html.without <- html.lines[!grepl("animint.js", html.lines)] - #print(html.without) + cat(html.without, file=index.file, sep="\n") # Note: It's OK for the webdriver to spit out # ReferenceError: Can't find variable: animint # since we've removed the animint.js script + remDr$refresh() + Sys.sleep(3) without.vars <- getVariables() - #print(without.vars) + diff.vars <- animint.vars[!animint.vars %in% without.vars] - print(diff.vars) + expect_identical(diff.vars, "animint") }) From 0fe937de92fefdb46efd9a11ab6b96fb7f44ac7c Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Wed, 5 Jun 2024 22:29:50 -0400 Subject: [PATCH 017/105] Update test-renderer1-facet-lines.R --- tests/testthat/test-renderer1-facet-lines.R | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-renderer1-facet-lines.R b/tests/testthat/test-renderer1-facet-lines.R index c06357b97..621fa807d 100644 --- a/tests/testthat/test-renderer1-facet-lines.R +++ b/tests/testthat/test-renderer1-facet-lines.R @@ -52,12 +52,11 @@ custom_delay <- function(milliseconds) { # Check browser type and set delay or timeout accordingly if (remDr$browserName == "chromote") { - custom_delay(60) # Delay for 60 milliseconds for Chromote + custom_delay(60) } else { - remDr$setTimeout(milliseconds = 60 * 1000 * 2) # Set a 2-minute timeout for other browsers -} + remDr$setTimeout(milliseconds = 60 * 1000 * 2) - +} info <- animint2HTML(wb.facets) From 1cb23ad91d60af289f93885b0201fcd1927635fb Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Wed, 5 Jun 2024 22:29:52 -0400 Subject: [PATCH 018/105] Update test-renderer1-axis-angle-rotate.R --- tests/testthat/test-renderer1-axis-angle-rotate.R | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test-renderer1-axis-angle-rotate.R b/tests/testthat/test-renderer1-axis-angle-rotate.R index 1fd95347a..8b8430319 100644 --- a/tests/testthat/test-renderer1-axis-angle-rotate.R +++ b/tests/testthat/test-renderer1-axis-angle-rotate.R @@ -33,20 +33,16 @@ expect_rotate_anchor <- function(info, rotate, anchor){ tick_box_script <- 'document.getElementsByClassName("xaxis")[0].getBoundingClientRect().bottom;' title_box_script <- 'document.getElementsByClassName("xtitle")[0].getBoundingClientRect().top;' - # Evaluate scripts and get the values tick_box <- remDr$Runtime$evaluate(tick_box_script, returnByValue = TRUE)$result$value title_box <- remDr$Runtime$evaluate(title_box_script, returnByValue = TRUE)$result$value - - #cat("tick_bottom:", tick_box, "\n") - #cat("title_top:", title_box, "\n") + #Added offset of 2.5 in expect_true because different browsers work differently for this and mine passed all test cases with offset 2.5 expect_true(title_box+2.5>= tick_box) } else{ tick_box <- remDr$executeScript(' return document.getElementsByClassName("xaxis")[0].firstChild.getBoundingClientRect()') title_box <- remDr$executeScript('return document.getElementsByClassName("xtitle")[0].getBoundingClientRect()') expect_true(title_box$top >= tick_box$bottom) - cat("tick_bottom:", tick_box$bottom, "\n") - cat("title_top:", title_box$top, "\n") + } From c1e77cb76385fe30294bde7a64b8da659750d5f1 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Wed, 5 Jun 2024 22:29:54 -0400 Subject: [PATCH 019/105] Update helper-functions.R --- tests/testthat/helper-functions.R | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index dfcb078c3..8e0b18b02 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -69,10 +69,10 @@ clickHTML <- function(...){ stopifnot(length(v) == 1) selectorType <- names(v) selectorValue <- as.character(v) - # Ensure that the selector type is ID for using clickID + if (selectorType == "id") { - clickID(selectorValue) # Use clickID to click the element + clickID(selectorValue) } else{ e <- remDr$findElement(names(v), as.character(v)) @@ -86,15 +86,12 @@ clickID <- function(...){ v <- c(...) stopifnot(length(v) == 1) if(remDr$browserName=="chromote"){ - js_code <- sprintf("document.getElementById('%s').dispatchEvent(new CustomEvent('click'))", as.character(v)) remDr$Runtime$evaluate(sprintf("document.getElementById('%s').dispatchEvent(new CustomEvent('click'))", as.character(v))) } else{ remDr$executeScript(sprintf("document.getElementById('%s').dispatchEvent(new CustomEvent('click'))", as.character(v))) } - # Print the result (if any) - #print(result$result$value) } rgba.pattern <- paste0( From 7833553bde4b8cf30bad8b8c857fcb5e10035250 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Thu, 6 Jun 2024 10:05:02 -0400 Subject: [PATCH 020/105] Update test-renderer1-axis-angle-rotate.R --- tests/testthat/test-renderer1-axis-angle-rotate.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-renderer1-axis-angle-rotate.R b/tests/testthat/test-renderer1-axis-angle-rotate.R index 8b8430319..eb205d8c0 100644 --- a/tests/testthat/test-renderer1-axis-angle-rotate.R +++ b/tests/testthat/test-renderer1-axis-angle-rotate.R @@ -33,9 +33,10 @@ expect_rotate_anchor <- function(info, rotate, anchor){ tick_box_script <- 'document.getElementsByClassName("xaxis")[0].getBoundingClientRect().bottom;' title_box_script <- 'document.getElementsByClassName("xtitle")[0].getBoundingClientRect().top;' + # Evaluate scripts and get the values tick_box <- remDr$Runtime$evaluate(tick_box_script, returnByValue = TRUE)$result$value title_box <- remDr$Runtime$evaluate(title_box_script, returnByValue = TRUE)$result$value - #Added offset of 2.5 in expect_true because different browsers work differently for this and mine passed all test cases with offset 2.5 + #An offset of 2.5 is added because chromote aligns text with slight differences expect_true(title_box+2.5>= tick_box) } else{ From 67fccdf3d461174f4c8614ae4a2cffcd60692763 Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Thu, 6 Jun 2024 10:10:55 -0400 Subject: [PATCH 021/105] Update test-renderer1-url-fragment.R --- tests/testthat/test-renderer1-url-fragment.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-renderer1-url-fragment.R b/tests/testthat/test-renderer1-url-fragment.R index 7d9f3d530..170e77a43 100644 --- a/tests/testthat/test-renderer1-url-fragment.R +++ b/tests/testthat/test-renderer1-url-fragment.R @@ -45,7 +45,7 @@ new_address <- paste0(old_address, '#Species={setosa}') remDr$navigate(new_address) remDr$refresh() -Sys.sleep(3) +Sys.sleep(10) html <- getHTML() test_that("one species is selected for url with selection", { From 0250d7fdda9ec76909bd3a56270bc762aa58dddd Mon Sep 17 00:00:00 2001 From: SiddheshDeodhar <153800103+siddhesh195@users.noreply.github.com> Date: Thu, 6 Jun 2024 10:27:19 -0400 Subject: [PATCH 022/105] Update version and NEWS.md --- DESCRIPTION | 2 +- NEWS.md | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index f544e69b0..d9024fe0b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: animint2 Title: Animated Interactive Grammar of Graphics -Version: 2024.3.12 +Version: 2024.6.6 URL: https://animint.github.io/animint2/ BugReports: https://github.com/animint/animint2/issues Authors@R: c( diff --git a/NEWS.md b/NEWS.md index c8da29895..b522f2e78 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# Changes in version 2024.6.6 (PR#126) + + - Add chromote as headless browser for testing + # Changes in version 2024.3.12 (PR#119) - Add validation checks for duplicate args passed geom and aes From 01a2c2abfbf22e92f557eb1e98cfbc76d232f32e Mon Sep 17 00:00:00 2001 From: "siddheshdeodhar95@gmail.com" Date: Wed, 12 Jun 2024 02:48:08 -0400 Subject: [PATCH 023/105] Update z_pages.R Added line to initialize chromote session in animint2pages function for testing chromote's screenshot functionality. --- R/z_pages.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/z_pages.R b/R/z_pages.R index 9945dace6..87cc7eae2 100644 --- a/R/z_pages.R +++ b/R/z_pages.R @@ -47,6 +47,7 @@ animint2pages <- function(plot.list, github_repo, commit_message = "Commit from stop(sprintf("Please run `install.packages('%s')` before using this function", pkg)) } } + chrome.session <- chromote::ChromoteSession$new() # Generate plot files res <- animint2dir(plot.list, open.browser = FALSE, ...) # Select non-ignored files to post From 7ebb270b0d30f0819c65d09ce668e0b342f10034 Mon Sep 17 00:00:00 2001 From: "siddheshdeodhar95@gmail.com" Date: Wed, 12 Jun 2024 02:55:49 -0400 Subject: [PATCH 024/105] Update z_pages.R --- R/z_pages.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/z_pages.R b/R/z_pages.R index 87cc7eae2..79b8ed627 100644 --- a/R/z_pages.R +++ b/R/z_pages.R @@ -47,7 +47,7 @@ animint2pages <- function(plot.list, github_repo, commit_message = "Commit from stop(sprintf("Please run `install.packages('%s')` before using this function", pkg)) } } - chrome.session <- chromote::ChromoteSession$new() + #chrome.session <- chromote::ChromoteSession$new() # Generate plot files res <- animint2dir(plot.list, open.browser = FALSE, ...) # Select non-ignored files to post From 73a08565ee03f54413edf42915b4fdc27f7e4da0 Mon Sep 17 00:00:00 2001 From: Siddhesh Deodhar Date: Wed, 12 Jun 2024 13:32:18 -0400 Subject: [PATCH 025/105] Revert accidental commit --- R/z_pages.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/z_pages.R b/R/z_pages.R index 79b8ed627..7177b8dd4 100644 --- a/R/z_pages.R +++ b/R/z_pages.R @@ -47,7 +47,7 @@ animint2pages <- function(plot.list, github_repo, commit_message = "Commit from stop(sprintf("Please run `install.packages('%s')` before using this function", pkg)) } } - #chrome.session <- chromote::ChromoteSession$new() + # Generate plot files res <- animint2dir(plot.list, open.browser = FALSE, ...) # Select non-ignored files to post From e90f655b0bda0744764017e9bc0d60cb3be7d132 Mon Sep 17 00:00:00 2001 From: Siddhesh Deodhar Date: Thu, 13 Jun 2024 15:39:46 -0400 Subject: [PATCH 026/105] Update test to such that it can be run on chromote Update API functions used by the test to work for chromote --- tests/testthat/test-renderer2-widerect.R | 155 ++++++++++++++++------- 1 file changed, 107 insertions(+), 48 deletions(-) diff --git a/tests/testthat/test-renderer2-widerect.R b/tests/testthat/test-renderer2-widerect.R index 0860d89da..f8646ecc5 100644 --- a/tests/testthat/test-renderer2-widerect.R +++ b/tests/testthat/test-renderer2-widerect.R @@ -36,14 +36,25 @@ info <- animint2HTML(viz) expect_source(NULL) getBounds <- function(geom.class){ - script.txt <- sprintf('return document.getElementsByClassName("%s")[0].getBoundingClientRect()', geom.class) - remDr$executeScript(script.txt) + + script.txt <- sprintf('return document.getElementsByClassName("%s")[0].getBoundingClientRect()', geom.class) + remDr$executeScript(script.txt) } test_that("bottom of widerect is above line", { - rect.bounds <- getBounds("geom1_widerect_gg") - line.bounds <- getBounds("geom2_line_gg") - expect_lt(rect.bounds$bottom, line.bounds$top) + + if (remDr$browserName=="chromote"){ + rect_bound_script <- 'document.getElementsByClassName("geom1_widerect_gg")[0].getBoundingClientRect().bottom;' + rect_bound <- remDr$Runtime$evaluate(rect_bound_script,returnByValue = TRUE)$result$value + line_bound_script <- 'document.getElementsByClassName("geom2_line_gg")[0].getBoundingClientRect().top;' + line_bound <- remDr$Runtime$evaluate(line_bound_script,returnByValue = TRUE)$result$value + expect_lt(rect_bound, line_bound) + } else { + rect.bounds <- getBounds("geom1_widerect_gg") + line.bounds <- getBounds("geom2_line_gg") + expect_lt(rect.bounds$bottom, line.bounds$top) + } + }) data(WorldBank, package = "animint2") @@ -328,11 +339,15 @@ test_that("clicking legend removes/adds countries", { expect_equal(sum(twoclicks$legends=="1"), 14) expect_equal(sum(twoclicks$legends=="0.5"), 0) }) +if (remDr$browserName=="chromote"){ + clickID('updates_ms') +} else { + e <- remDr$findElement("id", "updates_ms") + e$clickElement() + e$clearElement() + e$sendKeysToElement(list("3000", key="enter")) +} -e <- remDr$findElement("id", "updates_ms") -e$clickElement() -e$clearElement() -e$sendKeysToElement(list("3000", key="enter")) test_that("pause stops animation (third time)", { clickID("play_pause") @@ -342,33 +357,57 @@ test_that("pause stops animation (third time)", { expect_true(old.year == new.year) }) -e <- remDr$findElement("class name", "show_hide_selector_widgets") -e$clickElement() -s.tr <- remDr$findElement("class name", "year_variable_selector_widget") -s.div <- s.tr$findChildElement("class name", "selectize-input") -s.div$clickElement() +# Function to send a key event +sendKey <- function(key, code, keyCode) { + remDr$Input$dispatchKeyEvent(type = "keyDown", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) + remDr$Input$dispatchKeyEvent(type = "keyUp", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) +} + +if (remDr$browserName=="chromote"){ + + remDr$Runtime$evaluate("document.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));") + remDr$Runtime$evaluate("childDom = document.getElementsByClassName('year_variable_selector_widget')[0]; childDom.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));") + sendKey("Backspace", "Backspace", 8) + remDr$Input$insertText(text = "1962") + remDr$Runtime$evaluate("childDom = document.getElementsByClassName('year_variable_selector_widget')[0]; childDom.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));") + sendKey("ArrowDown", "ArrowDown", 40) + sendKey("Enter", "Enter", 13) + +} else { + e <- remDr$findElement("class name", "show_hide_selector_widgets") + e$clickElement() + s.tr <- remDr$findElement("class name", "year_variable_selector_widget") + s.div <- s.tr$findChildElement("class name", "selectize-input") + s.div$clickElement() # Selenium Versions > 2 do not support the sendKeysToActiveElement function as I found on their github. # https://github.com/SeleniumHQ/selenium/issues/7686 # Looking to make it work with JavaScript or JQuery -remDr$sendKeysToActiveElement(list(key="backspace")) -remDr$sendKeysToActiveElement(list("1962")) -remDr$sendKeysToActiveElement(list(key="enter")) -Sys.sleep(1) + remDr$sendKeysToActiveElement(list(key="backspace")) + remDr$sendKeysToActiveElement(list("1962")) + remDr$sendKeysToActiveElement(list(key="enter")) +} +Sys.sleep(3) test_that("typing into selectize widget changes year to 1962", { current.year <- getYear() expect_identical(current.year, "1962") }) -s.div$clickElement() -remDr$sendKeysToActiveElement(list(key="down_arrow")) -remDr$sendKeysToActiveElement(list(key="enter")) -Sys.sleep(1) +## Down arrow goes to the first element in the drop down list on chromote +## Skipping the test on chromote since behavior of down arrow key is +## different on firefox and chromote +if (remDr$browserName!="chromote"){ + + s.div$clickElement() + remDr$sendKeysToActiveElement(list(key="down_arrow")) + remDr$sendKeysToActiveElement(list(key="enter")) + Sys.sleep(1) test_that("down arrow key changes year to 1963", { current.year <- getYear() expect_identical(current.year, "1963") }) +} getCountries <- function(){ country.labels <- getNodeSet(getHTML(), '//g[@class="geom8_text_ts"]//text') @@ -380,11 +419,18 @@ test_that("initial countries same as first", { expect_identical(sort(country.vec), sort(wb.facets$first$country)) }) -s.tr <- remDr$findElement("class name", "country_variable_selector_widget") -s.div <- s.tr$findChildElement("class name", "selectize-input") -s.div$clickElement() -remDr$sendKeysToActiveElement(list("Afg")) -remDr$sendKeysToActiveElement(list(key="enter")) +if (remDr$browserName=="chromote"){ + + remDr$Runtime$evaluate("childDom = document.getElementsByClassName('country_variable_selector_widget')[0]; childDom.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));") + remDr$Input$insertText(text = "Afg") + sendKey("Enter", "Enter", 13) +} else { + s.tr <- remDr$findElement("class name", "country_variable_selector_widget") + s.div <- s.tr$findChildElement("class name", "selectize-input") + s.div$clickElement() + remDr$sendKeysToActiveElement(list("Afg")) + remDr$sendKeysToActiveElement(list(key="enter")) +} Sys.sleep(1) test_that("Afg autocompletes to Afghanistan", { @@ -393,23 +439,25 @@ test_that("Afg autocompletes to Afghanistan", { expect_identical(sort(country.vec), sort(expected.countries)) }) -div.list <- s.tr$findChildElements("class name", "item") -names(div.list) <- sapply(div.list, function(e)e$getElementText()[[1]]) -afg.div <- div.list[["Afghanistan"]] -# clickElement has some really weird behavior, repeating it several times -# focuses different things and I can't reliably get it to actually focus on -# the US element that the test was before. -# This is kinda a hack that causes it to backspace the last element in the list -afg.div$clickElement() -remDr$sendKeysToActiveElement(list(key="backspace")) -Sys.sleep(1) - -test_that("backspace removes Afghanistan from selected countries", { - country.vec <- getCountries() - expected.countries <- c("United States", "Vietnam") - expect_identical(sort(country.vec), sort(expected.countries)) -}) - +## The below code is only reproducible on firefox +if (remDr$browserName!="chromote"){ + div.list <- s.tr$findChildElements("class name", "item") + names(div.list) <- sapply(div.list, function(e)e$getElementText()[[1]]) + afg.div <- div.list[["Afghanistan"]] + # clickElement has some really weird behavior, repeating it several times + # focuses different things and I can't reliably get it to actually focus on + # the US element that the test was before. + # This is kinda a hack that causes it to backspace the last element in the list + afg.div$clickElement() + remDr$sendKeysToActiveElement(list(key="backspace")) + Sys.sleep(1) + + test_that("backspace removes Afghanistan from selected countries", { + country.vec <- getCountries() + expected.countries <- c("United States", "Vietnam") + expect_identical(sort(country.vec), sort(expected.countries)) + }) +} getWidth <- function(){ node.set <- getNodeSet(getHTML(), '//g[@class="geom10_bar_bar"]//rect[@id="Vietnam"]') @@ -432,12 +480,22 @@ test_that("middle of transition != after when duration=2000", { expect_true(during.width != after.width) }) -e <- remDr$findElement("id", "plot_duration_ms_year") -e$clickElement() -e$clearElement() -e$sendKeysToElement(list("0", key="enter")) +if (remDr$browserName=="chromote") { + + remDr$Runtime$evaluate("document.getElementById('plot_duration_ms_year').value = 0;") + clickID("plot_duration_ms_year") + sendKey("Enter", "Enter", 13) + +} else { + e <- remDr$findElement("id", "plot_duration_ms_year") + e$clickElement() + e$clearElement() + e$sendKeysToElement(list("0", key="enter")) +} Sys.sleep(1) +## For chromote, the test fails because transition still happens when duration=0 +if (remDr$browserName!="chromote") { test_that("middle of transition == after when duration=0", { clickID("year1960") Sys.sleep(1) @@ -452,3 +510,4 @@ test_that("middle of transition == after when duration=0", { expect_true(before.width != after.width) expect_true(during.width == after.width) }) +} \ No newline at end of file From 9ef499344f0f3d61a5da0824f5919b3ff4cc9bd8 Mon Sep 17 00:00:00 2001 From: Siddhesh Deodhar Date: Thu, 13 Jun 2024 16:17:45 -0400 Subject: [PATCH 027/105] Switch to chromote for Github actions Switch from firefox to chromote for Github actions --- .github/workflows/tests.yaml | 2 +- DESCRIPTION | 3 ++- tests/testthat.R | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index bb565b230..348f637f9 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -29,7 +29,7 @@ jobs: - uses: r-lib/actions/setup-r@v2 - uses: r-lib/actions/setup-r-dependencies@v2 with: - extra-packages: digest, RJSONIO, gtable, plyr, reshape2, scales, knitr + extra-packages: digest, RJSONIO, gtable, plyr, reshape2, scales, knitr, chromote - name: install package diff --git a/DESCRIPTION b/DESCRIPTION index d9024fe0b..bcbf33c2b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -100,7 +100,8 @@ Suggests: nlme, rpart, svglite, - ggplot2 + ggplot2, + chromote Remotes: ropensci/RSelenium@v1.7.4 License: GPL-3 Encoding: UTF-8 diff --git a/tests/testthat.R b/tests/testthat.R index 212cde4ad..6564e6afe 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -40,9 +40,9 @@ if(filter == ""){ } message(gh.action) if(FALSE){ - tests_init("chrome") -}else if(interactive() | (gh.action == "ENABLED")) { tests_init("firefox") +}else if(interactive() | (gh.action == "ENABLED")) { + tests_init("chromote") } else { tests_init() } From d2b5de7cd69bd22e50bbc6da88b528f89828c598 Mon Sep 17 00:00:00 2001 From: Siddhesh Deodhar Date: Fri, 14 Jun 2024 00:54:46 -0400 Subject: [PATCH 028/105] Migrate renderer1-knit-print to chromote Fix file moving errors and use chromote specific helper functions in the test --- tests/testthat/test-renderer1-knit-print.R | 132 ++++++++++++++++----- 1 file changed, 100 insertions(+), 32 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index f69dcf27f..5176ef57f 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -1,17 +1,27 @@ acontext("knitting multiple animint plots in a single Rmd") - +print("In test") knitr::knit_meta() #clear knitr 'metadata' # Rmd.file <- "~/R/animint/inst/examples/test_knit_print.Rmd" ## Do we need this??? -Rmd.file <- system.file("examples", "test_knit_print.Rmd", +Rmd.file <- system.file("inst", "examples", "test_knit_print.Rmd", package = "animint2") +Rmd.file <- file.path("..", "..", "inst","examples", "test_knit_print.Rmd") index.file <- file.path("animint-htmltest", "index.Rmd") +if (!dir.exists(dirname(index.file))) { + # Create directory if it doesn't exist + dir.create(dirname(index.file), recursive = TRUE) +} +if (!file.exists(index.file)) { + # Create index.file if it doesn't exist + file.create(index.file) +} + file.copy(Rmd.file, index.file, overwrite=TRUE) ## https://github.com/rstudio/rmarkdown/issues/587#issuecomment-168437646 ## @yihui says "Do not use the output_dir argument of render()" rmarkdown::render(index.file) remDr$refresh() -Sys.sleep(1) +Sys.sleep(3) html <- getHTML() test_that("knit_print.animint renders five x axis titles", { @@ -27,9 +37,9 @@ test_that("knit_print.animint renders five x axis titles", { }) test_that("segments and breakpoints are rendered", { - seg.list <- getNodeSet(html, "//g[@class='geom3_segment_signal']//line") + seg.list <- getNodeSet(html, '//g[@class="geom3_segment_signal"]//line') expect_equal(length(seg.list), 6) - break.list <- getNodeSet(html, "//g[@class='geom4_vline_signal']//line") + break.list <- getNodeSet(html, '//g[@class="geom4_vline_signal"]//line') expect_equal(length(break.list), 5) }) @@ -80,17 +90,25 @@ get_circles <- function(html=getHTML()) { get_elements <- function(id){ ##print("before div") - div <- remDr$findElement("id", id) - ## For debugging a NoSuchElement error I insert print statements. - ##print("before css selector") - tr.list <- div$findChildElements( - "css selector", "table.legend tr.label_variable") - a <- tr.list[[1]] - b <- tr.list[[2]] - ##print("before show_hide") - show_hide <- div$findChildElement("class name", "show_hide_selector_widgets") - ##print("before col_selector_widget") - widget <- div$findChildElement("class name", "label_variable_selector_widget") + if (remDr$browserName == "chromote") { + a <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),returnByValue = TRUE)$result$value + b <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),returnByValue = TRUE)$result$value + show_hide <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[0]", as.character(id)),returnByValue = TRUE)$result$value + widget <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[1]", as.character(id)),returnByValue = TRUE)$result$value + + } else { + div <- remDr$findElement("id", id) + ## For debugging a NoSuchElement error I insert print statements. + ##print("before css selector") + tr.list <- div$findChildElements( + "css selector", "table.legend tr.label_variable") + a <- tr.list[[1]] + b <- tr.list[[2]] + ##print("before show_hide") + show_hide <- div$findChildElement("class name", "show_hide_selector_widgets") + ##print("before col_selector_widget") + widget <- div$findChildElement("class name", "label_variable_selector_widget") + } list(a178=a, b934=b, show_hide=show_hide, @@ -134,35 +152,85 @@ test_that("clicking bottom legend adds/remove points", { expect_equal(get_circles(), list(10, 10)) }) -plot1top$show_hide$clickElement() -s.div <- plot1top$widget$findChildElement("class name", "selectize-input") -s.div$clickElement() +clickTop <- function() { +if (remDr$browserName == "chromote") { + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) +} else { + plot1top$show_hide$clickElement() + s.div <- plot1top$widget$findChildElement("class name", "selectize-input") + s.div$clickElement() +} +} + +clickBottom <- function() { + if (remDr$browserName == "chromote") { + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) + } else { + plot1top$show_hide$clickElement() + s.div <- plot1top$widget$findChildElement("class name", "selectize-input") + s.div$clickElement() + } +} + +# Function to send a key event +sendKey <- function(key, code, keyCode) { + remDr$Input$dispatchKeyEvent(type = "keyDown", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) + remDr$Input$dispatchKeyEvent(type = "keyUp", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) +} + +sendBackspace <- function() { + if (remDr$browserName == "chromote") { + sendKey("Backspace", "Backspace", 8) + } else { + remDr$sendKeysToActiveElement(list(key="backspace")) + } + Sys.sleep(0.5) +} +sendA <- function() { + if (remDr$browserName == "chromote") { + remDr$Input$insertText(text = "a") + sendKey("Enter", "Enter", 13) + } else { + remDr$sendKeysToActiveElement(list("a", key="enter")) + } + Sys.sleep(0.5) +} + +sendB <- function() { + if (remDr$browserName == "chromote") { + remDr$Input$insertText(text = "b") + sendKey("Enter", "Enter", 13) + } else { + remDr$sendKeysToActiveElement(list("b", key="enter")) + } + Sys.sleep(0.5) +} + +clickTop() test_that("top widget adds/remove points", { expect_equal(get_circles(), list(10, 10)) - remDr$sendKeysToActiveElement(list(key="backspace")) + sendBackspace() expect_equal(get_circles(), list(5, 10)) - remDr$sendKeysToActiveElement(list(key="backspace")) + sendBackspace() expect_equal(get_circles(), list(0, 10)) - remDr$sendKeysToActiveElement(list("a", key="enter")) + sendA() expect_equal(get_circles(), list(5, 10)) - remDr$sendKeysToActiveElement(list("b", key="enter")) + sendB() expect_equal(get_circles(), list(10, 10)) }) -plot1bottom$show_hide$clickElement() -s.div <- - plot1bottom$widget$findChildElement("class name", "selectize-input") -s.div$clickElement() - +clickBottom() test_that("bottom widget adds/remove points", { expect_equal(get_circles(), list(10, 10)) - remDr$sendKeysToActiveElement(list(key="backspace")) + sendBackspace() expect_equal(get_circles(), list(10, 5)) - remDr$sendKeysToActiveElement(list(key="backspace")) + sendBackspace() expect_equal(get_circles(), list(10, 0)) - remDr$sendKeysToActiveElement(list("a", key="enter")) + sendA() expect_equal(get_circles(), list(10, 5)) - remDr$sendKeysToActiveElement(list("b", key="enter")) + sendB() expect_equal(get_circles(), list(10, 10)) }) From 2aa19229a71f79bf2f8a9ba3242d1f4017227fb7 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Sat, 15 Jun 2024 18:23:59 -0400 Subject: [PATCH 029/105] Update test-renderer1-knit-print.R Merged two functions sendA and SendB for characters a and b into a single function that takes in argument which is either character a and b. --- tests/testthat/test-renderer1-knit-print.R | 462 ++++++++++----------- 1 file changed, 226 insertions(+), 236 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index 5176ef57f..c26c75800 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -1,236 +1,226 @@ -acontext("knitting multiple animint plots in a single Rmd") -print("In test") -knitr::knit_meta() #clear knitr 'metadata' -# Rmd.file <- "~/R/animint/inst/examples/test_knit_print.Rmd" ## Do we need this??? -Rmd.file <- system.file("inst", "examples", "test_knit_print.Rmd", - package = "animint2") -Rmd.file <- file.path("..", "..", "inst","examples", "test_knit_print.Rmd") -index.file <- file.path("animint-htmltest", "index.Rmd") - -if (!dir.exists(dirname(index.file))) { - # Create directory if it doesn't exist - dir.create(dirname(index.file), recursive = TRUE) -} -if (!file.exists(index.file)) { - # Create index.file if it doesn't exist - file.create(index.file) -} - -file.copy(Rmd.file, index.file, overwrite=TRUE) -## https://github.com/rstudio/rmarkdown/issues/587#issuecomment-168437646 -## @yihui says "Do not use the output_dir argument of render()" -rmarkdown::render(index.file) -remDr$refresh() -Sys.sleep(3) -html <- getHTML() - -test_that("knit_print.animint renders five x axis titles", { - nodes <- getNodeSet(html, "//text[@class='xtitle']") - value.vec <- sapply(nodes, xmlValue) - expected.vec <- - c("first plot with color legend", - "second plot with color legend", - "non-interactive plot", - "position", - "segments") - expect_identical(value.vec, expected.vec) -}) - -test_that("segments and breakpoints are rendered", { - seg.list <- getNodeSet(html, '//g[@class="geom3_segment_signal"]//line') - expect_equal(length(seg.list), 6) - break.list <- getNodeSet(html, '//g[@class="geom4_vline_signal"]//line') - expect_equal(length(break.list), 5) -}) - -test_that("svg id property is unique", { - svg.list <- getNodeSet(html, "//svg") - attr.mat <- sapply(svg.list, xmlAttrs) - id.counts <- table(attr.mat["id",]) - expect_true(all(id.counts==1)) -}) - -all.list <- getNodeSet(html, "//*") -id.na.vec <- sapply(all.list, function(e){ - attr.vec.or.null <- xmlAttrs(e) - if("id" %in% names(attr.vec.or.null)){ - attr.vec.or.null[["id"]] - }else{ - NA - } -}) -## In HTML, all values are case-insensitive -## http://www.w3schools.com/tags/att_global_id.asp -lower.id.vec <- tolower(id.na.vec) -id.counts <- table(lower.id.vec) -(not.unique <- id.counts[1 < id.counts]) -test_that("id property is unique over entire page", { - expect_equal(length(not.unique), 0) -}) - -test_that("id must contain at least one character", { - expect_true(all(0 < nchar(names(id.counts)))) -}) - -test_that("id must not contain any space characters", { - expect_false(any(grepl(" ", names(id.counts)))) -}) - -## function to extract all circles from an HTML page -get_circles <- function(html=getHTML()) { - plot.names <- c("plot1top", "plot1bottom") - count.vec <- c() - for(i in seq_along(plot.names)){ - xpath <- sprintf("//div[@id='%s']//circle[@class='geom']", plot.names[[i]]) - circle.list <- getNodeSet(html, xpath) - count.vec[[i]] <- length(circle.list) - } - count.vec -} - -get_elements <- function(id){ - ##print("before div") - if (remDr$browserName == "chromote") { - a <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),returnByValue = TRUE)$result$value - b <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),returnByValue = TRUE)$result$value - show_hide <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[0]", as.character(id)),returnByValue = TRUE)$result$value - widget <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[1]", as.character(id)),returnByValue = TRUE)$result$value - - } else { - div <- remDr$findElement("id", id) - ## For debugging a NoSuchElement error I insert print statements. - ##print("before css selector") - tr.list <- div$findChildElements( - "css selector", "table.legend tr.label_variable") - a <- tr.list[[1]] - b <- tr.list[[2]] - ##print("before show_hide") - show_hide <- div$findChildElement("class name", "show_hide_selector_widgets") - ##print("before col_selector_widget") - widget <- div$findChildElement("class name", "label_variable_selector_widget") - } - list(a178=a, - b934=b, - show_hide=show_hide, - widget=widget) -} - -plot1top <- get_elements("plot1top") -plot1bottom <- get_elements("plot1bottom") - -# get_circles() returns a list of circles so comparing with list(expected, expected) - -test_that("clicking top legend adds/remove points", { - expect_equal(get_circles(), list(10, 10)) - - clickID("plot1top_q_label_variable_a178") - expect_equal(get_circles(), list(5, 10)) - - clickID("plot1top_q_label_variable_b934") - expect_equal(get_circles(), list(0, 10)) - - clickID("plot1top_q_label_variable_b934") - expect_equal(get_circles(), list(5, 10)) - - clickID("plot1top_q_label_variable_a178") - expect_equal(get_circles(), list(10, 10)) -}) - -test_that("clicking bottom legend adds/remove points", { - expect_equal(get_circles(), list(10, 10)) - - clickID("plot1bottom_q_label_variable_a178") - expect_equal(get_circles(), list(10, 5)) - - clickID("plot1bottom_q_label_variable_b934") - expect_equal(get_circles(), list(10, 0)) - - clickID("plot1bottom_q_label_variable_b934") - expect_equal(get_circles(), list(10, 5)) - - clickID("plot1bottom_q_label_variable_a178") - expect_equal(get_circles(), list(10, 10)) -}) - -clickTop <- function() { -if (remDr$browserName == "chromote") { - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) -} else { - plot1top$show_hide$clickElement() - s.div <- plot1top$widget$findChildElement("class name", "selectize-input") - s.div$clickElement() -} -} - -clickBottom <- function() { - if (remDr$browserName == "chromote") { - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) - } else { - plot1top$show_hide$clickElement() - s.div <- plot1top$widget$findChildElement("class name", "selectize-input") - s.div$clickElement() - } -} - -# Function to send a key event -sendKey <- function(key, code, keyCode) { - remDr$Input$dispatchKeyEvent(type = "keyDown", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) - remDr$Input$dispatchKeyEvent(type = "keyUp", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) -} - -sendBackspace <- function() { - if (remDr$browserName == "chromote") { - sendKey("Backspace", "Backspace", 8) - } else { - remDr$sendKeysToActiveElement(list(key="backspace")) - } - Sys.sleep(0.5) -} - -sendA <- function() { - if (remDr$browserName == "chromote") { - remDr$Input$insertText(text = "a") - sendKey("Enter", "Enter", 13) - } else { - remDr$sendKeysToActiveElement(list("a", key="enter")) - } - Sys.sleep(0.5) -} - -sendB <- function() { - if (remDr$browserName == "chromote") { - remDr$Input$insertText(text = "b") - sendKey("Enter", "Enter", 13) - } else { - remDr$sendKeysToActiveElement(list("b", key="enter")) - } - Sys.sleep(0.5) -} - -clickTop() -test_that("top widget adds/remove points", { - expect_equal(get_circles(), list(10, 10)) - sendBackspace() - expect_equal(get_circles(), list(5, 10)) - sendBackspace() - expect_equal(get_circles(), list(0, 10)) - sendA() - expect_equal(get_circles(), list(5, 10)) - sendB() - expect_equal(get_circles(), list(10, 10)) -}) - -clickBottom() -test_that("bottom widget adds/remove points", { - expect_equal(get_circles(), list(10, 10)) - sendBackspace() - expect_equal(get_circles(), list(10, 5)) - sendBackspace() - expect_equal(get_circles(), list(10, 0)) - sendA() - expect_equal(get_circles(), list(10, 5)) - sendB() - expect_equal(get_circles(), list(10, 10)) -}) +acontext("knitting multiple animint plots in a single Rmd") +print("In test") +knitr::knit_meta() #clear knitr 'metadata' +# Rmd.file <- "~/R/animint/inst/examples/test_knit_print.Rmd" ## Do we need this??? +Rmd.file <- system.file("inst", "examples", "test_knit_print.Rmd", + package = "animint2") +Rmd.file <- file.path("..", "..", "inst","examples", "test_knit_print.Rmd") +index.file <- file.path("animint-htmltest", "index.Rmd") + +if (!dir.exists(dirname(index.file))) { + # Create directory if it doesn't exist + dir.create(dirname(index.file), recursive = TRUE) +} +if (!file.exists(index.file)) { + # Create index.file if it doesn't exist + file.create(index.file) +} + +file.copy(Rmd.file, index.file, overwrite=TRUE) +## https://github.com/rstudio/rmarkdown/issues/587#issuecomment-168437646 +## @yihui says "Do not use the output_dir argument of render()" +rmarkdown::render(index.file) +remDr$refresh() +Sys.sleep(3) +html <- getHTML() + +test_that("knit_print.animint renders five x axis titles", { + nodes <- getNodeSet(html, "//text[@class='xtitle']") + value.vec <- sapply(nodes, xmlValue) + expected.vec <- + c("first plot with color legend", + "second plot with color legend", + "non-interactive plot", + "position", + "segments") + expect_identical(value.vec, expected.vec) +}) + +test_that("segments and breakpoints are rendered", { + seg.list <- getNodeSet(html, '//g[@class="geom3_segment_signal"]//line') + expect_equal(length(seg.list), 6) + break.list <- getNodeSet(html, '//g[@class="geom4_vline_signal"]//line') + expect_equal(length(break.list), 5) +}) + +test_that("svg id property is unique", { + svg.list <- getNodeSet(html, "//svg") + attr.mat <- sapply(svg.list, xmlAttrs) + id.counts <- table(attr.mat["id",]) + expect_true(all(id.counts==1)) +}) + +all.list <- getNodeSet(html, "//*") +id.na.vec <- sapply(all.list, function(e){ + attr.vec.or.null <- xmlAttrs(e) + if("id" %in% names(attr.vec.or.null)){ + attr.vec.or.null[["id"]] + }else{ + NA + } +}) +## In HTML, all values are case-insensitive +## http://www.w3schools.com/tags/att_global_id.asp +lower.id.vec <- tolower(id.na.vec) +id.counts <- table(lower.id.vec) +(not.unique <- id.counts[1 < id.counts]) +test_that("id property is unique over entire page", { + expect_equal(length(not.unique), 0) +}) + +test_that("id must contain at least one character", { + expect_true(all(0 < nchar(names(id.counts)))) +}) + +test_that("id must not contain any space characters", { + expect_false(any(grepl(" ", names(id.counts)))) +}) + +## function to extract all circles from an HTML page +get_circles <- function(html=getHTML()) { + plot.names <- c("plot1top", "plot1bottom") + count.vec <- c() + for(i in seq_along(plot.names)){ + xpath <- sprintf("//div[@id='%s']//circle[@class='geom']", plot.names[[i]]) + circle.list <- getNodeSet(html, xpath) + count.vec[[i]] <- length(circle.list) + } + count.vec +} + +get_elements <- function(id){ + ##print("before div") + if (remDr$browserName == "chromote") { + a <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),returnByValue = TRUE)$result$value + b <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),returnByValue = TRUE)$result$value + show_hide <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[0]", as.character(id)),returnByValue = TRUE)$result$value + widget <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[1]", as.character(id)),returnByValue = TRUE)$result$value + + } else { + div <- remDr$findElement("id", id) + ## For debugging a NoSuchElement error I insert print statements. + ##print("before css selector") + tr.list <- div$findChildElements( + "css selector", "table.legend tr.label_variable") + a <- tr.list[[1]] + b <- tr.list[[2]] + ##print("before show_hide") + show_hide <- div$findChildElement("class name", "show_hide_selector_widgets") + ##print("before col_selector_widget") + widget <- div$findChildElement("class name", "label_variable_selector_widget") + } + list(a178=a, + b934=b, + show_hide=show_hide, + widget=widget) +} + +plot1top <- get_elements("plot1top") +plot1bottom <- get_elements("plot1bottom") + +# get_circles() returns a list of circles so comparing with list(expected, expected) + +test_that("clicking top legend adds/remove points", { + expect_equal(get_circles(), list(10, 10)) + + clickID("plot1top_q_label_variable_a178") + expect_equal(get_circles(), list(5, 10)) + + clickID("plot1top_q_label_variable_b934") + expect_equal(get_circles(), list(0, 10)) + + clickID("plot1top_q_label_variable_b934") + expect_equal(get_circles(), list(5, 10)) + + clickID("plot1top_q_label_variable_a178") + expect_equal(get_circles(), list(10, 10)) +}) + +test_that("clicking bottom legend adds/remove points", { + expect_equal(get_circles(), list(10, 10)) + + clickID("plot1bottom_q_label_variable_a178") + expect_equal(get_circles(), list(10, 5)) + + clickID("plot1bottom_q_label_variable_b934") + expect_equal(get_circles(), list(10, 0)) + + clickID("plot1bottom_q_label_variable_b934") + expect_equal(get_circles(), list(10, 5)) + + clickID("plot1bottom_q_label_variable_a178") + expect_equal(get_circles(), list(10, 10)) +}) + +clickTop <- function() { +if (remDr$browserName == "chromote") { + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) +} else { + plot1top$show_hide$clickElement() + s.div <- plot1top$widget$findChildElement("class name", "selectize-input") + s.div$clickElement() +} +} + +clickBottom <- function() { + if (remDr$browserName == "chromote") { + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) + } else { + plot1top$show_hide$clickElement() + s.div <- plot1top$widget$findChildElement("class name", "selectize-input") + s.div$clickElement() + } +} + +# Function to send a key event +sendKey <- function(key, code, keyCode) { + remDr$Input$dispatchKeyEvent(type = "keyDown", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) + remDr$Input$dispatchKeyEvent(type = "keyUp", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) +} + +sendBackspace <- function() { + if (remDr$browserName == "chromote") { + sendKey("Backspace", "Backspace", 8) + } else { + remDr$sendKeysToActiveElement(list(key="backspace")) + } + Sys.sleep(0.5) +} + +send <- function(alphabet) { + if (remDr$browserName == "chromote") { + remDr$Input$insertText(text = alphabet) + sendKey("Enter", "Enter", 13) + } else { + remDr$sendKeysToActiveElement(list(alphabet, key="enter")) + } + Sys.sleep(0.5) +} + +clickTop() +test_that("top widget adds/remove points", { + expect_equal(get_circles(), list(10, 10)) + sendBackspace() + expect_equal(get_circles(), list(5, 10)) + sendBackspace() + expect_equal(get_circles(), list(0, 10)) + sendA() + expect_equal(get_circles(), list(5, 10)) + sendB() + expect_equal(get_circles(), list(10, 10)) +}) + +clickBottom() +test_that("bottom widget adds/remove points", { + expect_equal(get_circles(), list(10, 10)) + sendBackspace() + expect_equal(get_circles(), list(10, 5)) + sendBackspace() + expect_equal(get_circles(), list(10, 0)) + send("a") + expect_equal(get_circles(), list(10, 5)) + send("b") + expect_equal(get_circles(), list(10, 10)) +}) From d45f3d93a1220c6f1fb30e4331f076f3f9b311a6 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Sat, 15 Jun 2024 18:37:03 -0400 Subject: [PATCH 030/105] Update test-renderer1-knit-print.R changed the calls of SendA and SendB into a single function call of Send with input string argument a or b. --- tests/testthat/test-renderer1-knit-print.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index c26c75800..4eb153525 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -206,9 +206,9 @@ test_that("top widget adds/remove points", { expect_equal(get_circles(), list(5, 10)) sendBackspace() expect_equal(get_circles(), list(0, 10)) - sendA() + send("a") expect_equal(get_circles(), list(5, 10)) - sendB() + send("b") expect_equal(get_circles(), list(10, 10)) }) From 979fb9d8522f94432fecc3e386b3813d9eba9320 Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Tue, 25 Jun 2024 11:51:33 -0400 Subject: [PATCH 031/105] edits that work on TDH ubuntu laptop with chrome --- tests/testthat/helper-HTML.R | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index b7a1054a4..e1d3ab854 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -6,7 +6,7 @@ animint2HTML <- function(plotList) { res <- animint2dir(plotList, out.dir = "animint-htmltest", open.browser = FALSE) remDr$refresh() - Sys.sleep(1) + Sys.sleep(2) res$html <- getHTML() ## [ERROR - 2019-06-05T18:30:55.358Z] Session [e7c4e500-871e-11e9-a9b5-8dab1f486f7e] - page.onError - msg: TypeError: 'undefined' is not an object (evaluating 's_info.type') ## [ERROR - 2019-06-05T18:30:55.360Z] Session [e7c4e500-871e-11e9-a9b5-8dab1f486f7e] - page.onError - stack: @@ -54,7 +54,13 @@ tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { chrome.session <- chromote::ChromoteSession$new() chrome.session$view() - chrome.session$refresh <- function()chrome.session$Page$reload() + chrome.session$refresh <- function(){ + ## from https://github.com/rstudio/chromote?tab=readme-ov-file#loading-a-page-reliably + prom <- chrome.session$Page$loadEventFired(wait_ = FALSE) # Get the promise for the loadEventFired + chrome.session$Page$reload() + # Block until p resolves + chrome.session$wait_for(prom) + } chrome.session$navigate <- function(u){ chrome.session$Page$navigate(u) } From 083b6113c499835e40a06a74b5d88ca1167b26e6 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 26 Jun 2024 14:01:39 -0400 Subject: [PATCH 032/105] kept only chromote APIs kept only chromote APIs --- tests/testthat/test-renderer2-widerect.R | 941 +++++++++++------------ 1 file changed, 428 insertions(+), 513 deletions(-) diff --git a/tests/testthat/test-renderer2-widerect.R b/tests/testthat/test-renderer2-widerect.R index f8646ecc5..03db8ffa2 100644 --- a/tests/testthat/test-renderer2-widerect.R +++ b/tests/testthat/test-renderer2-widerect.R @@ -1,513 +1,428 @@ -acontext("geom_widerect") -library(animint2) -expect_source <- function(expected){ - a.list <- getNodeSet(info$html, '//a[@id="a_source_href"]') - computed <- if(length(a.list)==0){ - NULL - }else{ - at.mat <- sapply(a.list, xmlAttrs) - at.mat["href",] - } - expect_identical(as.character(computed), as.character(expected)) -} - -recommendation <- data.frame( - min.C=21, - max.C=23) -set.seed(1) -temp.time <- data.frame( - time=strptime(paste0("2015-10-", 1:31), "%Y-%m-%d"), - temp.C=rnorm(31)) -viz <- animint( - gg=ggplot()+ - theme_bw()+ - theme_animint(height=200, width=2000)+ - geom_widerect(aes( - ymin=min.C, ymax=max.C), - color=NA, - fill="grey", - data=recommendation)+ - geom_line(aes( - time, temp.C), - data=temp.time) -) - -info <- animint2HTML(viz) -expect_source(NULL) - -getBounds <- function(geom.class){ - - script.txt <- sprintf('return document.getElementsByClassName("%s")[0].getBoundingClientRect()', geom.class) - remDr$executeScript(script.txt) -} - -test_that("bottom of widerect is above line", { - - if (remDr$browserName=="chromote"){ - rect_bound_script <- 'document.getElementsByClassName("geom1_widerect_gg")[0].getBoundingClientRect().bottom;' - rect_bound <- remDr$Runtime$evaluate(rect_bound_script,returnByValue = TRUE)$result$value - line_bound_script <- 'document.getElementsByClassName("geom2_line_gg")[0].getBoundingClientRect().top;' - line_bound <- remDr$Runtime$evaluate(line_bound_script,returnByValue = TRUE)$result$value - expect_lt(rect_bound, line_bound) - } else { - rect.bounds <- getBounds("geom1_widerect_gg") - line.bounds <- getBounds("geom2_line_gg") - expect_lt(rect.bounds$bottom, line.bounds$top) - } - -}) - -data(WorldBank, package = "animint2") -not.na <- subset(WorldBank, !(is.na(life.expectancy) | is.na(fertility.rate))) -BOTH <- function(df, top, side)data.frame( - df, - top=factor(top, c("Fertility rate", "Years")), - side=factor(side, c("Years", "Life expectancy"))) -TS <- function(df)BOTH(df, "Years", "Life expectancy") -SCATTER <- function(df)BOTH(df, "Fertility rate", "Life expectancy") -TS2 <- function(df)BOTH(df, "Fertility rate", "Years") -years <- unique(not.na[, "year", drop=FALSE]) -years$status <- ifelse(years$year %% 2, "odd", "even") -wb.facets <- animint( - ts=ggplot()+ - xlab("")+ - geom_tallrect(aes( - xmin=year-1/2, xmax=year+1/2, - linetype=status), - clickSelects="year", - data=TS(years), alpha=1/2)+ - theme_bw()+ - theme_animint(width=1000, height=800)+ - theme(panel.margin=grid::unit(0, "lines"))+ - geom_line(aes( - year, life.expectancy, group=country, colour=region, - id = country), - clickSelects="country", - data=TS(not.na), size=4, alpha=3/5)+ - geom_point(aes( - year, life.expectancy, color=region, size=population), - clickSelects="country", - showSelected="country", - data=TS(not.na))+ - geom_path(aes( - fertility.rate, year, group=country, colour=region), - clickSelects="country", - data=TS2(not.na), size=4, alpha=3/5)+ - geom_point(aes( - fertility.rate, year, color=region, size=population), - showSelected="country", clickSelects="country", - data=TS2(not.na))+ - geom_widerect(aes( - ymin=year-1/2, ymax=year+1/2, - linetype=status, - id=paste0("year", year)), - clickSelects="year", - data=TS2(years), alpha=1/2)+ - geom_point(aes( - fertility.rate, life.expectancy, - colour=region, size=population, - key=country), # key aesthetic for animated transitions! - clickSelects="country", - showSelected="year", - data=SCATTER(not.na))+ - geom_text(aes( - fertility.rate, life.expectancy, label=country, - key=country), #also use key here! - showSelected=c("country", "year"), - clickSelects="country", - data=SCATTER(not.na))+ - scale_size_animint(breaks=10^(5:9))+ - facet_grid(side ~ top, scales="free")+ - geom_text(aes( - 5, 85, label=paste0("year = ", year), - key=year), - showSelected="year", - data=SCATTER(years)), - bar=ggplot()+ - theme_animint(height=2400)+ - geom_bar(aes( - country, life.expectancy, fill=region, - key=country, id=gsub(" ", "_", country)), - showSelected="year", clickSelects="country", - data=not.na, stat="identity", position="identity")+ - coord_flip(), - time=list(variable="year", ms=2000), - duration=list(year=2000), - first=list(year=1975, country=c("United States", "Vietnam")), - selector.types=list(country="multiple"), - title="World Bank data (multiple selection, facets)", - source="https://github.com/animint/animint2/blob/master/tests/testthat/test-renderer2-widerect.R") - -info <- animint2HTML(wb.facets) -expect_source("https://github.com/animint/animint2/blob/master/tests/testthat/test-renderer2-widerect.R") - -rect.list <- getNodeSet( - info$html, '//svg[@id="plot_ts"]//rect[@class="border_rect"]') -expect_equal(length(rect.list), 4) -at.mat <- sapply(rect.list, xmlAttrs) - -test_that("three unique border_rect x values (no horiz space)", { - left.vec <- as.numeric(at.mat["x", ]) - width.vec <- as.numeric(at.mat["width", ]) - right.vec <- left.vec + width.vec - x.values <- unique(c(left.vec, right.vec)) - expect_equal(length(x.values), 3) -}) - -test_that("three unique border_rect y values (no vert space)", { - top.vec <- as.numeric(at.mat["y", ]) - height.vec <- as.numeric(at.mat["height", ]) - bottom.vec <- top.vec + height.vec - y.values <- unique(c(top.vec, bottom.vec)) - expect_equal(length(y.values), 3) -}) - -line.xpath <- '//g[@class="geom2_line_ts"]//g[@class="PANEL4"]//path' -opacityPattern <- - paste0("opacity:", - "(?.*?)", - ";") - -test_that("line opacity initially 0.1 or 0.6", { - - node.set <- getNodeSet(info$html, line.xpath) - opacity.list <- list() - for(node.i in seq_along(node.set)){ - node <- node.set[[node.i]] - a.vec <- xmlAttrs(node) - style.str <- a.vec[["style"]] - opacity.mat <- str_match_perl(style.str, opacityPattern) - node.id <- a.vec[["id"]] - opacity.list[[node.id]] <- as.numeric(opacity.mat[, "value"]) - } - opacity.vec <- do.call(c, opacity.list) - - selected.computed <- as.numeric(opacity.vec[wb.facets$first$country]) - selected.expected <- rep(0.6, length(selected.computed)) - expect_equal(selected.computed, selected.expected) - - unselected.computed <- - as.numeric(opacity.vec[!names(opacity.vec) %in% wb.facets$first$country]) - unselected.expected <- rep(0.1, length(unselected.computed)) - expect_equal(unselected.computed, unselected.expected) - -}) - -dasharrayPattern <- - paste0("stroke-dasharray:", - "(?.*?)", - ";") - -rect.xpaths <- - c('//g[@class="geom6_widerect_ts"]//g[@class="PANEL1"]//rect', - '//g[@class="geom1_tallrect_ts"]//g[@class="PANEL4"]//rect') - -test_that("wide/tallrect renders a for every year", { - for(rect.xpath in rect.xpaths){ - node.set <- getNodeSet(info$html, rect.xpath) - expect_equal(length(node.set), nrow(years)) - style.list <- list() - for(node.i in seq_along(node.set)){ - node <- node.set[[node.i]] - a.vec <- xmlAttrs(node) - style.list[[node.i]] <- a.vec[["style"]] - sizes <- as.numeric(a.vec[c("height", "width")]) - expect_true(all(sizes > 0)) - } - style.vec <- do.call(c, style.list) - dash.mat <- str_match_perl(style.vec, dasharrayPattern) - ## Use paste() to treat NA as a value instead of ignoring it. - dash.table <- table(paste(dash.mat[, "value"])) - ## There should be 2 unique values of stoke-dasharray. - expect_equal(length(dash.table), 2) - } -}) - -getYear <- function(){ - node.set <- getNodeSet(getHTML(), '//g[@class="geom9_text_ts"]//text') - expect_equal(length(node.set), 1) - value <- xmlValue(node.set[[1]]) - sub("year = ", "", value) -} - -test_that("animation updates", { - old.year <- getYear() - Sys.sleep(5) #wait for two animation frames. - new.year <- getYear() - expect_true(old.year != new.year) -}) - -clickID("plot_show_hide_animation_controls") - -test_that("pause stops animation", { - clickID("play_pause") - old.year <- getYear() - Sys.sleep(3) - new.year <- getYear() - expect_true(old.year == new.year) -}) - -test_that("play restarts animation", { - old.year <- getYear() - clickID("play_pause") - Sys.sleep(5) - new.year <- getYear() - expect_true(old.year != new.year) -}) - -test_that("pause stops animation (second time)", { - clickID("play_pause") - old.year <- getYear() - Sys.sleep(3) - new.year <- getYear() - expect_true(old.year == new.year) -}) - -clickID("plot_ts_status_variable_even") -clickID("plot_ts_status_variable_odd") -html.no.rects <- getHTML() - -test_that("clicking status legend hides tallrects", { - for(rect.xpath in rect.xpaths){ - node.set <- getNodeSet(html.no.rects, rect.xpath) - expect_equal(length(node.set), 0) - } -}) - -test_that("clicking status legend does not hide text", { - node.set <- - getNodeSet(html.no.rects, - '//g[@class="geom9_text_ts"]//text[@class="geom"]') - expect_equal(length(node.set), 1) -}) - -clickID("plot_ts_status_variable_even") -clickID("plot_ts_status_variable_odd") -html.with.rects <- getHTML() - -test_that("clicking status legend brings back tallrects", { - for(rect.xpath in rect.xpaths){ - node.set <- getNodeSet(html.with.rects, rect.xpath) - expect_equal(length(node.set), nrow(years)) - style.list <- list() - for(node.i in seq_along(node.set)){ - node <- node.set[[node.i]] - a.vec <- xmlAttrs(node) - style.list[[node.i]] <- a.vec[["style"]] - sizes <- as.numeric(a.vec[c("height", "width")]) - expect_true(all(sizes > 0)) - } - style.vec <- do.call(c, style.list) - dash.mat <- str_match_perl(style.vec, dasharrayPattern) - ## Use paste() to treat NA as a value instead of ignoring it. - dash.table <- table(paste(dash.mat[, "value"])) - ## There should be 2 unique values of stoke-dasharray. - expect_equal(length(dash.table), 2) - } -}) - -test_that("play restarts animation (second time)", { - old.year <- getYear() - clickID("play_pause") - Sys.sleep(5) - new.year <- getYear() - expect_true(old.year != new.year) -}) - -legend.td.xpath <- - '//tr[@class="region_variable"]//td[@class="legend_entry_label"]' -rects_and_legends <- function(){ - html <- getHTML() - list(rects=getNodeSet(html, '//rect[@id="United_States"]'), - legends=getStyleValue(html, legend.td.xpath, "opacity")) -} -test_that("clicking legend removes/adds countries", { - before <- rects_and_legends() - expect_equal(length(before$rects), 1) - expect_equal(sum(before$legends=="1"), 14) - expect_equal(sum(before$legends=="0.5"), 0) - clickID("plot_ts_region_variable_North_America") - Sys.sleep(1) - oneclick <- rects_and_legends() - expect_equal(length(oneclick$rects), 0) - expect_equal(sum(oneclick$legends=="1"), 12) - expect_equal(sum(oneclick$legends=="0.5"), 2) - clickID("plot_ts_region_variable_North_America") - Sys.sleep(1) - twoclicks <- rects_and_legends() - expect_equal(length(twoclicks$rects), 1) - expect_equal(sum(twoclicks$legends=="1"), 14) - expect_equal(sum(twoclicks$legends=="0.5"), 0) -}) -if (remDr$browserName=="chromote"){ - clickID('updates_ms') -} else { - e <- remDr$findElement("id", "updates_ms") - e$clickElement() - e$clearElement() - e$sendKeysToElement(list("3000", key="enter")) -} - - -test_that("pause stops animation (third time)", { - clickID("play_pause") - old.year <- getYear() - Sys.sleep(4) - new.year <- getYear() - expect_true(old.year == new.year) -}) - -# Function to send a key event -sendKey <- function(key, code, keyCode) { - remDr$Input$dispatchKeyEvent(type = "keyDown", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) - remDr$Input$dispatchKeyEvent(type = "keyUp", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) -} - -if (remDr$browserName=="chromote"){ - - remDr$Runtime$evaluate("document.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));") - remDr$Runtime$evaluate("childDom = document.getElementsByClassName('year_variable_selector_widget')[0]; childDom.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));") - sendKey("Backspace", "Backspace", 8) - remDr$Input$insertText(text = "1962") - remDr$Runtime$evaluate("childDom = document.getElementsByClassName('year_variable_selector_widget')[0]; childDom.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));") - sendKey("ArrowDown", "ArrowDown", 40) - sendKey("Enter", "Enter", 13) - -} else { - e <- remDr$findElement("class name", "show_hide_selector_widgets") - e$clickElement() - s.tr <- remDr$findElement("class name", "year_variable_selector_widget") - s.div <- s.tr$findChildElement("class name", "selectize-input") - s.div$clickElement() -# Selenium Versions > 2 do not support the sendKeysToActiveElement function as I found on their github. -# https://github.com/SeleniumHQ/selenium/issues/7686 -# Looking to make it work with JavaScript or JQuery - remDr$sendKeysToActiveElement(list(key="backspace")) - remDr$sendKeysToActiveElement(list("1962")) - remDr$sendKeysToActiveElement(list(key="enter")) -} -Sys.sleep(3) - -test_that("typing into selectize widget changes year to 1962", { - current.year <- getYear() - expect_identical(current.year, "1962") -}) - -## Down arrow goes to the first element in the drop down list on chromote -## Skipping the test on chromote since behavior of down arrow key is -## different on firefox and chromote -if (remDr$browserName!="chromote"){ - - s.div$clickElement() - remDr$sendKeysToActiveElement(list(key="down_arrow")) - remDr$sendKeysToActiveElement(list(key="enter")) - Sys.sleep(1) - -test_that("down arrow key changes year to 1963", { - current.year <- getYear() - expect_identical(current.year, "1963") -}) -} - -getCountries <- function(){ - country.labels <- getNodeSet(getHTML(), '//g[@class="geom8_text_ts"]//text') - sapply(country.labels, xmlValue) -} - -test_that("initial countries same as first", { - country.vec <- getCountries() - expect_identical(sort(country.vec), sort(wb.facets$first$country)) -}) - -if (remDr$browserName=="chromote"){ - - remDr$Runtime$evaluate("childDom = document.getElementsByClassName('country_variable_selector_widget')[0]; childDom.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));") - remDr$Input$insertText(text = "Afg") - sendKey("Enter", "Enter", 13) -} else { - s.tr <- remDr$findElement("class name", "country_variable_selector_widget") - s.div <- s.tr$findChildElement("class name", "selectize-input") - s.div$clickElement() - remDr$sendKeysToActiveElement(list("Afg")) - remDr$sendKeysToActiveElement(list(key="enter")) -} -Sys.sleep(1) - -test_that("Afg autocompletes to Afghanistan", { - country.vec <- getCountries() - expected.countries <- c("United States", "Vietnam", "Afghanistan") - expect_identical(sort(country.vec), sort(expected.countries)) -}) - -## The below code is only reproducible on firefox -if (remDr$browserName!="chromote"){ - div.list <- s.tr$findChildElements("class name", "item") - names(div.list) <- sapply(div.list, function(e)e$getElementText()[[1]]) - afg.div <- div.list[["Afghanistan"]] - # clickElement has some really weird behavior, repeating it several times - # focuses different things and I can't reliably get it to actually focus on - # the US element that the test was before. - # This is kinda a hack that causes it to backspace the last element in the list - afg.div$clickElement() - remDr$sendKeysToActiveElement(list(key="backspace")) - Sys.sleep(1) - - test_that("backspace removes Afghanistan from selected countries", { - country.vec <- getCountries() - expected.countries <- c("United States", "Vietnam") - expect_identical(sort(country.vec), sort(expected.countries)) - }) -} -getWidth <- function(){ - node.set <- - getNodeSet(getHTML(), '//g[@class="geom10_bar_bar"]//rect[@id="Vietnam"]') - expect_equal(length(node.set), 1) - alist <- xmlAttrs(node.set[[1]]) - alist[["width"]] -} - -test_that("middle of transition != after when duration=2000", { - clickID("year1960") - Sys.sleep(1) - before.width <- getWidth() - clickID("year2010") - during.width <- getWidth() - Sys.sleep(0.1) - after.width <- getWidth() - rbind(before=before.width, - during=during.width, - after=after.width) - expect_true(during.width != after.width) -}) - -if (remDr$browserName=="chromote") { - - remDr$Runtime$evaluate("document.getElementById('plot_duration_ms_year').value = 0;") - clickID("plot_duration_ms_year") - sendKey("Enter", "Enter", 13) - -} else { - e <- remDr$findElement("id", "plot_duration_ms_year") - e$clickElement() - e$clearElement() - e$sendKeysToElement(list("0", key="enter")) -} -Sys.sleep(1) - -## For chromote, the test fails because transition still happens when duration=0 -if (remDr$browserName!="chromote") { -test_that("middle of transition == after when duration=0", { - clickID("year1960") - Sys.sleep(1) - before.width <- getWidth() - clickID("year2010") - during.width <- getWidth() - Sys.sleep(0.1) - after.width <- getWidth() - rbind(before=before.width, - during=during.width, - after=after.width) - expect_true(before.width != after.width) - expect_true(during.width == after.width) -}) -} \ No newline at end of file +acontext("geom_widerect") +library(animint2) +expect_source <- function(expected){ + a.list <- getNodeSet(info$html, '//a[@id="a_source_href"]') + computed <- if(length(a.list)==0){ + NULL + }else{ + at.mat <- sapply(a.list, xmlAttrs) + at.mat["href",] + } + expect_identical(as.character(computed), as.character(expected)) +} + +recommendation <- data.frame( + min.C=21, + max.C=23) +set.seed(1) +temp.time <- data.frame( + time=strptime(paste0("2015-10-", 1:31), "%Y-%m-%d"), + temp.C=rnorm(31)) +viz <- animint( + gg=ggplot()+ + theme_bw()+ + theme_animint(height=200, width=2000)+ + geom_widerect(aes( + ymin=min.C, ymax=max.C), + color=NA, + fill="grey", + data=recommendation)+ + geom_line(aes( + time, temp.C), + data=temp.time) +) + +info <- animint2HTML(viz) +expect_source(NULL) + +getBounds <- function(geom.class){ + + script.txt <- sprintf('return document.getElementsByClassName("%s")[0].getBoundingClientRect()', geom.class) + remDr$executeScript(script.txt) +} + +test_that("bottom of widerect is above line", { + + + rect_bound_script <- 'document.getElementsByClassName("geom1_widerect_gg")[0].getBoundingClientRect().bottom;' + rect_bound <- remDr$Runtime$evaluate(rect_bound_script,returnByValue = TRUE)$result$value + line_bound_script <- 'document.getElementsByClassName("geom2_line_gg")[0].getBoundingClientRect().top;' + line_bound <- remDr$Runtime$evaluate(line_bound_script,returnByValue = TRUE)$result$value + expect_lt(rect_bound, line_bound) + + +}) + +data(WorldBank, package = "animint2") +not.na <- subset(WorldBank, !(is.na(life.expectancy) | is.na(fertility.rate))) +BOTH <- function(df, top, side)data.frame( + df, + top=factor(top, c("Fertility rate", "Years")), + side=factor(side, c("Years", "Life expectancy"))) +TS <- function(df)BOTH(df, "Years", "Life expectancy") +SCATTER <- function(df)BOTH(df, "Fertility rate", "Life expectancy") +TS2 <- function(df)BOTH(df, "Fertility rate", "Years") +years <- unique(not.na[, "year", drop=FALSE]) +years$status <- ifelse(years$year %% 2, "odd", "even") +wb.facets <- animint( + ts=ggplot()+ + xlab("")+ + geom_tallrect(aes( + xmin=year-1/2, xmax=year+1/2, + linetype=status), + clickSelects="year", + data=TS(years), alpha=1/2)+ + theme_bw()+ + theme_animint(width=1000, height=800)+ + theme(panel.margin=grid::unit(0, "lines"))+ + geom_line(aes( + year, life.expectancy, group=country, colour=region, + id = country), + clickSelects="country", + data=TS(not.na), size=4, alpha=3/5)+ + geom_point(aes( + year, life.expectancy, color=region, size=population), + clickSelects="country", + showSelected="country", + data=TS(not.na))+ + geom_path(aes( + fertility.rate, year, group=country, colour=region), + clickSelects="country", + data=TS2(not.na), size=4, alpha=3/5)+ + geom_point(aes( + fertility.rate, year, color=region, size=population), + showSelected="country", clickSelects="country", + data=TS2(not.na))+ + geom_widerect(aes( + ymin=year-1/2, ymax=year+1/2, + linetype=status, + id=paste0("year", year)), + clickSelects="year", + data=TS2(years), alpha=1/2)+ + geom_point(aes( + fertility.rate, life.expectancy, + colour=region, size=population, + key=country), # key aesthetic for animated transitions! + clickSelects="country", + showSelected="year", + data=SCATTER(not.na))+ + geom_text(aes( + fertility.rate, life.expectancy, label=country, + key=country), #also use key here! + showSelected=c("country", "year"), + clickSelects="country", + data=SCATTER(not.na))+ + scale_size_animint(breaks=10^(5:9))+ + facet_grid(side ~ top, scales="free")+ + geom_text(aes( + 5, 85, label=paste0("year = ", year), + key=year), + showSelected="year", + data=SCATTER(years)), + bar=ggplot()+ + theme_animint(height=2400)+ + geom_bar(aes( + country, life.expectancy, fill=region, + key=country, id=gsub(" ", "_", country)), + showSelected="year", clickSelects="country", + data=not.na, stat="identity", position="identity")+ + coord_flip(), + time=list(variable="year", ms=2000), + duration=list(year=2000), + first=list(year=1975, country=c("United States", "Vietnam")), + selector.types=list(country="multiple"), + title="World Bank data (multiple selection, facets)", + source="https://github.com/animint/animint2/blob/master/tests/testthat/test-renderer2-widerect.R") + +info <- animint2HTML(wb.facets) +expect_source("https://github.com/animint/animint2/blob/master/tests/testthat/test-renderer2-widerect.R") + +rect.list <- getNodeSet( + info$html, '//svg[@id="plot_ts"]//rect[@class="border_rect"]') +expect_equal(length(rect.list), 4) +at.mat <- sapply(rect.list, xmlAttrs) + +test_that("three unique border_rect x values (no horiz space)", { + left.vec <- as.numeric(at.mat["x", ]) + width.vec <- as.numeric(at.mat["width", ]) + right.vec <- left.vec + width.vec + x.values <- unique(c(left.vec, right.vec)) + expect_equal(length(x.values), 3) +}) + +test_that("three unique border_rect y values (no vert space)", { + top.vec <- as.numeric(at.mat["y", ]) + height.vec <- as.numeric(at.mat["height", ]) + bottom.vec <- top.vec + height.vec + y.values <- unique(c(top.vec, bottom.vec)) + expect_equal(length(y.values), 3) +}) + +line.xpath <- '//g[@class="geom2_line_ts"]//g[@class="PANEL4"]//path' +opacityPattern <- + paste0("opacity:", + "(?.*?)", + ";") + +test_that("line opacity initially 0.1 or 0.6", { + + node.set <- getNodeSet(info$html, line.xpath) + opacity.list <- list() + for(node.i in seq_along(node.set)){ + node <- node.set[[node.i]] + a.vec <- xmlAttrs(node) + style.str <- a.vec[["style"]] + opacity.mat <- str_match_perl(style.str, opacityPattern) + node.id <- a.vec[["id"]] + opacity.list[[node.id]] <- as.numeric(opacity.mat[, "value"]) + } + opacity.vec <- do.call(c, opacity.list) + + selected.computed <- as.numeric(opacity.vec[wb.facets$first$country]) + selected.expected <- rep(0.6, length(selected.computed)) + expect_equal(selected.computed, selected.expected) + + unselected.computed <- + as.numeric(opacity.vec[!names(opacity.vec) %in% wb.facets$first$country]) + unselected.expected <- rep(0.1, length(unselected.computed)) + expect_equal(unselected.computed, unselected.expected) + +}) + +dasharrayPattern <- + paste0("stroke-dasharray:", + "(?.*?)", + ";") + +rect.xpaths <- + c('//g[@class="geom6_widerect_ts"]//g[@class="PANEL1"]//rect', + '//g[@class="geom1_tallrect_ts"]//g[@class="PANEL4"]//rect') + +test_that("wide/tallrect renders a for every year", { + for(rect.xpath in rect.xpaths){ + node.set <- getNodeSet(info$html, rect.xpath) + expect_equal(length(node.set), nrow(years)) + style.list <- list() + for(node.i in seq_along(node.set)){ + node <- node.set[[node.i]] + a.vec <- xmlAttrs(node) + style.list[[node.i]] <- a.vec[["style"]] + sizes <- as.numeric(a.vec[c("height", "width")]) + expect_true(all(sizes > 0)) + } + style.vec <- do.call(c, style.list) + dash.mat <- str_match_perl(style.vec, dasharrayPattern) + ## Use paste() to treat NA as a value instead of ignoring it. + dash.table <- table(paste(dash.mat[, "value"])) + ## There should be 2 unique values of stoke-dasharray. + expect_equal(length(dash.table), 2) + } +}) + +getYear <- function(){ + node.set <- getNodeSet(getHTML(), '//g[@class="geom9_text_ts"]//text') + expect_equal(length(node.set), 1) + value <- xmlValue(node.set[[1]]) + sub("year = ", "", value) +} + +test_that("animation updates", { + old.year <- getYear() + Sys.sleep(5) #wait for two animation frames. + new.year <- getYear() + expect_true(old.year != new.year) +}) + +clickID("plot_show_hide_animation_controls") + +test_that("pause stops animation", { + clickID("play_pause") + old.year <- getYear() + Sys.sleep(3) + new.year <- getYear() + expect_true(old.year == new.year) +}) + +test_that("play restarts animation", { + old.year <- getYear() + clickID("play_pause") + Sys.sleep(5) + new.year <- getYear() + expect_true(old.year != new.year) +}) + +test_that("pause stops animation (second time)", { + clickID("play_pause") + old.year <- getYear() + Sys.sleep(3) + new.year <- getYear() + expect_true(old.year == new.year) +}) + +clickID("plot_ts_status_variable_even") +clickID("plot_ts_status_variable_odd") +html.no.rects <- getHTML() + +test_that("clicking status legend hides tallrects", { + for(rect.xpath in rect.xpaths){ + node.set <- getNodeSet(html.no.rects, rect.xpath) + expect_equal(length(node.set), 0) + } +}) + +test_that("clicking status legend does not hide text", { + node.set <- + getNodeSet(html.no.rects, + '//g[@class="geom9_text_ts"]//text[@class="geom"]') + expect_equal(length(node.set), 1) +}) + +clickID("plot_ts_status_variable_even") +clickID("plot_ts_status_variable_odd") +html.with.rects <- getHTML() + +test_that("clicking status legend brings back tallrects", { + for(rect.xpath in rect.xpaths){ + node.set <- getNodeSet(html.with.rects, rect.xpath) + expect_equal(length(node.set), nrow(years)) + style.list <- list() + for(node.i in seq_along(node.set)){ + node <- node.set[[node.i]] + a.vec <- xmlAttrs(node) + style.list[[node.i]] <- a.vec[["style"]] + sizes <- as.numeric(a.vec[c("height", "width")]) + expect_true(all(sizes > 0)) + } + style.vec <- do.call(c, style.list) + dash.mat <- str_match_perl(style.vec, dasharrayPattern) + ## Use paste() to treat NA as a value instead of ignoring it. + dash.table <- table(paste(dash.mat[, "value"])) + ## There should be 2 unique values of stoke-dasharray. + expect_equal(length(dash.table), 2) + } +}) + +test_that("play restarts animation (second time)", { + old.year <- getYear() + clickID("play_pause") + Sys.sleep(5) + new.year <- getYear() + expect_true(old.year != new.year) +}) + +legend.td.xpath <- + '//tr[@class="region_variable"]//td[@class="legend_entry_label"]' +rects_and_legends <- function(){ + html <- getHTML() + list(rects=getNodeSet(html, '//rect[@id="United_States"]'), + legends=getStyleValue(html, legend.td.xpath, "opacity")) +} +test_that("clicking legend removes/adds countries", { + before <- rects_and_legends() + expect_equal(length(before$rects), 1) + expect_equal(sum(before$legends=="1"), 14) + expect_equal(sum(before$legends=="0.5"), 0) + clickID("plot_ts_region_variable_North_America") + Sys.sleep(1) + oneclick <- rects_and_legends() + expect_equal(length(oneclick$rects), 0) + expect_equal(sum(oneclick$legends=="1"), 12) + expect_equal(sum(oneclick$legends=="0.5"), 2) + clickID("plot_ts_region_variable_North_America") + Sys.sleep(1) + twoclicks <- rects_and_legends() + expect_equal(length(twoclicks$rects), 1) + expect_equal(sum(twoclicks$legends=="1"), 14) + expect_equal(sum(twoclicks$legends=="0.5"), 0) +}) + +clickID('updates_ms') + + + +test_that("pause stops animation (third time)", { + clickID("play_pause") + old.year <- getYear() + Sys.sleep(4) + new.year <- getYear() + expect_true(old.year == new.year) +}) + +# Function to send a key event +sendKey <- function(key, code, keyCode) { + remDr$Input$dispatchKeyEvent(type = "keyDown", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) + remDr$Input$dispatchKeyEvent(type = "keyUp", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) +} + + +remDr$Runtime$evaluate("document.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));") +remDr$Runtime$evaluate("childDom = document.getElementsByClassName('year_variable_selector_widget')[0]; childDom.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));") +sendKey("Backspace", "Backspace", 8) +remDr$Input$insertText(text = "1962") +remDr$Runtime$evaluate("childDom = document.getElementsByClassName('year_variable_selector_widget')[0]; childDom.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));") +sendKey("ArrowDown", "ArrowDown", 40) +sendKey("Enter", "Enter", 13) + + +Sys.sleep(3) + +test_that("typing into selectize widget changes year to 1962", { + current.year <- getYear() + expect_identical(current.year, "1962") +}) + + + + +getCountries <- function(){ + country.labels <- getNodeSet(getHTML(), '//g[@class="geom8_text_ts"]//text') + sapply(country.labels, xmlValue) +} + +test_that("initial countries same as first", { + country.vec <- getCountries() + expect_identical(sort(country.vec), sort(wb.facets$first$country)) +}) + + +remDr$Runtime$evaluate("childDom = document.getElementsByClassName('country_variable_selector_widget')[0]; childDom.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));") +remDr$Input$insertText(text = "Afg") +sendKey("Enter", "Enter", 13) + +Sys.sleep(1) + +test_that("Afg autocompletes to Afghanistan", { + country.vec <- getCountries() + expected.countries <- c("United States", "Vietnam", "Afghanistan") + expect_identical(sort(country.vec), sort(expected.countries)) +}) + +## The below code is only reproducible on firefox + +getWidth <- function(){ + node.set <- + getNodeSet(getHTML(), '//g[@class="geom10_bar_bar"]//rect[@id="Vietnam"]') + expect_equal(length(node.set), 1) + alist <- xmlAttrs(node.set[[1]]) + alist[["width"]] +} + +test_that("middle of transition != after when duration=2000", { + clickID("year1960") + Sys.sleep(1) + before.width <- getWidth() + clickID("year2010") + during.width <- getWidth() + Sys.sleep(0.1) + after.width <- getWidth() + rbind(before=before.width, + during=during.width, + after=after.width) + expect_true(during.width != after.width) +}) + + +remDr$Runtime$evaluate("document.getElementById('plot_duration_ms_year').value = 0;") +clickID("plot_duration_ms_year") +sendKey("Enter", "Enter", 13) + From 44335928df2d162dc0bf7ee67d3db79aa3b75d70 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 26 Jun 2024 14:01:54 -0400 Subject: [PATCH 033/105] kept only chromote APIs kept only chromote APIs --- tests/testthat/test-renderer1-url-fragment.R | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test-renderer1-url-fragment.R b/tests/testthat/test-renderer1-url-fragment.R index 170e77a43..da830cf2d 100644 --- a/tests/testthat/test-renderer1-url-fragment.R +++ b/tests/testthat/test-renderer1-url-fragment.R @@ -33,12 +33,8 @@ test_that("one observation selected for url with no selection", { expect_equal(as.numeric(opacity.str[!is.selected]), rep(0.5, 298)) }) -if (remDr$browserName == "chromote") { - old_address <- remDr$Runtime$evaluate("window.location.href")$result$value -} else { - - old_address <- remDr$getCurrentUrl()[[1]] -} +old_address <- remDr$Runtime$evaluate("window.location.href")$result$value + new_address <- paste0(old_address, '#Species={setosa}') From cc85e09b373a6d561de455c806e700510ee6f19c Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 26 Jun 2024 14:02:09 -0400 Subject: [PATCH 034/105] kept only chromote APIs kept only chromote APIs --- tests/testthat/test-renderer1-global-variables.R | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/tests/testthat/test-renderer1-global-variables.R b/tests/testthat/test-renderer1-global-variables.R index c5d620e5c..c705e1745 100644 --- a/tests/testthat/test-renderer1-global-variables.R +++ b/tests/testthat/test-renderer1-global-variables.R @@ -6,26 +6,18 @@ viz <- list(scatter=ggplot()+ showSelected="Species", data=iris)) -myScript <- 'myArray = []; -for(var b in window) { - if(window.hasOwnProperty(b)) {myArray.push(b);} -} -return myArray;' -myScript2 <- 'myArray = []; +myScript <- 'myArray = []; for(var b in window) { if(window.hasOwnProperty(b)) {myArray.push(b);} } myArray;' getVariables <- function(){ - if (remDr$browserName=="chromote"){ - vars <- remDr$Runtime$evaluate(myScript2,returnByValue = TRUE)$result$value + vars <- remDr$Runtime$evaluate(myScript,returnByValue = TRUE)$result$value - } else { - vars <- remDr$executeScript(myScript) - } + # ignore the "plot" variable -- # https://github.com/tdhock/animint/pull/62#issuecomment-100008532 # also ignore jQuery1238915281937 variable: From 041fd2ad0cefa972bef3b6956b3c4ea30f83b2a3 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 26 Jun 2024 14:03:47 -0400 Subject: [PATCH 035/105] modified delay for chromote removed delay method for phantomjs and firefox and kept the custom delay function call for chromote --- tests/testthat/test-renderer1-facet-lines.R | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/testthat/test-renderer1-facet-lines.R b/tests/testthat/test-renderer1-facet-lines.R index 621fa807d..b5176b67b 100644 --- a/tests/testthat/test-renderer1-facet-lines.R +++ b/tests/testthat/test-renderer1-facet-lines.R @@ -50,13 +50,7 @@ custom_delay <- function(milliseconds) { Sys.sleep(milliseconds / 1000) } -# Check browser type and set delay or timeout accordingly -if (remDr$browserName == "chromote") { - custom_delay(60) -} else { - remDr$setTimeout(milliseconds = 60 * 1000 * 2) - -} +custom_delay(60) info <- animint2HTML(wb.facets) From 35cdf6fda39b917d1e341cb6bec447faac94bc53 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 26 Jun 2024 14:04:13 -0400 Subject: [PATCH 036/105] kept only chromote APIs kept only chromote APIs --- .../test-renderer1-axis-angle-rotate.R | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/tests/testthat/test-renderer1-axis-angle-rotate.R b/tests/testthat/test-renderer1-axis-angle-rotate.R index eb205d8c0..122a0c3a2 100644 --- a/tests/testthat/test-renderer1-axis-angle-rotate.R +++ b/tests/testthat/test-renderer1-axis-angle-rotate.R @@ -29,22 +29,17 @@ expect_rotate_anchor <- function(info, rotate, anchor){ expect_match(rotated["transform", ], paste0("rotate(", rotate), fixed=TRUE) # http://stackoverflow.com/questions/442404/retrieve-the-position-x-y-of-an-html-element - if (remDr$browserName=='chromote'){ - tick_box_script <- 'document.getElementsByClassName("xaxis")[0].getBoundingClientRect().bottom;' - title_box_script <- 'document.getElementsByClassName("xtitle")[0].getBoundingClientRect().top;' - - # Evaluate scripts and get the values - tick_box <- remDr$Runtime$evaluate(tick_box_script, returnByValue = TRUE)$result$value - title_box <- remDr$Runtime$evaluate(title_box_script, returnByValue = TRUE)$result$value - #An offset of 2.5 is added because chromote aligns text with slight differences - expect_true(title_box+2.5>= tick_box) - } - else{ - tick_box <- remDr$executeScript(' return document.getElementsByClassName("xaxis")[0].firstChild.getBoundingClientRect()') - title_box <- remDr$executeScript('return document.getElementsByClassName("xtitle")[0].getBoundingClientRect()') - expect_true(title_box$top >= tick_box$bottom) + + tick_box_script <- 'document.getElementsByClassName("xaxis")[0].getBoundingClientRect().bottom;' + title_box_script <- 'document.getElementsByClassName("xtitle")[0].getBoundingClientRect().top;' - } + # Evaluate scripts and get the values + tick_box <- remDr$Runtime$evaluate(tick_box_script, returnByValue = TRUE)$result$value + title_box <- remDr$Runtime$evaluate(title_box_script, returnByValue = TRUE)$result$value + #An offset of 2.5 is added because chromote aligns text with slight differences + expect_true(title_box+2.5>= tick_box) + + From 819963d8a09b387f97ba961f28335904d5fa6c66 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 26 Jun 2024 14:05:46 -0400 Subject: [PATCH 037/105] removed everything not required for chromote removed everything not required for chromote. removed argument of browser name and all initiliazation code for firefox and phantomjs --- tests/testthat/helper-HTML.R | 60 ++++++++---------------------------- 1 file changed, 12 insertions(+), 48 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index b7a1054a4..f170e7f66 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -30,7 +30,7 @@ getHTML <- function(){ #' @export #' @seealso \link{tests_run} #' -tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { +tests_init <- function(dir = ".", port = 4848, ...) { # try to exit out of previously initated processes ex <- tests_exit() # start a non-blocking local file server under path/to/animint/tests/testhat @@ -50,55 +50,19 @@ tests_init <- function(browserName = "phantomjs", dir = ".", port = 4848, ...) { if(OS == "Windows" || OS == "Darwin") { animint_server <- "host.docker.internal" } - if(browserName == "chromote"){ - chrome.session <- chromote::ChromoteSession$new() - - chrome.session$view() - chrome.session$refresh <- function()chrome.session$Page$reload() - chrome.session$navigate <- function(u){ - chrome.session$Page$navigate(u) + chrome.session <- chromote::ChromoteSession$new() + chrome.session$view() + chrome.session$refresh <- function()chrome.session$Page$reload() + chrome.session$navigate <- function(u){ + chrome.session$Page$navigate(u) } - chrome.session$getPageSource <- function(){ - doc <- chrome.session$DOM$getDocument() - chrome.session$DOM$getOuterHTML(doc$root$nodeId)$outerHTML + chrome.session$getPageSource <- function(){ + doc <- chrome.session$DOM$getDocument() + chrome.session$DOM$getOuterHTML(doc$root$nodeId)$outerHTML } - remDr <<- chrome.session - remDr$browserName <-"chromote" - remDr$navigate(sprintf("http://localhost:4848/animint-htmltest/")) - }else{ - if (browserName == "phantomjs") { - message("Starting phantomjs binary. To shut it down, run: \n pJS$stop()") - pJS <<- wdman::phantomjs( - port = remotePort, - phantomver = "latest" - ) - ## Give time for phantomjs binary to start - animint_server <- "localhost" - Sys.sleep(8) - } else if(browserName=="firefox"){ - ## If using firefox, you'll need to run selenium-firefox docker image in order to make it work correctly. - ## We're using docker to avoid version incompatibility issues. - message("You need to run selenium docker image(selenium/standalone-firefox:2.53.0) as specified in docs(https://github.com/tdhock/animint2/wiki/Testing). \nNote: Ignore if already running.") - }else stop("unrecognized browser name") - remDr <<- RSelenium::remoteDriver( - port = remotePort, - browser = browserName, - ) - ## wait for the remote driver to start-up - Sys.sleep(6) - remDr$open(silent = TRUE) - ## some tests don't run reliably with phantomjs (see tests-widerect.R) - Sys.setenv("ANIMINT_BROWSER" = browserName) - ## wait a maximum of 30 seconds when searchinsg for elements. - remDr$setTimeout(type = "implicit", milliseconds = 30000) - ## wait a maximum of 30 seconds for a particular type of operation to execute - remDr$setTimeout(type = "page load", milliseconds = 30000) - ## if we navigate to localhost:%s/htmltest directly, some browsers will - ## redirect to www.htmltest.com. A 'safer' approach is to navigate, then click. - remDr$browserName <-"" - - remDr$navigate(sprintf("http://%s:%s/animint-htmltest/", animint_server, port)) - } + remDr <<- chrome.session + remDr$navigate(sprintf("http://localhost:4848/animint-htmltest/")) + ## Why not just navigate to the right URL to begin with? ## e <- remDr$findElement("xpath", "//a[@href='animint-htmltest/']") From 752a5bf4bcfaaaad9954dcc23180b3d92623402f Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 26 Jun 2024 14:08:59 -0400 Subject: [PATCH 038/105] kept only chromote's way of using clickID and clickHTML kept only the chromote's way of using clickID and clickHTML functions --- tests/testthat/helper-functions.R | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index 8e0b18b02..5fa54748e 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -67,17 +67,9 @@ clickHTML <- function(...){ v <- c(...) stopifnot(length(v) == 1) - selectorType <- names(v) selectorValue <- as.character(v) - - if (selectorType == "id") - { - clickID(selectorValue) - } - else{ - e <- remDr$findElement(names(v), as.character(v)) - e$clickElement() - } + clickID(selectorValue) + Sys.sleep(1) getHTML() } @@ -85,14 +77,8 @@ clickHTML <- function(...){ clickID <- function(...){ v <- c(...) stopifnot(length(v) == 1) - if(remDr$browserName=="chromote"){ - remDr$Runtime$evaluate(sprintf("document.getElementById('%s').dispatchEvent(new CustomEvent('click'))", as.character(v))) - } - else{ - remDr$executeScript(sprintf("document.getElementById('%s').dispatchEvent(new CustomEvent('click'))", as.character(v))) - } - -} + remDr$Runtime$evaluate(sprintf("document.getElementById('%s').dispatchEvent(new CustomEvent('click'))", as.character(v))) +t} rgba.pattern <- paste0( "(?rgba?)", From 250763574512c2c62e15b5b2e6298668bb6f923d Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 26 Jun 2024 23:55:28 -0400 Subject: [PATCH 039/105] removed all code expect for chromote removed all code expect for chromote --- tests/testthat/helper-HTML.R | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index f170e7f66..1c0f61beb 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -63,7 +63,6 @@ tests_init <- function(dir = ".", port = 4848, ...) { remDr <<- chrome.session remDr$navigate(sprintf("http://localhost:4848/animint-htmltest/")) - ## Why not just navigate to the right URL to begin with? ## e <- remDr$findElement("xpath", "//a[@href='animint-htmltest/']") ## e$clickElement() From 996ac70ef558e2b91eff69dc2af640212c5881c5 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Thu, 27 Jun 2024 01:15:23 -0400 Subject: [PATCH 040/105] added browserName as first argument again added browserName as first argument again to see if failing checks pass on github --- tests/testthat/helper-HTML.R | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 1c0f61beb..032402bb4 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -30,7 +30,7 @@ getHTML <- function(){ #' @export #' @seealso \link{tests_run} #' -tests_init <- function(dir = ".", port = 4848, ...) { +tests_init <- function(browserName = "chromote",dir = ".", port = 4848, ...) { # try to exit out of previously initated processes ex <- tests_exit() # start a non-blocking local file server under path/to/animint/tests/testhat @@ -62,10 +62,7 @@ tests_init <- function(dir = ".", port = 4848, ...) { } remDr <<- chrome.session remDr$navigate(sprintf("http://localhost:4848/animint-htmltest/")) - - ## Why not just navigate to the right URL to begin with? - ## e <- remDr$findElement("xpath", "//a[@href='animint-htmltest/']") - ## e$clickElement() + invisible(TRUE) } ## get both horizontal and vertical grid lines From 5ccb456379648e03838d98bf4cdac05a283e28eb Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Thu, 27 Jun 2024 01:23:08 -0400 Subject: [PATCH 041/105] removed requirement for browser name in tests_init() removed requirement for browser name in tests_init() --- tests/testthat.R | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/testthat.R b/tests/testthat.R index 6564e6afe..1b3054d5f 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -39,13 +39,7 @@ if(filter == ""){ filter <- NULL } message(gh.action) -if(FALSE){ - tests_init("firefox") -}else if(interactive() | (gh.action == "ENABLED")) { - tests_init("chromote") -} else { - tests_init() -} +tests_init() tests_run(filter=filter) tests_exit() From 843c1640fec2b946a3735a49bf9030ffff2669e8 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Thu, 27 Jun 2024 01:41:44 -0400 Subject: [PATCH 042/105] removed code not related to chromote removed code not related to chromote --- tests/testthat/test-renderer1-knit-print.R | 57 ++++++---------------- 1 file changed, 14 insertions(+), 43 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index 4eb153525..4cb88705d 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -90,25 +90,12 @@ get_circles <- function(html=getHTML()) { get_elements <- function(id){ ##print("before div") - if (remDr$browserName == "chromote") { - a <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),returnByValue = TRUE)$result$value - b <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),returnByValue = TRUE)$result$value - show_hide <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[0]", as.character(id)),returnByValue = TRUE)$result$value - widget <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[1]", as.character(id)),returnByValue = TRUE)$result$value + a <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),returnByValue = TRUE)$result$value + b <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),returnByValue = TRUE)$result$value + show_hide <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[0]", as.character(id)),returnByValue = TRUE)$result$value + widget <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[1]", as.character(id)),returnByValue = TRUE)$result$value - } else { - div <- remDr$findElement("id", id) - ## For debugging a NoSuchElement error I insert print statements. - ##print("before css selector") - tr.list <- div$findChildElements( - "css selector", "table.legend tr.label_variable") - a <- tr.list[[1]] - b <- tr.list[[2]] - ##print("before show_hide") - show_hide <- div$findChildElement("class name", "show_hide_selector_widgets") - ##print("before col_selector_widget") - widget <- div$findChildElement("class name", "label_variable_selector_widget") - } + list(a178=a, b934=b, show_hide=show_hide, @@ -153,25 +140,15 @@ test_that("clicking bottom legend adds/remove points", { }) clickTop <- function() { -if (remDr$browserName == "chromote") { remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) -} else { - plot1top$show_hide$clickElement() - s.div <- plot1top$widget$findChildElement("class name", "selectize-input") - s.div$clickElement() -} + } clickBottom <- function() { - if (remDr$browserName == "chromote") { - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) - } else { - plot1top$show_hide$clickElement() - s.div <- plot1top$widget$findChildElement("class name", "selectize-input") - s.div$clickElement() - } + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) + } # Function to send a key event @@ -181,21 +158,15 @@ sendKey <- function(key, code, keyCode) { } sendBackspace <- function() { - if (remDr$browserName == "chromote") { - sendKey("Backspace", "Backspace", 8) - } else { - remDr$sendKeysToActiveElement(list(key="backspace")) - } + sendKey("Backspace", "Backspace", 8) + Sys.sleep(0.5) } send <- function(alphabet) { - if (remDr$browserName == "chromote") { - remDr$Input$insertText(text = alphabet) - sendKey("Enter", "Enter", 13) - } else { - remDr$sendKeysToActiveElement(list(alphabet, key="enter")) - } + remDr$Input$insertText(text = alphabet) + sendKey("Enter", "Enter", 13) + Sys.sleep(0.5) } From 31ba2a3f9924bc412c7830d9e06aee3cd2366260 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Thu, 27 Jun 2024 03:42:44 -0400 Subject: [PATCH 043/105] removed browsername argument removed browsername argument --- tests/testthat/helper-HTML.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 032402bb4..c245ac3bd 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -30,7 +30,7 @@ getHTML <- function(){ #' @export #' @seealso \link{tests_run} #' -tests_init <- function(browserName = "chromote",dir = ".", port = 4848, ...) { +tests_init <- function(dir = ".", port = 4848, ...) { # try to exit out of previously initated processes ex <- tests_exit() # start a non-blocking local file server under path/to/animint/tests/testhat From ec0b8a92c08712bd228ec0ae6fa16b86cc7e0fe3 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Thu, 27 Jun 2024 20:26:50 -0400 Subject: [PATCH 044/105] restored accidentally deleted code restored accidentally deleted codes for page loading reliability and delay. deleted unnecessary line related to docker. --- tests/testthat/helper-HTML.R | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index c245ac3bd..0d4edca36 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -6,7 +6,7 @@ animint2HTML <- function(plotList) { res <- animint2dir(plotList, out.dir = "animint-htmltest", open.browser = FALSE) remDr$refresh() - Sys.sleep(1) + Sys.sleep(2) res$html <- getHTML() ## [ERROR - 2019-06-05T18:30:55.358Z] Session [e7c4e500-871e-11e9-a9b5-8dab1f486f7e] - page.onError - msg: TypeError: 'undefined' is not an object (evaluating 's_info.type') ## [ERROR - 2019-06-05T18:30:55.360Z] Session [e7c4e500-871e-11e9-a9b5-8dab1f486f7e] - page.onError - stack: @@ -47,12 +47,18 @@ tests_init <- function(dir = ".", port = 4848, ...) { if(OS == "Linux") { animint_server <- "localhost" } - if(OS == "Windows" || OS == "Darwin") { - animint_server <- "host.docker.internal" - } + chrome.session <- chromote::ChromoteSession$new() chrome.session$view() - chrome.session$refresh <- function()chrome.session$Page$reload() + chrome.session$refresh <- function(){ + ## from https://github.com/rstudio/chromote?tab=readme-ov-file#loading-a-page-reliably + prom <- chrome.session$Page$loadEventFired(wait_ = FALSE) # Get the promise for the loadEventFired + chrome.session$Page$reload() + # Block until p resolves + chrome.session$wait_for(prom) + } + + chrome.session$navigate <- function(u){ chrome.session$Page$navigate(u) } From 6c9ab03c6e7c6f289152229a45a3c6a3bb40049d Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Sat, 6 Jul 2024 20:34:25 -0400 Subject: [PATCH 045/105] partial work of requested changes modified some files for requested changes like removing blank lines, merging two functions etc. --- NEWS.md | 2 +- R/z_pages.R | 1 - tests/testthat/helper-HTML.R | 6 ++++ tests/testthat/helper-functions.R | 5 +-- .../test-renderer1-axis-angle-rotate.R | 6 ---- tests/testthat/test-renderer1-facet-lines.R | 6 +--- .../test-renderer1-global-variables.R | 12 +------ tests/testthat/test-renderer1-knit-print.R | 35 ++++++------------- tests/testthat/test-renderer1-url-fragment.R | 6 ---- 9 files changed, 20 insertions(+), 59 deletions(-) diff --git a/NEWS.md b/NEWS.md index b522f2e78..feef1f507 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # Changes in version 2024.6.6 (PR#126) - - Add chromote as headless browser for testing + - Add chromote as headless browser for testing and remove phantomjs/firefox support # Changes in version 2024.3.12 (PR#119) diff --git a/R/z_pages.R b/R/z_pages.R index 7177b8dd4..9945dace6 100644 --- a/R/z_pages.R +++ b/R/z_pages.R @@ -47,7 +47,6 @@ animint2pages <- function(plot.list, github_repo, commit_message = "Commit from stop(sprintf("Please run `install.packages('%s')` before using this function", pkg)) } } - # Generate plot files res <- animint2dir(plot.list, open.browser = FALSE, ...) # Select non-ignored files to post diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 0d4edca36..9a957f962 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -86,3 +86,9 @@ get_grid_lines <- function(html, p_name, grid_class){ attr_v <- apply(attr_v, 2, as.numeric) return(list(hor=attr_h, vert=attr_v)) } + +# Function to send a key event +sendKey <- function(key, code, keyCode) { + remDr$Input$dispatchKeyEvent(type = "keyDown", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) + remDr$Input$dispatchKeyEvent(type = "keyUp", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) +} diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index 5fa54748e..8385f11a8 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -63,13 +63,10 @@ getSelectorWidgets <- function(html=getHTML()){ } clickHTML <- function(...){ - v <- c(...) - stopifnot(length(v) == 1) selectorValue <- as.character(v) clickID(selectorValue) - Sys.sleep(1) getHTML() } @@ -78,7 +75,7 @@ clickID <- function(...){ v <- c(...) stopifnot(length(v) == 1) remDr$Runtime$evaluate(sprintf("document.getElementById('%s').dispatchEvent(new CustomEvent('click'))", as.character(v))) -t} +} rgba.pattern <- paste0( "(?rgba?)", diff --git a/tests/testthat/test-renderer1-axis-angle-rotate.R b/tests/testthat/test-renderer1-axis-angle-rotate.R index 122a0c3a2..74901306b 100644 --- a/tests/testthat/test-renderer1-axis-angle-rotate.R +++ b/tests/testthat/test-renderer1-axis-angle-rotate.R @@ -38,12 +38,6 @@ expect_rotate_anchor <- function(info, rotate, anchor){ title_box <- remDr$Runtime$evaluate(title_box_script, returnByValue = TRUE)$result$value #An offset of 2.5 is added because chromote aligns text with slight differences expect_true(title_box+2.5>= tick_box) - - - - - - } test_that('no axis rotation is fine', { map <- diff --git a/tests/testthat/test-renderer1-facet-lines.R b/tests/testthat/test-renderer1-facet-lines.R index b5176b67b..2dc32f8e8 100644 --- a/tests/testthat/test-renderer1-facet-lines.R +++ b/tests/testthat/test-renderer1-facet-lines.R @@ -46,12 +46,8 @@ wb.facets <- first=list(year=1975, country=c("United States", "Vietnam")), selector.types=list(country="multiple"), title="World Bank data (multiple selection, facets)") -custom_delay <- function(milliseconds) { - Sys.sleep(milliseconds / 1000) -} - -custom_delay(60) +Sys.sleep(60 / 1000) info <- animint2HTML(wb.facets) test_that("if group is in nest_order, it is last", { diff --git a/tests/testthat/test-renderer1-global-variables.R b/tests/testthat/test-renderer1-global-variables.R index c705e1745..6b1e953af 100644 --- a/tests/testthat/test-renderer1-global-variables.R +++ b/tests/testthat/test-renderer1-global-variables.R @@ -14,10 +14,7 @@ for(var b in window) { myArray;' getVariables <- function(){ - - vars <- remDr$Runtime$evaluate(myScript,returnByValue = TRUE)$result$value - - + vars <- remDr$Runtime$evaluate(myScript,returnByValue = TRUE)$result$value # ignore the "plot" variable -- # https://github.com/tdhock/animint/pull/62#issuecomment-100008532 # also ignore jQuery1238915281937 variable: @@ -28,23 +25,16 @@ getVariables <- function(){ test_that("animint.js only defines 1 object, called animint", { info <- animint2HTML(viz) animint.vars <- getVariables() - index.file <- file.path("animint-htmltest", "index.html") - html.lines <- readLines(index.file) - html.without <- html.lines[!grepl("animint.js", html.lines)] - cat(html.without, file=index.file, sep="\n") # Note: It's OK for the webdriver to spit out # ReferenceError: Can't find variable: animint # since we've removed the animint.js script - remDr$refresh() Sys.sleep(3) without.vars <- getVariables() - diff.vars <- animint.vars[!animint.vars %in% without.vars] - expect_identical(diff.vars, "animint") }) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index 4cb88705d..895146302 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -8,11 +8,9 @@ Rmd.file <- file.path("..", "..", "inst","examples", "test_knit_print.Rmd") index.file <- file.path("animint-htmltest", "index.Rmd") if (!dir.exists(dirname(index.file))) { - # Create directory if it doesn't exist dir.create(dirname(index.file), recursive = TRUE) } if (!file.exists(index.file)) { - # Create index.file if it doesn't exist file.create(index.file) } @@ -94,8 +92,6 @@ get_elements <- function(id){ b <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),returnByValue = TRUE)$result$value show_hide <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[0]", as.character(id)),returnByValue = TRUE)$result$value widget <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[1]", as.character(id)),returnByValue = TRUE)$result$value - - list(a178=a, b934=b, show_hide=show_hide, @@ -138,39 +134,28 @@ test_that("clicking bottom legend adds/remove points", { clickID("plot1bottom_q_label_variable_a178") expect_equal(get_circles(), list(10, 10)) }) - -clickTop <- function() { - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) - -} - -clickBottom <- function() { - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) - -} - -# Function to send a key event -sendKey <- function(key, code, keyCode) { - remDr$Input$dispatchKeyEvent(type = "keyDown", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) - remDr$Input$dispatchKeyEvent(type = "keyUp", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) +clickSide<- function(position=NULL){ + if(position=="top"){ + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) + }else{ + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) + } } sendBackspace <- function() { sendKey("Backspace", "Backspace", 8) - Sys.sleep(0.5) } send <- function(alphabet) { remDr$Input$insertText(text = alphabet) sendKey("Enter", "Enter", 13) - Sys.sleep(0.5) } -clickTop() +clickSide("top") test_that("top widget adds/remove points", { expect_equal(get_circles(), list(10, 10)) sendBackspace() @@ -183,7 +168,7 @@ test_that("top widget adds/remove points", { expect_equal(get_circles(), list(10, 10)) }) -clickBottom() +clickSide("bottom") test_that("bottom widget adds/remove points", { expect_equal(get_circles(), list(10, 10)) sendBackspace() diff --git a/tests/testthat/test-renderer1-url-fragment.R b/tests/testthat/test-renderer1-url-fragment.R index da830cf2d..57da70530 100644 --- a/tests/testthat/test-renderer1-url-fragment.R +++ b/tests/testthat/test-renderer1-url-fragment.R @@ -14,7 +14,6 @@ viz <- list( info <- animint2HTML(viz) - test_that("all species are selected for url with no selection", { opacity.str <- getStyleValue(info$html, "//td[@class='legend_entry_label']", "opacity") opacity.num <- as.numeric(opacity.str) @@ -34,11 +33,7 @@ test_that("one observation selected for url with no selection", { }) old_address <- remDr$Runtime$evaluate("window.location.href")$result$value - - - new_address <- paste0(old_address, '#Species={setosa}') - remDr$navigate(new_address) remDr$refresh() Sys.sleep(10) @@ -63,4 +58,3 @@ test_that("one species is selected for url with selection", { ## }) remDr$navigate(old_address) - From c2d7bc554dff5b4ab61c9c12f25591e2d8d21031 Mon Sep 17 00:00:00 2001 From: Siddhesh Deodhar Date: Sat, 6 Jul 2024 23:45:15 -0400 Subject: [PATCH 046/105] Refactor the test and undo deletion of tests --- tests/testthat/test-renderer2-widerect.R | 67 ++++++++++++++++-------- 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/tests/testthat/test-renderer2-widerect.R b/tests/testthat/test-renderer2-widerect.R index 03db8ffa2..8d484cec6 100644 --- a/tests/testthat/test-renderer2-widerect.R +++ b/tests/testthat/test-renderer2-widerect.R @@ -35,22 +35,28 @@ viz <- animint( info <- animint2HTML(viz) expect_source(NULL) -getBounds <- function(geom.class){ - - script.txt <- sprintf('return document.getElementsByClassName("%s")[0].getBoundingClientRect()', geom.class) - remDr$executeScript(script.txt) +clickSelector <- function(selectorName) { + + script.txt <- sprintf('childDom = document.getElementsByClassName("%s")[0]; childDom.getElementsByClassName("selectize-input")[0].dispatchEvent(new CustomEvent("click"));', selectorName) + remDr$Runtime$evaluate(script.txt) +} + +getBounds <- function(geom.class, position){ + + if(position == "top") { + script.txt <- sprintf('document.getElementsByClassName("%s")[0].getBoundingClientRect().top', geom.class) + remDr$Runtime$evaluate(script.txt,returnByValue = TRUE)$result$value + } else { + script.txt <- sprintf('document.getElementsByClassName("%s")[0].getBoundingClientRect().bottom', geom.class) + remDr$Runtime$evaluate(script.txt,returnByValue = TRUE)$result$value + } } test_that("bottom of widerect is above line", { - - rect_bound_script <- 'document.getElementsByClassName("geom1_widerect_gg")[0].getBoundingClientRect().bottom;' - rect_bound <- remDr$Runtime$evaluate(rect_bound_script,returnByValue = TRUE)$result$value - line_bound_script <- 'document.getElementsByClassName("geom2_line_gg")[0].getBoundingClientRect().top;' - line_bound <- remDr$Runtime$evaluate(line_bound_script,returnByValue = TRUE)$result$value + rect_bound <- getBounds("geom1_widerect_gg", "bottom") + line_bound <- getBounds("geom2_line_gg", "top") expect_lt(rect_bound, line_bound) - - }) data(WorldBank, package = "animint2") @@ -348,18 +354,11 @@ test_that("pause stops animation (third time)", { expect_true(old.year == new.year) }) -# Function to send a key event -sendKey <- function(key, code, keyCode) { - remDr$Input$dispatchKeyEvent(type = "keyDown", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) - remDr$Input$dispatchKeyEvent(type = "keyUp", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) -} - - remDr$Runtime$evaluate("document.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));") -remDr$Runtime$evaluate("childDom = document.getElementsByClassName('year_variable_selector_widget')[0]; childDom.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));") +clickSelector("year_variable_selector_widget") sendKey("Backspace", "Backspace", 8) remDr$Input$insertText(text = "1962") -remDr$Runtime$evaluate("childDom = document.getElementsByClassName('year_variable_selector_widget')[0]; childDom.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));") +clickSelector("year_variable_selector_widget") sendKey("ArrowDown", "ArrowDown", 40) sendKey("Enter", "Enter", 13) @@ -384,8 +383,7 @@ test_that("initial countries same as first", { expect_identical(sort(country.vec), sort(wb.facets$first$country)) }) - -remDr$Runtime$evaluate("childDom = document.getElementsByClassName('country_variable_selector_widget')[0]; childDom.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));") +clickSelector("country_variable_selector_widget") remDr$Input$insertText(text = "Afg") sendKey("Enter", "Enter", 13) @@ -397,7 +395,15 @@ test_that("Afg autocompletes to Afghanistan", { expect_identical(sort(country.vec), sort(expected.countries)) }) -## The below code is only reproducible on firefox +clickSelector("country_variable_selector_widget") +sendKey("Backspace", "Backspace", 8) +Sys.sleep(1) + +test_that("backspace removes Afghanistan from selected countries", { + country.vec <- getCountries() + expected.countries <- c("United States", "Vietnam") + expect_identical(sort(country.vec), sort(expected.countries)) +}) getWidth <- function(){ node.set <- @@ -425,4 +431,19 @@ test_that("middle of transition != after when duration=2000", { remDr$Runtime$evaluate("document.getElementById('plot_duration_ms_year').value = 0;") clickID("plot_duration_ms_year") sendKey("Enter", "Enter", 13) +Sys.sleep(1) +test_that("middle of transition == after when duration=0", { + clickID("year1960") + Sys.sleep(1) + before.width <- getWidth() + clickID("year2010") + during.width <- getWidth() + Sys.sleep(0.1) + after.width <- getWidth() + rbind(before=before.width, + during=during.width, + after=after.width) + expect_true(before.width != after.width) + #expect_true(during.width == after.width) +}) From 1c62fa6ff446164bc78889377131d8ad7dd53836 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Tue, 9 Jul 2024 21:19:13 -0400 Subject: [PATCH 047/105] removed unnecessary lines in helper-HTML and changed system sleep to chromote's timeout sary lines in helper-HTML and changed system sleep to chromote's timeout --- tests/testthat/helper-HTML.R | 6 ------ tests/testthat/test-renderer1-facet-lines.R | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 9a957f962..67c448ef5 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -41,12 +41,6 @@ tests_init <- function(dir = ".", port = 4848, ...) { testDir <- file.path(testPath, "animint-htmltest") # if the htmltest directory exists, wipe clean, then create an empty folder unlink(testDir, recursive = TRUE) - # start-up remote driver - remotePort <- 4444L - OS <- Sys.info()[['sysname']] - if(OS == "Linux") { - animint_server <- "localhost" - } chrome.session <- chromote::ChromoteSession$new() chrome.session$view() diff --git a/tests/testthat/test-renderer1-facet-lines.R b/tests/testthat/test-renderer1-facet-lines.R index 2dc32f8e8..96f14071e 100644 --- a/tests/testthat/test-renderer1-facet-lines.R +++ b/tests/testthat/test-renderer1-facet-lines.R @@ -47,7 +47,7 @@ wb.facets <- selector.types=list(country="multiple"), title="World Bank data (multiple selection, facets)") -Sys.sleep(60 / 1000) +remDr$default_timeout <- 1 info <- animint2HTML(wb.facets) test_that("if group is in nest_order, it is last", { From bbadd2092cb0a86e6bcb2c658328ee81e5d07346 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Tue, 9 Jul 2024 22:02:38 -0400 Subject: [PATCH 048/105] added explanation to why offset was added in tick box and title box --- tests/testthat/test-renderer1-axis-angle-rotate.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-renderer1-axis-angle-rotate.R b/tests/testthat/test-renderer1-axis-angle-rotate.R index 74901306b..ab036b8ac 100644 --- a/tests/testthat/test-renderer1-axis-angle-rotate.R +++ b/tests/testthat/test-renderer1-axis-angle-rotate.R @@ -36,7 +36,10 @@ expect_rotate_anchor <- function(info, rotate, anchor){ # Evaluate scripts and get the values tick_box <- remDr$Runtime$evaluate(tick_box_script, returnByValue = TRUE)$result$value title_box <- remDr$Runtime$evaluate(title_box_script, returnByValue = TRUE)$result$value - #An offset of 2.5 is added because chromote aligns text with slight differences + #An offset of 2.5 px is added in line below but it may need to changed slightly if browser environment is different. + #more information of getBoundingClientRect() and why value it returns depends on specific browser environment is in the links below + #https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect + #https://stackoverflow.com/questions/40879171/in-javascript-why-does-getboundingclientrect-sometimes-return-floating-point expect_true(title_box+2.5>= tick_box) } test_that('no axis rotation is fine', { From 4022f0b76c71fc4ad5b61fe50771ed18a19389d1 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Tue, 9 Jul 2024 22:17:38 -0400 Subject: [PATCH 049/105] refactored code to make clear difference between if and else blocks refactored code to make clear difference between if and else blocks and also simplified calling the APIs --- tests/testthat/test-renderer1-knit-print.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index 895146302..a67961c14 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -136,11 +136,11 @@ test_that("clicking bottom legend adds/remove points", { }) clickSide<- function(position=NULL){ if(position=="top"){ - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) - }else{ - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) + remDr$Runtime$evaluate(sprintf("div = document.getElementById('plot1%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));",position)) + remDr$Runtime$evaluate(sprintf("div = document.getElementById('plot1%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));",position)) + }else if(position=="bottom"){ + remDr$Runtime$evaluate(sprintf("div = document.getElementById('plot1%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));",position)) + remDr$Runtime$evaluate(sprintf("div = document.getElementById('plot1%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));",position)) } } From 11c0fc3c8fd2d3a597abd6348659a6f5ec73e6d6 Mon Sep 17 00:00:00 2001 From: Siddhesh Deodhar Date: Tue, 9 Jul 2024 23:33:23 -0400 Subject: [PATCH 050/105] Fix setting plot duration in wirerect test --- tests/testthat/test-renderer2-widerect.R | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-renderer2-widerect.R b/tests/testthat/test-renderer2-widerect.R index 8d484cec6..44a876087 100644 --- a/tests/testthat/test-renderer2-widerect.R +++ b/tests/testthat/test-renderer2-widerect.R @@ -428,9 +428,7 @@ test_that("middle of transition != after when duration=2000", { }) -remDr$Runtime$evaluate("document.getElementById('plot_duration_ms_year').value = 0;") -clickID("plot_duration_ms_year") -sendKey("Enter", "Enter", 13) +remDr$Runtime$evaluate("var e = document.getElementById('plot_duration_ms_year'); e.value = 0;e.dispatchEvent(new Event('change'));") Sys.sleep(1) test_that("middle of transition == after when duration=0", { @@ -445,5 +443,5 @@ test_that("middle of transition == after when duration=0", { during=during.width, after=after.width) expect_true(before.width != after.width) - #expect_true(during.width == after.width) + expect_true(during.width == after.width) }) From 54d605b050bd7b4eb44c6d7774be9c5154da0731 Mon Sep 17 00:00:00 2001 From: Siddhesh Deodhar Date: Wed, 10 Jul 2024 00:00:24 -0400 Subject: [PATCH 051/105] Refactor widerect and knit-print --- tests/testthat/test-renderer1-knit-print.R | 22 +++++++--------------- tests/testthat/test-renderer2-widerect.R | 9 ++------- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index a67961c14..68dc3bf17 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -2,18 +2,10 @@ acontext("knitting multiple animint plots in a single Rmd") print("In test") knitr::knit_meta() #clear knitr 'metadata' # Rmd.file <- "~/R/animint/inst/examples/test_knit_print.Rmd" ## Do we need this??? -Rmd.file <- system.file("inst", "examples", "test_knit_print.Rmd", - package = "animint2") -Rmd.file <- file.path("..", "..", "inst","examples", "test_knit_print.Rmd") +Rmd.file <- system.file("examples", "test_knit_print.Rmd", + package = "animint2") index.file <- file.path("animint-htmltest", "index.Rmd") -if (!dir.exists(dirname(index.file))) { - dir.create(dirname(index.file), recursive = TRUE) -} -if (!file.exists(index.file)) { - file.create(index.file) -} - file.copy(Rmd.file, index.file, overwrite=TRUE) ## https://github.com/rstudio/rmarkdown/issues/587#issuecomment-168437646 ## @yihui says "Do not use the output_dir argument of render()" @@ -136,11 +128,11 @@ test_that("clicking bottom legend adds/remove points", { }) clickSide<- function(position=NULL){ if(position=="top"){ - remDr$Runtime$evaluate(sprintf("div = document.getElementById('plot1%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));",position)) - remDr$Runtime$evaluate(sprintf("div = document.getElementById('plot1%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));",position)) - }else if(position=="bottom"){ - remDr$Runtime$evaluate(sprintf("div = document.getElementById('plot1%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));",position)) - remDr$Runtime$evaluate(sprintf("div = document.getElementById('plot1%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));",position)) + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) + }else{ + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) + remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) } } diff --git a/tests/testthat/test-renderer2-widerect.R b/tests/testthat/test-renderer2-widerect.R index 44a876087..9e590c117 100644 --- a/tests/testthat/test-renderer2-widerect.R +++ b/tests/testthat/test-renderer2-widerect.R @@ -43,13 +43,8 @@ clickSelector <- function(selectorName) { getBounds <- function(geom.class, position){ - if(position == "top") { - script.txt <- sprintf('document.getElementsByClassName("%s")[0].getBoundingClientRect().top', geom.class) - remDr$Runtime$evaluate(script.txt,returnByValue = TRUE)$result$value - } else { - script.txt <- sprintf('document.getElementsByClassName("%s")[0].getBoundingClientRect().bottom', geom.class) - remDr$Runtime$evaluate(script.txt,returnByValue = TRUE)$result$value - } + script.txt <- sprintf('document.getElementsByClassName("%s")[0].getBoundingClientRect().%s', geom.class, position) + remDr$Runtime$evaluate(script.txt,returnByValue = TRUE)$result$value } test_that("bottom of widerect is above line", { From 937729244d9801c14bdc2b82ec1de7c8611bdc26 Mon Sep 17 00:00:00 2001 From: Toby Hocking Date: Thu, 11 Jul 2024 10:06:34 -0400 Subject: [PATCH 052/105] delete extra-packages --- .github/workflows/tests.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 348f637f9..abccfcf4c 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -28,9 +28,6 @@ jobs: - run: /usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt install texlive texlive-fonts-extra -y - uses: r-lib/actions/setup-r@v2 - uses: r-lib/actions/setup-r-dependencies@v2 - with: - extra-packages: digest, RJSONIO, gtable, plyr, reshape2, scales, knitr, chromote - - name: install package run: R CMD INSTALL . From 9f9129b06db85557c1774633539cea2851b00364 Mon Sep 17 00:00:00 2001 From: Toby Hocking Date: Thu, 11 Jul 2024 10:12:35 -0400 Subject: [PATCH 053/105] getBounds -> getClassBound --- tests/testthat/helper-HTML.R | 12 ++++++++---- .../testthat/test-renderer1-axis-angle-rotate.R | 10 ++-------- tests/testthat/test-renderer2-widerect.R | 16 ++++------------ 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 67c448ef5..4313f857c 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -41,7 +41,6 @@ tests_init <- function(dir = ".", port = 4848, ...) { testDir <- file.path(testPath, "animint-htmltest") # if the htmltest directory exists, wipe clean, then create an empty folder unlink(testDir, recursive = TRUE) - chrome.session <- chromote::ChromoteSession$new() chrome.session$view() chrome.session$refresh <- function(){ @@ -51,8 +50,6 @@ tests_init <- function(dir = ".", port = 4848, ...) { # Block until p resolves chrome.session$wait_for(prom) } - - chrome.session$navigate <- function(u){ chrome.session$Page$navigate(u) } @@ -62,7 +59,6 @@ tests_init <- function(dir = ".", port = 4848, ...) { } remDr <<- chrome.session remDr$navigate(sprintf("http://localhost:4848/animint-htmltest/")) - invisible(TRUE) } ## get both horizontal and vertical grid lines @@ -86,3 +82,11 @@ sendKey <- function(key, code, keyCode) { remDr$Input$dispatchKeyEvent(type = "keyDown", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) remDr$Input$dispatchKeyEvent(type = "keyUp", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) } + +getClassBound <- function(geom.class, position){ + script.txt <- sprintf( + 'document.getElementsByClassName("%s")[0].getBoundingClientRect().%s', + geom.class, position) + remDr$Runtime$evaluate(script.txt, returnByValue = TRUE)$result$value +} + diff --git a/tests/testthat/test-renderer1-axis-angle-rotate.R b/tests/testthat/test-renderer1-axis-angle-rotate.R index ab036b8ac..bfc5a56cc 100644 --- a/tests/testthat/test-renderer1-axis-angle-rotate.R +++ b/tests/testthat/test-renderer1-axis-angle-rotate.R @@ -28,14 +28,8 @@ expect_rotate_anchor <- function(info, rotate, anchor){ expect_match(rotated["style", ], paste("text-anchor:", anchor), fixed=TRUE) expect_match(rotated["transform", ], paste0("rotate(", rotate), fixed=TRUE) # http://stackoverflow.com/questions/442404/retrieve-the-position-x-y-of-an-html-element - - - tick_box_script <- 'document.getElementsByClassName("xaxis")[0].getBoundingClientRect().bottom;' - title_box_script <- 'document.getElementsByClassName("xtitle")[0].getBoundingClientRect().top;' - - # Evaluate scripts and get the values - tick_box <- remDr$Runtime$evaluate(tick_box_script, returnByValue = TRUE)$result$value - title_box <- remDr$Runtime$evaluate(title_box_script, returnByValue = TRUE)$result$value + tick_box <- getClassBound("xaxis", "bottom") + title_box <- getClassBound("xtitle", "top") #An offset of 2.5 px is added in line below but it may need to changed slightly if browser environment is different. #more information of getBoundingClientRect() and why value it returns depends on specific browser environment is in the links below #https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect diff --git a/tests/testthat/test-renderer2-widerect.R b/tests/testthat/test-renderer2-widerect.R index 9e590c117..7ca96b865 100644 --- a/tests/testthat/test-renderer2-widerect.R +++ b/tests/testthat/test-renderer2-widerect.R @@ -35,22 +35,14 @@ viz <- animint( info <- animint2HTML(viz) expect_source(NULL) -clickSelector <- function(selectorName) { - +clickSelector <- function(selectorName) { script.txt <- sprintf('childDom = document.getElementsByClassName("%s")[0]; childDom.getElementsByClassName("selectize-input")[0].dispatchEvent(new CustomEvent("click"));', selectorName) remDr$Runtime$evaluate(script.txt) } -getBounds <- function(geom.class, position){ - - script.txt <- sprintf('document.getElementsByClassName("%s")[0].getBoundingClientRect().%s', geom.class, position) - remDr$Runtime$evaluate(script.txt,returnByValue = TRUE)$result$value -} - -test_that("bottom of widerect is above line", { - - rect_bound <- getBounds("geom1_widerect_gg", "bottom") - line_bound <- getBounds("geom2_line_gg", "top") +test_that("bottom of widerect is above line", { + rect_bound <- getClassBound("geom1_widerect_gg", "bottom") + line_bound <- getClassBound("geom2_line_gg", "top") expect_lt(rect_bound, line_bound) }) From 9644cd2e4e470444202efc086060891c13d48adb Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Thu, 11 Jul 2024 11:16:33 -0400 Subject: [PATCH 054/105] removed print removed print --- tests/testthat/test-renderer1-knit-print.R | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index 68dc3bf17..147145002 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -1,5 +1,4 @@ acontext("knitting multiple animint plots in a single Rmd") -print("In test") knitr::knit_meta() #clear knitr 'metadata' # Rmd.file <- "~/R/animint/inst/examples/test_knit_print.Rmd" ## Do we need this??? Rmd.file <- system.file("examples", "test_knit_print.Rmd", From 8357e4555a11ce692d36b9f3442ab7443578e4c8 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Thu, 11 Jul 2024 11:24:58 -0400 Subject: [PATCH 055/105] removed white line --- tests/testthat/test-renderer1-axis-angle-rotate.R | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/testthat/test-renderer1-axis-angle-rotate.R b/tests/testthat/test-renderer1-axis-angle-rotate.R index bfc5a56cc..f303f2e70 100644 --- a/tests/testthat/test-renderer1-axis-angle-rotate.R +++ b/tests/testthat/test-renderer1-axis-angle-rotate.R @@ -68,7 +68,6 @@ test_that('and hjust=1 means style="text-anchor: end;"', { not=sg) info <- animint2HTML(map) expect_rotate_anchor(info, "-70", "end") - }) test_that('and hjust=0 means style="text-anchor: start;"', { From 42ce09defff2d62c6db6d5c7fdbd6bc4773d63d3 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Thu, 11 Jul 2024 11:34:15 -0400 Subject: [PATCH 056/105] expect_true changed to expect_gt --- tests/testthat/test-renderer1-axis-angle-rotate.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-renderer1-axis-angle-rotate.R b/tests/testthat/test-renderer1-axis-angle-rotate.R index f303f2e70..93b2b42c2 100644 --- a/tests/testthat/test-renderer1-axis-angle-rotate.R +++ b/tests/testthat/test-renderer1-axis-angle-rotate.R @@ -34,7 +34,7 @@ expect_rotate_anchor <- function(info, rotate, anchor){ #more information of getBoundingClientRect() and why value it returns depends on specific browser environment is in the links below #https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect #https://stackoverflow.com/questions/40879171/in-javascript-why-does-getboundingclientrect-sometimes-return-floating-point - expect_true(title_box+2.5>= tick_box) + expect_gt(title_box+2.5, tick_box) } test_that('no axis rotation is fine', { map <- From 2b39a331fe491d29df457bc80fcadfec5607e502 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Fri, 12 Jul 2024 23:09:59 -0400 Subject: [PATCH 057/105] undo whitespaces undo the addition of whitespaces --- tests/testthat/helper-HTML.R | 4 +++- tests/testthat/test-renderer1-axis-angle-rotate.R | 2 -- tests/testthat/test-renderer1-global-variables.R | 2 -- tests/testthat/test-renderer2-widerect.R | 7 ------- 4 files changed, 3 insertions(+), 12 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 4313f857c..4a3e549b9 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -76,7 +76,6 @@ get_grid_lines <- function(html, p_name, grid_class){ attr_v <- apply(attr_v, 2, as.numeric) return(list(hor=attr_h, vert=attr_v)) } - # Function to send a key event sendKey <- function(key, code, keyCode) { remDr$Input$dispatchKeyEvent(type = "keyDown", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) @@ -90,3 +89,6 @@ getClassBound <- function(geom.class, position){ remDr$Runtime$evaluate(script.txt, returnByValue = TRUE)$result$value } +runtime_evaluate <- function(script=NULL,returnByValue=FALSE){ + +} \ No newline at end of file diff --git a/tests/testthat/test-renderer1-axis-angle-rotate.R b/tests/testthat/test-renderer1-axis-angle-rotate.R index 93b2b42c2..aeed1fe66 100644 --- a/tests/testthat/test-renderer1-axis-angle-rotate.R +++ b/tests/testthat/test-renderer1-axis-angle-rotate.R @@ -50,7 +50,6 @@ test_that('axis.text.x=element_text(angle=90) means transform="rotate(-90)"', { not=sg) info <- animint2HTML(map) expect_rotate_anchor(info, "-90", "end") - }) test_that('axis.text.x=element_text(angle=70) means transform="rotate(-70)"', { @@ -59,7 +58,6 @@ test_that('axis.text.x=element_text(angle=70) means transform="rotate(-70)"', { not=sg) info <- animint2HTML(map) expect_rotate_anchor(info, "-70", "end") - }) test_that('and hjust=1 means style="text-anchor: end;"', { diff --git a/tests/testthat/test-renderer1-global-variables.R b/tests/testthat/test-renderer1-global-variables.R index 6b1e953af..c71b7dc58 100644 --- a/tests/testthat/test-renderer1-global-variables.R +++ b/tests/testthat/test-renderer1-global-variables.R @@ -6,7 +6,6 @@ viz <- list(scatter=ggplot()+ showSelected="Species", data=iris)) - myScript <- 'myArray = []; for(var b in window) { if(window.hasOwnProperty(b)) {myArray.push(b);} @@ -21,7 +20,6 @@ getVariables <- function(){ grep("plot|jQuery", vars, value=TRUE, invert=TRUE) } - test_that("animint.js only defines 1 object, called animint", { info <- animint2HTML(viz) animint.vars <- getVariables() diff --git a/tests/testthat/test-renderer2-widerect.R b/tests/testthat/test-renderer2-widerect.R index 7ca96b865..ef85736db 100644 --- a/tests/testthat/test-renderer2-widerect.R +++ b/tests/testthat/test-renderer2-widerect.R @@ -331,8 +331,6 @@ test_that("clicking legend removes/adds countries", { clickID('updates_ms') - - test_that("pause stops animation (third time)", { clickID("play_pause") old.year <- getYear() @@ -349,7 +347,6 @@ clickSelector("year_variable_selector_widget") sendKey("ArrowDown", "ArrowDown", 40) sendKey("Enter", "Enter", 13) - Sys.sleep(3) test_that("typing into selectize widget changes year to 1962", { @@ -357,9 +354,6 @@ test_that("typing into selectize widget changes year to 1962", { expect_identical(current.year, "1962") }) - - - getCountries <- function(){ country.labels <- getNodeSet(getHTML(), '//g[@class="geom8_text_ts"]//text') sapply(country.labels, xmlValue) @@ -414,7 +408,6 @@ test_that("middle of transition != after when duration=2000", { expect_true(during.width != after.width) }) - remDr$Runtime$evaluate("var e = document.getElementById('plot_duration_ms_year'); e.value = 0;e.dispatchEvent(new Event('change'));") Sys.sleep(1) From 6d79c0b8010f8048db295f53b6e32a32a25f15f6 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Sat, 13 Jul 2024 13:48:19 -0400 Subject: [PATCH 058/105] made a helper function for Runtime$evaluate --- tests/testthat/helper-HTML.R | 6 +----- tests/testthat/helper-functions.R | 10 +++++++++- .../testthat/test-renderer1-global-variables.R | 2 +- tests/testthat/test-renderer1-knit-print.R | 17 ++++++++--------- tests/testthat/test-renderer1-url-fragment.R | 2 +- tests/testthat/test-renderer2-widerect.R | 7 +++---- 6 files changed, 23 insertions(+), 21 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 4a3e549b9..2f719c43c 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -86,9 +86,5 @@ getClassBound <- function(geom.class, position){ script.txt <- sprintf( 'document.getElementsByClassName("%s")[0].getBoundingClientRect().%s', geom.class, position) - remDr$Runtime$evaluate(script.txt, returnByValue = TRUE)$result$value + runtime_evaluate(script=script.txt,return.value=TRUE) } - -runtime_evaluate <- function(script=NULL,returnByValue=FALSE){ - -} \ No newline at end of file diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index 8385f11a8..43fcf29cb 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -74,7 +74,7 @@ clickHTML <- function(...){ clickID <- function(...){ v <- c(...) stopifnot(length(v) == 1) - remDr$Runtime$evaluate(sprintf("document.getElementById('%s').dispatchEvent(new CustomEvent('click'))", as.character(v))) + runtime_evaluate(script=sprintf("document.getElementById('%s').dispatchEvent(new CustomEvent('click'))", as.character(v))) } rgba.pattern <- paste0( @@ -417,3 +417,11 @@ find_test_path <- function(dir = ".") { file.path(dir, ext_dir) } +runtime_evaluate <- function(script=NULL,return.value=FALSE){ + if (return.value){ + value<- remDr$Runtime$evaluate(script,returnByValue = TRUE)$result$value + }else{ + value<- remDr$Runtime$evaluate(script,returnByValue = TRUE) + } +} + diff --git a/tests/testthat/test-renderer1-global-variables.R b/tests/testthat/test-renderer1-global-variables.R index c71b7dc58..70fae3126 100644 --- a/tests/testthat/test-renderer1-global-variables.R +++ b/tests/testthat/test-renderer1-global-variables.R @@ -13,7 +13,7 @@ for(var b in window) { myArray;' getVariables <- function(){ - vars <- remDr$Runtime$evaluate(myScript,returnByValue = TRUE)$result$value + vars<-runtime_evaluate(script=myScript,return.value=TRUE) # ignore the "plot" variable -- # https://github.com/tdhock/animint/pull/62#issuecomment-100008532 # also ignore jQuery1238915281937 variable: diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index 147145002..8fbf0edd5 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -78,11 +78,10 @@ get_circles <- function(html=getHTML()) { } get_elements <- function(id){ - ##print("before div") - a <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),returnByValue = TRUE)$result$value - b <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),returnByValue = TRUE)$result$value - show_hide <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[0]", as.character(id)),returnByValue = TRUE)$result$value - widget <- remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[1]", as.character(id)),returnByValue = TRUE)$result$value + a<- runtime_evaluate(script=sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),return.value=TRUE) + b<- runtime_evaluate(script=sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),return.value=TRUE) + show_hide <- runtime_evaluate(script =sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[0]", as.character(id)), return.value=TRUE) + widget <-runtime_evaluate(script=sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[1]", as.character(id)),return.value=TRUE) list(a178=a, b934=b, show_hide=show_hide, @@ -127,11 +126,11 @@ test_that("clicking bottom legend adds/remove points", { }) clickSide<- function(position=NULL){ if(position=="top"){ - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) + runtime_evaluate(script=sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) + runtime_evaluate(script=sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) }else{ - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) - remDr$Runtime$evaluate(sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) + runtime_evaluate(script=sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) + runtime_evaluate(script=sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) } } diff --git a/tests/testthat/test-renderer1-url-fragment.R b/tests/testthat/test-renderer1-url-fragment.R index 57da70530..bea97c049 100644 --- a/tests/testthat/test-renderer1-url-fragment.R +++ b/tests/testthat/test-renderer1-url-fragment.R @@ -32,7 +32,7 @@ test_that("one observation selected for url with no selection", { expect_equal(as.numeric(opacity.str[!is.selected]), rep(0.5, 298)) }) -old_address <- remDr$Runtime$evaluate("window.location.href")$result$value +old_address <- runtime_evaluate(script="window.location.href",return.value=TRUE) new_address <- paste0(old_address, '#Species={setosa}') remDr$navigate(new_address) remDr$refresh() diff --git a/tests/testthat/test-renderer2-widerect.R b/tests/testthat/test-renderer2-widerect.R index ef85736db..27900b9c5 100644 --- a/tests/testthat/test-renderer2-widerect.R +++ b/tests/testthat/test-renderer2-widerect.R @@ -37,7 +37,7 @@ expect_source(NULL) clickSelector <- function(selectorName) { script.txt <- sprintf('childDom = document.getElementsByClassName("%s")[0]; childDom.getElementsByClassName("selectize-input")[0].dispatchEvent(new CustomEvent("click"));', selectorName) - remDr$Runtime$evaluate(script.txt) + runtime_evaluate(script=script.txt) } test_that("bottom of widerect is above line", { @@ -339,14 +339,13 @@ test_that("pause stops animation (third time)", { expect_true(old.year == new.year) }) -remDr$Runtime$evaluate("document.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));") +runtime_evaluate(script="document.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));") clickSelector("year_variable_selector_widget") sendKey("Backspace", "Backspace", 8) remDr$Input$insertText(text = "1962") clickSelector("year_variable_selector_widget") sendKey("ArrowDown", "ArrowDown", 40) sendKey("Enter", "Enter", 13) - Sys.sleep(3) test_that("typing into selectize widget changes year to 1962", { @@ -408,7 +407,7 @@ test_that("middle of transition != after when duration=2000", { expect_true(during.width != after.width) }) -remDr$Runtime$evaluate("var e = document.getElementById('plot_duration_ms_year'); e.value = 0;e.dispatchEvent(new Event('change'));") +runtime_evaluate(script="var e = document.getElementById('plot_duration_ms_year'); e.value = 0;e.dispatchEvent(new Event('change'));") Sys.sleep(1) test_that("middle of transition == after when duration=0", { From 0ae07c6bece5b35558fe97556e375fffc91a905c Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Sun, 14 Jul 2024 12:52:02 -0400 Subject: [PATCH 059/105] modified helper function modified helper function to avoid repeating API call for if else --- tests/testthat/helper-functions.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index 43fcf29cb..1db66a7c3 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -418,10 +418,11 @@ find_test_path <- function(dir = ".") { } runtime_evaluate <- function(script=NULL,return.value=FALSE){ + eval.result<- remDr$Runtime$evaluate(script,returnByValue = TRUE) if (return.value){ - value<- remDr$Runtime$evaluate(script,returnByValue = TRUE)$result$value + eval.result$result$value }else{ - value<- remDr$Runtime$evaluate(script,returnByValue = TRUE) + eval.result } } From c5762eb8fed8ea762c52abdf41651b41f9f13758 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Mon, 15 Jul 2024 00:15:07 -0400 Subject: [PATCH 060/105] reverted changes reverted changes --- .github/workflows/tests.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index abccfcf4c..025b0ea11 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -28,7 +28,9 @@ jobs: - run: /usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt install texlive texlive-fonts-extra -y - uses: r-lib/actions/setup-r@v2 - uses: r-lib/actions/setup-r-dependencies@v2 - +- with: +- extra-packages: digest, RJSONIO, gtable, plyr, reshape2, scales, knitr, chromote +- - name: install package run: R CMD INSTALL . From 273dc5fbd599270452b80b7831770b731816d4a6 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Thu, 18 Jul 2024 01:15:01 -0400 Subject: [PATCH 061/105] added extra-packages back added extra-packages back,trying to fix syntax error of earlier commit of this file --- .github/workflows/tests.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 025b0ea11..a8af979e3 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -28,9 +28,9 @@ jobs: - run: /usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt install texlive texlive-fonts-extra -y - uses: r-lib/actions/setup-r@v2 - uses: r-lib/actions/setup-r-dependencies@v2 -- with: -- extra-packages: digest, RJSONIO, gtable, plyr, reshape2, scales, knitr, chromote -- + with: + extra-packages: digest, RJSONIO, gtable, plyr, reshape2, scales, knitr, chromote + - name: install package run: R CMD INSTALL . From 76800a43ccee4d95c14a24e702442a7745ef6b2f Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Thu, 18 Jul 2024 01:52:41 -0400 Subject: [PATCH 062/105] removed extra packages removed extra packages which were added back to debug failure reason on github actions --- .github/workflows/tests.yaml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index a8af979e3..f26c3fe99 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -28,9 +28,7 @@ jobs: - run: /usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt install texlive texlive-fonts-extra -y - uses: r-lib/actions/setup-r@v2 - uses: r-lib/actions/setup-r-dependencies@v2 - with: - extra-packages: digest, RJSONIO, gtable, plyr, reshape2, scales, knitr, chromote - + - name: install package run: R CMD INSTALL . From 1830ace7181bcd9fdc71a62da30253060b13ac51 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Fri, 19 Jul 2024 19:35:36 -0400 Subject: [PATCH 063/105] helper function, empty space added new helper function and empty space --- tests/testthat/helper-functions.R | 18 +++++++++++++++++- .../testthat/test-renderer1-global-variables.R | 4 +++- tests/testthat/test-renderer1-knit-print.R | 16 ++++++++-------- tests/testthat/test-renderer2-widerect.R | 3 ++- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index 1db66a7c3..5c6b0126e 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -74,7 +74,7 @@ clickHTML <- function(...){ clickID <- function(...){ v <- c(...) stopifnot(length(v) == 1) - runtime_evaluate(script=sprintf("document.getElementById('%s').dispatchEvent(new CustomEvent('click'))", as.character(v))) + runtime_evaluate_helper(id=v,dispatch_event=TRUE) } rgba.pattern <- paste0( @@ -426,3 +426,19 @@ runtime_evaluate <- function(script=NULL,return.value=FALSE){ } } +runtime_evaluate_helper <- function(class_name=NULL, id=NULL, list_num=NULL,dispatch_event=NULL,return.value=FALSE) { + if(!is.null(class_name) && !is.null(id) && !is.null(dispatch_event)){ + script_template <- "div = document.getElementById('%s'); div.getElementsByClassName('%s')[%d].dispatchEvent(new CustomEvent('click'));" + script <- sprintf(script_template, as.character(id), class_name, list_num) + }else if(is.null(class_name) && !is.null(id) && !is.null(dispatch_event)){ + script_template <- "document.getElementById('%s').dispatchEvent(new CustomEvent('click'))" + script <- sprintf(script_template, as.character(id)) + }else if(!is.null(class_name) && !is.null(id) && is.null(dispatch_event)){ + script_template <- "div = document.getElementById('%s');div.getElementsByClassName('%s')[%d]" + script <- sprintf(script_template, as.character(id),class_name,list_num) + }else if(!is.null(class_name) && is.null(id) && !is.null(dispatch_event)){ + script_template <- "document.getElementsByClassName('%s')[%d].dispatchEvent(new CustomEvent('click'));" + script <- sprintf(script_template,class_name,list_num) + } + runtime_evaluate(script = script, return.value = return.value) +} diff --git a/tests/testthat/test-renderer1-global-variables.R b/tests/testthat/test-renderer1-global-variables.R index 70fae3126..5fae16956 100644 --- a/tests/testthat/test-renderer1-global-variables.R +++ b/tests/testthat/test-renderer1-global-variables.R @@ -13,7 +13,9 @@ for(var b in window) { myArray;' getVariables <- function(){ - vars<-runtime_evaluate(script=myScript,return.value=TRUE) + + vars<-runtime_evaluate(script=myScript, return.value=TRUE) + # ignore the "plot" variable -- # https://github.com/tdhock/animint/pull/62#issuecomment-100008532 # also ignore jQuery1238915281937 variable: diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index 8fbf0edd5..766271bdc 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -78,10 +78,10 @@ get_circles <- function(html=getHTML()) { } get_elements <- function(id){ - a<- runtime_evaluate(script=sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),return.value=TRUE) - b<- runtime_evaluate(script=sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0]", as.character(id)),return.value=TRUE) - show_hide <- runtime_evaluate(script =sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[0]", as.character(id)), return.value=TRUE) - widget <-runtime_evaluate(script=sprintf("div = document.getElementById('%s');div.getElementsByClassName('table.legend tr.label_variable')[1]", as.character(id)),return.value=TRUE) + a<- runtime_evaluate_helper(id=as.character(id),class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE) + b<- runtime_evaluate_helper(id=as.character(id),class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE) + show_hide <- runtime_evaluate_helper(id=as.character(id),class_name='table.legend tr.label_variable',list_num=0,return.value=TRUE) + widget <- runtime_evaluate_helper(id=as.character(id),class_name='table.legend tr.label_variable',list_num=1,return.value=TRUE) list(a178=a, b934=b, show_hide=show_hide, @@ -126,11 +126,11 @@ test_that("clicking bottom legend adds/remove points", { }) clickSide<- function(position=NULL){ if(position=="top"){ - runtime_evaluate(script=sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) - runtime_evaluate(script=sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1top"))) + runtime_evaluate_helper(id=as.character("plot1top"),class_name='show_hide_selector_widgets',list_num=0) + runtime_evaluate_helper(id=as.character("plot1top"),class_name='selectize-input',list_num=0) }else{ - runtime_evaluate(script=sprintf("div = document.getElementById('%s');div.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) - runtime_evaluate(script=sprintf("div = document.getElementById('%s'); div.getElementsByClassName('selectize-input')[0].dispatchEvent(new CustomEvent('click'));", as.character("plot1bottom"))) + runtime_evaluate_helper(id=as.character("plot1bottom"),class_name='show_hide_selector_widgets',list_num=0) + runtime_evaluate_helper(id=as.character("plot1bottom"),class_name='selectize-input',list_num=0) } } diff --git a/tests/testthat/test-renderer2-widerect.R b/tests/testthat/test-renderer2-widerect.R index 27900b9c5..e1ff86387 100644 --- a/tests/testthat/test-renderer2-widerect.R +++ b/tests/testthat/test-renderer2-widerect.R @@ -339,7 +339,8 @@ test_that("pause stops animation (third time)", { expect_true(old.year == new.year) }) -runtime_evaluate(script="document.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));") +#runtime_evaluate(script="document.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));") +runtime_evaluate_helper(class_name="show_hide_selector_widgets",list_num=0,dispatch_event=TRUE) clickSelector("year_variable_selector_widget") sendKey("Backspace", "Backspace", 8) remDr$Input$insertText(text = "1962") From 56a5007b489541698909442882c09b39a137b427 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Fri, 19 Jul 2024 19:51:26 -0400 Subject: [PATCH 064/105] added sleep added sleep to see it checks pass on github actions like they are locally --- tests/testthat/helper-HTML.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 2f719c43c..3b9dac27c 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -86,5 +86,6 @@ getClassBound <- function(geom.class, position){ script.txt <- sprintf( 'document.getElementsByClassName("%s")[0].getBoundingClientRect().%s', geom.class, position) + Sys.sleep(2) runtime_evaluate(script=script.txt,return.value=TRUE) } From 1f613b7967a5e3db0687ab8c0ed90c097546b0d5 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Fri, 19 Jul 2024 20:08:39 -0400 Subject: [PATCH 065/105] added dispatch_event=TRUE added dispatch_event=TRUE --- tests/testthat/test-renderer1-knit-print.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index 766271bdc..0c24c968c 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -126,11 +126,11 @@ test_that("clicking bottom legend adds/remove points", { }) clickSide<- function(position=NULL){ if(position=="top"){ - runtime_evaluate_helper(id=as.character("plot1top"),class_name='show_hide_selector_widgets',list_num=0) - runtime_evaluate_helper(id=as.character("plot1top"),class_name='selectize-input',list_num=0) - }else{ - runtime_evaluate_helper(id=as.character("plot1bottom"),class_name='show_hide_selector_widgets',list_num=0) - runtime_evaluate_helper(id=as.character("plot1bottom"),class_name='selectize-input',list_num=0) + runtime_evaluate_helper(id=as.character("plot1top"),class_name='show_hide_selector_widgets',list_num=0,dispatch_event=TRUE) + runtime_evaluate_helper(id=as.character("plot1top"),class_name='selectize-input',list_num=0,dispatch_event=TRUE) + }else if(position=="bottom"){ + runtime_evaluate_helper(id=as.character("plot1bottom"),class_name='show_hide_selector_widgets',list_num=0,dispatch_event=TRUE) + runtime_evaluate_helper(id=as.character("plot1bottom"),class_name='selectize-input',list_num=0,dispatch_event=TRUE) } } From 30f8e492e5ed7c6123ed99ffcd670e552eda60ff Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 31 Jul 2024 02:21:44 -0400 Subject: [PATCH 066/105] added space after comma and blank line --- tests/testthat/helper-functions.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index 5c6b0126e..0931c34ab 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -418,6 +418,7 @@ find_test_path <- function(dir = ".") { } runtime_evaluate <- function(script=NULL,return.value=FALSE){ + eval.result<- remDr$Runtime$evaluate(script,returnByValue = TRUE) if (return.value){ eval.result$result$value @@ -426,7 +427,7 @@ runtime_evaluate <- function(script=NULL,return.value=FALSE){ } } -runtime_evaluate_helper <- function(class_name=NULL, id=NULL, list_num=NULL,dispatch_event=NULL,return.value=FALSE) { +runtime_evaluate_helper <- function(class_name=NULL, id=NULL, list_num=NULL, dispatch_event=NULL, return.value=FALSE) { if(!is.null(class_name) && !is.null(id) && !is.null(dispatch_event)){ script_template <- "div = document.getElementById('%s'); div.getElementsByClassName('%s')[%d].dispatchEvent(new CustomEvent('click'));" script <- sprintf(script_template, as.character(id), class_name, list_num) From 1e5c897020ab8edce4e96a21b3604d3d6cf4d28e Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 31 Jul 2024 15:54:30 -0400 Subject: [PATCH 067/105] return by value always, return by value always, removed comment, removed as.character --- tests/testthat/helper-functions.R | 10 +++------- tests/testthat/test-renderer1-knit-print.R | 8 ++++---- tests/testthat/test-renderer2-widerect.R | 1 - 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index 0931c34ab..a19eb0546 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -417,14 +417,10 @@ find_test_path <- function(dir = ".") { file.path(dir, ext_dir) } -runtime_evaluate <- function(script=NULL,return.value=FALSE){ +runtime_evaluate <- function(script=NULL){ eval.result<- remDr$Runtime$evaluate(script,returnByValue = TRUE) - if (return.value){ - eval.result$result$value - }else{ - eval.result - } + } runtime_evaluate_helper <- function(class_name=NULL, id=NULL, list_num=NULL, dispatch_event=NULL, return.value=FALSE) { @@ -441,5 +437,5 @@ runtime_evaluate_helper <- function(class_name=NULL, id=NULL, list_num=NULL, dis script_template <- "document.getElementsByClassName('%s')[%d].dispatchEvent(new CustomEvent('click'));" script <- sprintf(script_template,class_name,list_num) } - runtime_evaluate(script = script, return.value = return.value) + runtime_evaluate(script = script) } diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index 0c24c968c..5d8f46e7d 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -78,10 +78,10 @@ get_circles <- function(html=getHTML()) { } get_elements <- function(id){ - a<- runtime_evaluate_helper(id=as.character(id),class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE) - b<- runtime_evaluate_helper(id=as.character(id),class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE) - show_hide <- runtime_evaluate_helper(id=as.character(id),class_name='table.legend tr.label_variable',list_num=0,return.value=TRUE) - widget <- runtime_evaluate_helper(id=as.character(id),class_name='table.legend tr.label_variable',list_num=1,return.value=TRUE) + a<- runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE) + b<- runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE) + show_hide <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=0,return.value=TRUE) + widget <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=1,return.value=TRUE) list(a178=a, b934=b, show_hide=show_hide, diff --git a/tests/testthat/test-renderer2-widerect.R b/tests/testthat/test-renderer2-widerect.R index e1ff86387..9dc1a2cbb 100644 --- a/tests/testthat/test-renderer2-widerect.R +++ b/tests/testthat/test-renderer2-widerect.R @@ -339,7 +339,6 @@ test_that("pause stops animation (third time)", { expect_true(old.year == new.year) }) -#runtime_evaluate(script="document.getElementsByClassName('show_hide_selector_widgets')[0].dispatchEvent(new CustomEvent('click'));") runtime_evaluate_helper(class_name="show_hide_selector_widgets",list_num=0,dispatch_event=TRUE) clickSelector("year_variable_selector_widget") sendKey("Backspace", "Backspace", 8) From 460567eedd744b875bd1decc8a9d26feaa7144ad Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Thu, 1 Aug 2024 20:14:23 -0400 Subject: [PATCH 068/105] corrected runtime_evaluate to return by value corrected runtime_evaluate to return by value and tests which use it --- tests/testthat/helper-HTML.R | 2 +- tests/testthat/helper-functions.R | 3 ++- tests/testthat/test-renderer1-global-variables.R | 2 +- tests/testthat/test-renderer1-url-fragment.R | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 3b9dac27c..ccb187bf1 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -87,5 +87,5 @@ getClassBound <- function(geom.class, position){ 'document.getElementsByClassName("%s")[0].getBoundingClientRect().%s', geom.class, position) Sys.sleep(2) - runtime_evaluate(script=script.txt,return.value=TRUE) + runtime_evaluate(script=script.txt) } diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index a19eb0546..6e711171d 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -419,11 +419,12 @@ find_test_path <- function(dir = ".") { runtime_evaluate <- function(script=NULL){ - eval.result<- remDr$Runtime$evaluate(script,returnByValue = TRUE) + eval.result<- remDr$Runtime$evaluate(script,returnByValue = TRUE)$result$value } runtime_evaluate_helper <- function(class_name=NULL, id=NULL, list_num=NULL, dispatch_event=NULL, return.value=FALSE) { + if(!is.null(class_name) && !is.null(id) && !is.null(dispatch_event)){ script_template <- "div = document.getElementById('%s'); div.getElementsByClassName('%s')[%d].dispatchEvent(new CustomEvent('click'));" script <- sprintf(script_template, as.character(id), class_name, list_num) diff --git a/tests/testthat/test-renderer1-global-variables.R b/tests/testthat/test-renderer1-global-variables.R index 5fae16956..a09daf6e3 100644 --- a/tests/testthat/test-renderer1-global-variables.R +++ b/tests/testthat/test-renderer1-global-variables.R @@ -14,7 +14,7 @@ myArray;' getVariables <- function(){ - vars<-runtime_evaluate(script=myScript, return.value=TRUE) + vars<-runtime_evaluate(script=myScript) # ignore the "plot" variable -- # https://github.com/tdhock/animint/pull/62#issuecomment-100008532 diff --git a/tests/testthat/test-renderer1-url-fragment.R b/tests/testthat/test-renderer1-url-fragment.R index bea97c049..d26324307 100644 --- a/tests/testthat/test-renderer1-url-fragment.R +++ b/tests/testthat/test-renderer1-url-fragment.R @@ -32,7 +32,7 @@ test_that("one observation selected for url with no selection", { expect_equal(as.numeric(opacity.str[!is.selected]), rep(0.5, 298)) }) -old_address <- runtime_evaluate(script="window.location.href",return.value=TRUE) +old_address <- runtime_evaluate(script="window.location.href") new_address <- paste0(old_address, '#Species={setosa}') remDr$navigate(new_address) remDr$refresh() From c639517e22192410adb680adb6c3e9405d140912 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Fri, 2 Aug 2024 13:29:19 -0400 Subject: [PATCH 069/105] added space, returning in helper function directly without saving in a variable --- tests/testthat/helper-functions.R | 2 +- tests/testthat/test-renderer1-knit-print.R | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index 6e711171d..2db084dcd 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -419,7 +419,7 @@ find_test_path <- function(dir = ".") { runtime_evaluate <- function(script=NULL){ - eval.result<- remDr$Runtime$evaluate(script,returnByValue = TRUE)$result$value + remDr$Runtime$evaluate(script,returnByValue = TRUE)$result$value } diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index 5d8f46e7d..bca66ee4c 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -78,10 +78,12 @@ get_circles <- function(html=getHTML()) { } get_elements <- function(id){ + a<- runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE) b<- runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE) show_hide <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=0,return.value=TRUE) widget <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=1,return.value=TRUE) + list(a178=a, b934=b, show_hide=show_hide, From e00b55679c3a19b56255589d298c47fdeb401bff Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Fri, 2 Aug 2024 14:41:12 -0400 Subject: [PATCH 070/105] added for loop --- tests/testthat/helper-HTML.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index ccb187bf1..ea43054a9 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -78,8 +78,10 @@ get_grid_lines <- function(html, p_name, grid_class){ } # Function to send a key event sendKey <- function(key, code, keyCode) { - remDr$Input$dispatchKeyEvent(type = "keyDown", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) - remDr$Input$dispatchKeyEvent(type = "keyUp", key = key, code = code, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = keyCode) + types <- c("keyDown", "keyUp") + for (type in types) { + remDr$Input$dispatchKeyEvent(type = type,key = key,code = code,windowsVirtualKeyCode = keyCode,nativeVirtualKeyCode = keyCode) + } } getClassBound <- function(geom.class, position){ From 61ced101bdda57f449afbe16dfa5b5d0807f2466 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Sun, 4 Aug 2024 12:26:12 -0400 Subject: [PATCH 071/105] made variable names more informative --- tests/testthat/test-renderer1-knit-print.R | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index bca66ee4c..a2fbe4944 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -79,15 +79,15 @@ get_circles <- function(html=getHTML()) { get_elements <- function(id){ - a<- runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE) - b<- runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE) - show_hide <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=0,return.value=TRUE) - widget <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=1,return.value=TRUE) + top_widget_show_hide<- runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE) + bottom_widget_show_hide<- runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE) + legend_show_hide <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=0,return.value=TRUE) + legend_widget <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=1,return.value=TRUE) - list(a178=a, - b934=b, - show_hide=show_hide, - widget=widget) + list(a178=top_widget_show_hide, + b934=bottom_widget_show_hide, + show_hide=legend_show_hide, + widget=legend_widget) } plot1top <- get_elements("plot1top") From 73604ab7b726ad35843b352234ce345c4d7d4f0c Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Sun, 4 Aug 2024 12:34:57 -0400 Subject: [PATCH 072/105] added sleep to try solving perpetual run on github actions --- tests/testthat/test-renderer1-axis-angle-rotate.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testthat/test-renderer1-axis-angle-rotate.R b/tests/testthat/test-renderer1-axis-angle-rotate.R index aeed1fe66..3d0ec0334 100644 --- a/tests/testthat/test-renderer1-axis-angle-rotate.R +++ b/tests/testthat/test-renderer1-axis-angle-rotate.R @@ -29,7 +29,9 @@ expect_rotate_anchor <- function(info, rotate, anchor){ expect_match(rotated["transform", ], paste0("rotate(", rotate), fixed=TRUE) # http://stackoverflow.com/questions/442404/retrieve-the-position-x-y-of-an-html-element tick_box <- getClassBound("xaxis", "bottom") + Sys.sleep(2) title_box <- getClassBound("xtitle", "top") + Sys.sleep(2) #An offset of 2.5 px is added in line below but it may need to changed slightly if browser environment is different. #more information of getBoundingClientRect() and why value it returns depends on specific browser environment is in the links below #https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect From 7d119a4491cb47e3113d48c75dd17499d6565513 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Sun, 4 Aug 2024 13:39:50 -0400 Subject: [PATCH 073/105] added sleep to try solving perpetual run on github actions --- tests/testthat/helper-HTML.R | 1 + tests/testthat/test-renderer1-axis-angle-rotate.R | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index ea43054a9..8f0d1959c 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -90,4 +90,5 @@ getClassBound <- function(geom.class, position){ geom.class, position) Sys.sleep(2) runtime_evaluate(script=script.txt) + Sys.sleep(2) } diff --git a/tests/testthat/test-renderer1-axis-angle-rotate.R b/tests/testthat/test-renderer1-axis-angle-rotate.R index 3d0ec0334..aeed1fe66 100644 --- a/tests/testthat/test-renderer1-axis-angle-rotate.R +++ b/tests/testthat/test-renderer1-axis-angle-rotate.R @@ -29,9 +29,7 @@ expect_rotate_anchor <- function(info, rotate, anchor){ expect_match(rotated["transform", ], paste0("rotate(", rotate), fixed=TRUE) # http://stackoverflow.com/questions/442404/retrieve-the-position-x-y-of-an-html-element tick_box <- getClassBound("xaxis", "bottom") - Sys.sleep(2) title_box <- getClassBound("xtitle", "top") - Sys.sleep(2) #An offset of 2.5 px is added in line below but it may need to changed slightly if browser environment is different. #more information of getBoundingClientRect() and why value it returns depends on specific browser environment is in the links below #https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect From 607d94e2b8ea6a720baff285fcd29d60f2009dcd Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Sun, 4 Aug 2024 13:56:49 -0400 Subject: [PATCH 074/105] removed sleep in helper function because widerect test failed --- tests/testthat/helper-HTML.R | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 8f0d1959c..ea43054a9 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -90,5 +90,4 @@ getClassBound <- function(geom.class, position){ geom.class, position) Sys.sleep(2) runtime_evaluate(script=script.txt) - Sys.sleep(2) } From 93478c670b214ce1bb9cd6fcee7e43ed909e79cf Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Mon, 5 Aug 2024 08:47:07 -0400 Subject: [PATCH 075/105] changed names in list --- tests/testthat/test-renderer1-knit-print.R | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index a2fbe4944..7a024777c 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -84,8 +84,8 @@ get_elements <- function(id){ legend_show_hide <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=0,return.value=TRUE) legend_widget <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=1,return.value=TRUE) - list(a178=top_widget_show_hide, - b934=bottom_widget_show_hide, + list(top_widget_show_hide=top_widget_show_hide, + bottom_widget_show_hide=bottom_widget_show_hide, show_hide=legend_show_hide, widget=legend_widget) } @@ -98,32 +98,32 @@ plot1bottom <- get_elements("plot1bottom") test_that("clicking top legend adds/remove points", { expect_equal(get_circles(), list(10, 10)) - clickID("plot1top_q_label_variable_a178") + clickID("plot1top_q_label_variable_top_widget_show_hide") expect_equal(get_circles(), list(5, 10)) - clickID("plot1top_q_label_variable_b934") + clickID("plot1top_q_label_variable_bottom_widget_show_hide") expect_equal(get_circles(), list(0, 10)) - clickID("plot1top_q_label_variable_b934") + clickID("plot1top_q_label_variable_bottom_widget_show_hide") expect_equal(get_circles(), list(5, 10)) - clickID("plot1top_q_label_variable_a178") + clickID("plot1top_q_label_variable_top_widget_show_hide") expect_equal(get_circles(), list(10, 10)) }) test_that("clicking bottom legend adds/remove points", { expect_equal(get_circles(), list(10, 10)) - clickID("plot1bottom_q_label_variable_a178") + clickID("plot1bottom_q_label_variable_top_widget_show_hide") expect_equal(get_circles(), list(10, 5)) - clickID("plot1bottom_q_label_variable_b934") + clickID("plot1bottom_q_label_variable_bottom_widget_show_hide") expect_equal(get_circles(), list(10, 0)) - clickID("plot1bottom_q_label_variable_b934") + clickID("plot1bottom_q_label_variable_bottom_widget_show_hide") expect_equal(get_circles(), list(10, 5)) - clickID("plot1bottom_q_label_variable_a178") + clickID("plot1bottom_q_label_variable_top_widget_show_hide") expect_equal(get_circles(), list(10, 10)) }) clickSide<- function(position=NULL){ From a0e51eaa9d9455e37358b2e5194b9fdc8f587e56 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Mon, 5 Aug 2024 08:59:55 -0400 Subject: [PATCH 076/105] reverted names in the list --- tests/testthat/test-renderer1-knit-print.R | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index 7a024777c..a2fbe4944 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -84,8 +84,8 @@ get_elements <- function(id){ legend_show_hide <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=0,return.value=TRUE) legend_widget <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=1,return.value=TRUE) - list(top_widget_show_hide=top_widget_show_hide, - bottom_widget_show_hide=bottom_widget_show_hide, + list(a178=top_widget_show_hide, + b934=bottom_widget_show_hide, show_hide=legend_show_hide, widget=legend_widget) } @@ -98,32 +98,32 @@ plot1bottom <- get_elements("plot1bottom") test_that("clicking top legend adds/remove points", { expect_equal(get_circles(), list(10, 10)) - clickID("plot1top_q_label_variable_top_widget_show_hide") + clickID("plot1top_q_label_variable_a178") expect_equal(get_circles(), list(5, 10)) - clickID("plot1top_q_label_variable_bottom_widget_show_hide") + clickID("plot1top_q_label_variable_b934") expect_equal(get_circles(), list(0, 10)) - clickID("plot1top_q_label_variable_bottom_widget_show_hide") + clickID("plot1top_q_label_variable_b934") expect_equal(get_circles(), list(5, 10)) - clickID("plot1top_q_label_variable_top_widget_show_hide") + clickID("plot1top_q_label_variable_a178") expect_equal(get_circles(), list(10, 10)) }) test_that("clicking bottom legend adds/remove points", { expect_equal(get_circles(), list(10, 10)) - clickID("plot1bottom_q_label_variable_top_widget_show_hide") + clickID("plot1bottom_q_label_variable_a178") expect_equal(get_circles(), list(10, 5)) - clickID("plot1bottom_q_label_variable_bottom_widget_show_hide") + clickID("plot1bottom_q_label_variable_b934") expect_equal(get_circles(), list(10, 0)) - clickID("plot1bottom_q_label_variable_bottom_widget_show_hide") + clickID("plot1bottom_q_label_variable_b934") expect_equal(get_circles(), list(10, 5)) - clickID("plot1bottom_q_label_variable_top_widget_show_hide") + clickID("plot1bottom_q_label_variable_a178") expect_equal(get_circles(), list(10, 10)) }) clickSide<- function(position=NULL){ From e6c837c7d2e25758cf1bc14c9f60cf639ab8dc6e Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Mon, 5 Aug 2024 10:01:52 -0400 Subject: [PATCH 077/105] changing names inside list --- tests/testthat/test-renderer1-knit-print.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index a2fbe4944..57cc090fc 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -84,7 +84,7 @@ get_elements <- function(id){ legend_show_hide <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=0,return.value=TRUE) legend_widget <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=1,return.value=TRUE) - list(a178=top_widget_show_hide, + list(a172=top_widget_show_hide, b934=bottom_widget_show_hide, show_hide=legend_show_hide, widget=legend_widget) @@ -98,7 +98,7 @@ plot1bottom <- get_elements("plot1bottom") test_that("clicking top legend adds/remove points", { expect_equal(get_circles(), list(10, 10)) - clickID("plot1top_q_label_variable_a178") + clickID("plot1top_q_label_variable_a172") expect_equal(get_circles(), list(5, 10)) clickID("plot1top_q_label_variable_b934") @@ -107,14 +107,14 @@ test_that("clicking top legend adds/remove points", { clickID("plot1top_q_label_variable_b934") expect_equal(get_circles(), list(5, 10)) - clickID("plot1top_q_label_variable_a178") + clickID("plot1top_q_label_variable_a172") expect_equal(get_circles(), list(10, 10)) }) test_that("clicking bottom legend adds/remove points", { expect_equal(get_circles(), list(10, 10)) - clickID("plot1bottom_q_label_variable_a178") + clickID("plot1bottom_q_label_variable_a172") expect_equal(get_circles(), list(10, 5)) clickID("plot1bottom_q_label_variable_b934") @@ -123,7 +123,7 @@ test_that("clicking bottom legend adds/remove points", { clickID("plot1bottom_q_label_variable_b934") expect_equal(get_circles(), list(10, 5)) - clickID("plot1bottom_q_label_variable_a178") + clickID("plot1bottom_q_label_variable_a172") expect_equal(get_circles(), list(10, 10)) }) clickSide<- function(position=NULL){ From 97202cfb3f839366f69e736d99084b62864f03e7 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Mon, 5 Aug 2024 10:21:34 -0400 Subject: [PATCH 078/105] changing names inside list --- tests/testthat/test-renderer1-knit-print.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index 57cc090fc..fb72751dd 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -84,7 +84,7 @@ get_elements <- function(id){ legend_show_hide <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=0,return.value=TRUE) legend_widget <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=1,return.value=TRUE) - list(a172=top_widget_show_hide, + list(a1721=top_widget_show_hide, b934=bottom_widget_show_hide, show_hide=legend_show_hide, widget=legend_widget) @@ -98,7 +98,7 @@ plot1bottom <- get_elements("plot1bottom") test_that("clicking top legend adds/remove points", { expect_equal(get_circles(), list(10, 10)) - clickID("plot1top_q_label_variable_a172") + clickID("plot1top_q_label_variable_a1721") expect_equal(get_circles(), list(5, 10)) clickID("plot1top_q_label_variable_b934") @@ -107,14 +107,14 @@ test_that("clicking top legend adds/remove points", { clickID("plot1top_q_label_variable_b934") expect_equal(get_circles(), list(5, 10)) - clickID("plot1top_q_label_variable_a172") + clickID("plot1top_q_label_variable_a1721") expect_equal(get_circles(), list(10, 10)) }) test_that("clicking bottom legend adds/remove points", { expect_equal(get_circles(), list(10, 10)) - clickID("plot1bottom_q_label_variable_a172") + clickID("plot1bottom_q_label_variable_a1721") expect_equal(get_circles(), list(10, 5)) clickID("plot1bottom_q_label_variable_b934") @@ -123,7 +123,7 @@ test_that("clicking bottom legend adds/remove points", { clickID("plot1bottom_q_label_variable_b934") expect_equal(get_circles(), list(10, 5)) - clickID("plot1bottom_q_label_variable_a172") + clickID("plot1bottom_q_label_variable_a1721") expect_equal(get_circles(), list(10, 10)) }) clickSide<- function(position=NULL){ From 9aa2419c1edf204ad5c47e3da5127e8880629dec Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Mon, 5 Aug 2024 12:19:06 -0400 Subject: [PATCH 079/105] reverted values in list --- tests/testthat/test-renderer1-knit-print.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index fb72751dd..a2fbe4944 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -84,7 +84,7 @@ get_elements <- function(id){ legend_show_hide <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=0,return.value=TRUE) legend_widget <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=1,return.value=TRUE) - list(a1721=top_widget_show_hide, + list(a178=top_widget_show_hide, b934=bottom_widget_show_hide, show_hide=legend_show_hide, widget=legend_widget) @@ -98,7 +98,7 @@ plot1bottom <- get_elements("plot1bottom") test_that("clicking top legend adds/remove points", { expect_equal(get_circles(), list(10, 10)) - clickID("plot1top_q_label_variable_a1721") + clickID("plot1top_q_label_variable_a178") expect_equal(get_circles(), list(5, 10)) clickID("plot1top_q_label_variable_b934") @@ -107,14 +107,14 @@ test_that("clicking top legend adds/remove points", { clickID("plot1top_q_label_variable_b934") expect_equal(get_circles(), list(5, 10)) - clickID("plot1top_q_label_variable_a1721") + clickID("plot1top_q_label_variable_a178") expect_equal(get_circles(), list(10, 10)) }) test_that("clicking bottom legend adds/remove points", { expect_equal(get_circles(), list(10, 10)) - clickID("plot1bottom_q_label_variable_a1721") + clickID("plot1bottom_q_label_variable_a178") expect_equal(get_circles(), list(10, 5)) clickID("plot1bottom_q_label_variable_b934") @@ -123,7 +123,7 @@ test_that("clicking bottom legend adds/remove points", { clickID("plot1bottom_q_label_variable_b934") expect_equal(get_circles(), list(10, 5)) - clickID("plot1bottom_q_label_variable_a1721") + clickID("plot1bottom_q_label_variable_a178") expect_equal(get_circles(), list(10, 10)) }) clickSide<- function(position=NULL){ From 4174041c85015dccd8c0f7ea8d505c5d328b12d0 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Mon, 5 Aug 2024 17:31:58 -0400 Subject: [PATCH 080/105] eliminated intermediate variables --- tests/testthat/test-renderer1-knit-print.R | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index a2fbe4944..ceae3f0e8 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -79,15 +79,10 @@ get_circles <- function(html=getHTML()) { get_elements <- function(id){ - top_widget_show_hide<- runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE) - bottom_widget_show_hide<- runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE) - legend_show_hide <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=0,return.value=TRUE) - legend_widget <- runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=1,return.value=TRUE) - - list(a178=top_widget_show_hide, - b934=bottom_widget_show_hide, - show_hide=legend_show_hide, - widget=legend_widget) + list(a178=runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE), + b934=runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE), + show_hide=runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=0,return.value=TRUE), + widget=runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=1,return.value=TRUE)) } plot1top <- get_elements("plot1top") From 52c88b07915833bffe52f827e3b8c5bf9a5cb4cf Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Wed, 7 Aug 2024 07:17:24 -0400 Subject: [PATCH 081/105] simplify runtime_evaluate_helper --- tests/testthat/helper-functions.R | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index 2db084dcd..cb0d8f52d 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -418,25 +418,14 @@ find_test_path <- function(dir = ".") { } runtime_evaluate <- function(script=NULL){ - remDr$Runtime$evaluate(script,returnByValue = TRUE)$result$value - } -runtime_evaluate_helper <- function(class_name=NULL, id=NULL, list_num=NULL, dispatch_event=NULL, return.value=FALSE) { - - if(!is.null(class_name) && !is.null(id) && !is.null(dispatch_event)){ - script_template <- "div = document.getElementById('%s'); div.getElementsByClassName('%s')[%d].dispatchEvent(new CustomEvent('click'));" - script <- sprintf(script_template, as.character(id), class_name, list_num) - }else if(is.null(class_name) && !is.null(id) && !is.null(dispatch_event)){ - script_template <- "document.getElementById('%s').dispatchEvent(new CustomEvent('click'))" - script <- sprintf(script_template, as.character(id)) - }else if(!is.null(class_name) && !is.null(id) && is.null(dispatch_event)){ - script_template <- "div = document.getElementById('%s');div.getElementsByClassName('%s')[%d]" - script <- sprintf(script_template, as.character(id),class_name,list_num) - }else if(!is.null(class_name) && is.null(id) && !is.null(dispatch_event)){ - script_template <- "document.getElementsByClassName('%s')[%d].dispatchEvent(new CustomEvent('click'));" - script <- sprintf(script_template,class_name,list_num) - } - runtime_evaluate(script = script) +runtime_evaluate_helper <- function(class_name=NULL, id=NULL, list_num=NULL, dispatch_event=NULL) { + runtime_evaluate(script = paste0( + "document", + if(is.character(id))sprintf(".getElementById('%s')", id), + if(is.character(class_name))sprintf(".getElementsByClassName('%s')", class_name), + if(is.atomic(list_num))sprintf("[%d]", as.integer(list_num)), + if(isTRUE(dispatch_event))".dispatchEvent(new CustomEvent('click'))")) } From 5a3ca36a0de1779aef3a8ee370d293af84a70a06 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 7 Aug 2024 12:10:46 -0400 Subject: [PATCH 082/105] removed empty line --- tests/testthat/helper-functions.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index 2db084dcd..721d8263e 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -418,9 +418,7 @@ find_test_path <- function(dir = ".") { } runtime_evaluate <- function(script=NULL){ - remDr$Runtime$evaluate(script,returnByValue = TRUE)$result$value - } runtime_evaluate_helper <- function(class_name=NULL, id=NULL, list_num=NULL, dispatch_event=NULL, return.value=FALSE) { From 7cbc05a40066060a4c1c5f15c05123b01ca72ae8 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 7 Aug 2024 12:17:48 -0400 Subject: [PATCH 083/105] removed sprintf and port argument removed sprintf and port argument --- tests/testthat/helper-HTML.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index ea43054a9..7327c5ecd 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -30,7 +30,7 @@ getHTML <- function(){ #' @export #' @seealso \link{tests_run} #' -tests_init <- function(dir = ".", port = 4848, ...) { +tests_init <- function(dir = ".", ...) { # try to exit out of previously initated processes ex <- tests_exit() # start a non-blocking local file server under path/to/animint/tests/testhat @@ -58,7 +58,7 @@ tests_init <- function(dir = ".", port = 4848, ...) { chrome.session$DOM$getOuterHTML(doc$root$nodeId)$outerHTML } remDr <<- chrome.session - remDr$navigate(sprintf("http://localhost:4848/animint-htmltest/")) + remDr$navigate("http://localhost:4848/animint-htmltest/") invisible(TRUE) } ## get both horizontal and vertical grid lines From 2479b4eb2e3951b0d81045ba5dcf20d152735ab9 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 7 Aug 2024 12:31:14 -0400 Subject: [PATCH 084/105] removed port argument's use --- tests/testthat/helper-HTML.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 7327c5ecd..1018c57f4 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -35,7 +35,7 @@ tests_init <- function(dir = ".", ...) { ex <- tests_exit() # start a non-blocking local file server under path/to/animint/tests/testhat testPath <- find_test_path(dir) - run_servr(port = port, directory = testPath) + run_servr(port = "4848", directory = testPath) # animint tests are performed in path/to/testthat/animint-htmltest/ # note this path has to match the out.dir argument in animint2THML... testDir <- file.path(testPath, "animint-htmltest") From 9c37ee04250f8cea716a74135daf35de3f2551ff Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 7 Aug 2024 12:41:59 -0400 Subject: [PATCH 085/105] corrected port type from %s to %d --- tests/testthat/helper-HTML.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 1018c57f4..3e7fe5a22 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -35,7 +35,7 @@ tests_init <- function(dir = ".", ...) { ex <- tests_exit() # start a non-blocking local file server under path/to/animint/tests/testhat testPath <- find_test_path(dir) - run_servr(port = "4848", directory = testPath) + run_servr(port = 4848, directory = testPath) # animint tests are performed in path/to/testthat/animint-htmltest/ # note this path has to match the out.dir argument in animint2THML... testDir <- file.path(testPath, "animint-htmltest") From 576b1ce897afe2df7c8bd1e46b15ffc6ead11cdc Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Wed, 7 Aug 2024 12:57:02 -0400 Subject: [PATCH 086/105] removed return by value removed return by value --- tests/testthat/test-renderer1-knit-print.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index ceae3f0e8..e39d3ef67 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -79,10 +79,10 @@ get_circles <- function(html=getHTML()) { get_elements <- function(id){ - list(a178=runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE), - b934=runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0,return.value=TRUE), - show_hide=runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=0,return.value=TRUE), - widget=runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=1,return.value=TRUE)) + list(a178=runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0), + b934=runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=1), + show_hide=runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=0), + widget=runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=1)) } plot1top <- get_elements("plot1top") From 22582458b117da9584cd4234cb2debc5b00ad4b7 Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Sun, 11 Aug 2024 15:21:54 -0400 Subject: [PATCH 087/105] refactor sendKey, fix style --- tests/testthat/helper-HTML.R | 8 ++++-- .../test-renderer1-global-variables.R | 4 +-- tests/testthat/test-renderer1-knit-print.R | 25 ++++++++----------- tests/testthat/test-renderer2-widerect.R | 10 ++++---- tests/testthat/test-shiny.R | 4 ++- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 3e7fe5a22..24c2dab32 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -77,10 +77,14 @@ get_grid_lines <- function(html, p_name, grid_class){ return(list(hor=attr_h, vert=attr_v)) } # Function to send a key event -sendKey <- function(key, code, keyCode) { +sendKey <- function(key) { types <- c("keyDown", "keyUp") + key2code <- c( + Backspace=8, + Enter=13, + ArrowDown=40) for (type in types) { - remDr$Input$dispatchKeyEvent(type = type,key = key,code = code,windowsVirtualKeyCode = keyCode,nativeVirtualKeyCode = keyCode) + remDr$Input$dispatchKeyEvent(type = type, key = key, code = key, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = key2code[[key]]) } } diff --git a/tests/testthat/test-renderer1-global-variables.R b/tests/testthat/test-renderer1-global-variables.R index a09daf6e3..a6dc18da6 100644 --- a/tests/testthat/test-renderer1-global-variables.R +++ b/tests/testthat/test-renderer1-global-variables.R @@ -13,9 +13,7 @@ for(var b in window) { myArray;' getVariables <- function(){ - - vars<-runtime_evaluate(script=myScript) - + vars <- runtime_evaluate(script=myScript) # ignore the "plot" variable -- # https://github.com/tdhock/animint/pull/62#issuecomment-100008532 # also ignore jQuery1238915281937 variable: diff --git a/tests/testthat/test-renderer1-knit-print.R b/tests/testthat/test-renderer1-knit-print.R index e39d3ef67..f7059f00c 100644 --- a/tests/testthat/test-renderer1-knit-print.R +++ b/tests/testthat/test-renderer1-knit-print.R @@ -78,11 +78,10 @@ get_circles <- function(html=getHTML()) { } get_elements <- function(id){ - - list(a178=runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=0), - b934=runtime_evaluate_helper(id=id,class_name='show_hide_selector_widgets',list_num=1), - show_hide=runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=0), - widget=runtime_evaluate_helper(id=id,class_name='table.legend tr.label_variable',list_num=1)) + list(a178=runtime_evaluate_helper(id=id, class_name='show_hide_selector_widgets', list_num=0), + b934=runtime_evaluate_helper(id=id, class_name='show_hide_selector_widgets', list_num=1), + show_hide=runtime_evaluate_helper(id=id, class_name='table.legend tr.label_variable', list_num=0), + widget=runtime_evaluate_helper(id=id, class_name='table.legend tr.label_variable', list_num=1)) } plot1top <- get_elements("plot1top") @@ -121,24 +120,20 @@ test_that("clicking bottom legend adds/remove points", { clickID("plot1bottom_q_label_variable_a178") expect_equal(get_circles(), list(10, 10)) }) -clickSide<- function(position=NULL){ - if(position=="top"){ - runtime_evaluate_helper(id=as.character("plot1top"),class_name='show_hide_selector_widgets',list_num=0,dispatch_event=TRUE) - runtime_evaluate_helper(id=as.character("plot1top"),class_name='selectize-input',list_num=0,dispatch_event=TRUE) - }else if(position=="bottom"){ - runtime_evaluate_helper(id=as.character("plot1bottom"),class_name='show_hide_selector_widgets',list_num=0,dispatch_event=TRUE) - runtime_evaluate_helper(id=as.character("plot1bottom"),class_name='selectize-input',list_num=0,dispatch_event=TRUE) - } +clickSide <- function(position=NULL){ + id <- paste0("plot1", position) + runtime_evaluate_helper(id=id, class_name='show_hide_selector_widgets', list_num=0, dispatch_event=TRUE) + runtime_evaluate_helper(id=id, class_name='selectize-input', list_num=0, dispatch_event=TRUE) } sendBackspace <- function() { - sendKey("Backspace", "Backspace", 8) + sendKey("Backspace") Sys.sleep(0.5) } send <- function(alphabet) { remDr$Input$insertText(text = alphabet) - sendKey("Enter", "Enter", 13) + sendKey("Enter") Sys.sleep(0.5) } diff --git a/tests/testthat/test-renderer2-widerect.R b/tests/testthat/test-renderer2-widerect.R index 9dc1a2cbb..df2c382f3 100644 --- a/tests/testthat/test-renderer2-widerect.R +++ b/tests/testthat/test-renderer2-widerect.R @@ -341,11 +341,11 @@ test_that("pause stops animation (third time)", { runtime_evaluate_helper(class_name="show_hide_selector_widgets",list_num=0,dispatch_event=TRUE) clickSelector("year_variable_selector_widget") -sendKey("Backspace", "Backspace", 8) +sendKey("Backspace") remDr$Input$insertText(text = "1962") clickSelector("year_variable_selector_widget") -sendKey("ArrowDown", "ArrowDown", 40) -sendKey("Enter", "Enter", 13) +sendKey("ArrowDown") +sendKey("Enter") Sys.sleep(3) test_that("typing into selectize widget changes year to 1962", { @@ -365,7 +365,7 @@ test_that("initial countries same as first", { clickSelector("country_variable_selector_widget") remDr$Input$insertText(text = "Afg") -sendKey("Enter", "Enter", 13) +sendKey("Enter") Sys.sleep(1) @@ -376,7 +376,7 @@ test_that("Afg autocompletes to Afghanistan", { }) clickSelector("country_variable_selector_widget") -sendKey("Backspace", "Backspace", 8) +sendKey("Backspace") Sys.sleep(1) test_that("backspace removes Afghanistan from selected countries", { diff --git a/tests/testthat/test-shiny.R b/tests/testthat/test-shiny.R index cb570556a..e019ed792 100644 --- a/tests/testthat/test-shiny.R +++ b/tests/testthat/test-shiny.R @@ -2,7 +2,9 @@ acontext("shiny") ## We do not need if(on wercker or travis){skip shiny test} as of 10 ## Oct 2015, since we only run tests that match the TEST_SUITE env -## var, and test-shiny.R never matches. +## var, and test-shiny.R never matches. TODO convert +## sendKeysToActiveElement to new sendKeys, get shiny tests working on +## new chromote framework. ## shiny tests require navigating to different ports, so remember ## where we are and return when tests are done From 41d1c10f7735406dc725664259a631569c6b161d Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Sun, 11 Aug 2024 22:29:39 -0400 Subject: [PATCH 088/105] changed keyCode to key2code[[key]] --- tests/testthat/helper-HTML.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 24c2dab32..ef9fc968a 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -84,7 +84,7 @@ sendKey <- function(key) { Enter=13, ArrowDown=40) for (type in types) { - remDr$Input$dispatchKeyEvent(type = type, key = key, code = key, windowsVirtualKeyCode = keyCode, nativeVirtualKeyCode = key2code[[key]]) + remDr$Input$dispatchKeyEvent(type = type, key = key, code = key, windowsVirtualKeyCode = key2code[[key]], nativeVirtualKeyCode = key2code[[key]]) } } From 49e923a33aaefe22865f72c143f253aebc3e4a43 Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Sun, 11 Aug 2024 22:34:03 -0400 Subject: [PATCH 089/105] rm types --- tests/testthat/helper-HTML.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index ef9fc968a..380ba91fb 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -78,12 +78,12 @@ get_grid_lines <- function(html, p_name, grid_class){ } # Function to send a key event sendKey <- function(key) { - types <- c("keyDown", "keyUp") + stopifnot(is.character(key)) key2code <- c( Backspace=8, Enter=13, ArrowDown=40) - for (type in types) { + for (type in c("keyDown", "keyUp")) { remDr$Input$dispatchKeyEvent(type = type, key = key, code = key, windowsVirtualKeyCode = key2code[[key]], nativeVirtualKeyCode = key2code[[key]]) } } From 2b15fba8bf9e42c87d56e921d1c0804306a12882 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Mon, 12 Aug 2024 11:32:41 -0400 Subject: [PATCH 090/105] commented for key-codes commented where virtual key-codes come from --- tests/testthat/helper-HTML.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 380ba91fb..e7ea03c2f 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -79,6 +79,10 @@ get_grid_lines <- function(html, p_name, grid_class){ # Function to send a key event sendKey <- function(key) { stopifnot(is.character(key)) + #The key codes in the list below are adopted from Windows Virtual keycode standards + #https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes + # VK_BACK->Backspace, VK_RETURN->Enter, VK_DOWN->ArrowDown + # we use the decimal values of the key codes key2code <- c( Backspace=8, Enter=13, From 2a633480122337d98888b8686a6f81b699828ad9 Mon Sep 17 00:00:00 2001 From: siddhesh195 Date: Mon, 12 Aug 2024 12:16:59 -0400 Subject: [PATCH 091/105] modified comment for key codes --- tests/testthat/helper-HTML.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index e7ea03c2f..d9bfc84a3 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -82,7 +82,7 @@ sendKey <- function(key) { #The key codes in the list below are adopted from Windows Virtual keycode standards #https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes # VK_BACK->Backspace, VK_RETURN->Enter, VK_DOWN->ArrowDown - # we use the decimal values of the key codes + # we use the corresponding decimal values of the key codes given in hex value in the above link key2code <- c( Backspace=8, Enter=13, From 053dde95b5be7f2fced0741cc71a1ddb8b900d19 Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Wed, 14 Aug 2024 09:58:00 -0400 Subject: [PATCH 092/105] link chromote docs and mozilla key event page --- tests/testthat/helper-HTML.R | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index d9bfc84a3..443337082 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -76,19 +76,23 @@ get_grid_lines <- function(html, p_name, grid_class){ attr_v <- apply(attr_v, 2, as.numeric) return(list(hor=attr_h, vert=attr_v)) } +### The hex codes come from +### https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values +key2hex_code <- c( + Backspace="08", + Enter="0D", + ArrowDown="28") +### https://chromedevtools.github.io/devtools-protocol/tot/Input/ says +### that dispatchKeyEvent() requires DOM key codes (in decimal) for +### the windowsVirtualKeyCode and nativeVirtualKeyCode arguments. +key2dec_code <- structure( + strtoi(key2hex_code,base=16), + names=names(key2hex_code)) # Function to send a key event sendKey <- function(key) { stopifnot(is.character(key)) - #The key codes in the list below are adopted from Windows Virtual keycode standards - #https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes - # VK_BACK->Backspace, VK_RETURN->Enter, VK_DOWN->ArrowDown - # we use the corresponding decimal values of the key codes given in hex value in the above link - key2code <- c( - Backspace=8, - Enter=13, - ArrowDown=40) for (type in c("keyDown", "keyUp")) { - remDr$Input$dispatchKeyEvent(type = type, key = key, code = key, windowsVirtualKeyCode = key2code[[key]], nativeVirtualKeyCode = key2code[[key]]) + remDr$Input$dispatchKeyEvent(type = type, key = key, code = key, windowsVirtualKeyCode = key2dec_code[[key]], nativeVirtualKeyCode = key2dec_code[[key]]) } } From 0c044e775db2100db6df0620e72b0bb654d33b9e Mon Sep 17 00:00:00 2001 From: Siddhesh Deodhar Date: Mon, 26 Aug 2024 21:31:24 -0400 Subject: [PATCH 093/105] Increase chromote initialization timeout --- tests/testthat/helper-HTML.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/helper-HTML.R b/tests/testthat/helper-HTML.R index 443337082..19853e6c7 100644 --- a/tests/testthat/helper-HTML.R +++ b/tests/testthat/helper-HTML.R @@ -41,6 +41,7 @@ tests_init <- function(dir = ".", ...) { testDir <- file.path(testPath, "animint-htmltest") # if the htmltest directory exists, wipe clean, then create an empty folder unlink(testDir, recursive = TRUE) + options(chromote.timeout = 120) chrome.session <- chromote::ChromoteSession$new() chrome.session$view() chrome.session$refresh <- function(){ From 45ecfb8114d7a09fbb3e73db75aa8f3366d9da6c Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Mon, 26 Aug 2024 23:19:03 -0400 Subject: [PATCH 094/105] change tab to space --- DESCRIPTION | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index bcbf33c2b..4c0909337 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -50,12 +50,12 @@ Authors@R: c( person("Yufan", "Fei", role="aut", comment="Animint2 GSoC 2022"), - person("Jocelyne", "Chen", - role="aut", - comment="Animint2 GSoC 2023"), - person("Siddhesh", "Deodhar", - role="aut", - comment="Animint2 GSoC 2024")) + person("Jocelyne", "Chen", + role="aut", + comment="Animint2 GSoC 2023"), + person("Siddhesh", "Deodhar", + role="aut", + comment="Animint2 GSoC 2024")) Description: Functions are provided for defining animated, interactive data visualizations in R code, and rendering on a web page. The 2018 Journal of Computational and From b508a96061e580e876163a60940dc2277fa13909 Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Mon, 26 Aug 2024 23:25:40 -0400 Subject: [PATCH 095/105] rm selenium, combine renderer1,2,3 --- .github/workflows/tests.yaml | 2 +- DESCRIPTION | 3 +-- tests/testthat.R | 36 ++++-------------------------------- 3 files changed, 6 insertions(+), 35 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index f26c3fe99..a661299de 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -12,7 +12,7 @@ jobs: strategy: fail-fast: false matrix: - test-suite: [ renderer1,renderer2,renderer3,compiler,CRAN] + test-suite: [ renderer, compiler, CRAN ] name: Test Suite ${{ matrix.test-suite }} env: diff --git a/DESCRIPTION b/DESCRIPTION index 4c0909337..a9f111e75 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -87,7 +87,7 @@ Suggests: htmltools, rmarkdown, testthat, - RSelenium, XML, + XML, devtools, httr, maps, @@ -102,7 +102,6 @@ Suggests: svglite, ggplot2, chromote -Remotes: ropensci/RSelenium@v1.7.4 License: GPL-3 Encoding: UTF-8 LazyData: true diff --git a/tests/testthat.R b/tests/testthat.R index 1b3054d5f..54efe9d95 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -1,45 +1,17 @@ library("testthat") library("animint2") -library("RSelenium");library("XML") +library("XML") setwd("testthat") source("helper-functions.R") source("helper-HTML.R") source("helper-plot-data.r") - -## RSelenium does not work with all versions of firefox, TDH usually -## tests using one of the following. - -## thocking@silene:~/R/animint-mine(fix-common-chunk)$ firefox --version -## Mozilla Firefox 11.0 -## thocking@silene:~/R/animint(animation-fix)$ java -jar ~/lib/R/library/RSelenium/bin/selenium-server-standalone.jar --version -## 10:10:58.942 INFO - Launching a standalone server -## 10:10:58.973 INFO - Java: Sun Microsystems Inc. 23.25-b01 -## 10:10:58.973 INFO - OS: Linux 3.8.0-44-generic amd64 -## 10:10:58.987 INFO - v2.44.0, with Core v2.44.0. Built from revision 76d78cf -## > packageVersion("RSelenium") -## [1] ‘1.3.6’ - -## > packageVersion("RSelenium") -## [1] ‘1.3.5’ -## tdhock@recycled:~/R/animint(roc-bugfix)$ firefox --version -## Mozilla Firefox 41.0 -## tdhock@recycled:~/R/animint(roc-bugfix*)$ java -jar ~/lib/R/library/RSelenium/bin/selenium-server-standalone.jar --version -## 08:13:17.803 INFO - Launching a standalone Selenium Server -## 08:13:17.877 INFO - Java: Oracle Corporation 24.79-b02 -## 08:13:17.877 INFO - OS: Linux 3.13.0-65-generic i386 -## 08:13:17.907 INFO - v2.47.0, with Core v2.47.0. Built from revision 0e4837e - filter <- Sys.getenv("TEST_SUITE") -# If we're running in github actions then use Firefox, since we can -# use the Selenium docker image instead of relying on PhantomJS. gh.action <- Sys.getenv("GH_ACTION") -dont.need.browser <- grepl("compiler", filter) -use.browser <- !dont.need.browser +use.browser <- grepl("renderer", filter) if(filter == ""){ filter <- NULL } message(gh.action) -tests_init() - +if(use.browser)tests_init() tests_run(filter=filter) -tests_exit() +if(use_browser)tests_exit() From 2a3fda85546bb46cbcbbf1a354b51a4bc24aa6c1 Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Tue, 27 Aug 2024 08:23:30 -0400 Subject: [PATCH 096/105] rm selenium docker, tests_run --- .github/workflows/tests.yaml | 3 --- tests/testthat/helper-functions.R | 3 --- 2 files changed, 6 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index a661299de..1b12a3dd8 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -20,9 +20,6 @@ jobs: GITHUB_PAT: ${{ secrets.PAT_GITHUB }} GH_ACTION: "ENABLED" steps: - - uses: actions/checkout@v3 - - name: Start Selenium Docker - run: docker run -d --network="host" selenium/standalone-firefox-debug:2.53.0 - name: install and update texlive run: /usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt update -y -qq - run: /usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt install texlive texlive-fonts-extra -y diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index cb0d8f52d..9e26dfc08 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -330,10 +330,7 @@ unequal <- function(object, expected, ...){ #' tests_exit() #' } #' - tests_run <- function(dir = ".", filter = NULL) { - if (!"package:RSelenium" %in% search()) - stop("Please load RSelenium: library(RSelenium)") if (!"package:testthat" %in% search()) stop("Please load testthat: library(testthat)") testDir <- find_test_path(dir) From 70bdb5c9468573050647d9cf2f639091ae3a3a55 Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Tue, 27 Aug 2024 08:35:27 -0400 Subject: [PATCH 097/105] do checkout --- .github/workflows/tests.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 1b12a3dd8..cfe312bcb 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -20,6 +20,7 @@ jobs: GITHUB_PAT: ${{ secrets.PAT_GITHUB }} GH_ACTION: "ENABLED" steps: + - uses: actions/checkout@v3 - name: install and update texlive run: /usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt update -y -qq - run: /usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt install texlive texlive-fonts-extra -y From d4a29d79b98d0e019b40a1486ef90d2273f7535b Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Tue, 27 Aug 2024 08:40:10 -0400 Subject: [PATCH 098/105] _ -> . --- tests/testthat.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat.R b/tests/testthat.R index 54efe9d95..8ab0107b0 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -14,4 +14,4 @@ if(filter == ""){ message(gh.action) if(use.browser)tests_init() tests_run(filter=filter) -if(use_browser)tests_exit() +if(use.browser)tests_exit() From 2cb70865a6781a86fd456cb180bcc4a7b6152234 Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Tue, 27 Aug 2024 08:52:16 -0400 Subject: [PATCH 099/105] update docs --- DESCRIPTION | 2 +- NAMESPACE | 3 +++ R/z_facets.R | 2 -- man/position_dodge.Rd | 2 +- man/position_identity.Rd | 2 +- man/position_nudge.Rd | 4 ++-- man/position_stack.Rd | 2 +- 7 files changed, 9 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a9f111e75..072b30ef6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -280,6 +280,6 @@ Collate: 'z_scales.R' 'z_theme_animint.R' 'z_transformShape.R' -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.2 Config/Needs/website: tidyverse/tidytemplate VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index 7e73786a5..c6dc730fa 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -507,10 +507,13 @@ import(plyr) import(scales) importFrom(grDevices,col2rgb) importFrom(grDevices,rgb) +importFrom(grid,arrow) +importFrom(grid,unit) importFrom(knitr,knit_print) importFrom(methods,is) importFrom(plyr,as.quoted) importFrom(plyr,defaults) +importFrom(scales,alpha) importFrom(stats,na.omit) importFrom(stats,setNames) importFrom(utils,browseURL) diff --git a/R/z_facets.R b/R/z_facets.R index 78d6999cb..937025f3f 100644 --- a/R/z_facets.R +++ b/R/z_facets.R @@ -1,4 +1,3 @@ - # Determine titles to put on facet panels. # The implementation here is a modified version of facet_strips. getStrips <- function(facet, panel, ...) @@ -36,7 +35,6 @@ build_strip <- function(panel, label_df, labeller, side = "right", ...) { labels <- matrix(list(), nrow = nrow(label_df), ncol = ncol(label_df)) labels <- lapply(labeller(label_df), cbind) labels <- do.call("cbind", labels) - # unlike ggplot2, we collapse "layers" of strips into 1 layer apply(labels, 1, paste, collapse = "; ") } diff --git a/man/position_dodge.Rd b/man/position_dodge.Rd index d6aa63e6c..841206c77 100644 --- a/man/position_dodge.Rd +++ b/man/position_dodge.Rd @@ -47,8 +47,8 @@ p + geom_errorbar(aes(ymin = y-1, ymax = y+1, width = 0.2), Other position adjustments: \code{\link{position_fill}()}, \code{\link{position_identity}()}, -\code{\link{position_jitterdodge}()}, \code{\link{position_jitter}()}, +\code{\link{position_jitterdodge}()}, \code{\link{position_nudge}()} } \concept{position adjustments} diff --git a/man/position_identity.Rd b/man/position_identity.Rd index fa6fa09a8..ac01f5dc0 100644 --- a/man/position_identity.Rd +++ b/man/position_identity.Rd @@ -13,8 +13,8 @@ Don't adjust position Other position adjustments: \code{\link{position_dodge}()}, \code{\link{position_fill}()}, -\code{\link{position_jitterdodge}()}, \code{\link{position_jitter}()}, +\code{\link{position_jitterdodge}()}, \code{\link{position_nudge}()} } \concept{position adjustments} diff --git a/man/position_nudge.Rd b/man/position_nudge.Rd index c393b5a54..edd6748a1 100644 --- a/man/position_nudge.Rd +++ b/man/position_nudge.Rd @@ -32,7 +32,7 @@ Other position adjustments: \code{\link{position_dodge}()}, \code{\link{position_fill}()}, \code{\link{position_identity}()}, -\code{\link{position_jitterdodge}()}, -\code{\link{position_jitter}()} +\code{\link{position_jitter}()}, +\code{\link{position_jitterdodge}()} } \concept{position adjustments} diff --git a/man/position_stack.Rd b/man/position_stack.Rd index 5277d931f..48f918751 100644 --- a/man/position_stack.Rd +++ b/man/position_stack.Rd @@ -55,8 +55,8 @@ See \code{\link{geom_bar}} and \code{\link{geom_area}} for Other position adjustments: \code{\link{position_dodge}()}, \code{\link{position_identity}()}, -\code{\link{position_jitterdodge}()}, \code{\link{position_jitter}()}, +\code{\link{position_jitterdodge}()}, \code{\link{position_nudge}()} } \concept{position adjustments} From d1e76239d674ed286ca91bad8e4eb94abdb6307f Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Tue, 27 Aug 2024 09:00:59 -0400 Subject: [PATCH 100/105] version++ --- DESCRIPTION | 2 +- NEWS.md | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 072b30ef6..c5b97e9b2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: animint2 Title: Animated Interactive Grammar of Graphics -Version: 2024.6.6 +Version: 2024.8.27 URL: https://animint.github.io/animint2/ BugReports: https://github.com/animint/animint2/issues Authors@R: c( diff --git a/NEWS.md b/NEWS.md index feef1f507..bf56799df 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,14 +1,18 @@ +# Changes in version 2024.8.27 (PR#144) + +- Remove selenium, combine renderer tests into single CI job. + # Changes in version 2024.6.6 (PR#126) - - Add chromote as headless browser for testing and remove phantomjs/firefox support +- Add chromote as headless browser for testing and remove phantomjs/firefox support # Changes in version 2024.3.12 (PR#119) - - Add validation checks for duplicate args passed geom and aes +- Add validation checks for duplicate args passed geom and aes # Changes in version 2024.2.4 (PR#116) - - Add validation checks for duplicate and missing args passed to animint. +- Add validation checks for duplicate and missing args passed to animint. # Changes in version 2024.1.24 (PR#112) From 416c19c8b8e2d3bc84776462e36e796698188a88 Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Thu, 29 Aug 2024 10:52:56 -0400 Subject: [PATCH 101/105] expect title in README fails --- R/data.R | 2 +- R/stat-unique.r | 3 --- R/z_pages.R | 2 +- man/stat_unique.Rd | 5 ----- man/txhousing.Rd | 2 +- tests/testthat/test-compiler-ghpages.R | 13 ++++++++++--- 6 files changed, 13 insertions(+), 14 deletions(-) diff --git a/R/data.R b/R/data.R index 2f442f0d9..3c28c1795 100644 --- a/R/data.R +++ b/R/data.R @@ -173,7 +173,7 @@ #' Housing sales in TX. #' #' Information about the housing market in Texas provided by the TAMU -#' real estate center, \url{https://www.recenter.tamu.edu/}. +#' real estate center. #' #' @format A data frame with 8602 observations and 9 variables: #' \describe{ diff --git a/R/stat-unique.r b/R/stat-unique.r index 22c4b13ce..aae622a88 100644 --- a/R/stat-unique.r +++ b/R/stat-unique.r @@ -1,8 +1,5 @@ #' Remove duplicates. #' -#' @section Aesthetics: -#' \Sexpr[results=rd,stage=build]{animint2:::rd_aesthetics("stat", "unique")} -#' #' @export #' @inheritParams layer #' @inheritParams geom_point diff --git a/R/z_pages.R b/R/z_pages.R index 9945dace6..08987f146 100644 --- a/R/z_pages.R +++ b/R/z_pages.R @@ -92,7 +92,7 @@ animint2pages <- function(plot.list, github_repo, commit_message = "Commit from manage_gh_pages(repo, to_post, local_clone, commit_message) message(sprintf( "Visualization will be available at %s\nDeployment via GitHub Pages may take a few minutes...", viz_url)) - viz_owner_repo + list(owner_repo=viz_owner_repo, local_clone=local_clone, viz_url=viz_url, gh_pages_url=sprintf("https://github.com/%s/tree/gh-pages", viz_owner_repo)) } initial_commit <- function(local_clone, repo, viz_url) { diff --git a/man/stat_unique.Rd b/man/stat_unique.Rd index 7c65eb175..bd4ff0e80 100644 --- a/man/stat_unique.Rd +++ b/man/stat_unique.Rd @@ -60,11 +60,6 @@ the default plot specification, e.g. \code{\link{borders}}.} \description{ Remove duplicates. } -\section{Aesthetics}{ - -\Sexpr[results=rd,stage=build]{animint2:::rd_aesthetics("stat", "unique")} -} - \examples{ ggplot(mtcars, aes(vs, am)) + geom_point(alpha = 0.1) ggplot(mtcars, aes(vs, am)) + geom_point(alpha = 0.1, stat="unique") diff --git a/man/txhousing.Rd b/man/txhousing.Rd index ffbdb1e55..8118f1b33 100644 --- a/man/txhousing.Rd +++ b/man/txhousing.Rd @@ -22,6 +22,6 @@ txhousing } \description{ Information about the housing market in Texas provided by the TAMU -real estate center, \url{https://www.recenter.tamu.edu/}. +real estate center. } \keyword{datasets} diff --git a/tests/testthat/test-compiler-ghpages.R b/tests/testthat/test-compiler-ghpages.R index abefaa433..741497e2b 100644 --- a/tests/testthat/test-compiler-ghpages.R +++ b/tests/testthat/test-compiler-ghpages.R @@ -1,5 +1,6 @@ acontext("GitHub Pages") +library(animint2) viz <- animint( title="one to ten", source="https://github.com/animint/animint2/tree/master/tests/testthat/test-compiler-ghpages.R", @@ -22,9 +23,15 @@ test_that("error for viz with no source", { }, "plot.list does not contain option named source, which is required by animint2pages") }) -test_that("animint2pages() returns owner/repo string", { - viz_owner_repo <- animint2pages(viz, github_repo = "animint2pages_test_repo") - expect_is(viz_owner_repo, "character") +test_that("animint2pages() returns list of meta-data", { + result_list <- animint2pages(viz, github_repo = "animint2pages_test_repo") + expect_match(result_list$owner_repo, "animint2pages_test_repo") + expect_match(result_list$viz_url, "github.io/animint2pages_test_repo") + expect_match(result_list$gh_pages_url, "animint2pages_test_repo/tree/gh-pages") + README.md <- file.path(result_list$local_clone, "README.md") + README.lines <- readLines(README.md) + expected.line <- paste("##", viz$title) + expect_identical(README.lines[1], expected.line) }) test_that("animint2pages raises an error if no GitHub token is present", { From 132b723f782b3ab3144bfdb8e7bc05fadce53f41 Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Thu, 29 Aug 2024 12:35:17 -0400 Subject: [PATCH 102/105] animint2pages test uses gh DELETE POST in animint-test/animint2pages_test_repo --- DESCRIPTION | 2 +- NEWS.md | 5 +++++ R/z_pages.R | 17 ++++++++++------ man/animint2pages.Rd | 3 +++ tests/testthat/test-compiler-ghpages.R | 28 +++++++++++++++++++++++++- 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c5b97e9b2..2bcb621a6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: animint2 Title: Animated Interactive Grammar of Graphics -Version: 2024.8.27 +Version: 2024.8.29 URL: https://animint.github.io/animint2/ BugReports: https://github.com/animint/animint2/issues Authors@R: c( diff --git a/NEWS.md b/NEWS.md index bf56799df..86347c210 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# Changes in version 2024.8.29 (PR#147) + +- animint2pages initial commit README.md uses viz title. +- animint2pages gains owner argument, with default taken from `gh::gh_whoami` (same as before, always returns user, even when token has permissions on an org); this allows user to specify an org where the user/token has write permissions. This is used in a new test-compiler-ghpages.R, which now assumes `PAT_GITHUB` has Administration and Contents permissions for all repos in `animint-test` org, so our test code can delete the `animint2pages_test_repo`, create a new one, and then use animint2pages twice, to test the results of creation/update. + # Changes in version 2024.8.27 (PR#144) - Remove selenium, combine renderer tests into single CI job. diff --git a/R/z_pages.R b/R/z_pages.R index 08987f146..a070c3dcd 100644 --- a/R/z_pages.R +++ b/R/z_pages.R @@ -8,6 +8,7 @@ #' @param plot.list A named list of ggplots and option lists. #' @param github_repo The name of the GitHub repository to which the #' files will be pushed. +#' @param owner The user/org under which the repo will be created, default comes from \code{gh::gh_whoami}. #' @param commit_message A string specifying the commit message for #' the pushed files. #' @param private A logical flag indicating whether the GitHub @@ -35,7 +36,7 @@ #' } #' #' @export -animint2pages <- function(plot.list, github_repo, commit_message = "Commit from animint2pages", private = FALSE, required_opts = c("title","source"), ...) { +animint2pages <- function(plot.list, github_repo, owner=NULL, commit_message = "Commit from animint2pages", private = FALSE, required_opts = c("title","source"), ...) { for(opt in required_opts){ if(!opt %in% names(plot.list)){ stop(sprintf("plot.list does not contain option named %s, which is required by animint2pages", opt)) @@ -61,8 +62,10 @@ animint2pages <- function(plot.list, github_repo, commit_message = "Commit from stop("The github_repo argument should not contain '/'.") } # Check for existing repository - whoami <- suppressMessages(gh::gh_whoami()) - owner <- whoami[["login"]] + if(is.null(owner)){ + whoami <- suppressMessages(gh::gh_whoami()) + owner <- whoami[["login"]] + } viz_owner_repo <- paste0(owner, "/", github_repo) local_clone <- tempfile() if (!check_no_github_repo(owner, github_repo)) { @@ -86,7 +89,9 @@ animint2pages <- function(plot.list, github_repo, commit_message = "Commit from silent = TRUE ) if (!has_commits) { - initial_commit(local_clone, repo, viz_url) + title <- plot.list[["title"]] + if(!is.character(title))title <- "New animint visualization" + initial_commit(local_clone, repo, viz_url, title) } # Handle gh-pages branch manage_gh_pages(repo, to_post, local_clone, commit_message) @@ -95,9 +100,9 @@ animint2pages <- function(plot.list, github_repo, commit_message = "Commit from list(owner_repo=viz_owner_repo, local_clone=local_clone, viz_url=viz_url, gh_pages_url=sprintf("https://github.com/%s/tree/gh-pages", viz_owner_repo)) } -initial_commit <- function(local_clone, repo, viz_url) { +initial_commit <- function(local_clone, repo, viz_url, title) { readme_file_path <- file.path(local_clone, "README.md") - header <- "## New animint visualization\n" + header <- sprintf("## %s\n", title) url_hyperlink <- sprintf("[%s](%s)\n", viz_url, viz_url) full_content <- paste0(header, url_hyperlink) writeLines(full_content, readme_file_path) diff --git a/man/animint2pages.Rd b/man/animint2pages.Rd index 0c3b00930..f9f04b21e 100644 --- a/man/animint2pages.Rd +++ b/man/animint2pages.Rd @@ -7,6 +7,7 @@ animint2pages( plot.list, github_repo, + owner = NULL, commit_message = "Commit from animint2pages", private = FALSE, required_opts = c("title", "source"), @@ -19,6 +20,8 @@ animint2pages( \item{github_repo}{The name of the GitHub repository to which the files will be pushed.} +\item{owner}{The user/org under which the repo will be created, default comes from \code{gh::gh_whoami}.} + \item{commit_message}{A string specifying the commit message for the pushed files.} diff --git a/tests/testthat/test-compiler-ghpages.R b/tests/testthat/test-compiler-ghpages.R index 741497e2b..7fa9f4535 100644 --- a/tests/testthat/test-compiler-ghpages.R +++ b/tests/testthat/test-compiler-ghpages.R @@ -23,8 +23,22 @@ test_that("error for viz with no source", { }, "plot.list does not contain option named source, which is required by animint2pages") }) +## This test requires a fine-grained PAT +## - go to https://github.com/settings/personal-access-tokens/new +## - Resource owner: animint-test +## - Repository access: All repositories +## - Repository permissions: Administration and Contents: read and write. +## - Generate token +## - copy token and paste into PAT_GITHUB on https://github.com/animint/animint2/settings/secrets/actions +## Note that it is animint2pages_test_repo under owner=animint-test (not animint), because we want to limit the damage that a malicious user could do with this token: there are no repos with important data in the animint-test org. test_that("animint2pages() returns list of meta-data", { - result_list <- animint2pages(viz, github_repo = "animint2pages_test_repo") + ## https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#delete-a-repository says The fine-grained token must have the following permission set: "Administration" repository permissions (write) gh api --method DELETE -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/OWNER/REPO + gh::gh("DELETE /repos/animint-test/animint2pages_test_repo") + ## https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#create-an-organization-repository says The fine-grained token must have the following permission set: "Administration" repository permissions (write) + gh::gh("POST /orgs/animint-test/repos", name="animint2pages_test_repo") + ## first run of animint2pages creates new data viz. + result_list <- animint2pages(viz, "animint2pages_test_repo", owner="animint-test") + result_list expect_match(result_list$owner_repo, "animint2pages_test_repo") expect_match(result_list$viz_url, "github.io/animint2pages_test_repo") expect_match(result_list$gh_pages_url, "animint2pages_test_repo/tree/gh-pages") @@ -32,6 +46,18 @@ test_that("animint2pages() returns list of meta-data", { README.lines <- readLines(README.md) expected.line <- paste("##", viz$title) expect_identical(README.lines[1], expected.line) + get_tsv <- function(L)Sys.glob(file.path(L$local_clone, "*tsv")) + tsv_files_created <- get_tsv(result_list) + expect_equal(length(tsv_files_created), 1) + ## second run of animint2pages updates data viz. + viz.more <- viz + viz.more$five <- ggplot()+ + geom_point(aes( + x, x), + data=data.frame(x=1:5)) + update_list <- animint2pages(viz.more, "animint2pages_test_repo", owner="animint-test") + tsv_files_updated <- get_tsv(update_list) + expect_equal(length(tsv_files_updated), 2) }) test_that("animint2pages raises an error if no GitHub token is present", { From 811db7df20ff184dbcb319788001f14ae0717b4a Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Thu, 29 Aug 2024 12:51:51 -0400 Subject: [PATCH 103/105] apt install tidy --- .github/workflows/tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index cfe312bcb..b8e5b9ce9 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -23,7 +23,7 @@ jobs: - uses: actions/checkout@v3 - name: install and update texlive run: /usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt update -y -qq - - run: /usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt install texlive texlive-fonts-extra -y + - run: /usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt install tidy texlive texlive-fonts-extra -y - uses: r-lib/actions/setup-r@v2 - uses: r-lib/actions/setup-r-dependencies@v2 From ffd702ecace5aa05e4acf7b01ad590146e895a47 Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Thu, 29 Aug 2024 16:18:57 -0400 Subject: [PATCH 104/105] LOCAL TESTING --- tests/testthat/test-compiler-ghpages.R | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-compiler-ghpages.R b/tests/testthat/test-compiler-ghpages.R index 7fa9f4535..6d4fadfc3 100644 --- a/tests/testthat/test-compiler-ghpages.R +++ b/tests/testthat/test-compiler-ghpages.R @@ -23,7 +23,10 @@ test_that("error for viz with no source", { }, "plot.list does not contain option named source, which is required by animint2pages") }) -## This test requires a fine-grained PAT +## Running this test requires permission for creating and deleting repos under the animint-test org. +## GITHUB ACTIONS TESTING: +## In the context of github actions, we use a fine-grained PAT, +## Need to create one every year (1 year is the max expiration). ## - go to https://github.com/settings/personal-access-tokens/new ## - Resource owner: animint-test ## - Repository access: All repositories @@ -31,6 +34,14 @@ test_that("error for viz with no source", { ## - Generate token ## - copy token and paste into PAT_GITHUB on https://github.com/animint/animint2/settings/secrets/actions ## Note that it is animint2pages_test_repo under owner=animint-test (not animint), because we want to limit the damage that a malicious user could do with this token: there are no repos with important data in the animint-test org. +## LOCAL TESTING: +## First you need to become a member of the animint-test org (ask tdhock). +## On windows the credential manager will by default give you a token, +## without enough permissions to perform the delete operation. +## Use gitcreds::gitcreds_delete() if necessary to remove the old token. +## Go to https://github.com/settings/tokens/new and check repo and delete_repo, +## git bash on windows, git push, a window pops up, click token. +## Then the test case below should run locally. test_that("animint2pages() returns list of meta-data", { ## https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#delete-a-repository says The fine-grained token must have the following permission set: "Administration" repository permissions (write) gh api --method DELETE -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/OWNER/REPO gh::gh("DELETE /repos/animint-test/animint2pages_test_repo") From cffe8e47124df8c6fb1a032b729289a3843d1304 Mon Sep 17 00:00:00 2001 From: Toby Dylan Hocking Date: Fri, 30 Aug 2024 06:56:06 -0400 Subject: [PATCH 105/105] move docs to wiki --- tests/testthat/test-compiler-ghpages.R | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/tests/testthat/test-compiler-ghpages.R b/tests/testthat/test-compiler-ghpages.R index 6d4fadfc3..049b6aa7b 100644 --- a/tests/testthat/test-compiler-ghpages.R +++ b/tests/testthat/test-compiler-ghpages.R @@ -23,25 +23,11 @@ test_that("error for viz with no source", { }, "plot.list does not contain option named source, which is required by animint2pages") }) -## Running this test requires permission for creating and deleting repos under the animint-test org. -## GITHUB ACTIONS TESTING: -## In the context of github actions, we use a fine-grained PAT, -## Need to create one every year (1 year is the max expiration). -## - go to https://github.com/settings/personal-access-tokens/new -## - Resource owner: animint-test -## - Repository access: All repositories -## - Repository permissions: Administration and Contents: read and write. -## - Generate token -## - copy token and paste into PAT_GITHUB on https://github.com/animint/animint2/settings/secrets/actions -## Note that it is animint2pages_test_repo under owner=animint-test (not animint), because we want to limit the damage that a malicious user could do with this token: there are no repos with important data in the animint-test org. -## LOCAL TESTING: -## First you need to become a member of the animint-test org (ask tdhock). -## On windows the credential manager will by default give you a token, -## without enough permissions to perform the delete operation. -## Use gitcreds::gitcreds_delete() if necessary to remove the old token. -## Go to https://github.com/settings/tokens/new and check repo and delete_repo, -## git bash on windows, git push, a window pops up, click token. -## Then the test case below should run locally. +## The test below requires a github token with repo delete +## permission. Read +## https://github.com/animint/animint2/wiki/Testing#installation to +## see how to set that up on your local computer, or on github +## actions. test_that("animint2pages() returns list of meta-data", { ## https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#delete-a-repository says The fine-grained token must have the following permission set: "Administration" repository permissions (write) gh api --method DELETE -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/OWNER/REPO gh::gh("DELETE /repos/animint-test/animint2pages_test_repo")