Browse Source

Merge pull request #7145 from electron/device-scale-factor-failures

Get specs passing on displays using certain device scale factors
Phil Haack 8 years ago
parent
commit
cadbd7beb1
1 changed files with 37 additions and 14 deletions
  1. 37 14
      spec/api-browser-window-spec.js

+ 37 - 14
spec/api-browser-window-spec.js

@@ -273,9 +273,7 @@ describe('browser-window module', function () {
     it('sets the window size', function (done) {
       var size = [300, 400]
       w.once('resize', function () {
-        var newSize = w.getSize()
-        assert.equal(newSize[0], size[0])
-        assert.equal(newSize[1], size[1])
+        assertBoundsEqual(w.getSize(), size)
         done()
       })
       w.setSize(size[0], size[1])
@@ -288,12 +286,12 @@ describe('browser-window module', function () {
       assert.deepEqual(w.getMaximumSize(), [0, 0])
 
       w.setMinimumSize(100, 100)
-      assert.deepEqual(w.getMinimumSize(), [100, 100])
-      assert.deepEqual(w.getMaximumSize(), [0, 0])
+      assertBoundsEqual(w.getMinimumSize(), [100, 100])
+      assertBoundsEqual(w.getMaximumSize(), [0, 0])
 
       w.setMaximumSize(900, 600)
-      assert.deepEqual(w.getMinimumSize(), [100, 100])
-      assert.deepEqual(w.getMaximumSize(), [900, 600])
+      assertBoundsEqual(w.getMinimumSize(), [100, 100])
+      assertBoundsEqual(w.getMaximumSize(), [900, 600])
     })
   })
 
@@ -303,9 +301,7 @@ describe('browser-window module', function () {
       w.setAspectRatio(1 / 2)
       w.setAspectRatio(0)
       w.once('resize', function () {
-        var newSize = w.getSize()
-        assert.equal(newSize[0], size[0])
-        assert.equal(newSize[1], size[1])
+        assertBoundsEqual(w.getSize(), size)
         done()
       })
       w.setSize(size[0], size[1])
@@ -354,7 +350,7 @@ describe('browser-window module', function () {
     it('sets the content size and position', function (done) {
       var bounds = {x: 10, y: 10, width: 250, height: 250}
       w.once('resize', function () {
-        assert.deepEqual(w.getContentBounds(), bounds)
+        assertBoundsEqual(w.getContentBounds(), bounds)
         done()
       })
       w.setContentBounds(bounds)
@@ -515,9 +511,7 @@ describe('browser-window module', function () {
       size.width += 100
       size.height += 100
       w.setSize(size.width, size.height)
-      var after = w.getSize()
-      assert.equal(after[0], size.width)
-      assert.equal(after[1], size.height)
+      assertBoundsEqual(w.getSize(), [size.width, size.height])
     })
   })
 
@@ -1375,3 +1369,32 @@ describe('browser-window module', function () {
     })
   })
 })
+
+const assertBoundsEqual = (actual, expect) => {
+  if (!isScaleFactorRounding()) {
+    assert.deepEqual(expect, actual)
+  } else if (Array.isArray(actual)) {
+    assertWithinDelta(actual[0], expect[0], 1, 'x')
+    assertWithinDelta(actual[1], expect[1], 1, 'y')
+  } else {
+    assertWithinDelta(actual.x, expect.x, 1, 'x')
+    assertWithinDelta(actual.y, expect.y, 1, 'y')
+    assertWithinDelta(actual.width, expect.width, 1, 'width')
+    assertWithinDelta(actual.height, expect.height, 1, 'height')
+  }
+}
+
+const assertWithinDelta = (actual, expect, delta, label) => {
+  const result = Math.abs(actual - expect)
+  assert.ok(result <= delta, `${label} value of ${expect} was not within ${delta} of ${actual}`)
+}
+
+// Is the display's scale factor possibly causing rounding of pixel coordinate
+// values?
+const isScaleFactorRounding = () => {
+  const {scaleFactor} = screen.getPrimaryDisplay()
+  // Return true if scale factor is non-integer value
+  if (Math.round(scaleFactor) !== scaleFactor) return true
+  // Return true if scale factor is odd number above 2
+  return scaleFactor > 2 && scaleFactor % 2 === 1
+}