Skip to content

Commit

Permalink
Add unit tests for KeyboardStyling
Browse files Browse the repository at this point in the history
  • Loading branch information
vickcoo committed Sep 5, 2024
1 parent e3ed1de commit 0481f67
Show file tree
Hide file tree
Showing 2 changed files with 295 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Scribe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
147797C02A2D0CDF0044A53E /* SettingsTableData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 147797BF2A2D0CDF0044A53E /* SettingsTableData.swift */; };
14AC56842A24AED3006B1DDF /* AboutViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14AC56832A24AED3006B1DDF /* AboutViewController.swift */; };
14AC568A2A261663006B1DDF /* InformationScreenVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14AC56892A261663006B1DDF /* InformationScreenVC.swift */; };
1900C00E2C88BF980017A874 /* TestKeyboardStyling.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1900C00D2C88BF980017A874 /* TestKeyboardStyling.swift */; };
198369CC2C7980BA00C1B583 /* KeyboardProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 198369CB2C7980BA00C1B583 /* KeyboardProvider.swift */; };
198369CD2C7980BA00C1B583 /* KeyboardProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 198369CB2C7980BA00C1B583 /* KeyboardProvider.swift */; };
198369CE2C7980BA00C1B583 /* KeyboardProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 198369CB2C7980BA00C1B583 /* KeyboardProvider.swift */; };
Expand Down Expand Up @@ -904,6 +905,7 @@
147797BF2A2D0CDF0044A53E /* SettingsTableData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsTableData.swift; sourceTree = "<group>"; };
14AC56832A24AED3006B1DDF /* AboutViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutViewController.swift; sourceTree = "<group>"; };
14AC56892A261663006B1DDF /* InformationScreenVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InformationScreenVC.swift; sourceTree = "<group>"; };
1900C00D2C88BF980017A874 /* TestKeyboardStyling.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestKeyboardStyling.swift; sourceTree = "<group>"; };
198369CB2C7980BA00C1B583 /* KeyboardProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardProvider.swift; sourceTree = "<group>"; };
19DC85F92C7772FC006E32FD /* KeyboardBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardBuilder.swift; sourceTree = "<group>"; };
30453963293B9D18003AE55B /* InformationToolTipData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InformationToolTipData.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1394,6 +1396,7 @@
isa = PBXGroup;
children = (
D13E0DC82C86530E007F00AF /* TestExtensions.swift */,
1900C00D2C88BF980017A874 /* TestKeyboardStyling.swift */,
);
path = KeyboardsBase;
sourceTree = "<group>";
Expand Down Expand Up @@ -2433,6 +2436,7 @@
buildActionMask = 2147483647;
files = (
693150472C881DCE005F99E8 /* BaseTableViewControllerTest.swift in Sources */,
1900C00E2C88BF980017A874 /* TestKeyboardStyling.swift in Sources */,
D13E0DC92C86530E007F00AF /* TestExtensions.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
291 changes: 291 additions & 0 deletions Tests/Keyboards/KeyboardsBase/TestKeyboardStyling.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
/**
* Tests for keyboard styling used in Scribe keyboards.
*
* Copyright (C) 2024 Scribe
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import Foundation
@testable import Scribe
import XCTest

// MARK: styleBtn

class KeyboardStylingTest: XCTestCase {
func testStyleBtnNormalButton() {
let button = UIButton(type: .system)
let title = "return"
let radius = 4.0

styleBtn(btn: button, title: title, radius: radius)

XCTAssertEqual(button.configuration, nil)
XCTAssertEqual(button.clipsToBounds, false)
XCTAssertEqual(button.layer.masksToBounds, false)
XCTAssertEqual(button.layer.cornerRadius, radius)
XCTAssertEqual(button.titleLabel?.text, title)
XCTAssertEqual(button.contentHorizontalAlignment, .center)
XCTAssertEqual(button.titleColor(for: .normal), keyCharColor)
XCTAssertEqual(button.layer.shadowColor, keyShadowColor)
XCTAssertEqual(button.layer.shadowOffset, CGSize(width: 0.0, height: 1.0))
XCTAssertEqual(button.layer.shadowOpacity, 1.0)
XCTAssertEqual(button.layer.shadowRadius, 0.0)
}

func testStyleBtnWithInvalidCommandMsg() {
let button = UIButton(type: .system)
let title = "Not in Wikidata"
let radius = 4.0

invalidCommandMsg = "Not in Wikidata"
styleBtn(btn: button, title: title, radius: radius)

XCTAssertEqual(button.configuration?.baseForegroundColor, UITraitCollection.current.userInterfaceStyle == .light ? specialKeyColor : keyColor)
XCTAssertEqual(button.configuration?.image, UIImage(systemName: "info.circle.fill"))
XCTAssertEqual(button.configuration?.imagePlacement, .trailing)
XCTAssertEqual(button.configuration?.imagePadding, 3)
}

func testStyleBtnWitheScribeTitle() {
let button = UIButton(type: .system)
let title = "Scribe"
let radius = 4.0

styleBtn(btn: button, title: title, radius: radius)

XCTAssertNotEqual(button.layer.shadowColor, keyShadowColor)
XCTAssertNotEqual(button.layer.shadowOffset, CGSize(width: 0.0, height: 1.0))
XCTAssertNotEqual(button.layer.shadowOpacity, 1.0)
XCTAssertNotEqual(button.layer.shadowRadius, 0.0)
}

func testStyleBtnWithEmojisToShow() {
let button = UIButton(type: .system)
let title = "return"
let radius = 4.0

emojisToShow = .one
styleBtn(btn: button, title: title, radius: radius)

XCTAssertEqual(button.layer.shadowOpacity, 0)
}
}

// MARK: getPhoneIconConfig

extension KeyboardStylingTest {
func testGetPhoneIconConfigWithInvalidIconNameInPortaint() {
letterKeyWidth = 100
isLandscapeView = false
let iconName = "abc"
let expected = UIImage.SymbolConfiguration(
pointSize: letterKeyWidth / 1.75,
weight: .light,
scale: .medium
)

let result = getPhoneIconConfig(iconName: iconName)

XCTAssertEqual(expected, result)
}

func testGetPhoneIconConfigWithValidIconNameInPortaint() {
letterKeyWidth = 100
isLandscapeView = false
let iconName = "delete.left"
let expected = UIImage.SymbolConfiguration(
pointSize: letterKeyWidth / 1.55,
weight: .light,
scale: .medium
)

let result = getPhoneIconConfig(iconName: iconName)

XCTAssertEqual(expected, result)
}

func testGetPhoneIconConfigWithValidIconNameInLandscape() {
letterKeyWidth = 100
isLandscapeView = true
let iconName = "delete.left"
let expected = UIImage.SymbolConfiguration(
pointSize: letterKeyWidth / 3.2,
weight: .light,
scale: .medium
)

let result = getPhoneIconConfig(iconName: iconName)

XCTAssertEqual(expected, result)
}

func testGetPhoneIconConfigWithInvalidIconNameInLandscape() {
letterKeyWidth = 100
isLandscapeView = true
let iconName = "abc"
let expected = UIImage.SymbolConfiguration(
pointSize: letterKeyWidth / 3.5,
weight: .light,
scale: .medium
)

let result = getPhoneIconConfig(iconName: iconName)

XCTAssertEqual(expected, result)
}
}

// MARK: getPadIconConfig

extension KeyboardStylingTest {
func testGetPadIconConfigWithInvalidIconNameInPortraint() {
letterKeyWidth = 100
isLandscapeView = false
let expected = UIImage.SymbolConfiguration(
pointSize: letterKeyWidth / 3,
weight: .light,
scale: .medium
)

let result = getPadIconConfig(iconName: "abc")

XCTAssertEqual(expected, result)
}

func testGetPadIconConfigWithValidIconNameInPortraint() {
letterKeyWidth = 100
isLandscapeView = false
let iconName = "delete.left"
let expected = UIImage.SymbolConfiguration(
pointSize: letterKeyWidth / 2.75,
weight: .light,
scale: .medium
)

let result = getPadIconConfig(iconName: iconName)

XCTAssertEqual(expected, result)
}

func testGetPadIconConfigWithInvalidIconNameInLandscape() {
letterKeyWidth = 100
isLandscapeView = true
let iconName = "abc"
let expected = UIImage.SymbolConfiguration(
pointSize: letterKeyWidth / 3.75,
weight: .light,
scale: .medium
)

let result = getPadIconConfig(iconName: iconName)

XCTAssertEqual(expected, result)
}

func testGetPadIconConfigWithValidIconNameInLandscape() {
letterKeyWidth = 100
isLandscapeView = true
let iconName = "delete.left"
let expected = UIImage.SymbolConfiguration(
pointSize: letterKeyWidth / 3.4,
weight: .light,
scale: .medium
)

let result = getPadIconConfig(iconName: iconName)

XCTAssertEqual(expected, result)
}

func testGetPadIconConfigWithValidIconNameGlobe() {
letterKeyWidth = 100
isLandscapeView = false
let iconName = "globe"
let expected = UIImage.SymbolConfiguration(
pointSize: letterKeyWidth / 2.75,
weight: .light,
scale: .medium
)

let result = getPadIconConfig(iconName: iconName)

XCTAssertEqual(expected, result)
}
}

// MARK: styleIconBtn

extension KeyboardStylingTest {
func testStyleIconBtn() {
let color = UIColor.black
let iconName = "delete.left"
let button = UIButton(type: .system)

let iconConfig = getPhoneIconConfig(iconName: iconName)
let image = UIImage(systemName: iconName, withConfiguration: iconConfig)

styleIconBtn(btn: button, color: color, iconName: iconName)

XCTAssertEqual(image, button.imageView?.image)
XCTAssertEqual(button.tintColor, color)
}
}

// MARK: styleDeleteButton

extension KeyboardStylingTest {
func testStyleDeleteButton() {
let pressedButton = UIButton()
let unpressButton = UIButton()
let pressedIconName = "delete.left.fill"
let unpressIconName = "delete.left"
let pressedButtonIconConfig = getPhoneIconConfig(iconName: pressedIconName)
let unpressButtonIconConfig = getPhoneIconConfig(iconName: unpressIconName)
let pressedButtonImage = UIImage(systemName: pressedIconName, withConfiguration: pressedButtonIconConfig)
let unpressButtonImage = UIImage(systemName: unpressIconName, withConfiguration: unpressButtonIconConfig)

styleDeleteButton(pressedButton, isPressed: true)
styleDeleteButton(unpressButton, isPressed: false)

XCTAssertEqual(pressedButton.imageView?.image, pressedButtonImage)
XCTAssertEqual(unpressButton.imageView?.image, unpressButtonImage)
}
}

// MARK: addPadding

extension KeyboardStylingTest {
func testAddPadding() {
let stackView = UIStackView()
let width = CGFloat(10)
let key = "@"

paddingViews = []
XCTAssertEqual(paddingViews.count, 0)
XCTAssertEqual(stackView.subviews.count, 0)

addPadding(to: stackView, width: width, key: key)

XCTAssertEqual(paddingViews.count, 1)
XCTAssertEqual(stackView.subviews.count, 1)

let padding = stackView.subviews.first as! UIButton
let widthConstraint = padding.constraints.first { $0.firstAttribute == .width }
XCTAssertEqual(padding.titleColor(for: .normal), .clear)
XCTAssertEqual(padding.alpha, 0.0)
XCTAssertEqual(padding.isUserInteractionEnabled, false)
XCTAssertEqual(widthConstraint?.constant, CGFloat(10))
}
}

0 comments on commit 0481f67

Please sign in to comment.