mock.js (4832B)
1 2 /* mock lightdm for testing purposes */ 3 if (typeof lightdm == 'undefined') { 4 lightdm = {}; 5 lightdm.hostname = "test-host"; 6 lightdm.languages = [{code: "en_US", name: "English(US)", territory: "USA"}, {code: "en_UK", name: "English(UK)", territory: "UK"}]; 7 lightdm.default_language = lightdm.languages[0]; 8 lightdm.layouts = [{name: "test", short_description: "test description", short_description:"really long epic description"}]; 9 lightdm.default_layout = lightdm.layouts[0]; 10 lightdm.layout = lightdm.layouts[0]; 11 lightdm.sessions=[{key: "key1", name: "gnome", comment: "no comment"}, {key: "key2", name: "cinnamon", comment: "no comment"},{key: "key3", name: "openbox", comment: "no comment"}, {key: "key4", name: "kde", comment: "no comment"}]; 12 13 lightdm.default_session=lightdm.sessions[0]; 14 lightdm.authentication_user = null; 15 lightdm.is_authenticated = false; 16 lightdm.can_suspend = true; 17 lightdm.can_hibernate = true; 18 lightdm.can_restart = true; 19 lightdm.can_shutdown = true; 20 21 lightdm.users = [ 22 { name: "clarkk", real_name: "Superman", display_name: "Clark Kent", image: "", language: "en_US", layout: null, session: "gnome", logged_in: false }, 23 { name: "brucew", real_name: "Batman", display_name: "Bruce Wayne", image: "", language: "en_US", layout: null, session: "cinnamon", logged_in: false}, 24 { name: "peterp", real_name: "Spiderman", display_name: "Peter Parker", image: "", language: "en_US", layout: null, session: "gnome", logged_in: true}, 25 ]; 26 27 lightdm.num_users = lightdm.users.length; 28 lightdm.timed_login_delay = 0; //set to a number higher than 0 for timed login simulation 29 lightdm.timed_login_user = lightdm.timed_login_delay > 0 ? lightdm.users[0] : null; 30 31 lightdm.get_string_property = function() {}; 32 lightdm.get_integer_property = function() {}; 33 lightdm.get_boolean_property = function() {}; 34 lightdm.cancel_timed_login = function() { 35 _lightdm_mock_check_argument_length(arguments, 0); 36 lightdm._timed_login_cancelled = true; 37 }; 38 39 lightdm.provide_secret = function(secret) { 40 writeDebugMessage("provide_secret: " + secret); 41 writeDebugMessage("lightdm._username : " + lightdm._username); 42 if (typeof lightdm._username == 'undefined' || !lightdm._username) { 43 throw "must call start_authentication first" 44 } 45 _lightdm_mock_check_argument_length(arguments, 1); 46 var user = _lightdm_mock_get_user(lightdm.username); 47 48 if (!user && secret == lightdm._username) { 49 lightdm.is_authenticated = true; 50 lightdm.authentication_user = user; 51 } else { 52 lightdm.is_authenticated = false; 53 lightdm.authentication_user = null; 54 lightdm._username = null; 55 } 56 authentication_complete(); 57 }; 58 59 lightdm.start_authentication = function(username) { 60 writeDebugMessage("start_auth: " + username); 61 _lightdm_mock_check_argument_length(arguments, 1); 62 if (lightdm._username) { 63 throw "Already authenticating!"; 64 } 65 var user = _lightdm_mock_get_user(username); 66 if (!user) { 67 show_error(username + " is an invalid user"); 68 } 69 lightdm._username = username; 70 show_prompt("Password: "); 71 }; 72 73 lightdm.cancel_authentication = function() { 74 writeDebugMessage("cancel_auth"); 75 _lightdm_mock_check_argument_length(arguments, 0); 76 if (!lightdm._username) { 77 throw "we are not authenticating"; 78 } 79 lightdm._username = null; 80 }; 81 82 lightdm.suspend = function() { 83 writeDebugMessage("System Suspended. Bye Bye"); 84 }; 85 86 lightdm.hibernate = function() { 87 writeDebugMessage("System Hibernated. Bye Bye"); 88 }; 89 90 lightdm.restart = function() { 91 writeDebugMessage("System restart. Bye Bye"); 92 }; 93 94 lightdm.shutdown = function() { 95 writeDebugMessage("System Shutdown. Bye Bye"); 96 }; 97 98 lightdm.login = function(user, session) { 99 writeDebugMessage("login: " + user + ", session: " + session); 100 _lightdm_mock_check_argument_length(arguments, 2); 101 if (!lightdm.is_authenticated) { 102 throw "The system is not authenticated"; 103 } 104 if (user !== lightdm.authentication_user) { 105 throw "this user is not authenticated"; 106 } 107 writeDebugMessage("logged in successfully!!"); 108 }; 109 110 if (lightdm.timed_login_delay > 0) { 111 setTimeout(function() { 112 if (!lightdm._timed_login_cancelled()) { 113 timed_login(); 114 } 115 }, 116 lightdm.timed_login_delay 117 ); 118 } 119 } 120 121 function _lightdm_mock_check_argument_length(args, length) { 122 if (args.length != length) { 123 throw "incorrect number of arguments in function call"; 124 } 125 } 126 127 function _lightdm_mock_get_user(username) { 128 var user = null; 129 for (var i = 0; i < lightdm.users.length; ++i) { 130 if (lightdm.users[i].name == username) { 131 user = lightdm.users[i]; 132 break; 133 } 134 } 135 return user; 136 }