not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
7.4 KiB

  1. // Unit testing on presence
  2. describe('Presence', function() {
  3. var testPresenceServerUrl = "http://localhost";
  4. var testPresenceServerPort = 8040;
  5. var networkId = "9446effc-a186-4199-8730-9f2be39a11d2";
  6. var activityName = "sugarizer.mocha.activity";
  7. var sharedId = null;
  8. var sharedId2 = null;
  9. describe('#joinNetwork()', function() {
  10. this.timeout(5000);
  11. it("not indicate connected before connection", function() {
  12. chai.assert.isFalse(presence.isConnected());
  13. });
  14. it("should fail when server don't exist", function(done) {
  15. initSugarizer({name: "mocha1", server: {url: "http://xxxxx", presence: 8888}, networkId: networkId});
  16. presence.joinNetwork(function(error, presence) {
  17. chai.assert.isNotNull(error);
  18. done();
  19. });
  20. });
  21. it("should connect user", function(done) {
  22. initSugarizer({name: "mocha1", server: {url: testPresenceServerUrl, presence: testPresenceServerPort}, networkId: networkId});
  23. presence.joinNetwork(function(error, presence) {
  24. chai.assert.isNull(error);
  25. chai.assert.isNotNull(presence);
  26. done();
  27. });
  28. });
  29. });
  30. describe('#isConnected()', function() {
  31. it("should indicate connected", function() {
  32. chai.assert.isTrue(presence.isConnected());
  33. });
  34. });
  35. describe('#listUsers()', function() {
  36. it("should list user as connected", function(done) {
  37. presence.listUsers(function(users) {
  38. chai.assert.equal(1, users.length);
  39. chai.assert.equal("mocha1", users[0].name);
  40. chai.assert.equal(networkId, users[0].networkId);
  41. chai.assert.deepEqual({ stroke: '#AC32FF', fill: '#FF8F00' }, users[0].colorvalue);
  42. done();
  43. });
  44. });
  45. });
  46. describe('#getUserInfo()', function() {
  47. it("should retrieve user information", function() {
  48. var userInfo = presence.getUserInfo();
  49. chai.assert.equal("mocha1", userInfo.name);
  50. chai.assert.equal(networkId, userInfo.networkId);
  51. chai.assert.deepEqual({ stroke: '#AC32FF', fill: '#FF8F00' }, userInfo.colorvalue);
  52. });
  53. });
  54. describe('#createSharedActivity()', function() {
  55. it("should not contain shared activity at first", function(done) {
  56. presence.listSharedActivities(function(activities) {
  57. chai.assert.equal(0, activities.length);
  58. done();
  59. });
  60. });
  61. it("should create a shared activity at first", function(done) {
  62. presence.createSharedActivity(activityName, function(id) {
  63. sharedId = id;
  64. chai.assert.isNotNull(sharedId);
  65. chai.assert.equal(36, sharedId.length);
  66. done();
  67. });
  68. });
  69. it("should list one shared activity", function(done) {
  70. presence.listSharedActivities(function(activities) {
  71. chai.assert.equal(1, activities.length);
  72. done();
  73. });
  74. });
  75. it("should create a another shared activity", function(done) {
  76. presence.createSharedActivity(activityName, function(id) {
  77. sharedId2 = id;
  78. chai.assert.isNotNull(sharedId2);
  79. chai.assert.equal(36, sharedId2.length);
  80. chai.assert.notEqual(sharedId, sharedId2);
  81. done();
  82. });
  83. });
  84. it("should list two shared activity", function(done) {
  85. presence.listSharedActivities(function(activities) {
  86. chai.assert.equal(2, activities.length);
  87. done();
  88. });
  89. });
  90. });
  91. describe('#listSharedActivities()', function() {
  92. it("should list shared activity and details", function(done) {
  93. presence.listSharedActivities(function(activities) {
  94. chai.assert.equal(2, activities.length);
  95. chai.assert.equal(activityName, activities[0].activityId);
  96. chai.assert.equal(sharedId, activities[0].id);
  97. chai.assert.deepEqual({ stroke: '#AC32FF', fill: '#FF8F00' }, activities[0].colorvalue);
  98. chai.assert.equal(1, activities[0].users.length);
  99. chai.assert.equal(networkId, activities[0].users[0]);
  100. chai.assert.equal(activityName, activities[1].activityId);
  101. chai.assert.equal(sharedId2, activities[1].id);
  102. chai.assert.deepEqual({ stroke: '#AC32FF', fill: '#FF8F00' }, activities[1].colorvalue);
  103. chai.assert.equal(1, activities[1].users.length);
  104. chai.assert.equal(networkId, activities[1].users[0]);
  105. done();
  106. });
  107. });
  108. });
  109. describe('#listSharedActivityUsers()', function() {
  110. it("should list user connected on first activity", function(done) {
  111. presence.listSharedActivityUsers(sharedId, function(users) {
  112. chai.assert.equal(1, users.length);
  113. chai.assert.equal("mocha1", users[0].name);
  114. chai.assert.equal(networkId, users[0].networkId);
  115. chai.assert.deepEqual({ stroke: '#AC32FF', fill: '#FF8F00' }, users[0].colorvalue);
  116. done();
  117. });
  118. });
  119. it("should list user connected on second activity", function(done) {
  120. presence.listSharedActivityUsers(sharedId2, function(users) {
  121. chai.assert.equal(1, users.length);
  122. chai.assert.equal("mocha1", users[0].name);
  123. chai.assert.equal(networkId, users[0].networkId);
  124. chai.assert.deepEqual({ stroke: '#AC32FF', fill: '#FF8F00' }, users[0].colorvalue);
  125. done();
  126. });
  127. });
  128. });
  129. describe('#sendMessage()', function() {
  130. it("should not receive message to other shared group", function(done) {
  131. presence.onDataReceived(function(data) {
  132. chai.assert.fail();
  133. done();
  134. });
  135. presence.sendMessage("ffffffff-ffff-ffff-ffff-ffffffffffff", "Hello!");
  136. done();
  137. });
  138. it("should receive message sent", function(done) {
  139. presence.onDataReceived(function(data) {
  140. chai.assert.equal("Hello!", data);
  141. done();
  142. });
  143. presence.sendMessage(sharedId, "Hello!");
  144. });
  145. it("should receive message sent to other group", function(done) {
  146. presence.onDataReceived(function(data) {
  147. chai.assert.equal("World!", data);
  148. done();
  149. });
  150. presence.sendMessage(sharedId2, "World!");
  151. });
  152. });
  153. describe('#leaveSharedActivity()', function() {
  154. it("should leave activity", function(done) {
  155. presence.onSharedActivityUserChanged(function() {
  156. done();
  157. });
  158. presence.leaveSharedActivity(sharedId2, function() {
  159. });
  160. });
  161. it("should list one leaving shared activity", function(done) {
  162. presence.listSharedActivities(function(activities) {
  163. chai.assert.equal(1, activities.length);
  164. chai.assert.equal(activityName, activities[0].activityId);
  165. chai.assert.equal(sharedId, activities[0].id);
  166. chai.assert.deepEqual({ stroke: '#AC32FF', fill: '#FF8F00' }, activities[0].colorvalue);
  167. chai.assert.equal(1, activities[0].users.length);
  168. chai.assert.equal(networkId, activities[0].users[0]);
  169. done();
  170. });
  171. });
  172. it("should not receive message when left shared group", function(done) {
  173. presence.onDataReceived(function(data) {
  174. chai.assert.fail();
  175. done();
  176. });
  177. presence.sendMessage(sharedId2, "Hello!");
  178. done();
  179. });
  180. it("should leave second activity", function(done) {
  181. presence.onSharedActivityUserChanged(function(moves) {
  182. chai.assert.equal(-1, moves.move);
  183. chai.assert.equal("mocha1", moves.user.name);
  184. chai.assert.equal(networkId, moves.user.networkId);
  185. chai.assert.deepEqual({ stroke: '#AC32FF', fill: '#FF8F00' }, moves.user.colorvalue);
  186. done();
  187. });
  188. presence.leaveSharedActivity(sharedId, function() {
  189. });
  190. });
  191. it("should list no shared activity", function(done) {
  192. presence.listSharedActivities(function(activities) {
  193. chai.assert.equal(0, activities.length);
  194. done();
  195. });
  196. });
  197. });
  198. describe('#leaveNetwork()', function() {
  199. it("should disconnect", function() {
  200. presence.onConnectionClosed(function() {
  201. chai.assert.isTrue(true);
  202. });
  203. presence.leaveNetwork();
  204. });
  205. it("should do nothing when disconnected", function() {
  206. presence.onConnectionClosed(function() {
  207. chai.assert.isFalse(false);
  208. });
  209. presence.leaveNetwork();
  210. chai.assert.isFalse(presence.isConnected());
  211. });
  212. });
  213. });