|
| 1 | +// |
| 2 | +// SharingTest.swift |
| 3 | +// NextcloudTests |
| 4 | +// |
| 5 | +// Created by A200020526 on 07/06/23. |
| 6 | +// Copyright © 2023 Marino Faggiana. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import XCTest |
| 10 | +@testable import Nextcloud |
| 11 | +final class SharingTest: XCTestCase { |
| 12 | + |
| 13 | + var button: UIButton? |
| 14 | + var ncShare: NCShare? |
| 15 | + |
| 16 | + override func setUpWithError() throws { |
| 17 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 18 | + super.setUp() |
| 19 | + button = UIButton() |
| 20 | + ncShare = NCShare() |
| 21 | + } |
| 22 | + |
| 23 | + override func tearDownWithError() throws { |
| 24 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 25 | + button = nil |
| 26 | + ncShare = nil |
| 27 | + super.tearDown() |
| 28 | + } |
| 29 | + |
| 30 | + func testPerformanceExample() throws { |
| 31 | + // This is an example of a performance test case. |
| 32 | + self.measure { |
| 33 | + // Put the code you want to measure the time of here. |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + //Date exntesion test case |
| 39 | + func testTomorrow() { |
| 40 | + let tomorrow = Date.tomorrow |
| 41 | + let expectedTomorrow = Calendar.current.date(byAdding: .day, value: 1, to: Date())! |
| 42 | + XCTAssertEqual(tomorrow.extendedIso8601String, expectedTomorrow.extendedIso8601String, "Tomorrow date should be correct.") |
| 43 | + } |
| 44 | + func testToday() { |
| 45 | + let today = Date.today |
| 46 | + let currentDate = Date() |
| 47 | + XCTAssertEqual(today.extendedIso8601String, currentDate.extendedIso8601String, "Today date should be correct.") |
| 48 | + } |
| 49 | + |
| 50 | + func testDayAfter() { |
| 51 | + let date = Date() |
| 52 | + let dayAfter = date.dayAfter |
| 53 | + let expectedDayAfter = Calendar.current.date(byAdding: .day, value: 1, to: date)! |
| 54 | + XCTAssertEqual(dayAfter.extendedIso8601String, expectedDayAfter.extendedIso8601String, "Day after date should be correct.") |
| 55 | + } |
| 56 | + |
| 57 | + //Date Formatter extension Test Case |
| 58 | + func testShareExpDate() { |
| 59 | + let dateFormatter = DateFormatter.shareExpDate |
| 60 | + |
| 61 | + XCTAssertEqual(dateFormatter.formatterBehavior, .behavior10_4, "Formatter behavior should be correct.") |
| 62 | + XCTAssertEqual(dateFormatter.dateStyle, .medium, "Date style should be correct.") |
| 63 | + XCTAssertEqual(dateFormatter.dateFormat, NCShareAdvancePermission.displayDateFormat, "Date format should be correct.") |
| 64 | + } |
| 65 | + |
| 66 | + //Button Extension test case |
| 67 | + func testSetBackgroundColor() { |
| 68 | + // Arrange |
| 69 | + let color = UIColor.red |
| 70 | + let state: UIControl.State = .normal |
| 71 | + |
| 72 | + // Act |
| 73 | + button?.setBackgroundColor(color, for: state) |
| 74 | + |
| 75 | + // Assert |
| 76 | + XCTAssertNotNil(button?.currentBackgroundImage, "Button background image not nil") |
| 77 | + } |
| 78 | + |
| 79 | + func testSetBackgroundColorForDifferentStates() { |
| 80 | + // Arrange |
| 81 | + |
| 82 | + let selectedColor = UIColor.green |
| 83 | + |
| 84 | + // Act |
| 85 | + button?.isSelected = true |
| 86 | + button?.setBackgroundColor(selectedColor, for: .selected) |
| 87 | + |
| 88 | + // Assert |
| 89 | + XCTAssertNotNil(button?.currentBackgroundImage, "Button background image not nil") |
| 90 | + button?.isSelected = false |
| 91 | + XCTAssertNil(button?.currentBackgroundImage,"Button background image will be nil") |
| 92 | + button?.isHighlighted = true |
| 93 | + XCTAssertNil(button?.currentBackgroundImage, "Button background image will be nil") |
| 94 | + } |
| 95 | + |
| 96 | + //UIView extension shadow test case |
| 97 | + func testAddShadowWithLocation() { |
| 98 | + // Create a UIView instance |
| 99 | + let view = UIView() |
| 100 | + |
| 101 | + // Set the shadow with bottom location |
| 102 | + view.addShadow(location: .bottom, height: 2, color: .red, opacity: 0.4, radius: 2) |
| 103 | + |
| 104 | + // Verify that the shadow offset is set correctly for the bottom location |
| 105 | + let bottomShadowOffset = view.layer.shadowOffset |
| 106 | + XCTAssertEqual(bottomShadowOffset, CGSize(width: 0, height: 2), "Shadow offset not set correctly for bottom location") |
| 107 | + |
| 108 | + // Verify that the shadow color is set correctly |
| 109 | + let shadowColor = view.layer.shadowColor |
| 110 | + XCTAssertEqual(shadowColor, UIColor.red.cgColor, "Shadow color not set correctly") |
| 111 | + |
| 112 | + // Verify that the shadow opacity is set correctly |
| 113 | + let shadowOpacity = view.layer.shadowOpacity |
| 114 | + XCTAssertEqual(shadowOpacity, 0.4, "Shadow opacity not set correctly") |
| 115 | + |
| 116 | + // Verify that the shadow radius is set correctly |
| 117 | + let shadowRadius = view.layer.shadowRadius |
| 118 | + XCTAssertEqual(shadowRadius, 2.0, "Shadow radius not set correctly") |
| 119 | + } |
| 120 | + |
| 121 | + func testAddShadowWithOffset() { |
| 122 | + // Create a UIView instance |
| 123 | + let view = UIView() |
| 124 | + |
| 125 | + // Set the shadow with a custom offset |
| 126 | + view.addShadow(offset: CGSize(width: 0, height: -4), color: .blue, opacity: 0.6, radius: 3) |
| 127 | + |
| 128 | + // Verify that the shadow offset is set correctly |
| 129 | + let shadowOffset = view.layer.shadowOffset |
| 130 | + XCTAssertEqual(shadowOffset, CGSize(width: 0, height: -4), "Shadow offset not set correctly") |
| 131 | + |
| 132 | + // Verify that the shadow color is set correctly |
| 133 | + let shadowColor = view.layer.shadowColor |
| 134 | + XCTAssertEqual(shadowColor, UIColor.blue.cgColor, "Shadow color not set correctly") |
| 135 | + |
| 136 | + // Verify that the shadow opacity is set correctly |
| 137 | + let shadowOpacity = view.layer.shadowOpacity |
| 138 | + XCTAssertEqual(shadowOpacity, 0.6, "Shadow opacity not set correctly") |
| 139 | + |
| 140 | + // Verify that the shadow radius is set correctly |
| 141 | + let shadowRadius = view.layer.shadowRadius |
| 142 | + XCTAssertEqual(shadowRadius, 3.0, "Shadow radius not set correctly") |
| 143 | + } |
| 144 | + |
| 145 | + func testAddShadowForLocation() { |
| 146 | + // Create a UIView instance |
| 147 | + let view = UIView() |
| 148 | + |
| 149 | + // Add shadow to the bottom |
| 150 | + view.addShadow(location: .bottom, color: UIColor.black) |
| 151 | + |
| 152 | + // Verify that the shadow properties are set correctly for the bottom location |
| 153 | + XCTAssertEqual(view.layer.shadowOffset, CGSize(width: 0, height: 2), "Shadow offset not set correctly for bottom location") |
| 154 | + XCTAssertEqual(view.layer.shadowColor, UIColor.black.cgColor, "Shadow color not set correctly for bottom location") |
| 155 | + XCTAssertEqual(view.layer.shadowOpacity, 0.4, "Shadow opacity not set correctly for bottom location") |
| 156 | + XCTAssertEqual(view.layer.shadowRadius, 2.0, "Shadow radius not set correctly for bottom location") |
| 157 | + |
| 158 | + // Add shadow to the top |
| 159 | + view.addShadow(location: .top) |
| 160 | + |
| 161 | + // Verify that the shadow properties are set correctly for the top location |
| 162 | + XCTAssertEqual(view.layer.shadowOffset, CGSize(width: 0, height: -2), "Shadow offset not set correctly for top location") |
| 163 | + XCTAssertEqual(view.layer.shadowColor, NCBrandColor.shared.customerDarkGrey.cgColor, "Shadow color not set correctly for top location") |
| 164 | + XCTAssertEqual(view.layer.shadowOpacity, 0.4, "Shadow opacity not set correctly for top location") |
| 165 | + XCTAssertEqual(view.layer.shadowRadius, 2.0, "Shadow radius not set correctly for top location") |
| 166 | + } |
| 167 | + |
| 168 | + func testAddShadowForOffset() { |
| 169 | + // Create a UIView instance |
| 170 | + let view = UIView() |
| 171 | + |
| 172 | + // Add shadow with custom offset |
| 173 | + view.addShadow(offset: CGSize(width: 2, height: 2)) |
| 174 | + |
| 175 | + // Verify that the shadow properties are set correctly for the custom offset |
| 176 | + XCTAssertEqual(view.layer.shadowOffset, CGSize(width: 2, height: 2), "Shadow offset not set correctly for custom offset") |
| 177 | + XCTAssertEqual(view.layer.shadowColor, UIColor.black.cgColor, "Shadow color not set correctly for custom offset") |
| 178 | + XCTAssertEqual(view.layer.shadowOpacity, 0.5, "Shadow opacity not set correctly for custom offset") |
| 179 | + XCTAssertEqual(view.layer.shadowRadius, 5.0, "Shadow radius not set correctly for custom offset") |
| 180 | + } |
| 181 | + |
| 182 | + |
| 183 | + func testHasUploadPermission() { |
| 184 | + // Create an instance of NCShare |
| 185 | + let share = NCShare() |
| 186 | + |
| 187 | + // Define the input parameters |
| 188 | + let tableShareWithUploadPermission = tableShare() |
| 189 | + tableShareWithUploadPermission.permissions = NCGlobal.shared.permissionMaxFileShare |
| 190 | + |
| 191 | + let tableShareWithoutUploadPermission = tableShare() |
| 192 | + tableShareWithoutUploadPermission.permissions = NCGlobal.shared.permissionReadShare |
| 193 | + |
| 194 | + // Call the hasUploadPermission function |
| 195 | + let hasUploadPermission1 = share.hasUploadPermission(tableShare: tableShareWithUploadPermission) |
| 196 | + let hasUploadPermission2 = share.hasUploadPermission(tableShare: tableShareWithoutUploadPermission) |
| 197 | + |
| 198 | + // Verify the results |
| 199 | + XCTAssertTrue(hasUploadPermission1, "hasUploadPermission returned false for a tableShare with upload permission") |
| 200 | + XCTAssertFalse(hasUploadPermission2, "hasUploadPermission returned true for a tableShare without upload permission") |
| 201 | + } |
| 202 | + |
| 203 | + func testGetImageShareType() { |
| 204 | + let sut = NCShareCommon() // Replace with the actual class containing the getImageShareType function |
| 205 | + |
| 206 | + // Test case 1: SHARE_TYPE_USER |
| 207 | + let shareType1 = sut.SHARE_TYPE_USER |
| 208 | + let result1 = sut.getImageShareType(shareType: shareType1) |
| 209 | + XCTAssertEqual(result1, UIImage(named: "shareTypeEmail")?.imageColor(NCBrandColor.shared.label)) |
| 210 | + |
| 211 | + // Test case 2: SHARE_TYPE_GROUP |
| 212 | + let shareType2 = sut.SHARE_TYPE_GROUP |
| 213 | + let result2 = sut.getImageShareType(shareType: shareType2) |
| 214 | + XCTAssertEqual(result2, UIImage(named: "shareTypeGroup")?.imageColor(NCBrandColor.shared.label)) |
| 215 | + |
| 216 | + // Test case 3: SHARE_TYPE_LINK |
| 217 | + let shareType3 = sut.SHARE_TYPE_LINK |
| 218 | + let result3 = sut.getImageShareType(shareType: shareType3) |
| 219 | + XCTAssertEqual(result3, UIImage(named: "shareTypeLink")?.imageColor(NCBrandColor.shared.label)) |
| 220 | + |
| 221 | + // Test case 4: SHARE_TYPE_EMAIL (with isDropDown=false) |
| 222 | + let shareType4 = sut.SHARE_TYPE_EMAIL |
| 223 | + let result4 = sut.getImageShareType(shareType: shareType4) |
| 224 | + XCTAssertEqual(result4, UIImage(named: "shareTypeUser")?.imageColor(NCBrandColor.shared.label)) |
| 225 | + |
| 226 | + // Test case 5: SHARE_TYPE_EMAIL (with isDropDown=true) |
| 227 | + let shareType5 = sut.SHARE_TYPE_EMAIL |
| 228 | + let isDropDown5 = true |
| 229 | + let result5 = sut.getImageShareType(shareType: shareType5, isDropDown: isDropDown5) |
| 230 | + XCTAssertEqual(result5, UIImage(named: "email")?.imageColor(NCBrandColor.shared.label)) |
| 231 | + } |
| 232 | +} |
0 commit comments