|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +type VoiceOverGetRequest struct { |
| 9 | + DeviceID string `json:"deviceId"` |
| 10 | +} |
| 11 | + |
| 12 | +type VoiceOverSetRequest struct { |
| 13 | + DeviceID string `json:"deviceId"` |
| 14 | + State string `json:"state"` |
| 15 | +} |
| 16 | + |
| 17 | +type VoiceOverSpeechRequest struct { |
| 18 | + DeviceID string `json:"deviceId"` |
| 19 | +} |
| 20 | + |
| 21 | +type VoiceOverMoveRequest struct { |
| 22 | + DeviceID string `json:"deviceId"` |
| 23 | + Direction string `json:"direction"` |
| 24 | +} |
| 25 | + |
| 26 | +type VoiceOverStateResult struct { |
| 27 | + Enabled bool `json:"enabled"` |
| 28 | +} |
| 29 | + |
| 30 | +type VoiceOverSpeechResult struct { |
| 31 | + Utterance string `json:"utterance"` |
| 32 | +} |
| 33 | + |
| 34 | +// voiceOverController is implemented by devices whose agent drives VoiceOver |
| 35 | +// through XCUIDevice.voiceOverService: iOS 27+ simulators and real devices |
| 36 | +type voiceOverController interface { |
| 37 | + GetVoiceOver() (bool, error) |
| 38 | + SetVoiceOver(enabled bool) error |
| 39 | + VoiceOverSpeech() (string, error) |
| 40 | + MoveVoiceOver(direction string) (string, error) |
| 41 | +} |
| 42 | + |
| 43 | +var voiceOverDirections = []string{"next", "previous", "in", "out"} |
| 44 | + |
| 45 | +// parseVoiceOverState accepts "on" or "off" |
| 46 | +func parseVoiceOverState(state string) (bool, error) { |
| 47 | + switch strings.ToLower(state) { |
| 48 | + case "on": |
| 49 | + return true, nil |
| 50 | + case "off": |
| 51 | + return false, nil |
| 52 | + } |
| 53 | + return false, fmt.Errorf("invalid voiceover state '%s', must be 'on' or 'off'", state) |
| 54 | +} |
| 55 | + |
| 56 | +func parseVoiceOverDirection(direction string) (string, error) { |
| 57 | + lower := strings.ToLower(direction) |
| 58 | + for _, d := range voiceOverDirections { |
| 59 | + if lower == d { |
| 60 | + return d, nil |
| 61 | + } |
| 62 | + } |
| 63 | + return "", fmt.Errorf("invalid voiceover direction '%s', must be 'next', 'previous', 'in' or 'out'", direction) |
| 64 | +} |
| 65 | + |
| 66 | +func findVoiceOverDevice(deviceID string) (voiceOverController, string, error) { |
| 67 | + targetDevice, err := FindDeviceWithAgent(deviceID) |
| 68 | + if err != nil { |
| 69 | + return nil, "", err |
| 70 | + } |
| 71 | + |
| 72 | + controller, ok := targetDevice.(voiceOverController) |
| 73 | + if !ok { |
| 74 | + return nil, "", fmt.Errorf("device %s does not support voiceover, only iOS 27+ devices and simulators do", targetDevice.ID()) |
| 75 | + } |
| 76 | + |
| 77 | + return controller, targetDevice.ID(), nil |
| 78 | +} |
| 79 | + |
| 80 | +// VoiceOverGetCommand reports whether VoiceOver is running |
| 81 | +func VoiceOverGetCommand(req VoiceOverGetRequest) *CommandResponse { |
| 82 | + controller, id, err := findVoiceOverDevice(req.DeviceID) |
| 83 | + if err != nil { |
| 84 | + return NewErrorResponse(err) |
| 85 | + } |
| 86 | + |
| 87 | + enabled, err := controller.GetVoiceOver() |
| 88 | + if err != nil { |
| 89 | + return NewErrorResponse(fmt.Errorf("failed to get voiceover state on device %s: %v", id, err)) |
| 90 | + } |
| 91 | + |
| 92 | + return NewSuccessResponse(VoiceOverStateResult{Enabled: enabled}) |
| 93 | +} |
| 94 | + |
| 95 | +// VoiceOverSetCommand turns VoiceOver on or off. VoiceOver stays on after |
| 96 | +// mobilecli exits, and while it is on a tap focuses an element instead of |
| 97 | +// activating it. |
| 98 | +func VoiceOverSetCommand(req VoiceOverSetRequest) *CommandResponse { |
| 99 | + enabled, err := parseVoiceOverState(req.State) |
| 100 | + if err != nil { |
| 101 | + return NewErrorResponse(err) |
| 102 | + } |
| 103 | + |
| 104 | + controller, id, err := findVoiceOverDevice(req.DeviceID) |
| 105 | + if err != nil { |
| 106 | + return NewErrorResponse(err) |
| 107 | + } |
| 108 | + |
| 109 | + if err := controller.SetVoiceOver(enabled); err != nil { |
| 110 | + return NewErrorResponse(fmt.Errorf("failed to set voiceover on device %s: %v", id, err)) |
| 111 | + } |
| 112 | + |
| 113 | + return NewSuccessResponse(VoiceOverStateResult{Enabled: enabled}) |
| 114 | +} |
| 115 | + |
| 116 | +// VoiceOverSpeechCommand returns what VoiceOver speaks for the focused element |
| 117 | +func VoiceOverSpeechCommand(req VoiceOverSpeechRequest) *CommandResponse { |
| 118 | + controller, id, err := findVoiceOverDevice(req.DeviceID) |
| 119 | + if err != nil { |
| 120 | + return NewErrorResponse(err) |
| 121 | + } |
| 122 | + |
| 123 | + utterance, err := controller.VoiceOverSpeech() |
| 124 | + if err != nil { |
| 125 | + return NewErrorResponse(fmt.Errorf("failed to read voiceover speech on device %s: %v", id, err)) |
| 126 | + } |
| 127 | + |
| 128 | + return NewSuccessResponse(VoiceOverSpeechResult{Utterance: utterance}) |
| 129 | +} |
| 130 | + |
| 131 | +// VoiceOverMoveCommand moves the VoiceOver cursor and returns what it speaks |
| 132 | +func VoiceOverMoveCommand(req VoiceOverMoveRequest) *CommandResponse { |
| 133 | + direction, err := parseVoiceOverDirection(req.Direction) |
| 134 | + if err != nil { |
| 135 | + return NewErrorResponse(err) |
| 136 | + } |
| 137 | + |
| 138 | + controller, id, err := findVoiceOverDevice(req.DeviceID) |
| 139 | + if err != nil { |
| 140 | + return NewErrorResponse(err) |
| 141 | + } |
| 142 | + |
| 143 | + utterance, err := controller.MoveVoiceOver(direction) |
| 144 | + if err != nil { |
| 145 | + return NewErrorResponse(fmt.Errorf("failed to move voiceover %s on device %s: %v", direction, id, err)) |
| 146 | + } |
| 147 | + |
| 148 | + return NewSuccessResponse(VoiceOverSpeechResult{Utterance: utterance}) |
| 149 | +} |
0 commit comments