Browse Source

Merge pull request #37 from AToMPM/toolbar-tests

Toolbar tests
vasco-sousa 7 years ago
parent
commit
b361c40cc0
8 changed files with 218 additions and 75 deletions
  1. 5 0
      __worker.js
  2. 7 1
      asworker.js
  3. 38 31
      client/data_utils.js
  4. 1 0
      client/gui_utils.js
  5. 5 4
      package.json
  6. 8 39
      tests/03_model_test.js
  7. 40 0
      tests/04_toolbar_test.js
  8. 114 0
      tests/test_utils.js

+ 5 - 0
__worker.js

@@ -308,6 +308,11 @@ function __postMessage(msg)
 				  _utils.jsons(msg.data) : 
 				  msg.data)));
 
+	//make sure that reason is a string
+	if (typeof msg.reason == 'object'){
+		msg.reason = _utils.jsons(msg.reason);
+	}
+
 	if( 'respIndex' in msg )
 		__onRequestResponse(msg.respIndex);
 

+ 7 - 1
asworker.js

@@ -192,7 +192,13 @@
 							 'hitchhiker':reqData['hitchhiker'],
 							 'respIndex':resp});
 					},
-					function(err) 	{__postInternalErrorMsg(resp,err);}
+					function (err) {
+
+						if (err['code'].includes("ENOENT")) {
+							err = "Error! File not found: " + err['path'];
+						}
+						__postInternalErrorMsg(resp, err);
+					}
 			);
 		},
 

+ 38 - 31
client/data_utils.js

@@ -293,37 +293,44 @@ DataUtils = function(){
 	/**
 	 * Request that the specified model be loaded
 	 */
-	this.loadm = function(fname,insert) {
-		HttpUtils.httpReq(
-				'PUT',
-				HttpUtils.url('/current.model', __NO_USERNAME),
-				{'m':HttpUtils.url(fname,__NO_WID),
-				 'insert':insert},
-				function(statusCode,resp)
-				{
-					if( ! utils.isHttpSuccessCode(statusCode) )
-					{
-						if( (matches = resp.match(/metamodel not loaded :: (.*)/)) )
-						{
-							var missing = matches[1]+'.metamodel';
-							console.warn('auto-loading missing metamodel :: '+missing);
-							DataUtils.loadmm(
-									missing,
-									function(_statusCode,_resp)
-									{
-										if( ! utils.isHttpSuccessCode(_statusCode) )
-											WindowManagement.openDialog(_ERROR,_resp);
-										else
-											DataUtils.loadm(fname,insert);
-									});
-						}
-						else
-							WindowManagement.openDialog(_ERROR,resp);
-					}
-					else
-						WindowManagement.setWindowTitle();
-				});				
-	};
+    this.loadm = function (fname, insert) {
+        HttpUtils.httpReq(
+            'PUT',
+            HttpUtils.url('/current.model', __NO_USERNAME),
+            {
+                'm': HttpUtils.url(fname, __NO_WID),
+                'insert': insert
+            },
+            function (statusCode, resp) {
+                if (utils.isHttpSuccessCode(statusCode)) {
+                    WindowManagement.setWindowTitle();
+                    return;
+                }
+
+                if ((matches = resp.match(/metamodel not loaded :: (.*)/))) {
+                    var missing = matches[1] + '.metamodel';
+                    console.warn('auto-loading missing metamodel :: ' + missing);
+                    DataUtils.loadmm(missing,
+                        function (_statusCode, _resp) {
+                            if (!utils.isHttpSuccessCode(_statusCode)) {
+
+                                if (_resp.includes("ENOENT")) {
+                                    _resp = utils.jsonp(_resp);
+                                    _resp = "Error! File not found: " + _resp['path'];
+                                }
+                                WindowManagement.openDialog(_ERROR, _resp);
+                            }
+                            else {
+                                DataUtils.loadm(fname, insert);
+                            }
+                        });
+                }
+                else
+                    WindowManagement.openDialog(_ERROR, resp);
+
+
+            });
+    };
 	
 	/*
 	CASE 1: asmm is already loaded but with a different csmm

+ 1 - 0
client/gui_utils.js

@@ -637,6 +637,7 @@ GUIUtils = function(){
 					callback(input); 
 				}
 			});
+			ok.attr("id", "dialog_btn");
 			ok.html('ok');
 			dialog.append(ok);
 		}

+ 5 - 4
package.json

@@ -16,11 +16,12 @@
     "socket.io-client": "^0.9.16"
   },
   "devDependencies": {
+    "chromedriver": "^2.38.3",
+    "glob": "^7.1.2",
     "nightwatch": "^0.9.21",
-    "selenium-server": "^3.12.0",
-    "chromedriver": "^2.38.3"
+    "selenium-server": "^3.12.0"
   },
-  "scripts" : {
-    "test" : "./run_tests.sh"
+  "scripts": {
+    "test": "./run_tests.sh"
   }
 }

+ 8 - 39
tests/03_model_test.js

@@ -1,54 +1,20 @@
-
-function loadModel(client, fnames){
-
-    for (const name of fnames) {
-
-        console.log("Loading: " + name);
-        client.execute(
-            function (fname) {
-                _loadModel(fname);
-            }, [name], null
-        );
-
-        client.pause(1000);
-
-        client.getTitle(function(title) {
-            this.assert.ok(title.includes(name), "File: " + name + " is opened");
-        });
-    }
-
-}
+let test_utils = require('./test_utils');
+let user = "./users/testuser/";
 
 module.exports = {
 
     beforeEach : function (client) {
         client.url('http://localhost:8124/atompm').pause(300);
-
-
     },
 
     'Login' : function (client) {
-
-        client.execute(
-            function() {
-                UserManagement.login('testuser');
-            }, [], null
-        );
-
-        client.pause(300);
-
-        client.getTitle(function(title) {
-            this.assert.ok(title.includes("AToMPM - [Unnamed]"), "AToMPM is opened");
-        });
+        test_utils.login(client);
     },
 
     'Load model' : function (client) {
 
         let filename = 'Formalisms/ClassicDEVS/ClassicDEVS.model';
-
-
-        loadModel(client, [filename]);
-
+        test_utils.load_model(client, [filename]);
     },
 
     'Load two models' : function (client) {
@@ -58,9 +24,12 @@ module.exports = {
             'Formalisms/Annotation/AnnotationMM.model'
         ];
 
-        loadModel(client, filenames);
+        test_utils.load_model(client, filenames);
     },
 
+    'Load all models' : function (client) {
+        test_utils.getFiles(client, user, '/**/*.model', test_utils.load_model);
+    },
 
     after : function (client) {
         client.end();

+ 40 - 0
tests/04_toolbar_test.js

@@ -0,0 +1,40 @@
+
+let test_utils = require('./test_utils');
+let user = "./users/testuser/";
+
+module.exports = {
+
+    beforeEach: function (client) {
+        client.url('http://localhost:8124/atompm').pause(300);
+    },
+
+    'Login': function (client) {
+
+        test_utils.login(client);
+    },
+
+    'Load main menu toolbar': function (client) {
+        let filename = 'Toolbars/MainMenu/MainMenu.buttons.model';
+        test_utils.load_toolbar(client, [filename]);
+    },
+
+    'Load all toolbars': function (client) {
+
+        console.log("Testing toolbars...");
+        test_utils.getFiles(client, user, '/**/*.buttons.model', test_utils.load_toolbar);
+
+        console.log("Testing metamodels...");
+        test_utils.getFiles(client, user, '/**/*Icons.metamodel', test_utils.load_toolbar);
+
+        console.log("Testing pattern metamodels...");
+        test_utils.getFiles(client, user, '/**/*Icons.pattern.metamodel', test_utils.load_toolbar);
+    },
+
+    after: function (client) {
+        client.end();
+    },
+
+
+};
+
+

+ 114 - 0
tests/test_utils.js

@@ -0,0 +1,114 @@
+function login(client) {
+    client.execute(
+        function () {
+            UserManagement.login('testuser');
+        }, [], null
+    );
+
+    client.pause(500);
+
+    client.getTitle(function (title) {
+        this.assert.ok(title.includes("AToMPM - [Unnamed]"), "AToMPM is opened");
+    });
+}
+
+function load_model(client, fnames) {
+
+    for (const name of fnames) {
+        client.execute(
+            function (fname) {
+                _loadModel(fname);
+            }, [name], null
+        );
+
+        client.pause(1000);
+
+        client.element('css selector', '#dialog_btn', function (result) {
+            if (result.status != -1) {
+                //Dialog has popped up, so check the text and click the button
+                client.assert.containsText("#div_dialog_0", "File not found");
+                client.click("#dialog_btn");
+
+                //client.verify.ok(false, "File: " + name + " failed to load!"); //don't stop testing
+                console.error("File: " + name + " failed to load!");
+
+            } else {
+                //Model loaded, so check the title
+                client.getTitle(function (title) {
+                    this.assert.ok(title.includes(name), "Check for model: " + name);
+                });
+            }
+        });
+
+    }
+
+}
+
+function load_toolbar(client, fnames) {
+
+    for (let name of fnames) {
+
+        client.execute(
+            function (fname) {
+                _loadToolbar(fname);
+            }, [name], null
+        );
+
+        let toolbar_name = name.replace(/\//g, "\\2f ").replace(/\./g, "\\2e ");
+        toolbar_name = "#div_toolbar_" + toolbar_name;
+
+        client.element('css selector', '#dialog_btn', function (result) {
+            if (result.status != -1) {
+                //Dialog has popped up, so check the text and click the button
+                client.assert.containsText("#div_dialog_0", "File not found");
+                client.click("#dialog_btn");
+
+                //client.verify.ok(false, "File: " + name + " failed to load!"); //don't stop testing
+                console.error("File: " + name + " failed to load!");
+            } else {
+                //Toolbar loaded, so check for it
+                client.waitForElementPresent(toolbar_name, 2000, "Check for toolbar: " + name);
+            }
+        });
+
+            }
+
+}
+
+let user = "./users/testuser/";
+let glob = require('glob');
+
+let getFiles = function (client, dir, pattern, load_function, files_to_skip) {
+    glob(dir + pattern, callback(client, load_function, files_to_skip));
+};
+
+function callback(client, load_function, files_to_skip) {
+    return function (err, res) {
+        if (err) {
+            assert(false, "Error in reading directory: " + user + "Toolbars");
+        } else {
+
+            let filenames = [];
+            for (let i in res) {
+                let fn = res[i];
+                fn = "\/" + fn.replace(user, "");
+
+                //skip files we know will fail
+                if (files_to_skip == undefined || !files_to_skip.includes(fn)) {
+                    filenames.push(fn);
+                }
+            }
+
+            //console.log(filenames);
+            load_function(client, filenames);
+        }
+    }
+}
+
+module.exports = {
+    '@disabled': true,
+    login,
+    load_model,
+    load_toolbar,
+    getFiles
+};