darkwing/server/services/browser_profile_services/config/
screen.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/// Configuration module for screen-related settings in browser profiles.
/// Handles screen dimensions, pixel density, and color depth based on
/// platform and mode settings.
use crate::{
  database::browser_profile::Platform,
  server::{
    dtos::{
      browser_profile_dto::{BrowserProfileFullData, Mode},
      start_dto::StartRequest,
    },
    error::Error,
  },
};
use serde::{Deserialize, Serialize};

use super::{FromStartRequest, Navigator};

/// Represents screen configuration settings for a browser profile.
///
/// Contains information about:
/// - Whether real screen dimensions should be used
/// - Screen width and height
/// - Device pixel ratio (DPR)
/// - Color depth
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
pub struct Screen {
  /// Whether to use real screen dimensions from the user's device
  pub is_real: bool,
  /// Screen width in pixels
  pub width: i16,
  /// Screen height in pixels
  pub height: i16,
  /// Device pixel ratio (physical pixels per CSS pixel)
  pub dpr: f32,
  /// Color depth in bits per pixel
  pub depth: i16,
}

/// Implementation of screen configuration generation from start request data
impl FromStartRequest<Screen> for Screen {
  /// Creates a new Screen configuration from browser profile and start request
  /// data.
  ///
  /// # Arguments
  /// * `bp` - Full browser profile data containing screen preferences
  /// * `request` - Start request containing user's actual screen dimensions
  /// * `_navigator` - Navigator configuration (unused)
  /// * `_screen` - Base screen configuration (unused)
  /// * `_token` - Authentication token (unused)
  ///
  /// # Returns
  /// * `Result<Screen, Error>` - New screen configuration or error
  fn from_start_request(
    bp: &BrowserProfileFullData,
    request: &StartRequest,
    _navigator: &Navigator,
    _screen: &Screen,
    _token: &str,
  ) -> Result<Self, Error> {
    let is_real = bp.screen.mode == Mode::Real;
    let mut width = bp.screen.width.unwrap_or(1366) as i16;
    let mut height = bp.screen.height.unwrap_or(768) as i16;
    let mut dpr = 1.0;
    let mut depth = 24;

    if is_real {
      width = request.user_screen_width as i16;
      height = request.user_screen_height as i16;
    }

    if bp.platform == Platform::Macos {
      dpr = 2.0;
      depth = 30;
    }

    Ok(Self {
      is_real,
      width,
      height,
      dpr,
      depth,
    })
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::server::services::browser_profile_services::config::consts::LINUX_MOCK_PROFILE;

  /// Creates a base browser profile for testing with default screen settings
  fn create_base_profile() -> BrowserProfileFullData {
    BrowserProfileFullData {
      screen: crate::server::dtos::browser_profile_dto::Screen {
        mode: Mode::Manual,
        width: Some(1920),
        height: Some(1080),
      },
      ..LINUX_MOCK_PROFILE.clone()
    }
  }

  /// Creates a base start request for testing with mock screen dimensions
  fn create_base_request() -> StartRequest {
    StartRequest {
      user_screen_width: 1440,
      user_screen_height: 900,
      ..StartRequest::get_mock()
    }
  }

  /// Tests screen configuration generation in manual mode
  #[test]
  fn test_manual_mode() -> Result<(), Error> {
    let profile = create_base_profile();
    let request = create_base_request();
    let navigator = Navigator::default();
    let screen = Screen::default();
    let token = String::new();

    let result = Screen::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    assert!(!result.is_real);
    assert_eq!(result.width, 1920);
    assert_eq!(result.height, 1080);
    assert_eq!(result.dpr, 1.0);
    assert_eq!(result.depth, 24);
    Ok(())
  }

  /// Tests screen configuration generation in real mode
  #[test]
  fn test_real_mode() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.screen.mode = Mode::Real;
    let request = create_base_request();
    let navigator = Navigator::default();
    let screen = Screen::default();
    let token = String::new();

    let result = Screen::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    assert!(result.is_real);
    assert_eq!(result.width, 1440);
    assert_eq!(result.height, 900);
    assert_eq!(result.dpr, 1.0);
    assert_eq!(result.depth, 24);
    Ok(())
  }

  /// Tests screen configuration with default resolution values
  #[test]
  fn test_default_resolution() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.screen.width = None;
    profile.screen.height = None;
    let request = create_base_request();
    let navigator = Navigator::default();
    let screen = Screen::default();
    let token = String::new();

    let result = Screen::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    assert!(!result.is_real);
    assert_eq!(result.width, 1366);
    assert_eq!(result.height, 768);
    assert_eq!(result.dpr, 1.0);
    assert_eq!(result.depth, 24);
    Ok(())
  }

  /// Tests screen configuration for macOS platform
  #[test]
  fn test_macos_platform() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.platform = Platform::Macos;
    let request = create_base_request();
    let navigator = Navigator::default();
    let screen = Screen::default();
    let token = String::new();

    let result = Screen::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    assert!(!result.is_real);
    assert_eq!(result.width, 1920);
    assert_eq!(result.height, 1080);
    assert_eq!(result.dpr, 2.0);
    assert_eq!(result.depth, 30);
    Ok(())
  }

  /// Tests screen configuration for Windows platform
  #[test]
  fn test_windows_platform() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.platform = Platform::Windows;
    let request = create_base_request();
    let navigator = Navigator::default();
    let screen = Screen::default();
    let token = String::new();

    let result = Screen::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    assert!(!result.is_real);
    assert_eq!(result.width, 1920);
    assert_eq!(result.height, 1080);
    assert_eq!(result.dpr, 1.0);
    assert_eq!(result.depth, 24);
    Ok(())
  }
}