| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
import { expect } from 'chai'; |
| 4 |
|
| 5 |
import { CellRenderer, GraphicsContext, ImageRenderer } from '@lumino/datagrid'; |
| 6 |
|
| 7 |
class LoggingGraphicsContext extends GraphicsContext { |
| 8 |
drawImage( |
| 9 |
img: HTMLImageElement, |
| 10 |
x: number, |
| 11 |
y: number, |
| 12 |
width?: number, |
| 13 |
height?: number |
| 14 |
): void { |
| 15 |
if (width && height) { |
| 16 |
super.drawImage(img, x, y, width, height); |
| 17 |
} else { |
| 18 |
super.drawImage(img, x, y); |
| 19 |
} |
| 20 |
this.images.push({ img, x, y, width, height }); |
| 21 |
} |
| 22 |
|
| 23 |
images: any[] = []; |
| 24 |
} |
| 25 |
|
| 26 |
describe('@lumino/datagrid', () => { |
| 27 |
let gc: LoggingGraphicsContext; |
| 28 |
let img: HTMLImageElement; |
| 29 |
const defaultCellConfig: CellRenderer.CellConfig = { |
| 30 |
x: 5, |
| 31 |
y: 6, |
| 32 |
width: 240, |
| 33 |
height: 20, |
| 34 |
row: 0, |
| 35 |
column: 0, |
| 36 |
region: 'body', |
| 37 |
value: '', |
| 38 |
metadata: {} |
| 39 |
}; |
| 40 |
beforeEach(() => { |
| 41 |
const canvas = document.createElement('canvas'); |
| 42 |
const context = canvas.getContext('2d')!; |
| 43 |
gc = new LoggingGraphicsContext(context); |
| 44 |
img = document.createElement('img'); |
| 45 |
img.width = 12; |
| 46 |
img.height = 2; |
| 47 |
ImageRenderer['dataCache'].set('test-image', img); |
| 48 |
}); |
| 49 |
describe('ImageRenderer', () => { |
| 50 |
describe('drawImage()', () => { |
| 51 |
it('should take full available height by default', () => { |
| 52 |
const renderer = new ImageRenderer(); |
| 53 |
renderer.drawImage(gc, { |
| 54 |
...defaultCellConfig, |
| 55 |
value: 'test-image' |
| 56 |
}); |
| 57 |
expect(gc.images.pop()).to.deep.eq({ |
| 58 |
img, |
| 59 |
x: 5, |
| 60 |
y: 6, |
| 61 |
width: 120, |
| 62 |
height: 20 |
| 63 |
}); |
| 64 |
}); |
| 65 |
|
| 66 |
it('should take full available width when requested', () => { |
| 67 |
const renderer = new ImageRenderer({ |
| 68 |
width: '100%', |
| 69 |
height: '' |
| 70 |
}); |
| 71 |
renderer.drawImage(gc, { |
| 72 |
...defaultCellConfig, |
| 73 |
value: 'test-image' |
| 74 |
}); |
| 75 |
expect(gc.images.pop()).to.deep.eq({ |
| 76 |
img, |
| 77 |
x: 5, |
| 78 |
y: 6, |
| 79 |
width: 240, |
| 80 |
height: 40 |
| 81 |
}); |
| 82 |
}); |
| 83 |
|
| 84 |
it('should take full available width and height when requested', () => { |
| 85 |
const renderer = new ImageRenderer({ |
| 86 |
width: '100%', |
| 87 |
height: '100%' |
| 88 |
}); |
| 89 |
renderer.drawImage(gc, { |
| 90 |
...defaultCellConfig, |
| 91 |
value: 'test-image' |
| 92 |
}); |
| 93 |
expect(gc.images.pop()).to.deep.eq({ |
| 94 |
img, |
| 95 |
x: 5, |
| 96 |
y: 6, |
| 97 |
width: 240, |
| 98 |
height: 20 |
| 99 |
}); |
| 100 |
}); |
| 101 |
|
| 102 |
it('should take width in pixels', () => { |
| 103 |
const renderer = new ImageRenderer({ |
| 104 |
width: '24px', |
| 105 |
height: '' |
| 106 |
}); |
| 107 |
renderer.drawImage(gc, { |
| 108 |
...defaultCellConfig, |
| 109 |
value: 'test-image' |
| 110 |
}); |
| 111 |
expect(gc.images.pop()).to.deep.eq({ |
| 112 |
img, |
| 113 |
x: 5, |
| 114 |
y: 6, |
| 115 |
width: 24, |
| 116 |
height: 4 |
| 117 |
}); |
| 118 |
}); |
| 119 |
|
| 120 |
it('should take width and height pixels', () => { |
| 121 |
const renderer = new ImageRenderer({ |
| 122 |
width: '24px', |
| 123 |
height: '13px' |
| 124 |
}); |
| 125 |
renderer.drawImage(gc, { |
| 126 |
...defaultCellConfig, |
| 127 |
value: 'test-image' |
| 128 |
}); |
| 129 |
expect(gc.images.pop()).to.deep.eq({ |
| 130 |
img, |
| 131 |
x: 5, |
| 132 |
y: 6, |
| 133 |
width: 24, |
| 134 |
height: 13 |
| 135 |
}); |
| 136 |
}); |
| 137 |
|
| 138 |
it('should take width in pixels and height in percentage', () => { |
| 139 |
const renderer = new ImageRenderer({ |
| 140 |
width: '24px', |
| 141 |
height: '50%' |
| 142 |
}); |
| 143 |
renderer.drawImage(gc, { |
| 144 |
...defaultCellConfig, |
| 145 |
value: 'test-image' |
| 146 |
}); |
| 147 |
expect(gc.images.pop()).to.deep.eq({ |
| 148 |
img, |
| 149 |
x: 5, |
| 150 |
y: 6, |
| 151 |
width: 24, |
| 152 |
height: 10 |
| 153 |
}); |
| 154 |
}); |
| 155 |
}); |
| 156 |
}); |
| 157 |
}); |