Debian Codegraph @

bqplot/js/src/test/figure.ts

raw
1 import { expect } from 'chai';
2 import { DummyManager } from './dummy-manager';
3 import bqplot = require('..');
4 // import {Figure} from '../src/Figure.js';
5 // import {FigureModel} from '../src/FigureModel.js';
6 import { create_figure_scatter, create_model_bqplot } from './widget-utils';
7 import * as d3Timer from 'd3-timer';
8
9 // text pixel coordinate
10 const test_x = 200;
11 const test_y = 200;
12 const pixel_red = [255, 0, 0, 255];
13 const pixel_blue = [0, 0, 255, 255];
14
15 describe('figure >', () => {
16 beforeEach(async function () {
17 this.manager = new DummyManager({ bqplot: bqplot });
18 });
19
20 it('should not create a WebGL renderer if not needed', async function () {
21 const x = {
22 dtype: 'float32',
23 value: new DataView(new Float32Array([0.5, 0.5]).buffer),
24 };
25 const y = {
26 dtype: 'float32',
27 value: new DataView(new Float32Array([2.0, 2.5]).buffer),
28 };
29 const { figure } = await create_figure_scatter(this.manager, x, y);
30
31 expect(figure.renderer).to.be.undefined;
32 });
33
34 it('should create a WebGL renderer when needed', async function () {
35 const x = {
36 dtype: 'float32',
37 value: new DataView(new Float32Array([0.5, 0.5]).buffer),
38 };
39 const y = {
40 dtype: 'float32',
41 value: new DataView(new Float32Array([2.0, 2.5]).buffer),
42 };
43 const { figure } = await create_figure_scatter(this.manager, x, y, true);
44
45 expect(figure.renderer).to.not.be.undefined;
46 });
47
48 it('canvas/png render check', async function () {
49 const x = {
50 dtype: 'float32',
51 value: new DataView(new Float32Array([0.5, 0.5]).buffer),
52 };
53 const y = {
54 dtype: 'float32',
55 value: new DataView(new Float32Array([2.0, 2.5]).buffer),
56 };
57 const { scatter, figure } = await create_figure_scatter(this.manager, x, y);
58 // we render a huge red scatter point, and check if the path of svg->canvas (and thus
59 // png) results in a red pixels at the test coordinates.
60 scatter.model.set('default_size', 1e6);
61 scatter.model.set('colors', ['red']);
62 scatter.d3el.selectAll('.object_grp').data();
63
64 d3Timer.timerFlush(); // this makes sure the animations are all executed
65 const canvas = await figure.get_rendered_canvas();
66 const context = canvas.getContext('2d');
67 const pixel = context.getImageData(test_x, test_y, 1, 1);
68 expect(Array.prototype.slice.call(pixel.data)).to.deep.equals(pixel_red);
69 });
70
71 it('canvas/png render gl check', async function () {
72 const x = {
73 dtype: 'float32',
74 value: new DataView(new Float32Array([0.5, 0.5]).buffer),
75 };
76 const y = {
77 dtype: 'float32',
78 value: new DataView(new Float32Array([2.0, 2.5]).buffer),
79 };
80 const { scatter: scatterGlView, figure } = await create_figure_scatter(
81 this.manager,
82 x,
83 y,
84 true
85 );
86
87 // we render a huge red scatter point, and check if the path of svg->canvas (and thus
88 // png) results in a red pixels at the test coordinates.
89 scatterGlView.model.set('default_size', 1e6);
90 scatterGlView.model.set('colors', ['red']);
91 // scatterGlView.d3el.selectAll(".object_grp").data();
92
93 d3Timer.timerFlush(); // this makes sure the animations are all executed
94 let canvas = await figure.get_rendered_canvas();
95 let context = canvas.getContext('2d');
96 let pixel = context.getImageData(test_x, test_y, 1, 1);
97 expect(Array.prototype.slice.call(pixel.data)).to.deep.equals(pixel_red);
98 // add a normal (blue) scatter, which should be overlaid on top
99 const scales = {
100 x: scatterGlView.model.get('scales').x.toJSON(),
101 y: scatterGlView.model.get('scales').y.toJSON(),
102 };
103 const color = null;
104 const size = null;
105 const opacity = null;
106 const rotation = null;
107 const skew = null;
108 const scatter = await create_model_bqplot(
109 this.manager,
110 'Scatter',
111 'scatter1',
112 {
113 scales: scales,
114 x: x,
115 y: y,
116 color: color,
117 size: size,
118 opacity: opacity,
119 rotation: rotation,
120 skew: skew,
121 colors: ['blue'],
122 visible: true,
123 default_size: 1e6,
124 selected_style: {},
125 unselected_style: {},
126 hovered_style: {},
127 unhovered_style: {},
128 preserve_domain: {},
129 _view_module_version: '*',
130 _view_module: 'bqplot',
131 }
132 );
133 figure.model.set('marks', [scatter, scatterGlView.model]);
134 await Promise.all(figure.mark_views.views);
135 d3Timer.timerFlush(); // this makes sure the animations are all executed
136 canvas = await figure.get_rendered_canvas();
137 context = canvas.getContext('2d');
138 pixel = context.getImageData(test_x, test_y, 1, 1);
139 expect(Array.prototype.slice.call(pixel.data)).to.deep.equals(pixel_blue);
140 });
141
142 it('marks removed before created', async function () {
143 const x = {
144 dtype: 'float32',
145 value: new DataView(new Float32Array([0.5, 0.5]).buffer),
146 };
147 const y = {
148 dtype: 'float32',
149 value: new DataView(new Float32Array([2.0, 2.5]).buffer),
150 };
151 const { scatter, figure } = await create_figure_scatter(this.manager, x, y);
152
153 // we start with the scatter and legend
154 expect(figure.fig_marks.node().children).lengthOf(2);
155 // we remove the scatter
156 figure.model.set('marks', []);
157 await Promise.all(figure.mark_views.views);
158 expect(figure.fig_marks.node().children).lengthOf(1);
159 // we set the marks twice without waiting, so we should never see the second scatter in the DOM
160 figure.model.set('marks', [scatter.model, scatter.model]);
161 const previousViewsPromise = Promise.all(figure.mark_views.views);
162 figure.model.set('marks', [scatter.model]);
163 await Promise.all(figure.mark_views.views);
164 await previousViewsPromise; // we also want to wait for these promises to be resolved so the dummy
165 // DOM node van be removed in the remove event handler in Figure.add_mark
166 expect(figure.fig_marks.node().children).lengthOf(2);
167 });
168 });