CotEditor/Tests/ThemeTests.swift
2024-06-26 20:30:54 +09:00

103 lines
3.4 KiB
Swift

//
// ThemeTests.swift
// Tests
//
// CotEditor
// https://coteditor.com
//
// Created by 1024jp on 2016-03-15.
//
// ---------------------------------------------------------------------------
//
// © 2016-2024 1024jp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import AppKit
import UniformTypeIdentifiers
import Testing
import Syntax
@testable import CotEditor
actor ThemeTests {
private let themeDirectoryName = "Themes"
@Test func defaultTheme() throws {
let themeName = "Dendrobates"
let theme = try self.loadThemeWithName(themeName)
#expect(theme.name == themeName)
#expect(theme.text.color == NSColor.black.usingColorSpace(.genericRGB))
#expect(theme.insertionPoint.color == NSColor.black.usingColorSpace(.genericRGB))
withKnownIssue("Test-side issue") {
#expect(theme.invisibles.color.brightnessComponent == 0.725) // accuracy: 0.01
}
#expect(theme.background.color == NSColor.white.usingColorSpace(.genericRGB))
withKnownIssue("Test-side issue") {
#expect(theme.lineHighlight.color.brightnessComponent == 0.929) // accuracy: 0.01
}
#expect(theme.effectiveSecondarySelectionColor(for: NSAppearance(named: .aqua)!) == .unemphasizedSelectedContentBackgroundColor)
#expect(!theme.isDarkTheme)
for type in SyntaxType.allCases {
let style = try #require(theme.style(for: type))
#expect(style.color.hueComponent > 0)
}
#expect(!theme.isDarkTheme)
}
@Test func darkTheme() throws {
let themeName = "Anura (Dark)"
let theme = try self.loadThemeWithName(themeName)
#expect(theme.name == themeName)
#expect(theme.isDarkTheme)
}
/// Tests if all of bundled themes are valid.
@Test func bundledThemes() throws {
let themeDirectoryURL = try #require(Bundle(for: type(of: self)).url(forResource: self.themeDirectoryName, withExtension: nil))
let urls = try FileManager.default.contentsOfDirectory(at: themeDirectoryURL, includingPropertiesForKeys: nil, options: [.skipsSubdirectoryDescendants, .skipsHiddenFiles])
.filter { UTType.cotTheme.preferredFilenameExtension == $0.pathExtension }
#expect(!urls.isEmpty)
for url in urls {
#expect(throws: Never.self) { try Theme(contentsOf: url) }
}
}
}
private extension ThemeTests {
func loadThemeWithName(_ name: String) throws -> Theme {
guard
let url = Bundle(for: type(of: self)).url(forResource: name, withExtension: UTType.cotTheme.preferredFilenameExtension, subdirectory: self.themeDirectoryName)
else { throw CocoaError(.fileNoSuchFile) }
return try Theme(contentsOf: url)
}
}