diff --git a/Cargo.lock b/Cargo.lock index 9835ed5a38..c1171dad5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4507,6 +4507,7 @@ dependencies = [ "firefox-versioning", "once_cell", "parking_lot", + "pretty_assertions", "serde", "serde_json", "thiserror 1.0.31", diff --git a/components/search/Cargo.toml b/components/search/Cargo.toml index 527e669c19..c4e161496d 100644 --- a/components/search/Cargo.toml +++ b/components/search/Cargo.toml @@ -20,3 +20,4 @@ uniffi = { version = "0.28.2", features = ["build"] } [dev-dependencies] once_cell = "1.18.0" +pretty_assertions = "0.6" diff --git a/components/search/src/configuration_types.rs b/components/search/src/configuration_types.rs index 297a2f045f..7569b81e32 100644 --- a/components/search/src/configuration_types.rs +++ b/components/search/src/configuration_types.rs @@ -33,7 +33,7 @@ impl JSONEngineMethod { /// Defines an individual search engine URL. This is defined separately to /// `types::SearchEngineUrl` as various fields may be optional in the supplied /// configuration. -#[derive(Debug, uniffi::Record, PartialEq, Deserialize, Clone)] +#[derive(Debug, uniffi::Record, PartialEq, Deserialize, Clone, Default)] #[serde(rename_all = "camelCase")] pub(crate) struct JSONEngineUrl { /// The PrePath and FilePath of the URL. May include variables for engines @@ -55,7 +55,7 @@ pub(crate) struct JSONEngineUrl { } /// Reflects `types::SearchEngineUrls`, but using `EngineUrl`. -#[derive(Debug, uniffi::Record, PartialEq, Deserialize, Clone)] +#[derive(Debug, uniffi::Record, PartialEq, Deserialize, Clone, Default)] #[serde(rename_all = "camelCase")] pub(crate) struct JSONEngineUrls { /// The URL to use for searches. @@ -69,7 +69,7 @@ pub(crate) struct JSONEngineUrls { } /// Represents the engine base section of the configuration. -#[derive(Debug, Deserialize, Clone)] +#[derive(Debug, Default, Deserialize, Clone)] #[serde(rename_all = "camelCase")] pub(crate) struct JSONEngineBase { /// A list of aliases for this engine. diff --git a/components/search/src/filter.rs b/components/search/src/filter.rs index cff832cbc8..4878c4a3c0 100644 --- a/components/search/src/filter.rs +++ b/components/search/src/filter.rs @@ -194,9 +194,13 @@ mod tests { use super::*; use crate::*; use once_cell::sync::Lazy; + use pretty_assertions::assert_eq; #[test] fn test_from_configuration_details_fallsback_to_defaults() { + // This test doesn't use `..Default::default()` as we want to + // be explicit about `JSONEngineBase` and handling `None` + // options/default values. let result = SearchEngineDefinition::from_configuration_details( "test", JSONEngineBase { @@ -338,14 +342,8 @@ mod tests { static ENGINES_LIST: Lazy> = Lazy::new(|| { vec![ SearchEngineDefinition { - aliases: Vec::new(), - charset: String::from("UTF-8"), - classification: SearchEngineClassification::General, identifier: "engine1".to_string(), - partner_code: String::new(), name: "Test".to_string(), - order_hint: None, - telemetry_suffix: None, urls: SearchEngineUrls { search: SearchEngineUrl { base: "https://example.com".to_string(), @@ -353,19 +351,13 @@ mod tests { params: Vec::new(), search_term_param_name: None, }, - suggestions: None, - trending: None, + ..Default::default() }, + ..Default::default() }, SearchEngineDefinition { - aliases: Vec::new(), - charset: "UTF-8".to_string(), - classification: SearchEngineClassification::General, identifier: "engine2".to_string(), - partner_code: String::new(), name: "Test 2".to_string(), - order_hint: None, - telemetry_suffix: None, urls: SearchEngineUrls { search: SearchEngineUrl { base: "https://example.com/2".to_string(), @@ -373,19 +365,13 @@ mod tests { params: Vec::new(), search_term_param_name: None, }, - suggestions: None, - trending: None, + ..Default::default() }, + ..Default::default() }, SearchEngineDefinition { - aliases: Vec::new(), - charset: "UTF-8".to_string(), - classification: SearchEngineClassification::General, identifier: "engine3".to_string(), - partner_code: String::new(), name: "Test 3".to_string(), - order_hint: None, - telemetry_suffix: None, urls: SearchEngineUrls { search: SearchEngineUrl { base: "https://example.com/3".to_string(), @@ -393,19 +379,13 @@ mod tests { params: Vec::new(), search_term_param_name: None, }, - suggestions: None, - trending: None, + ..Default::default() }, + ..Default::default() }, SearchEngineDefinition { - aliases: Vec::new(), - charset: "UTF-8".to_string(), - classification: SearchEngineClassification::General, identifier: "engine4wildcardmatch".to_string(), - partner_code: String::new(), name: "Test 4".to_string(), - order_hint: None, - telemetry_suffix: None, urls: SearchEngineUrls { search: SearchEngineUrl { base: "https://example.com/4".to_string(), @@ -413,9 +393,9 @@ mod tests { params: Vec::new(), search_term_param_name: None, }, - suggestions: None, - trending: None, + ..Default::default() }, + ..Default::default() }, ] }); @@ -431,13 +411,7 @@ mod tests { }), &SearchUserEnvironment { locale: "fi".into(), - region: "FR".into(), - update_channel: SearchUpdateChannel::Default, - distribution_id: String::new(), - experiment: String::new(), - app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }, ); @@ -460,31 +434,14 @@ mod tests { default: "engine1".to_string(), default_private: String::new(), environment: JSONVariantEnvironment { - all_regions_and_locales: false, - excluded_locales: vec![], - excluded_regions: vec![], locales: vec!["en-GB".to_string()], - regions: vec![], - distributions: vec![], - excluded_distributions: vec![], - applications: vec![], - channels: vec![], - device_type: vec![], - min_version: "".to_string(), - max_version: "".to_string(), - experiment: "".to_string(), + ..Default::default() }, }], }), &SearchUserEnvironment { locale: "fi".into(), - region: "FR".into(), - update_channel: SearchUpdateChannel::Default, - distribution_id: String::new(), - experiment: String::new(), - app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }, ); @@ -507,31 +464,14 @@ mod tests { default: "engine1".to_string(), default_private: String::new(), environment: JSONVariantEnvironment { - all_regions_and_locales: false, - excluded_locales: vec![], - excluded_regions: vec![], locales: vec!["fi".to_string()], - regions: vec![], - distributions: vec![], - excluded_distributions: vec![], - applications: vec![], - channels: vec![], - device_type: vec![], - min_version: "".to_string(), - max_version: "".to_string(), - experiment: "".to_string(), + ..Default::default() }, }], }), &SearchUserEnvironment { locale: "fi".into(), - region: "FR".into(), - update_channel: SearchUpdateChannel::Default, - distribution_id: String::new(), - experiment: String::new(), - app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }, ); @@ -554,31 +494,14 @@ mod tests { default: "engine4*".to_string(), default_private: String::new(), environment: JSONVariantEnvironment { - all_regions_and_locales: false, - excluded_locales: vec![], - excluded_regions: vec![], locales: vec!["fi".to_string()], - regions: vec![], - distributions: vec![], - excluded_distributions: vec![], - applications: vec![], - channels: vec![], - device_type: vec![], - min_version: "".to_string(), - max_version: "".to_string(), - experiment: "".to_string(), + ..Default::default() }, }], }), &SearchUserEnvironment { locale: "fi".into(), - region: "FR".into(), - update_channel: SearchUpdateChannel::Default, - distribution_id: String::new(), - experiment: String::new(), - app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }, ); @@ -602,51 +525,23 @@ mod tests { default: "engine4*".to_string(), default_private: String::new(), environment: JSONVariantEnvironment { - all_regions_and_locales: false, - excluded_locales: vec![], - excluded_regions: vec![], locales: vec!["fi".to_string()], - regions: vec![], - distributions: vec![], - excluded_distributions: vec![], - applications: vec![], - channels: vec![], - device_type: vec![], - min_version: "".to_string(), - max_version: "".to_string(), - experiment: "".to_string(), + ..Default::default() }, }, JSONSpecificDefaultRecord { default: "engine3".to_string(), default_private: String::new(), environment: JSONVariantEnvironment { - all_regions_and_locales: false, - excluded_locales: vec![], - excluded_regions: vec![], locales: vec!["fi".to_string()], - regions: vec![], - distributions: vec![], - excluded_distributions: vec![], - applications: vec![], - channels: vec![], - device_type: vec![], - min_version: "".to_string(), - max_version: "".to_string(), - experiment: "".to_string(), + ..Default::default() }, }, ], }), &SearchUserEnvironment { locale: "fi".into(), - region: "FR".into(), - update_channel: SearchUpdateChannel::Default, - distribution_id: String::new(), - experiment: String::new(), - app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }, ); @@ -671,14 +566,7 @@ mod tests { specific_defaults: Vec::new(), }), &SearchUserEnvironment { - locale: "fi".into(), - region: "FR".into(), - update_channel: SearchUpdateChannel::Default, - distribution_id: String::new(), - experiment: String::new(), - app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }, ); @@ -702,31 +590,14 @@ mod tests { default: String::new(), default_private: "engine1".to_string(), environment: JSONVariantEnvironment { - all_regions_and_locales: false, - excluded_locales: vec![], - excluded_regions: vec![], locales: vec!["en-GB".to_string()], - regions: vec![], - distributions: vec![], - excluded_distributions: vec![], - applications: vec![], - channels: vec![], - device_type: vec![], - min_version: "".to_string(), - max_version: "".to_string(), - experiment: "".to_string(), + ..Default::default() }, }], }), &SearchUserEnvironment { locale: "fi".into(), - region: "FR".into(), - update_channel: SearchUpdateChannel::Default, - distribution_id: String::new(), - experiment: String::new(), - app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }, ); @@ -750,31 +621,14 @@ mod tests { default: String::new(), default_private: "engine1".to_string(), environment: JSONVariantEnvironment { - all_regions_and_locales: false, - excluded_locales: vec![], - excluded_regions: vec![], locales: vec!["fi".to_string()], - regions: vec![], - distributions: vec![], - excluded_distributions: vec![], - applications: vec![], - channels: vec![], - device_type: vec![], - min_version: "".to_string(), - max_version: "".to_string(), - experiment: "".to_string(), + ..Default::default() }, }], }), &SearchUserEnvironment { locale: "fi".into(), - region: "FR".into(), - update_channel: SearchUpdateChannel::Default, - distribution_id: String::new(), - experiment: String::new(), - app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }, ); @@ -798,31 +652,14 @@ mod tests { default: String::new(), default_private: "engine4*".to_string(), environment: JSONVariantEnvironment { - all_regions_and_locales: false, - excluded_locales: vec![], - excluded_regions: vec![], locales: vec!["fi".to_string()], - regions: vec![], - distributions: vec![], - excluded_distributions: vec![], - applications: vec![], - channels: vec![], - device_type: vec![], - min_version: "".to_string(), - max_version: "".to_string(), - experiment: "".to_string(), + ..Default::default() }, }], }), &SearchUserEnvironment { locale: "fi".into(), - region: "FR".into(), - update_channel: SearchUpdateChannel::Default, - distribution_id: String::new(), - experiment: String::new(), - app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }, ); diff --git a/components/search/src/selector.rs b/components/search/src/selector.rs index ef0638f3a4..09793bcee6 100644 --- a/components/search/src/selector.rs +++ b/components/search/src/selector.rs @@ -70,6 +70,7 @@ impl SearchEngineSelector { mod tests { use super::*; use crate::types::*; + use pretty_assertions::assert_eq; use serde_json::json; #[test] @@ -210,14 +211,7 @@ mod tests { let selector = Arc::new(SearchEngineSelector::new()); let result = selector.filter_engine_configuration(SearchUserEnvironment { - locale: "fi".into(), - region: "FR".into(), - update_channel: SearchUpdateChannel::Default, - distribution_id: String::new(), - experiment: String::new(), - app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }); assert!( @@ -310,14 +304,7 @@ mod tests { ); let result = selector.filter_engine_configuration(SearchUserEnvironment { - locale: "fi".into(), - region: "FR".into(), - update_channel: SearchUpdateChannel::Default, - distribution_id: String::new(), - experiment: String::new(), - app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }); assert!( @@ -330,14 +317,10 @@ mod tests { RefinedSearchConfig { engines: vec!( SearchEngineDefinition { - aliases: Vec::new(), charset: "UTF-8".to_string(), classification: SearchEngineClassification::General, identifier: "test1".to_string(), name: "Test 1".to_string(), - order_hint: None, - partner_code: String::new(), - telemetry_suffix: None, urls: SearchEngineUrls { search: SearchEngineUrl { base: "https://example.com/1".to_string(), @@ -367,7 +350,8 @@ mod tests { }], search_term_param_name: None }) - } + }, + ..Default::default() }, SearchEngineDefinition { aliases: Vec::new(), @@ -478,14 +462,9 @@ mod tests { ); let mut result = Arc::clone(&selector).filter_engine_configuration(SearchUserEnvironment { - locale: "fi".into(), - region: "FR".into(), - update_channel: SearchUpdateChannel::Default, distribution_id: String::new(), - experiment: String::new(), app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }); assert!( @@ -498,14 +477,10 @@ mod tests { RefinedSearchConfig { engines: vec!( SearchEngineDefinition { - aliases: Vec::new(), charset: "UTF-8".to_string(), classification: SearchEngineClassification::General, identifier: "test1".to_string(), name: "Test 1".to_string(), - order_hint: None, - partner_code: String::new(), - telemetry_suffix: None, urls: SearchEngineUrls { search: SearchEngineUrl { base: "https://example.com/1".to_string(), @@ -513,25 +488,20 @@ mod tests { params: Vec::new(), search_term_param_name: Some("q".to_string()) }, - suggestions: None, - trending: None - } - }, - ), + ..Default::default() + }, + ..Default::default() + }, + ), app_default_engine_id: Some("test1".to_string()), app_private_default_engine_id: None }, "Should have selected test1 for all matching locales, as the environments do not match for the other two" ); result = Arc::clone(&selector).filter_engine_configuration(SearchUserEnvironment { - locale: "fi".into(), - region: "FR".into(), - update_channel: SearchUpdateChannel::Default, distribution_id: String::new(), - experiment: String::new(), app_name: SearchApplicationName::FocusIos, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }); assert!( @@ -544,14 +514,10 @@ mod tests { RefinedSearchConfig { engines: vec!( SearchEngineDefinition { - aliases: Vec::new(), charset: "UTF-8".to_string(), classification: SearchEngineClassification::General, identifier: "test1".to_string(), name: "Test 1".to_string(), - order_hint: None, - partner_code: String::new(), - telemetry_suffix: None, urls: SearchEngineUrls { search: SearchEngineUrl { base: "https://example.com/1".to_string(), @@ -559,19 +525,15 @@ mod tests { params: Vec::new(), search_term_param_name: Some("q".to_string()) }, - suggestions: None, - trending: None - } + ..Default::default() + }, + ..Default::default() }, SearchEngineDefinition { - aliases: Vec::new(), charset: "UTF-8".to_string(), classification: SearchEngineClassification::General, identifier: "test2".to_string(), name: "Test 2".to_string(), - order_hint: None, - partner_code: String::new(), - telemetry_suffix: None, urls: SearchEngineUrls { search: SearchEngineUrl { base: "https://example.com/2".to_string(), @@ -579,9 +541,9 @@ mod tests { params: Vec::new(), search_term_param_name: Some("search".to_string()) }, - suggestions: None, - trending: None - } + ..Default::default() + }, + ..Default::default() }, ), app_default_engine_id: Some("test1".to_string()), @@ -591,14 +553,9 @@ mod tests { ); result = Arc::clone(&selector).filter_engine_configuration(SearchUserEnvironment { - locale: "fi".into(), - region: "FR".into(), - update_channel: SearchUpdateChannel::Default, distribution_id: "starship".to_string(), - experiment: String::new(), app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }); assert!( @@ -611,14 +568,10 @@ mod tests { RefinedSearchConfig { engines: vec!( SearchEngineDefinition { - aliases: Vec::new(), charset: "UTF-8".to_string(), classification: SearchEngineClassification::General, identifier: "test1".to_string(), name: "Test 1".to_string(), - order_hint: None, - partner_code: String::new(), - telemetry_suffix: None, urls: SearchEngineUrls { search: SearchEngineUrl { base: "https://example.com/1".to_string(), @@ -626,19 +579,15 @@ mod tests { params: Vec::new(), search_term_param_name: Some("q".to_string()) }, - suggestions: None, - trending: None - } + ..Default::default() + }, + ..Default::default() }, SearchEngineDefinition { - aliases: Vec::new(), charset: "UTF-8".to_string(), classification: SearchEngineClassification::General, identifier: "test3".to_string(), name: "Test 3".to_string(), - order_hint: None, - partner_code: String::new(), - telemetry_suffix: None, urls: SearchEngineUrls { search: SearchEngineUrl { base: "https://example.com/3".to_string(), @@ -646,10 +595,10 @@ mod tests { params: Vec::new(), search_term_param_name: Some("trek".to_string()) }, - suggestions: None, - trending: None - } - }, + ..Default::default() + }, + ..Default::default() + }, ), app_default_engine_id: Some("test1".to_string()), app_private_default_engine_id: None @@ -747,14 +696,10 @@ mod tests { ); let expected_engines = vec![ SearchEngineDefinition { - aliases: Vec::new(), charset: "UTF-8".to_string(), classification: SearchEngineClassification::General, identifier: "test".to_string(), name: "Test".to_string(), - order_hint: None, - partner_code: String::new(), - telemetry_suffix: None, urls: SearchEngineUrls { search: SearchEngineUrl { base: "https://example.com".to_string(), @@ -762,19 +707,15 @@ mod tests { params: Vec::new(), search_term_param_name: None, }, - suggestions: None, - trending: None, + ..Default::default() }, + ..Default::default() }, SearchEngineDefinition { - aliases: Vec::new(), charset: "UTF-8".to_string(), classification: SearchEngineClassification::General, identifier: "distro-default".to_string(), name: "Distribution Default".to_string(), - order_hint: None, - partner_code: String::new(), - telemetry_suffix: None, urls: SearchEngineUrls { search: SearchEngineUrl { base: "https://example.com".to_string(), @@ -782,19 +723,15 @@ mod tests { params: Vec::new(), search_term_param_name: None, }, - suggestions: None, - trending: None, + ..Default::default() }, + ..Default::default() }, SearchEngineDefinition { - aliases: Vec::new(), charset: "UTF-8".to_string(), classification: SearchEngineClassification::General, identifier: "private-default-FR".to_string(), name: "Private default FR".to_string(), - order_hint: None, - partner_code: String::new(), - telemetry_suffix: None, urls: SearchEngineUrls { search: SearchEngineUrl { base: "https://example.com".to_string(), @@ -802,21 +739,15 @@ mod tests { params: Vec::new(), search_term_param_name: None, }, - suggestions: None, - trending: None, + ..Default::default() }, + ..Default::default() }, ]; let result = Arc::clone(&selector).filter_engine_configuration(SearchUserEnvironment { - locale: "fi".into(), - region: "GB".into(), - update_channel: SearchUpdateChannel::Default, distribution_id: "test-distro".to_string(), - experiment: String::new(), - app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }); assert!( result.is_ok(), @@ -834,14 +765,9 @@ mod tests { ); let result = Arc::clone(&selector).filter_engine_configuration(SearchUserEnvironment { - locale: "fi".into(), region: "fr".into(), - update_channel: SearchUpdateChannel::Default, distribution_id: String::new(), - experiment: String::new(), - app_name: SearchApplicationName::Firefox, - version: String::new(), - device_type: SearchDeviceType::None, + ..Default::default() }); assert!( result.is_ok(), diff --git a/components/search/src/types.rs b/components/search/src/types.rs index 98851650b0..16a8c2e863 100644 --- a/components/search/src/types.rs +++ b/components/search/src/types.rs @@ -103,7 +103,7 @@ pub struct SearchUrlParam { } /// Defines an individual search engine URL. -#[derive(Debug, uniffi::Record, PartialEq, Deserialize, Clone)] +#[derive(Debug, uniffi::Record, PartialEq, Deserialize, Clone, Default)] pub struct SearchEngineUrl { /// The PrePath and FilePath of the URL. May include variables for engines /// which have a variable FilePath, e.g. `{searchTerm}` for when a search @@ -124,7 +124,7 @@ pub struct SearchEngineUrl { } /// The URLs associated with the search engine. -#[derive(Debug, uniffi::Record, PartialEq, Deserialize, Clone)] +#[derive(Debug, uniffi::Record, PartialEq, Deserialize, Clone, Default)] pub struct SearchEngineUrls { /// The URL to use for searches. pub search: SearchEngineUrl, @@ -137,11 +137,11 @@ pub struct SearchEngineUrls { } /// The list of acceptable classifications for a search engine. -#[derive(Debug, uniffi::Enum, PartialEq, Deserialize, Clone)] +#[derive(Debug, uniffi::Enum, PartialEq, Deserialize, Clone, Default)] #[serde(rename_all = "lowercase")] pub enum SearchEngineClassification { General = 2, - #[serde(other)] + #[default] Unknown = 1, } @@ -155,7 +155,7 @@ impl SearchEngineClassification { } /// A definition for an individual search engine to be presented to the user. -#[derive(Debug, uniffi::Record, PartialEq, Clone)] +#[derive(Debug, uniffi::Record, PartialEq, Clone, Default)] pub struct SearchEngineDefinition { /// A list of aliases for this engine. pub aliases: Vec,