Skip to content

Commit

Permalink
Merge pull request octalmage#371 from octalmage/targetpractice
Browse files Browse the repository at this point in the history
Add integration tests.
  • Loading branch information
octalmage authored Feb 26, 2018
2 parents dbdc30d + f2bbeab commit 15cb9a3
Show file tree
Hide file tree
Showing 12 changed files with 2,945 additions and 301 deletions.
2,546 changes: 2,546 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"main": "index.js",
"typings": "index.d.ts",
"scripts": {
"test": "node test/all.js",
"test": "run-script-os",
"test:darwin:linux": "jasmine 'test/**/*.js'",
"test:win32": "jasmine test/**/*.js",
"install": "prebuild-install || node-gyp rebuild"
},
"repository": {
Expand Down Expand Up @@ -41,7 +43,9 @@
"prebuild-install": "^2.1.1"
},
"devDependencies": {
"tape": "^3.5.0",
"prebuild": "v6.1.0"
"jasmine": "^2.99.0",
"prebuild": "v6.1.0",
"run-script-os": "^1.0.3",
"targetpractice": "0.0.7"
}
}
3 changes: 2 additions & 1 deletion src/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ void scrollMouse(int x, int y)
mouseScrollInputH.mi.dwFlags = MOUSEEVENTF_HWHEEL;
mouseScrollInputH.mi.time = 0;
mouseScrollInputH.mi.dwExtraInfo = 0;
mouseScrollInputH.mi.mouseData = x;
// Flip x to match other platforms.
mouseScrollInputH.mi.mouseData = -x;

mouseScrollInputV.type = INPUT_MOUSE;
mouseScrollInputV.mi.dx = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/robotjs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ NAN_METHOD(scrollMouse)
{
return Nan::ThrowError("Invalid number of arguments.");
}

int x = info[0]->Int32Value();
int y = info[1]->Int32Value();

Expand Down
5 changes: 0 additions & 5 deletions test/all.js

This file was deleted.

133 changes: 49 additions & 84 deletions test/bitmap.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,56 @@
var test = require('tape');
var robot = require('..');

var params =
{
'width': 'number',
'height': 'number',
'byteWidth': 'number',
'bitsPerPixel': 'number',
'bytesPerPixel': 'number',
'image': 'object'
};

test('Get a bitmap.', function(t)
{
t.plan(1);
t.ok(robot.screen.capture(), 'got a bitmap.');
});

test('Get a bitmap and check the parameters.', function(t)
{
t.plan(6);
var img = robot.screen.capture();

for (var x in params)
{
t.equal(typeof img[x], params[x], 'make sure ' + x + ' is a ' + params[x] + '.');
}
});

test('Get a bitmap of a specific size.', function(t)
{
var size = 10;
t.plan(2);
var img = robot.screen.capture(0, 0, size, size);

// Support for higher density screens.
var multi = img.width / size;
var size = size * multi;
t.equals(img.height, size, 'make sure image is expected height.');
t.equals(img.width, size, 'make sure image is expected width.');
});

test('Get a bitmap and make sure the colorAt works as expected.', function(t)
{
t.plan(7);
var img = robot.screen.capture();
var hex = img.colorAt(0, 0);

t.ok(/^[0-9A-F]{6}$/i.test(hex), "colorAt returned valid hex.");

var screenSize = robot.getScreenSize();
var width = screenSize.width;
var height = screenSize.height;

// Support for higher density screens.
var multi = img.width / width;
width = width * multi;
height = height * multi;

t.throws(function()
describe('Bitmap', () => {
var params = {
'width': 'number',
'height': 'number',
'byteWidth': 'number',
'bitsPerPixel': 'number',
'bytesPerPixel': 'number',
'image': 'object'
};

it('Get a bitmap and check the parameters.', function() {
var img = robot.screen.capture();

for (var x in params)
{
expect(typeof img[x]).toEqual(params[x]);
}
});

it('Get a bitmap of a specific size.', function()
{
img.colorAt(0, height);
}, /are outside the bitmap/, 'colorAt (0, screen.height) threw an error.');
var size = 10;
var img = robot.screen.capture(0, 0, size, size);

t.doesNotThrow(function()
{
img.colorAt(0, height-1);
}, /are outside the bitmap/, 'colorAt (0, screen.height-1) did not throw an error.');

t.throws(function()
{
img.colorAt(width, 0);
}, /are outside the bitmap/, 'colorAt (screen.width, 0) threw an error.');

t.doesNotThrow(function()
{
img.colorAt(width-1, 0);
}, /are outside the bitmap/, 'colorAt (screen.width-1, 0) did not throw an error.');

t.throws(function()
{
img.colorAt(9999999999999, 0);
}, /are outside the bitmap/, 'colorAt (9999999999999, 0) threw an error.');
// Support for higher density screens.
var multi = img.width / size;
var size = size * multi;
expect(img.height).toEqual(size);
expect(img.width).toEqual(size);
});

// Regression test for https://github.com/octalmage/robotjs/commit/c41f38217fd73f59e6ca63015b51565cd1e7cfb7
t.throws(function()
it('Get a bitmap and make sure the colorAt works as expected.', function()
{
img.colorAt(0, 9999999999999);
}, /are outside the bitmap/, 'colorAt (0, 9999999999999) threw an error.');
var img = robot.screen.capture();
var hex = img.colorAt(0, 0);

// t.ok(.it(hex), "colorAt returned valid hex.");
expect(hex).toMatch(/^[0-9A-F]{6}$/i);

var screenSize = robot.getScreenSize();
var width = screenSize.width;
var height = screenSize.height;

// Support for higher density screens.
var multi = img.width / width;
width = width * multi;
height = height * multi;
expect(() => img.colorAt(0, height)).toThrowError(/are outside the bitmap/);
expect(() => img.colorAt(0, height-1)).not.toThrow();
expect(() => img.colorAt(width, 0)).toThrowError(/are outside the bitmap/);
expect(() => img.colorAt(9999999999999, 0)).toThrowError(/are outside the bitmap/);
expect(() => img.colorAt(0, 9999999999999)).toThrowError(/are outside the bitmap/);
});
});
38 changes: 38 additions & 0 deletions test/integration/keyboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* jshint esversion: 6 */
var robot = require('../..');
var targetpractice = require('targetpractice/index.js');
var os = require('os');

robot.setMouseDelay(100);

var target, elements;

describe('Integration/Keyboard', () => {
beforeEach(done => {
target = targetpractice.start();
target.once('elements', message => {
elements = message;
done();
});
});

afterEach(() => {
targetpractice.stop();
target = null;
});

it('types', done => {
const stringToType = 'hello world';
// Currently Target Practice waits for the "user" to finish typing before sending the event.
target.once('type', element => {
expect(element.id).toEqual('input_1');
expect(element.text).toEqual(stringToType);
done();
});

const input_1 = elements.input_1;
robot.moveMouse(input_1.x, input_1.y);
robot.mouseClick();
robot.typeString(stringToType);
});
});
95 changes: 95 additions & 0 deletions test/integration/mouse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/* jshint esversion: 6 */
var robot = require('../..');
var targetpractice = require('targetpractice/index.js');
var os = require('os');

robot.setMouseDelay(100);

var target, elements;

describe('Integration/Mouse', () => {
beforeEach(done => {
target = targetpractice.start();
target.once('elements', message => {
elements = message;
done();
});
});

afterEach(() => {
targetpractice.stop();
target = null;
});

it('clicks', done => {
// Alright we got a click event, did we click the button we wanted?
target.once('click', function(e)
{
expect(e.id).toEqual('button_1');
expect(e.type).toEqual('click');
done();
});

// For this test we want a button.
var button_1 = elements.button_1;
// Click it!
robot.moveMouse(button_1.x, button_1.y);
robot.mouseClick();
});

it('scrolls vertically', done => {
target.once('scroll', element => {
/**
* TODO: This is gross! The scroll distance is different for each OS. I want
* to look into this further, but at least these numbers are consistent.
*/
var expectedScroll;
switch(os.platform()) {
case 'linux':
expectedScroll = 180;
break;
case 'win32':
expectedScroll = 8;
break;
default:
expectedScroll = 10;
}
expect(element.id).toEqual('textarea_1');
expect(element.scroll_y).toEqual(expectedScroll);
done();
});

var textarea_1 = elements.textarea_1;
robot.moveMouse(textarea_1.x, textarea_1.y);
robot.mouseClick();
robot.scrollMouse(0, -10);
});

it('scrolls horizontally', done => {
target.once('scroll', element => {
/**
* TODO: This is gross! The scroll distance is different for each OS. I want
* to look into this further, but at least these numbers are consistent.
*/
var expectedScroll;
switch(os.platform()) {
case 'linux':
expectedScroll = 530;
break;
case 'win32':
expectedScroll = 8;
break;
default:
expectedScroll = 10;
}
expect(element.id).toEqual('textarea_1');
expect(element.scroll_x).toEqual(expectedScroll);
done();
});

var textarea_1 = elements.textarea_1;
robot.moveMouse(textarea_1.x, textarea_1.y);
robot.mouseClick();
robot.scrollMouse(-10, 0);
});
});
25 changes: 25 additions & 0 deletions test/integration/screen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* jshint esversion: 6 */
var robot = require('../..');
var targetpractice = require('targetpractice/index.js');
var elements, target;

describe('Integration/Screen', () => {
beforeEach(done => {
target = targetpractice.start();
target.once('elements', message => {
elements = message;
done();
});
});

afterEach(() => {
targetpractice.stop();
target = null;
});

it('reads a pixel color', () => {
var color_1 = elements.color_1;
const color = robot.getPixelColor(color_1.x, color_1.y);
expect(color).toEqual('c0ff33');
});
});
Loading

0 comments on commit 15cb9a3

Please sign in to comment.