summaryrefslogtreecommitdiff
path: root/macos/Tests/NSScreenTests.swift
blob: f7431bf05fe16e5157285ff275dff561c14e33f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
//  WindowPositionTests.swift
//  GhosttyTests
//
//  Tests for window positioning coordinate conversion functionality.
//

import Testing
import AppKit
@testable import Ghostty

struct NSScreenExtensionTests {
    /// Test positive coordinate conversion from top-left to bottom-left
    @Test func testPositiveCoordinateConversion() async throws {
        // Mock screen with 1000x800 visible frame starting at (0, 100)
        let mockScreenFrame = NSRect(x: 0, y: 100, width: 1000, height: 800)
        let mockScreen = MockNSScreen(visibleFrame: mockScreenFrame)
        
        // Mock window size
        let windowSize = CGSize(width: 400, height: 300)
        
        // Test top-left positioning: x=15, y=15
        let origin = mockScreen.origin(
            fromTopLeftOffsetX: 15,
            offsetY: 15,
            windowSize: windowSize)
        
        // Expected: x = 0 + 15 = 15, y = (100 + 800) - 15 - 300 = 585
        #expect(origin.x == 15)
        #expect(origin.y == 585)
    }
    
    /// Test zero coordinates (exact top-left corner)
    @Test func testZeroCoordinates() async throws {
        let mockScreenFrame = NSRect(x: 0, y: 100, width: 1000, height: 800)
        let mockScreen = MockNSScreen(visibleFrame: mockScreenFrame)
        let windowSize = CGSize(width: 400, height: 300)
        
        let origin = mockScreen.origin(
            fromTopLeftOffsetX: 0,
            offsetY: 0,
            windowSize: windowSize)
        
        // Expected: x = 0, y = (100 + 800) - 0 - 300 = 600
        #expect(origin.x == 0)
        #expect(origin.y == 600)
    }
    
    /// Test with offset screen (not starting at origin)
    @Test func testOffsetScreen() async throws {
        // Secondary monitor at position (1440, 0) with 1920x1080 resolution
        let mockScreenFrame = NSRect(x: 1440, y: 0, width: 1920, height: 1080)
        let mockScreen = MockNSScreen(visibleFrame: mockScreenFrame)
        let windowSize = CGSize(width: 600, height: 400)
        
        let origin = mockScreen.origin(
            fromTopLeftOffsetX: 100,
            offsetY: 50,
            windowSize: windowSize)
        
        // Expected: x = 1440 + 100 = 1540, y = (0 + 1080) - 50 - 400 = 630
        #expect(origin.x == 1540)
        #expect(origin.y == 630)
    }
    
    /// Test large coordinates
    @Test func testLargeCoordinates() async throws {
        let mockScreenFrame = NSRect(x: 0, y: 0, width: 1920, height: 1080)
        let mockScreen = MockNSScreen(visibleFrame: mockScreenFrame)
        let windowSize = CGSize(width: 400, height: 300)
        
        let origin = mockScreen.origin(
            fromTopLeftOffsetX: 500,
            offsetY: 200,
            windowSize: windowSize)
        
        // Expected: x = 0 + 500 = 500, y = (0 + 1080) - 200 - 300 = 580
        #expect(origin.x == 500)
        #expect(origin.y == 580)
    }
}

/// Mock NSScreen class for testing coordinate conversion
private class MockNSScreen: NSScreen {
    private let mockVisibleFrame: NSRect
    
    init(visibleFrame: NSRect) {
        self.mockVisibleFrame = visibleFrame
        super.init()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override var visibleFrame: NSRect {
        return mockVisibleFrame
    }
}