| 1 | /** |
| 2 | * @fileoverview Tests for window.devicePixelRatio > 1. |
| 3 | * |
| 4 | * @author danvdk@gmail.com (Dan Vanderkam) |
| 5 | */ |
| 6 | |
| 7 | import Dygraph from '../../src/dygraph'; |
| 8 | |
| 9 | describe("hidpi", function() { |
| 10 | |
| 11 | cleanupAfterEach(); |
| 12 | |
| 13 | var savePixelRatio; |
| 14 | beforeEach(function() { |
| 15 | savePixelRatio = window.devicePixelRatio; |
| 16 | window.devicePixelRatio = 2; |
| 17 | }); |
| 18 | |
| 19 | afterEach(function() { |
| 20 | window.devicePixelRatio = savePixelRatio; |
| 21 | }); |
| 22 | |
| 23 | it('testDoesntCreateScrollbars', function() { |
| 24 | var sw = document.body.scrollWidth; |
| 25 | var cw = document.body.clientWidth; |
| 26 | |
| 27 | var graph = document.getElementById("graph"); |
| 28 | graph.style.width = "70%"; // more than half. |
| 29 | graph.style.height = "200px"; |
| 30 | |
| 31 | var opts = {}; |
| 32 | var data = "X,Y\n" + |
| 33 | "0,-1\n" + |
| 34 | "1,0\n" + |
| 35 | "2,1\n" + |
| 36 | "3,0\n" |
| 37 | ; |
| 38 | |
| 39 | var g = new Dygraph(graph, data, opts); |
| 40 | |
| 41 | // Adding the graph shouldn't cause the width of the page to change. |
| 42 | // (essentially, we're checking that we don't end up with a scrollbar) |
| 43 | // See http://stackoverflow.com/a/2146905/388951 |
| 44 | assert.equal(cw, document.body.clientWidth); |
| 45 | assert.equal(sw, document.body.scrollWidth); |
| 46 | }); |
| 47 | |
| 48 | it('should be influenced by options.pixelRatio', function () { |
| 49 | var graph = document.getElementById("graph"); |
| 50 | |
| 51 | // make sure devicePixelRatio is still setup to not 1. |
| 52 | assert(devicePixelRatio > 1.5, 'devicePixelRatio is not much greater than 1.'); |
| 53 | |
| 54 | var data = "X,Y\n" + |
| 55 | "0,-1\n" + |
| 56 | "1,0\n" + |
| 57 | "2,1\n" + |
| 58 | "3,0\n"; |
| 59 | |
| 60 | // first try a default one |
| 61 | var g1 = new Dygraph(graph, data, {}); |
| 62 | var area1 = g1.getArea(); |
| 63 | |
| 64 | var g2 = new Dygraph(graph, data, { pixelRatio: 1 }); |
| 65 | var area2 = g2.getArea(); |
| 66 | |
| 67 | var g3 = new Dygraph(graph, data, { pixelRatio: 3 }); |
| 68 | var area3 = g3.getArea(); |
| 69 | |
| 70 | assert.deepEqual(area1, area2, 'areas 1 and 2 are not the same'); |
| 71 | assert.deepEqual(area2, area3, 'areas 2 and 3 are not the same'); |
| 72 | |
| 73 | assert.notEqual(g1.canvas_.width, g2.canvas_.width, |
| 74 | 'Expected, for devicePixelRatio != 1, ' |
| 75 | + 'that setting options.pixelRatio would change the canvas width'); |
| 76 | assert.equal(g2.canvas_.width * 3, g3.canvas_.width, |
| 77 | 'Expected that pixelRatio of 3 vs 1 would triple the canvas width.'); |
| 78 | }); |
| 79 | |
| 80 | }); |