Skip to content

Commit

Permalink
Jasmine migration in progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
octalmage committed Feb 25, 2018
1 parent facc34f commit 24ed91d
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 237 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"prebuild-install": "^2.1.1"
},
"devDependencies": {
"jasmine": "^3.0.0",
"prebuild": "v6.1.0",
"tape": "^3.5.0",
"targetpractice": "0.0.7"
Expand Down
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/);
});
});
133 changes: 53 additions & 80 deletions test/integration/mouse.js
Original file line number Diff line number Diff line change
@@ -1,109 +1,82 @@
/* jshint esversion: 6 */
var test = require('tape');
var robot = require('../..');
var targetpractice = require('targetpractice/index.js');
var os = require('os');
var elements;

robot.setMouseDelay(100);

test('Test clicking.', { timeout: 10000 }, function(t)
{
t.plan(2);
let target, elements;

// Start the UI.
var target = targetpractice.start();
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();
});

// Wait for the list of elements.
target.once('elements', function(elements)
{
// 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();
});

// Alright we got a click event, did we click the button we wanted?
target.once('click', function(e)
{
t.equal(e.id, "button_1", 'Confirm button_1 was clicked.');
t.equal(e.type, "click", 'Confirm event was a click.');
});

// Close the UI.
t.once('end', function()
{
targetpractice.stop();
});
});

test('Test typing.', { timeout: 10000 }, function(t)
{
t.plan(2);

var target = targetpractice.start();
var stringToType = "hello world";
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();
});

target.on('elements', function(elements)
{
var input_1 = elements.input_1;
const input_1 = elements.input_1;
robot.moveMouse(input_1.x, input_1.y);
robot.mouseClick();
robot.typeString(stringToType);
});

// Currently Target Practice waits for the "user" to finish typing before sending the event.
target.once('type', function(element)
{
t.equal(element.id, "input_1", 'Confirm input_1 was used.');
t.equal(element.text, stringToType, `Confirm that ${stringToType} was typed.`);
});

t.once('end', function()
{
targetpractice.stop();
});
});

test('Test scrolling.', { timeout: 10000 }, function(t)
{
t.plan(2);
it('scrolls', 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.
*/
let 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 target = targetpractice.start();

target.once('elements', function(elements)
{
var textarea_1 = elements.textarea_1;
robot.moveMouse(textarea_1.x, textarea_1.y);
robot.mouseClick();
robot.scrollMouse(0, -10);
});

target.once('scroll', function(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.
*/
let expectedScroll;
switch(os.platform()) {
case 'linux':
expectedScroll = 180;
break;
case 'win32':
expectedScroll = 8;
break;
default:
expectedScroll = 10;
}
t.equal(element.id, 'textarea_1', 'Confirm textarea_1 was used.');
t.equal(element.scroll_y, expectedScroll, 'Confirm scroll to 10.');
});

t.once('end', function()
{
targetpractice.stop();
});
});
33 changes: 15 additions & 18 deletions test/integration/screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@
var test = require('tape');
var robot = require('../..');
var targetpractice = require('targetpractice/index.js');
var elements;
let elements, target;

robot.setMouseDelay(100);

test('Test reading the Screen.', { timeout: 10000 }, function(t)
{
t.plan(1);
describe('Integration/Screen', () => {
beforeEach(done => {
target = targetpractice.start();
target.once('elements', message => {
elements = message;
done();
});
});

// Start the UI.
var target = targetpractice.start();
afterEach(() => {
targetpractice.stop();
target = null;
});

// Wait for the list of elements.
target.once('elements', function(elements)
{
it('reads a pixel color', () => {
var color_1 = elements.color_1;
const color = robot.getPixelColor(color_1.x, color_1.y);
t.equal(color, 'c0ff33', 'Color is what we expected.');
});

// Close the UI.
t.once('end', function()
{
targetpractice.stop();
expect(color).toEqual('c0ff33');
});
});
Loading

0 comments on commit 24ed91d

Please sign in to comment.