settings/accessibility/
accessibility_fidl_handler.rs

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::accessibility::types::{AccessibilityInfo, CaptionsSettings, ColorBlindnessType};
use crate::base::{SettingInfo, SettingType};
use crate::handler::base::Request;
use crate::ingress::{request, watch, Scoped};
use crate::job::source::{Error as JobError, ErrorResponder};
use crate::job::Job;
use fidl::prelude::*;
use fidl_fuchsia_settings::{
    AccessibilityRequest, AccessibilitySetResponder, AccessibilitySetResult, AccessibilitySettings,
    AccessibilityWatchResponder,
};

impl ErrorResponder for AccessibilitySetResponder {
    fn id(&self) -> &'static str {
        "Accessibility_Set"
    }

    fn respond(self: Box<Self>, error: fidl_fuchsia_settings::Error) -> Result<(), fidl::Error> {
        self.send(Err(error))
    }
}

impl request::Responder<Scoped<AccessibilitySetResult>> for AccessibilitySetResponder {
    fn respond(self, Scoped(response): Scoped<AccessibilitySetResult>) {
        let _ = self.send(response);
    }
}

impl watch::Responder<AccessibilitySettings, zx::Status> for AccessibilityWatchResponder {
    fn respond(self, response: Result<AccessibilitySettings, zx::Status>) {
        match response {
            Ok(settings) => {
                let _ = self.send(&settings);
            }
            Err(error) => {
                self.control_handle().shutdown_with_epitaph(error);
            }
        }
    }
}

impl TryFrom<AccessibilityRequest> for Job {
    type Error = JobError;

    fn try_from(item: AccessibilityRequest) -> Result<Self, Self::Error> {
        #[allow(unreachable_patterns)]
        match item {
            AccessibilityRequest::Set { settings, responder } => {
                Ok(request::Work::new(SettingType::Accessibility, to_request(settings), responder)
                    .into())
            }
            AccessibilityRequest::Watch { responder } => {
                Ok(watch::Work::new_job(SettingType::Accessibility, responder))
            }
            _ => {
                tracing::warn!("Received a call to an unsupported API: {:?}", item);
                Err(JobError::Unsupported)
            }
        }
    }
}

impl From<SettingInfo> for AccessibilitySettings {
    fn from(response: SettingInfo) -> Self {
        if let SettingInfo::Accessibility(info) = response {
            return AccessibilitySettings {
                audio_description: info.audio_description,
                screen_reader: info.screen_reader,
                color_inversion: info.color_inversion,
                enable_magnification: info.enable_magnification,
                color_correction: info.color_correction.map(ColorBlindnessType::into),
                captions_settings: info.captions_settings.map(CaptionsSettings::into),
                ..Default::default()
            };
        }

        panic!("incorrect value sent to accessibility");
    }
}

fn to_request(settings: AccessibilitySettings) -> Request {
    Request::SetAccessibilityInfo(AccessibilityInfo {
        audio_description: settings.audio_description,
        screen_reader: settings.screen_reader,
        color_inversion: settings.color_inversion,
        enable_magnification: settings.enable_magnification,
        color_correction: settings
            .color_correction
            .map(fidl_fuchsia_settings::ColorBlindnessType::into),
        captions_settings: settings
            .captions_settings
            .map(fidl_fuchsia_settings::CaptionsSettings::into),
    })
}

#[cfg(test)]
mod tests {
    use crate::accessibility::types::{CaptionFontFamily, CaptionFontStyle, ColorRgba, EdgeStyle};
    use fidl_fuchsia_settings::ColorBlindnessType;

    use super::*;

    #[fuchsia::test]
    fn test_request_try_from_settings_request_empty() {
        let request = to_request(AccessibilitySettings::default());

        const EXPECTED_ACCESSIBILITY_INFO: AccessibilityInfo = AccessibilityInfo {
            audio_description: None,
            screen_reader: None,
            color_inversion: None,
            enable_magnification: None,
            color_correction: None,
            captions_settings: None,
        };

        assert_eq!(request, Request::SetAccessibilityInfo(EXPECTED_ACCESSIBILITY_INFO));
    }

    #[fuchsia::test]
    fn test_try_from_settings_request() {
        const TEST_COLOR: ColorRgba =
            ColorRgba { red: 238.0, green: 23.0, blue: 128.0, alpha: 255.0 };
        const EXPECTED_FONT_STYLE: CaptionFontStyle = CaptionFontStyle {
            family: Some(CaptionFontFamily::Casual),
            color: Some(TEST_COLOR),
            relative_size: Some(1.0),
            char_edge_style: Some(EdgeStyle::Raised),
        };
        const EXPECTED_CAPTION_SETTINGS: CaptionsSettings = CaptionsSettings {
            for_media: Some(true),
            for_tts: Some(true),
            font_style: Some(EXPECTED_FONT_STYLE),
            window_color: Some(TEST_COLOR),
            background_color: Some(TEST_COLOR),
        };
        const EXPECTED_ACCESSIBILITY_INFO: AccessibilityInfo = AccessibilityInfo {
            audio_description: Some(true),
            screen_reader: Some(true),
            color_inversion: Some(true),
            enable_magnification: Some(true),
            color_correction: Some(crate::accessibility::types::ColorBlindnessType::Protanomaly),
            captions_settings: Some(EXPECTED_CAPTION_SETTINGS),
        };

        let accessibility_settings = AccessibilitySettings {
            audio_description: Some(true),
            screen_reader: Some(true),
            color_inversion: Some(true),
            enable_magnification: Some(true),
            color_correction: Some(ColorBlindnessType::Protanomaly),
            captions_settings: Some(EXPECTED_CAPTION_SETTINGS.into()),
            ..Default::default()
        };

        let request = to_request(accessibility_settings);

        assert_eq!(request, Request::SetAccessibilityInfo(EXPECTED_ACCESSIBILITY_INFO));
    }
}