diff --git a/tests/jsunit/index.html b/tests/jsunit/index.html index 38ad75f9a..f1324018a 100644 --- a/tests/jsunit/index.html +++ b/tests/jsunit/index.html @@ -18,8 +18,6 @@ - - diff --git a/tests/jsunit/workspace_comment_test.js b/tests/jsunit/workspace_comment_test.js deleted file mode 100644 index 9bb630252..000000000 --- a/tests/jsunit/workspace_comment_test.js +++ /dev/null @@ -1,142 +0,0 @@ -/** - * @license - * Copyright 2017 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ -'use strict'; - -var workspace; - -function workspaceCommentTest_setUp() { - workspace = new Blockly.Workspace(); -} - -function workspaceCommentTest_tearDown() { - workspace.dispose(); -} - -function test_noWorkspaceComments() { - workspaceCommentTest_setUp(); - try { - assertEquals('Empty workspace: no comments (1).', 0, workspace.getTopComments(true).length); - assertEquals('Empty workspace: no comments (2).', 0, workspace.getTopComments(false).length); - workspace.clear(); - assertEquals('Empty workspace: no comments (3).', 0, workspace.getTopComments(true).length); - assertEquals('Empty workspace: no comments (4).', 0, workspace.getTopComments(false).length); - } finally { - workspaceCommentTest_tearDown(); - } -} - -function test_oneWorkspaceComment() { - workspaceCommentTest_setUp(); - try { - var comment = new Blockly.WorkspaceComment(workspace, 'comment text', 0, 0, 'comment id'); - assertEquals('One comment on workspace (1).', 1, workspace.getTopComments(true).length); - assertEquals('One comment on workspace (2).', 1, workspace.getTopComments(false).length); - assertEquals('Comment db contains this comment.', comment, workspace.commentDB_['comment id']); - workspace.clear(); - assertEquals('Cleared workspace: no comments (3).', 0, workspace.getTopComments(true).length); - assertEquals('Cleared workspace: no comments (4).', 0, workspace.getTopComments(false).length); - assertFalse('Comment DB does not contain this comment.', 'comment id' in workspace.commentDB_); - } finally { - workspaceCommentTest_tearDown(); - } -} - -function test_getWorkspaceCommentById() { - workspaceCommentTest_setUp(); - try { - var comment = new Blockly.WorkspaceComment(workspace, 'comment text', 0, 0, 'comment id'); - assertEquals('Getting a comment by id.', comment, workspace.getCommentById('comment id')); - assertEquals('No comment found.', null, workspace.getCommentById('not a comment')); - comment.dispose(); - assertEquals('Can\'t find the comment.', null, workspace.getCommentById('comment id')); - } finally { - workspaceCommentTest_tearDown(); - } -} - -function test_disposeWsCommentTwice() { - workspaceCommentTest_setUp(); - try { - var comment = new Blockly.WorkspaceComment(workspace, 'comment text', 0, 0, 'comment id'); - comment.dispose(); - // Nothing should go wrong the second time dispose is called. - comment.dispose(); - } finally { - workspaceCommentTest_tearDown(); - } -} - -function test_wsCommentHeightWidth() { - workspaceCommentTest_setUp(); - try { - var comment = - new Blockly.WorkspaceComment(workspace, 'comment text', 10, 20, 'comment id'); - assertEquals('Initial width', 20, comment.getWidth()); - assertEquals('Initial height', 10, comment.getHeight()); - - comment.setWidth(30); - assertEquals('New width should be different', 30, comment.getWidth()); - assertEquals('New height should not be different', 10, comment.getHeight()); - - comment.setHeight(40); - assertEquals('New width should not be different', 30, comment.getWidth()); - assertEquals('New height should be different', 40, comment.getHeight()); - comment.dispose(); - } finally { - workspaceCommentTest_tearDown(); - } -} - -function test_wsCommentXY() { - workspaceCommentTest_setUp(); - try { - var comment = - new Blockly.WorkspaceComment(workspace, 'comment text', 10, 20, 'comment id'); - var xy = comment.getXY(); - assertEquals('Initial X position', 0, xy.x); - assertEquals('Initial Y position', 0, xy.y); - - comment.moveBy(10, 100); - xy = comment.getXY(); - assertEquals('New X position', 10, xy.x); - assertEquals('New Y position', 100, xy.y); - comment.dispose(); - } finally { - workspaceCommentTest_tearDown(); - } -} - -function test_wsCommentContent() { - workspaceCommentTest_setUp(); - - Blockly.Events.fire = temporary_fireEvent; - temporary_fireEvent.firedEvents_ = []; - try { - var comment = - new Blockly.WorkspaceComment(workspace, 'comment text', 10, 20, 'comment id'); - assertEquals( - 'Check comment text', 'comment text', comment.getContent()); - assertEquals( - 'Workspace undo stack has one event', 1, workspace.undoStack_.length); - - comment.setContent('comment text'); - assertEquals( - 'Comment text has not changed', 'comment text', comment.getContent()); - // Setting the text to the old value does not fire an event. - assertEquals( - 'Workspace undo stack has one event', 1, workspace.undoStack_.length); - - comment.setContent('new comment text'); - assertEquals( - 'Comment text has changed', 'new comment text', comment.getContent()); - assertEquals( - 'Workspace undo stack has two events', 2, workspace.undoStack_.length); - comment.dispose(); - } finally { - workspaceCommentTest_tearDown(); - Blockly.Events.fire = savedFireFunc; - } -} diff --git a/tests/mocha/index.html b/tests/mocha/index.html index a1c57c74b..d7704c917 100644 --- a/tests/mocha/index.html +++ b/tests/mocha/index.html @@ -71,6 +71,8 @@ + + diff --git a/tests/mocha/workspace_comment_test.js b/tests/mocha/workspace_comment_test.js new file mode 100644 index 000000000..8a354b918 --- /dev/null +++ b/tests/mocha/workspace_comment_test.js @@ -0,0 +1,208 @@ +/** + * @license + * Copyright 2020 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +goog.require('Blockly.WorkspaceComment'); + +suite('Workspace comment', function() { + setup(function() { + this.workspace = new Blockly.Workspace(); + }); + + teardown(function() { + this.workspace.dispose(); + }); + + suite('getTopComments(ordered=true)', function() { + test('No comments', function() { + chai.assert.equal(this.workspace.getTopComments(true).length, 0); + }); + + test('One comment', function() { + var comment = new Blockly.WorkspaceComment( + this.workspace, 'comment text', 0, 0, 'comment id'); + chai.assert.equal(this.workspace.getTopComments(true).length, 1); + chai.assert.equal(this.workspace.commentDB_['comment id'], comment); + }); + + test('After clear empty workspace', function() { + this.workspace.clear(); + chai.assert.equal(this.workspace.getTopComments(true).length, 0); + }); + + test('After clear non-empty workspace', function() { + new Blockly.WorkspaceComment( + this.workspace, 'comment text', 0, 0, 'comment id'); + this.workspace.clear(); + chai.assert.equal(this.workspace.getTopComments(true).length, 0); + chai.assert.isFalse('comment id' in this.workspace.commentDB_); + }); + + test('After dispose', function() { + var comment = new Blockly.WorkspaceComment( + this.workspace, 'comment text', 0, 0, 'comment id'); + comment.dispose(); + chai.assert.equal(this.workspace.getTopComments(true).length, 0); + chai.assert.isFalse('comment id' in this.workspace.commentDB_); + }); + }); + + suite('getTopComments(ordered=false)', function() { + test('No comments', function() { + chai.assert.equal(this.workspace.getTopComments(false).length, 0); + }); + + test('One comment', function() { + var comment = new Blockly.WorkspaceComment( + this.workspace, 'comment text', 0, 0, 'comment id'); + chai.assert.equal(this.workspace.getTopComments(false).length, 1); + chai.assert.equal(this.workspace.commentDB_['comment id'], comment); + }); + + test('After clear empty workspace', function() { + this.workspace.clear(); + chai.assert.equal(this.workspace.getTopComments(false).length, 0); + }); + + test('After clear non-empty workspace', function() { + new Blockly.WorkspaceComment( + this.workspace, 'comment text', 0, 0, 'comment id'); + this.workspace.clear(); + chai.assert.equal(this.workspace.getTopComments(false).length, 0); + chai.assert.isFalse('comment id' in this.workspace.commentDB_); + }); + + test('After dispose', function() { + var comment = new Blockly.WorkspaceComment( + this.workspace, 'comment text', 0, 0, 'comment id'); + comment.dispose(); + chai.assert.equal(this.workspace.getTopComments(false).length, 0); + chai.assert.isFalse('comment id' in this.workspace.commentDB_); + }); + }); + + suite('getCommentById', function() { + test('Trivial', function() { + var comment = new Blockly.WorkspaceComment( + this.workspace, 'comment text', 0, 0, 'comment id'); + chai.assert.equal(this.workspace.getCommentById(comment.id), comment); + }); + + test('Null id', function() { + chai.assert.isNull(this.workspace.getCommentById(null)); + }); + + test('Non-existent id', function() { + chai.assert.isNull(this.workspace.getCommentById('badId')); + }); + + test('After dispose', function() { + var comment = new Blockly.WorkspaceComment( + this.workspace, 'comment text', 0, 0, 'comment id'); + comment.dispose(); + chai.assert.isNull(this.workspace.getCommentById(comment.id)); + }); + }); + + suite('dispose', function() { + test('Called twice', function() { + var comment = new Blockly.WorkspaceComment( + this.workspace, 'comment text', 0, 0, 'comment id'); + comment.dispose(); + // Nothing should go wrong the second time dispose is called. + comment.dispose(); + }); + }); + + suite('Width and height', function() { + setup(function() { + this.comment = new Blockly.WorkspaceComment( + this.workspace, 'comment text', 10, 20, 'comment id'); + }); + + test('Initial values', function() { + chai.assert.equal(this.comment.getWidth(), 20, 'Width'); + chai.assert.equal(this.comment.getHeight(), 10, 'Height'); + }); + + test('setWidth does not affect height', function() { + this.comment.setWidth(30); + chai.assert.equal(this.comment.getWidth(), 30, 'Width'); + chai.assert.equal(this.comment.getHeight(), 10, 'Height'); + }); + + test('setHeight does not affect width', function() { + this.comment.setHeight(30); + chai.assert.equal(this.comment.getWidth(), 20, 'Width'); + chai.assert.equal(this.comment.getHeight(), 30, 'Height'); + }); + }); + + suite('XY position', function() { + setup(function() { + this.comment = new Blockly.WorkspaceComment( + this.workspace, 'comment text', 10, 20, 'comment id'); + }); + + test('Initial position', function() { + var xy = this.comment.getXY(); + chai.assert.equal(xy.x, 0, 'Initial X position'); + chai.assert.equal(xy.y, 0,'Initial Y position'); + }); + + test('moveBy', function() { + this.comment.moveBy(10, 100); + var xy = this.comment.getXY(); + chai.assert.equal(xy.x, 10, 'New X position'); + chai.assert.equal(xy.y, 100, 'New Y position'); + }); + + }); + + suite('Content', function() { + function temporary_fireEvent(event) { + if (!Blockly.Events.isEnabled()) { + return; + } + Blockly.Events.FIRE_QUEUE_.push(event); + Blockly.Events.fireNow_(); + } + + setup(function() { + this.savedFireFunc_ = Blockly.Events.fire; + Blockly.Events.fire = temporary_fireEvent; + temporary_fireEvent.firedEvents_ = []; + + this.comment = new Blockly.WorkspaceComment( + this.workspace, 'comment text', 0, 0, 'comment id'); + }); + + teardown(function() { + Blockly.Events.fire = this.savedFireFunc_; + }); + + test('After creation', function() { + chai.assert.equal( + this.comment.getContent(), 'comment text'); + chai.assert.equal( + this.workspace.undoStack_.length, 1,'Workspace undo stack'); + }); + + test('Set to same value', function() { + this.comment.setContent('comment text'); + chai.assert.equal(this.comment.getContent(), 'comment text'); + // Setting the text to the old value does not fire an event. + chai.assert.equal( + this.workspace.undoStack_.length, 1, 'Workspace undo stack'); + }); + + test('Set to different value', function() { + this.comment.setContent('new comment text'); + chai.assert.equal(this.comment.getContent(), 'new comment text'); + chai.assert.equal( + this.workspace.undoStack_.length, 2, 'Workspace undo stack'); + }); + }); +});