forked from animint/animint2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-renderer1-knit-print.R
158 lines (141 loc) · 5.16 KB
/
test-renderer1-knit-print.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
acontext("knitting multiple animint plots in a single Rmd")
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",
package = "animint2")
index.file <- file.path("animint-htmltest", "index.Rmd")
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)
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='a_geom3_segment_signal']//line")
expect_equal(length(seg.list), 6)
break.list <- getNodeSet(html, "//g[@class='a_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='a_geom']", plot.names[[i]])
circle.list <- getNodeSet(html, xpath)
count.vec[[i]] <- length(circle.list)
}
count.vec
}
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.a_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", "a_label_variable_selector_widget")
list(a178=a,
b934=b,
show_hide=show_hide,
widget=widget)
}
plot1top <- get_elements("plot1top")
plot1bottom <- get_elements("plot1bottom")
test_that("clicking top legend adds/remove points", {
expect_equal(get_circles(), c(10, 10))
plot1top$a178$clickElement()
expect_equal(get_circles(), c(5, 10))
plot1top$b934$clickElement()
expect_equal(get_circles(), c(0, 10))
plot1top$b934$clickElement()
expect_equal(get_circles(), c(5, 10))
plot1top$a178$clickElement()
expect_equal(get_circles(), c(10, 10))
})
test_that("clicking bottom legend adds/remove points", {
expect_equal(get_circles(), c(10, 10))
plot1bottom$a178$clickElement()
expect_equal(get_circles(), c(10, 5))
plot1bottom$b934$clickElement()
expect_equal(get_circles(), c(10, 0))
plot1bottom$b934$clickElement()
expect_equal(get_circles(), c(10, 5))
plot1bottom$a178$clickElement()
expect_equal(get_circles(), c(10, 10))
})
plot1top$show_hide$clickElement()
s.div <- plot1top$widget$findChildElement("class name", "selectize-input")
s.div$clickElement()
test_that("top widget adds/remove points", {
expect_equal(get_circles(), c(10, 10))
remDr$sendKeysToActiveElement(list(key="backspace"))
expect_equal(get_circles(), c(5, 10))
remDr$sendKeysToActiveElement(list(key="backspace"))
expect_equal(get_circles(), c(0, 10))
remDr$sendKeysToActiveElement(list("a", key="enter"))
expect_equal(get_circles(), c(5, 10))
remDr$sendKeysToActiveElement(list("b", key="enter"))
expect_equal(get_circles(), c(10, 10))
})
plot1bottom$show_hide$clickElement()
s.div <-
plot1bottom$widget$findChildElement("class name", "selectize-input")
s.div$clickElement()
test_that("bottom widget adds/remove points", {
expect_equal(get_circles(), c(10, 10))
remDr$sendKeysToActiveElement(list(key="backspace"))
expect_equal(get_circles(), c(10, 5))
remDr$sendKeysToActiveElement(list(key="backspace"))
expect_equal(get_circles(), c(10, 0))
remDr$sendKeysToActiveElement(list("a", key="enter"))
expect_equal(get_circles(), c(10, 5))
remDr$sendKeysToActiveElement(list("b", key="enter"))
expect_equal(get_circles(), c(10, 10))
})