UnitBaseChatViewControllerTest.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import XCTest
  6. @testable import NextcloudTalk
  7. final class UnitBaseChatViewControllerTest: TestBaseRealm {
  8. var baseController: BaseChatViewController!
  9. var testMessage: NCChatMessage!
  10. let fileMessageParameters = """
  11. {
  12. "actor": {
  13. "type": "user",
  14. "id": "admin",
  15. "name": "admin"
  16. },
  17. "file": {
  18. "type": "file",
  19. "id": "9",
  20. "name": "photo-1517603250781-c4eac1449a80.jpeg",
  21. "size": 444676,
  22. "path": "Media/photo-1517603250781-c4eac1449a80.jpeg",
  23. "link": "https://nextcloud-mm.local/index.php/f/9",
  24. "etag": "60fb4ececc370787b1cdc5623ff4a189",
  25. "permissions": 27,
  26. "mimetype": "image/jpeg",
  27. "preview-available": "yes",
  28. "width": 1491,
  29. "height": 837
  30. }
  31. }
  32. """
  33. override func setUpWithError() throws {
  34. try super.setUpWithError()
  35. baseController = BaseChatViewController(for: NCRoom())!
  36. testMessage = NCChatMessage()
  37. }
  38. func testInvisibleCellHeight() throws {
  39. // Empty message should have a height of 0
  40. testMessage.message = ""
  41. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 0.0)
  42. // Test update message
  43. testMessage.message = "System Message"
  44. testMessage.systemMessage = "message_deleted"
  45. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 0.0)
  46. }
  47. func testSystemMessageCellHeight() throws {
  48. testMessage.message = "System Message"
  49. testMessage.systemMessage = "test_system_message"
  50. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 30.0)
  51. }
  52. func testCellHeight() throws {
  53. // Normal chat message
  54. testMessage.message = "test"
  55. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 70.0)
  56. // Multiline chat message
  57. testMessage.message = "test\nasd\nasd"
  58. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 110.0)
  59. // Normal chat message with reaction
  60. testMessage.message = "test"
  61. testMessage.addTemporaryReaction("👍")
  62. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 110.0)
  63. }
  64. func testGroupedCellHeight() throws {
  65. // Grouped chat message
  66. testMessage.message = "test"
  67. testMessage.isGroupMessage = true
  68. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 30.0)
  69. // Grouped chat message with reaction
  70. testMessage.addTemporaryReaction("👍")
  71. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 70.0)
  72. }
  73. func testGroupedCellWithQuoteHeight() throws {
  74. // Add an existing message to the database
  75. let existingMessage = NCChatMessage()
  76. existingMessage.messageId = 1
  77. existingMessage.internalId = "internal-1"
  78. existingMessage.message = "existing"
  79. try? realm.transaction {
  80. realm.add(existingMessage)
  81. }
  82. // Chat message with a quote
  83. testMessage.message = "test"
  84. testMessage.parentId = "internal-1"
  85. testMessage.isGroupMessage = true
  86. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 130.0)
  87. }
  88. func testCellWithUrlHeight() throws {
  89. // Chat message with URL preview
  90. testMessage.message = "test - https://nextcloud.com"
  91. updateCapabilities { cap in
  92. cap.referenceApiSupported = true
  93. }
  94. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 175.0)
  95. // Test URL with grouped message
  96. testMessage.isGroupMessage = true
  97. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 135.0)
  98. }
  99. func testCellWithPollHeight() throws {
  100. testMessage.messageParametersJSONString = """
  101. {
  102. "actor": {
  103. "type": "user",
  104. "id": "admin",
  105. "name": "admin"
  106. },
  107. "object": {
  108. "type": "talk-poll",
  109. "id": "1",
  110. "name": "Test"
  111. }
  112. }
  113. """
  114. testMessage.message = "{object}"
  115. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 88.0)
  116. }
  117. func testCellWithGeolocationHeight() {
  118. testMessage.messageParametersJSONString = """
  119. {
  120. "actor": {
  121. "type": "user",
  122. "id": "admin",
  123. "name": "admin"
  124. },
  125. "object": {
  126. "name": "Geteilter Ort",
  127. "longitude": "6.764340827050928",
  128. "latitude": "53.20406320313201",
  129. "type": "geo-location",
  130. "id": "geo:53.20406320313201,6.764340827050928",
  131. "icon-url": "https://nextcloud-mm.local/ocs/v2.php/apps/spreed/api/v1/room/2yjsf6i6/avatar?v=02234d2d"
  132. }
  133. }
  134. """
  135. testMessage.message = "{object}"
  136. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 200.0)
  137. }
  138. func testCellWithFileHeight() {
  139. // Test without file caption
  140. testMessage.messageParametersJSONString = fileMessageParameters
  141. testMessage.message = "{file}"
  142. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 190.0)
  143. }
  144. func testCellWithFileCaptionHeight() {
  145. testMessage.messageParametersJSONString = fileMessageParameters
  146. testMessage.message = "File caption..."
  147. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 210.0)
  148. }
  149. func testCellWithFileCaptionUrlHeight() {
  150. updateCapabilities { cap in
  151. cap.referenceApiSupported = true
  152. }
  153. testMessage.messageParametersJSONString = fileMessageParameters
  154. testMessage.message = "File caption... https://nextcloud.com"
  155. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 315.0)
  156. }
  157. func testCellWithFileAndQuoteHeight() {
  158. testMessage.messageParametersJSONString = fileMessageParameters
  159. testMessage.message = "File caption..."
  160. // Add an existing message to the database
  161. let existingMessage = NCChatMessage()
  162. existingMessage.messageId = 1
  163. existingMessage.internalId = "internal-1"
  164. existingMessage.message = "existing"
  165. try? realm.transaction {
  166. realm.add(existingMessage)
  167. }
  168. testMessage.parentId = "internal-1"
  169. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 270.0)
  170. }
  171. func testCellWithVoiceMessageHeight() {
  172. testMessage.message = "abc"
  173. testMessage.messageType = "voice-message"
  174. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 102.0)
  175. }
  176. func testCellWithQuoteHeight() throws {
  177. // Add an existing message to the database
  178. let existingMessage = NCChatMessage()
  179. existingMessage.messageId = 1
  180. existingMessage.internalId = "internal-1"
  181. existingMessage.message = "existing"
  182. try? realm.transaction {
  183. realm.add(existingMessage)
  184. }
  185. // Chat message with a quote
  186. testMessage.message = "test"
  187. testMessage.parentId = "internal-1"
  188. XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 130.0)
  189. }
  190. }