| 1 |
// Take a look at the license at the top of the repository in the LICENSE file. |
| 2 |
|
| 3 |
use glib::{prelude::*, subclass::prelude::*}; |
| 4 |
|
| 5 |
mod module { |
| 6 |
use super::*; |
| 7 |
|
| 8 |
mod imp { |
| 9 |
use super::*; |
| 10 |
|
| 11 |
// impl for a type module (must extend `glib::TypeModule` and must implement `glib::TypePlugin`). |
| 12 |
#[derive(Default)] |
| 13 |
pub struct MyModule; |
| 14 |
|
| 15 |
#[glib::object_subclass] |
| 16 |
impl ObjectSubclass for MyModule { |
| 17 |
const NAME: &'static str = "MyModule"; |
| 18 |
type Type = super::MyModule; |
| 19 |
type ParentType = glib::TypeModule; |
| 20 |
type Interfaces = (glib::TypePlugin,); |
| 21 |
} |
| 22 |
|
| 23 |
impl ObjectImpl for MyModule {} |
| 24 |
|
| 25 |
impl TypePluginImpl for MyModule {} |
| 26 |
|
| 27 |
impl TypeModuleImpl for MyModule { |
| 28 |
fn load(&self) -> bool { |
| 29 |
// registers flags as dynamic types. |
| 30 |
let my_module = self.obj(); |
| 31 |
let type_module: &glib::TypeModule = my_module.upcast_ref(); |
| 32 |
super::MyModuleFlags::on_implementation_load(type_module) |
| 33 |
&& super::MyModuleFlagsLazy::on_implementation_load(type_module) |
| 34 |
} |
| 35 |
|
| 36 |
fn unload(&self) { |
| 37 |
// marks the flags as unregistered. |
| 38 |
let my_module = self.obj(); |
| 39 |
let type_module: &glib::TypeModule = my_module.upcast_ref(); |
| 40 |
super::MyModuleFlagsLazy::on_implementation_unload(type_module); |
| 41 |
super::MyModuleFlags::on_implementation_unload(type_module); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// flags to register as a dynamic type. |
| 47 |
#[glib::flags(name = "MyModuleFlags")] |
| 48 |
#[flags_dynamic] |
| 49 |
enum MyModuleFlags { |
| 50 |
#[flags_value(name = "Flag A", nick = "nick-a")] |
| 51 |
A = 0b00000001, |
| 52 |
#[flags_value(name = "Flag B")] |
| 53 |
B = 0b00000010, |
| 54 |
#[flags_value(skip)] |
| 55 |
AB = Self::A.bits() | Self::B.bits(), |
| 56 |
C = 0b00000100, |
| 57 |
} |
| 58 |
|
| 59 |
// flags to lazy register as a dynamic type. |
| 60 |
#[glib::flags(name = "MyModuleFlagsLazy")] |
| 61 |
#[flags_dynamic(lazy_registration = true)] |
| 62 |
enum MyModuleFlagsLazy { |
| 63 |
#[flags_value(name = "Flag A", nick = "nick-a")] |
| 64 |
A = 0b00000001, |
| 65 |
#[flags_value(name = "Flag B")] |
| 66 |
B = 0b00000010, |
| 67 |
#[flags_value(skip)] |
| 68 |
AB = Self::A.bits() | Self::B.bits(), |
| 69 |
C = 0b00000100, |
| 70 |
} |
| 71 |
|
| 72 |
// a module (must extend `glib::TypeModule` and must implement `glib::TypePlugin`). |
| 73 |
glib::wrapper! { |
| 74 |
pub struct MyModule(ObjectSubclass<imp::MyModule>) |
| 75 |
@extends glib::TypeModule, @implements glib::TypePlugin; |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn dynamic_flags() { |
| 80 |
// 1st: creates a single module to test with. |
| 81 |
let module = glib::Object::new::<MyModule>(); |
| 82 |
// 1st: uses it to test lifecycle of flags registered as dynamic types. |
| 83 |
dynamic_flags_lifecycle(&module); |
| 84 |
// 2nd: uses it to test behavior of flags registered as dynamic types. |
| 85 |
dynamic_flags_behavior(&module); |
| 86 |
} |
| 87 |
|
| 88 |
// tests lifecycle of flags registered as dynamic types within a module. |
| 89 |
fn dynamic_flags_lifecycle(module: &MyModule) { |
| 90 |
// checks types of flags to register as dynamic types are invalid (module is not loaded yet). |
| 91 |
assert!(!MyModuleFlags::static_type().is_valid()); |
| 92 |
assert!(!MyModuleFlagsLazy::static_type().is_valid()); |
| 93 |
|
| 94 |
// simulates the GLib type system to load/unload the module. |
| 95 |
TypeModuleExt::use_(module); |
| 96 |
TypeModuleExt::unuse(module); |
| 97 |
|
| 98 |
// checks types of flags registered as dynamic types are valid (module is unloaded). |
| 99 |
assert!(MyModuleFlags::static_type().is_valid()); |
| 100 |
// checks types of flags that are lazy registered as dynamic types are valid (module is unloaded). |
| 101 |
assert!(!MyModuleFlagsLazy::static_type().is_valid()); |
| 102 |
|
| 103 |
// simulates the GLib type system to load the module. |
| 104 |
TypeModuleExt::use_(module); |
| 105 |
|
| 106 |
// checks types of flags registered as dynamic types are valid (module is loaded). |
| 107 |
let flags_type = MyModuleFlags::static_type(); |
| 108 |
assert!(flags_type.is_valid()); |
| 109 |
let flags_lazy_type = MyModuleFlagsLazy::static_type(); |
| 110 |
assert!(flags_lazy_type.is_valid()); |
| 111 |
|
| 112 |
// checks plugin of flags registered as dynamic types is `MyModule`. |
| 113 |
assert_eq!( |
| 114 |
flags_type.plugin().as_ref(), |
| 115 |
Some(module.upcast_ref::<glib::TypePlugin>()) |
| 116 |
); |
| 117 |
assert_eq!( |
| 118 |
flags_lazy_type.plugin().as_ref(), |
| 119 |
Some(module.upcast_ref::<glib::TypePlugin>()) |
| 120 |
); |
| 121 |
|
| 122 |
// simulates the GLib type system to unload the module. |
| 123 |
TypeModuleExt::unuse(module); |
| 124 |
|
| 125 |
// checks types of flags registered as dynamic types are still valid (should have been marked as unloaded by the GLib type system but this cannot be checked). |
| 126 |
assert!(MyModuleFlags::static_type().is_valid()); |
| 127 |
assert!(MyModuleFlagsLazy::static_type().is_valid()); |
| 128 |
|
| 129 |
// simulates the GLib type system to reload the module. |
| 130 |
TypeModuleExt::use_(module); |
| 131 |
|
| 132 |
// checks types of flags registered as dynamic types are still valid (should have been marked as loaded by the GLib type system but this cannot be checked). |
| 133 |
assert!(MyModuleFlags::static_type().is_valid()); |
| 134 |
assert!(MyModuleFlagsLazy::static_type().is_valid()); |
| 135 |
|
| 136 |
// simulates the GLib type system to unload the module. |
| 137 |
TypeModuleExt::unuse(module); |
| 138 |
} |
| 139 |
|
| 140 |
// tests behavior of flags registered as dynamic types within a module. |
| 141 |
fn dynamic_flags_behavior(module: &MyModule) { |
| 142 |
use glib::prelude::*; |
| 143 |
use glib::translate::{FromGlib, IntoGlib}; |
| 144 |
|
| 145 |
// simulates the GLib type system to load the module. |
| 146 |
TypeModuleExt::use_(module); |
| 147 |
|
| 148 |
assert_eq!(MyModuleFlags::A.bits(), 1); |
| 149 |
assert_eq!(MyModuleFlags::B.bits(), 2); |
| 150 |
assert_eq!(MyModuleFlags::AB.bits(), 3); |
| 151 |
|
| 152 |
assert_eq!(MyModuleFlags::empty().into_glib(), 0); |
| 153 |
assert_eq!(MyModuleFlags::A.into_glib(), 1); |
| 154 |
assert_eq!(MyModuleFlags::B.into_glib(), 2); |
| 155 |
assert_eq!(MyModuleFlags::AB.into_glib(), 3); |
| 156 |
|
| 157 |
assert_eq!( |
| 158 |
unsafe { MyModuleFlags::from_glib(0) }, |
| 159 |
MyModuleFlags::empty() |
| 160 |
); |
| 161 |
assert_eq!(unsafe { MyModuleFlags::from_glib(1) }, MyModuleFlags::A); |
| 162 |
assert_eq!(unsafe { MyModuleFlags::from_glib(2) }, MyModuleFlags::B); |
| 163 |
assert_eq!(unsafe { MyModuleFlags::from_glib(3) }, MyModuleFlags::AB); |
| 164 |
|
| 165 |
let t = MyModuleFlags::static_type(); |
| 166 |
assert!(t.is_a(glib::Type::FLAGS)); |
| 167 |
assert_eq!(t.name(), "MyModuleFlags"); |
| 168 |
|
| 169 |
let e = glib::FlagsClass::with_type(t).expect("FlagsClass::new failed"); |
| 170 |
let values = e.values(); |
| 171 |
assert_eq!(values.len(), 3); |
| 172 |
assert_eq!(values[0].name(), "Flag A"); |
| 173 |
assert_eq!(values[0].nick(), "nick-a"); |
| 174 |
assert_eq!(values[1].name(), "Flag B"); |
| 175 |
assert_eq!(values[1].nick(), "b"); |
| 176 |
assert_eq!(values[2].name(), "C"); |
| 177 |
assert_eq!(values[2].nick(), "c"); |
| 178 |
|
| 179 |
let v = e.value(1).expect("FlagsClass::get_value(1) failed"); |
| 180 |
assert_eq!(v.name(), "Flag A"); |
| 181 |
assert_eq!(v.nick(), "nick-a"); |
| 182 |
let v = e.value(2).expect("FlagsClass::get_value(2) failed"); |
| 183 |
assert_eq!(v.name(), "Flag B"); |
| 184 |
assert_eq!(v.nick(), "b"); |
| 185 |
let v = e.value(4).expect("FlagsClass::get_value(4) failed"); |
| 186 |
assert_eq!(v.name(), "C"); |
| 187 |
assert_eq!(v.nick(), "c"); |
| 188 |
|
| 189 |
// within flags registered as dynamic types, values are usables only if |
| 190 |
// at least one type class ref exists (see `glib::FlagsClass`). |
| 191 |
assert_eq!( |
| 192 |
MyModuleFlags::empty().to_value().get::<MyModuleFlags>(), |
| 193 |
Ok(MyModuleFlags::empty()) |
| 194 |
); |
| 195 |
|
| 196 |
assert_eq!( |
| 197 |
MyModuleFlags::A.to_value().get::<MyModuleFlags>(), |
| 198 |
Ok(MyModuleFlags::A) |
| 199 |
); |
| 200 |
assert_eq!( |
| 201 |
MyModuleFlags::B.to_value().get::<MyModuleFlags>(), |
| 202 |
Ok(MyModuleFlags::B) |
| 203 |
); |
| 204 |
assert_eq!( |
| 205 |
MyModuleFlags::AB.to_value().get::<MyModuleFlags>(), |
| 206 |
Ok(MyModuleFlags::AB) |
| 207 |
); |
| 208 |
|
| 209 |
assert!(e.value_by_name("Flag A").is_some()); |
| 210 |
assert!(e.value_by_name("Flag B").is_some()); |
| 211 |
assert!(e.value_by_name("AB").is_none()); |
| 212 |
assert!(e.value_by_name("C").is_some()); |
| 213 |
|
| 214 |
assert!(e.value_by_nick("nick-a").is_some()); |
| 215 |
assert!(e.value_by_nick("b").is_some()); |
| 216 |
assert!(e.value_by_nick("ab").is_none()); |
| 217 |
assert!(e.value_by_nick("c").is_some()); |
| 218 |
|
| 219 |
assert_eq!(MyModuleFlagsLazy::A.bits(), 1); |
| 220 |
assert_eq!(MyModuleFlagsLazy::B.bits(), 2); |
| 221 |
assert_eq!(MyModuleFlagsLazy::AB.bits(), 3); |
| 222 |
|
| 223 |
assert_eq!(MyModuleFlagsLazy::empty().into_glib(), 0); |
| 224 |
assert_eq!(MyModuleFlagsLazy::A.into_glib(), 1); |
| 225 |
assert_eq!(MyModuleFlagsLazy::B.into_glib(), 2); |
| 226 |
assert_eq!(MyModuleFlagsLazy::AB.into_glib(), 3); |
| 227 |
|
| 228 |
assert_eq!( |
| 229 |
unsafe { MyModuleFlagsLazy::from_glib(0) }, |
| 230 |
MyModuleFlagsLazy::empty() |
| 231 |
); |
| 232 |
assert_eq!( |
| 233 |
unsafe { MyModuleFlagsLazy::from_glib(1) }, |
| 234 |
MyModuleFlagsLazy::A |
| 235 |
); |
| 236 |
assert_eq!( |
| 237 |
unsafe { MyModuleFlagsLazy::from_glib(2) }, |
| 238 |
MyModuleFlagsLazy::B |
| 239 |
); |
| 240 |
assert_eq!( |
| 241 |
unsafe { MyModuleFlagsLazy::from_glib(3) }, |
| 242 |
MyModuleFlagsLazy::AB |
| 243 |
); |
| 244 |
|
| 245 |
let t = MyModuleFlagsLazy::static_type(); |
| 246 |
assert!(t.is_a(glib::Type::FLAGS)); |
| 247 |
assert_eq!(t.name(), "MyModuleFlagsLazy"); |
| 248 |
|
| 249 |
let e = glib::FlagsClass::with_type(t).expect("FlagsClass::new failed"); |
| 250 |
let values = e.values(); |
| 251 |
assert_eq!(values.len(), 3); |
| 252 |
assert_eq!(values[0].name(), "Flag A"); |
| 253 |
assert_eq!(values[0].nick(), "nick-a"); |
| 254 |
assert_eq!(values[1].name(), "Flag B"); |
| 255 |
assert_eq!(values[1].nick(), "b"); |
| 256 |
assert_eq!(values[2].name(), "C"); |
| 257 |
assert_eq!(values[2].nick(), "c"); |
| 258 |
|
| 259 |
let v = e.value(1).expect("FlagsClass::get_value(1) failed"); |
| 260 |
assert_eq!(v.name(), "Flag A"); |
| 261 |
assert_eq!(v.nick(), "nick-a"); |
| 262 |
let v = e.value(2).expect("FlagsClass::get_value(2) failed"); |
| 263 |
assert_eq!(v.name(), "Flag B"); |
| 264 |
assert_eq!(v.nick(), "b"); |
| 265 |
let v = e.value(4).expect("FlagsClass::get_value(4) failed"); |
| 266 |
assert_eq!(v.name(), "C"); |
| 267 |
assert_eq!(v.nick(), "c"); |
| 268 |
|
| 269 |
// within flags registered as dynamic types, values are usables only if |
| 270 |
// at least one type class ref exists (see `glib::FlagsClass`). |
| 271 |
assert_eq!( |
| 272 |
MyModuleFlagsLazy::empty() |
| 273 |
.to_value() |
| 274 |
.get::<MyModuleFlagsLazy>(), |
| 275 |
Ok(MyModuleFlagsLazy::empty()) |
| 276 |
); |
| 277 |
|
| 278 |
assert_eq!( |
| 279 |
MyModuleFlagsLazy::A.to_value().get::<MyModuleFlagsLazy>(), |
| 280 |
Ok(MyModuleFlagsLazy::A) |
| 281 |
); |
| 282 |
assert_eq!( |
| 283 |
MyModuleFlagsLazy::B.to_value().get::<MyModuleFlagsLazy>(), |
| 284 |
Ok(MyModuleFlagsLazy::B) |
| 285 |
); |
| 286 |
assert_eq!( |
| 287 |
MyModuleFlagsLazy::AB.to_value().get::<MyModuleFlagsLazy>(), |
| 288 |
Ok(MyModuleFlagsLazy::AB) |
| 289 |
); |
| 290 |
|
| 291 |
assert!(e.value_by_name("Flag A").is_some()); |
| 292 |
assert!(e.value_by_name("Flag B").is_some()); |
| 293 |
assert!(e.value_by_name("AB").is_none()); |
| 294 |
assert!(e.value_by_name("C").is_some()); |
| 295 |
|
| 296 |
assert!(e.value_by_nick("nick-a").is_some()); |
| 297 |
assert!(e.value_by_nick("b").is_some()); |
| 298 |
assert!(e.value_by_nick("ab").is_none()); |
| 299 |
assert!(e.value_by_nick("c").is_some()); |
| 300 |
|
| 301 |
// simulates the GLib type system to unload the module. |
| 302 |
TypeModuleExt::unuse(module); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
pub mod plugin { |
| 307 |
use super::*; |
| 308 |
|
| 309 |
pub mod imp { |
| 310 |
use glib::FlagsClass; |
| 311 |
|
| 312 |
use super::*; |
| 313 |
use std::cell::Cell; |
| 314 |
|
| 315 |
// impl for a type plugin (must implement `glib::TypePlugin`). |
| 316 |
#[derive(Default)] |
| 317 |
pub struct MyPlugin { |
| 318 |
my_flags_type_values: Cell<Option<&'static glib::enums::FlagsValues>>, |
| 319 |
my_flags_lazy_type_values: Cell<Option<&'static glib::enums::FlagsValues>>, |
| 320 |
} |
| 321 |
|
| 322 |
#[glib::object_subclass] |
| 323 |
impl ObjectSubclass for MyPlugin { |
| 324 |
const NAME: &'static str = "MyPlugin"; |
| 325 |
type Type = super::MyPlugin; |
| 326 |
type Interfaces = (glib::TypePlugin,); |
| 327 |
} |
| 328 |
|
| 329 |
impl ObjectImpl for MyPlugin {} |
| 330 |
|
| 331 |
impl TypePluginImpl for MyPlugin { |
| 332 |
fn use_plugin(&self) { |
| 333 |
// register flags as dynamic types. |
| 334 |
let my_plugin = self.obj(); |
| 335 |
super::MyPluginFlags::on_implementation_load(my_plugin.as_ref()); |
| 336 |
super::MyPluginFlagsLazy::on_implementation_load(my_plugin.as_ref()); |
| 337 |
} |
| 338 |
|
| 339 |
fn unuse_plugin(&self) { |
| 340 |
// marks flags as unregistered. |
| 341 |
let my_plugin = self.obj(); |
| 342 |
super::MyPluginFlagsLazy::on_implementation_unload(my_plugin.as_ref()); |
| 343 |
super::MyPluginFlags::on_implementation_unload(my_plugin.as_ref()); |
| 344 |
} |
| 345 |
|
| 346 |
fn complete_type_info( |
| 347 |
&self, |
| 348 |
type_: glib::Type, |
| 349 |
) -> (glib::TypeInfo, glib::TypeValueTable) { |
| 350 |
let flags_type_values = match type_ { |
| 351 |
type_ if type_ == super::MyPluginFlags::static_type() => { |
| 352 |
self.my_flags_type_values.get() |
| 353 |
} |
| 354 |
type_ if type_ == super::MyPluginFlagsLazy::static_type() => { |
| 355 |
self.my_flags_lazy_type_values.get() |
| 356 |
} |
| 357 |
_ => panic!("unexpected type"), |
| 358 |
} |
| 359 |
.expect("flags type values"); |
| 360 |
let type_info = FlagsClass::complete_type_info(type_, flags_type_values) |
| 361 |
.expect("FlagsClass::type_info failed"); |
| 362 |
(type_info, glib::TypeValueTable::default()) |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
impl TypePluginRegisterImpl for MyPlugin { |
| 367 |
fn register_dynamic_flags( |
| 368 |
&self, |
| 369 |
type_name: &str, |
| 370 |
const_static_values: &'static glib::enums::FlagsValues, |
| 371 |
) -> glib::Type { |
| 372 |
let type_ = glib::Type::from_name(type_name).unwrap_or_else(|| { |
| 373 |
glib::Type::register_dynamic( |
| 374 |
glib::Type::FLAGS, |
| 375 |
type_name, |
| 376 |
self.obj().upcast_ref::<glib::TypePlugin>(), |
| 377 |
glib::TypeFlags::NONE, |
| 378 |
) |
| 379 |
}); |
| 380 |
if type_.is_valid() { |
| 381 |
match type_name { |
| 382 |
"MyPluginFlags" => self.my_flags_type_values.set(Some(const_static_values)), |
| 383 |
"MyPluginFlagsLazy" => self |
| 384 |
.my_flags_lazy_type_values |
| 385 |
.set(Some(const_static_values)), |
| 386 |
_ => panic!("unexpected"), |
| 387 |
}; |
| 388 |
} |
| 389 |
type_ |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
// flags to register as a dynamic type. |
| 395 |
#[glib::flags(name = "MyPluginFlags")] |
| 396 |
#[flags_dynamic(plugin_type = MyPlugin)] |
| 397 |
enum MyPluginFlags { |
| 398 |
#[flags_value(name = "Flag A", nick = "nick-a")] |
| 399 |
A = 0b00000001, |
| 400 |
#[flags_value(name = "Flag B")] |
| 401 |
B = 0b00000010, |
| 402 |
#[flags_value(skip)] |
| 403 |
AB = Self::A.bits() | Self::B.bits(), |
| 404 |
C = 0b00000100, |
| 405 |
} |
| 406 |
|
| 407 |
// flags to lazy register as a dynamic type. |
| 408 |
#[glib::flags(name = "MyPluginFlagsLazy")] |
| 409 |
#[flags_dynamic(plugin_type = MyPlugin, lazy_registration = true)] |
| 410 |
enum MyPluginFlagsLazy { |
| 411 |
#[flags_value(name = "Flag A", nick = "nick-a")] |
| 412 |
A = 0b00000001, |
| 413 |
#[flags_value(name = "Flag B")] |
| 414 |
B = 0b00000010, |
| 415 |
#[flags_value(skip)] |
| 416 |
AB = Self::A.bits() | Self::B.bits(), |
| 417 |
C = 0b00000100, |
| 418 |
} |
| 419 |
|
| 420 |
// a plugin (must implement `glib::TypePlugin`). |
| 421 |
glib::wrapper! { |
| 422 |
pub struct MyPlugin(ObjectSubclass<imp::MyPlugin>) @implements glib::TypePlugin; |
| 423 |
} |
| 424 |
|
| 425 |
#[test] |
| 426 |
fn dynamic_flags() { |
| 427 |
// 1st: creates a single plugin to test with. |
| 428 |
let plugin = glib::Object::new::<MyPlugin>(); |
| 429 |
// 1st: uses it to test lifecycle of flags registered as dynamic types. |
| 430 |
dynamic_flags_lifecycle(&plugin); |
| 431 |
// 2nd: uses it to test behavior of flags registered as dynamic types. |
| 432 |
dynamic_flags_behavior(&plugin); |
| 433 |
} |
| 434 |
|
| 435 |
// tests lifecycle of flags registered as dynamic types within a plugin. |
| 436 |
fn dynamic_flags_lifecycle(plugin: &MyPlugin) { |
| 437 |
use glib::prelude::*; |
| 438 |
|
| 439 |
// checks types of flags to register as dynamic types are invalid (plugin is not used yet). |
| 440 |
assert!(!MyPluginFlags::static_type().is_valid()); |
| 441 |
assert!(!MyPluginFlagsLazy::static_type().is_valid()); |
| 442 |
|
| 443 |
// simulates the GLib type system to use/unuse the plugin. |
| 444 |
TypePluginExt::use_(plugin); |
| 445 |
TypePluginExt::unuse(plugin); |
| 446 |
|
| 447 |
// checks types of flags registered as dynamic types are valid (plugin is unused). |
| 448 |
assert!(MyPluginFlags::static_type().is_valid()); |
| 449 |
// checks types of flags that are lazy registered as dynamic types are still invalid (plugin is unused). |
| 450 |
assert!(!MyPluginFlagsLazy::static_type().is_valid()); |
| 451 |
|
| 452 |
// simulates the GLib type system to use the plugin. |
| 453 |
TypePluginExt::use_(plugin); |
| 454 |
|
| 455 |
// checks types of flags registered as dynamic types are valid (plugin is used). |
| 456 |
let flags_type = MyPluginFlags::static_type(); |
| 457 |
assert!(flags_type.is_valid()); |
| 458 |
let flags_lazy_type = MyPluginFlagsLazy::static_type(); |
| 459 |
assert!(flags_lazy_type.is_valid()); |
| 460 |
|
| 461 |
// checks plugin of flags registered as dynamic types is `MyPlugin`. |
| 462 |
assert_eq!( |
| 463 |
flags_type.plugin().as_ref(), |
| 464 |
Some(plugin.upcast_ref::<glib::TypePlugin>()) |
| 465 |
); |
| 466 |
assert_eq!( |
| 467 |
flags_lazy_type.plugin().as_ref(), |
| 468 |
Some(plugin.upcast_ref::<glib::TypePlugin>()) |
| 469 |
); |
| 470 |
|
| 471 |
// simulates the GLib type system to unuse the plugin. |
| 472 |
TypePluginExt::unuse(plugin); |
| 473 |
|
| 474 |
// checks types of flags registered as dynamic types are still valid. |
| 475 |
assert!(MyPluginFlags::static_type().is_valid()); |
| 476 |
assert!(MyPluginFlagsLazy::static_type().is_valid()); |
| 477 |
|
| 478 |
// simulates the GLib type system to reuse the plugin. |
| 479 |
TypePluginExt::use_(plugin); |
| 480 |
|
| 481 |
// checks types of flags registered as dynamic types are still valid. |
| 482 |
assert!(MyPluginFlags::static_type().is_valid()); |
| 483 |
assert!(MyPluginFlagsLazy::static_type().is_valid()); |
| 484 |
|
| 485 |
// simulates the GLib type system to unuse the plugin. |
| 486 |
TypePluginExt::unuse(plugin); |
| 487 |
} |
| 488 |
|
| 489 |
// tests behavior of flags registered as dynamic types within a plugin. |
| 490 |
fn dynamic_flags_behavior(plugin: &MyPlugin) { |
| 491 |
use glib::prelude::*; |
| 492 |
use glib::translate::{FromGlib, IntoGlib}; |
| 493 |
|
| 494 |
// simulates the GLib type system to use the plugin. |
| 495 |
TypePluginExt::use_(plugin); |
| 496 |
|
| 497 |
assert_eq!(MyPluginFlags::A.bits(), 1); |
| 498 |
assert_eq!(MyPluginFlags::B.bits(), 2); |
| 499 |
assert_eq!(MyPluginFlags::AB.bits(), 3); |
| 500 |
|
| 501 |
assert_eq!(MyPluginFlags::empty().into_glib(), 0); |
| 502 |
assert_eq!(MyPluginFlags::A.into_glib(), 1); |
| 503 |
assert_eq!(MyPluginFlags::B.into_glib(), 2); |
| 504 |
assert_eq!(MyPluginFlags::AB.into_glib(), 3); |
| 505 |
|
| 506 |
assert_eq!( |
| 507 |
unsafe { MyPluginFlags::from_glib(0) }, |
| 508 |
MyPluginFlags::empty() |
| 509 |
); |
| 510 |
assert_eq!(unsafe { MyPluginFlags::from_glib(1) }, MyPluginFlags::A); |
| 511 |
assert_eq!(unsafe { MyPluginFlags::from_glib(2) }, MyPluginFlags::B); |
| 512 |
assert_eq!(unsafe { MyPluginFlags::from_glib(3) }, MyPluginFlags::AB); |
| 513 |
|
| 514 |
let t = MyPluginFlags::static_type(); |
| 515 |
assert!(t.is_a(glib::Type::FLAGS)); |
| 516 |
assert_eq!(t.name(), "MyPluginFlags"); |
| 517 |
|
| 518 |
let e = glib::FlagsClass::with_type(t).expect("FlagsClass::new failed"); |
| 519 |
let values = e.values(); |
| 520 |
assert_eq!(values.len(), 3); |
| 521 |
assert_eq!(values[0].name(), "Flag A"); |
| 522 |
assert_eq!(values[0].nick(), "nick-a"); |
| 523 |
assert_eq!(values[1].name(), "Flag B"); |
| 524 |
assert_eq!(values[1].nick(), "b"); |
| 525 |
assert_eq!(values[2].name(), "C"); |
| 526 |
assert_eq!(values[2].nick(), "c"); |
| 527 |
|
| 528 |
let v = e.value(1).expect("FlagsClass::get_value(1) failed"); |
| 529 |
assert_eq!(v.name(), "Flag A"); |
| 530 |
assert_eq!(v.nick(), "nick-a"); |
| 531 |
let v = e.value(2).expect("FlagsClass::get_value(2) failed"); |
| 532 |
assert_eq!(v.name(), "Flag B"); |
| 533 |
assert_eq!(v.nick(), "b"); |
| 534 |
let v = e.value(4).expect("FlagsClass::get_value(4) failed"); |
| 535 |
assert_eq!(v.name(), "C"); |
| 536 |
assert_eq!(v.nick(), "c"); |
| 537 |
|
| 538 |
// within flags registered as dynamic types, values are usables only if |
| 539 |
// at least one type class ref exists (see `glib::FlagsClass`). |
| 540 |
assert_eq!( |
| 541 |
MyPluginFlags::empty().to_value().get::<MyPluginFlags>(), |
| 542 |
Ok(MyPluginFlags::empty()) |
| 543 |
); |
| 544 |
|
| 545 |
assert_eq!( |
| 546 |
MyPluginFlags::A.to_value().get::<MyPluginFlags>(), |
| 547 |
Ok(MyPluginFlags::A) |
| 548 |
); |
| 549 |
assert_eq!( |
| 550 |
MyPluginFlags::B.to_value().get::<MyPluginFlags>(), |
| 551 |
Ok(MyPluginFlags::B) |
| 552 |
); |
| 553 |
assert_eq!( |
| 554 |
MyPluginFlags::AB.to_value().get::<MyPluginFlags>(), |
| 555 |
Ok(MyPluginFlags::AB) |
| 556 |
); |
| 557 |
|
| 558 |
assert!(e.value_by_name("Flag A").is_some()); |
| 559 |
assert!(e.value_by_name("Flag B").is_some()); |
| 560 |
assert!(e.value_by_name("AB").is_none()); |
| 561 |
assert!(e.value_by_name("C").is_some()); |
| 562 |
|
| 563 |
assert!(e.value_by_nick("nick-a").is_some()); |
| 564 |
assert!(e.value_by_nick("b").is_some()); |
| 565 |
assert!(e.value_by_nick("ab").is_none()); |
| 566 |
assert!(e.value_by_nick("c").is_some()); |
| 567 |
|
| 568 |
assert_eq!(MyPluginFlagsLazy::A.bits(), 1); |
| 569 |
assert_eq!(MyPluginFlagsLazy::B.bits(), 2); |
| 570 |
assert_eq!(MyPluginFlagsLazy::AB.bits(), 3); |
| 571 |
|
| 572 |
assert_eq!(MyPluginFlagsLazy::empty().into_glib(), 0); |
| 573 |
assert_eq!(MyPluginFlagsLazy::A.into_glib(), 1); |
| 574 |
assert_eq!(MyPluginFlagsLazy::B.into_glib(), 2); |
| 575 |
assert_eq!(MyPluginFlagsLazy::AB.into_glib(), 3); |
| 576 |
|
| 577 |
assert_eq!( |
| 578 |
unsafe { MyPluginFlagsLazy::from_glib(0) }, |
| 579 |
MyPluginFlagsLazy::empty() |
| 580 |
); |
| 581 |
assert_eq!( |
| 582 |
unsafe { MyPluginFlagsLazy::from_glib(1) }, |
| 583 |
MyPluginFlagsLazy::A |
| 584 |
); |
| 585 |
assert_eq!( |
| 586 |
unsafe { MyPluginFlagsLazy::from_glib(2) }, |
| 587 |
MyPluginFlagsLazy::B |
| 588 |
); |
| 589 |
assert_eq!( |
| 590 |
unsafe { MyPluginFlagsLazy::from_glib(3) }, |
| 591 |
MyPluginFlagsLazy::AB |
| 592 |
); |
| 593 |
|
| 594 |
let t = MyPluginFlagsLazy::static_type(); |
| 595 |
assert!(t.is_a(glib::Type::FLAGS)); |
| 596 |
assert_eq!(t.name(), "MyPluginFlagsLazy"); |
| 597 |
|
| 598 |
let e = glib::FlagsClass::with_type(t).expect("FlagsClass::new failed"); |
| 599 |
let values = e.values(); |
| 600 |
assert_eq!(values.len(), 3); |
| 601 |
assert_eq!(values[0].name(), "Flag A"); |
| 602 |
assert_eq!(values[0].nick(), "nick-a"); |
| 603 |
assert_eq!(values[1].name(), "Flag B"); |
| 604 |
assert_eq!(values[1].nick(), "b"); |
| 605 |
assert_eq!(values[2].name(), "C"); |
| 606 |
assert_eq!(values[2].nick(), "c"); |
| 607 |
|
| 608 |
let v = e.value(1).expect("FlagsClass::get_value(1) failed"); |
| 609 |
assert_eq!(v.name(), "Flag A"); |
| 610 |
assert_eq!(v.nick(), "nick-a"); |
| 611 |
let v = e.value(2).expect("FlagsClass::get_value(2) failed"); |
| 612 |
assert_eq!(v.name(), "Flag B"); |
| 613 |
assert_eq!(v.nick(), "b"); |
| 614 |
let v = e.value(4).expect("FlagsClass::get_value(4) failed"); |
| 615 |
assert_eq!(v.name(), "C"); |
| 616 |
assert_eq!(v.nick(), "c"); |
| 617 |
|
| 618 |
// within flags registered as dynamic types, values are usables only if |
| 619 |
// at least one type class ref exists (see `glib::FlagsClass`). |
| 620 |
assert_eq!( |
| 621 |
MyPluginFlagsLazy::empty() |
| 622 |
.to_value() |
| 623 |
.get::<MyPluginFlagsLazy>(), |
| 624 |
Ok(MyPluginFlagsLazy::empty()) |
| 625 |
); |
| 626 |
|
| 627 |
assert_eq!( |
| 628 |
MyPluginFlagsLazy::A.to_value().get::<MyPluginFlagsLazy>(), |
| 629 |
Ok(MyPluginFlagsLazy::A) |
| 630 |
); |
| 631 |
assert_eq!( |
| 632 |
MyPluginFlagsLazy::B.to_value().get::<MyPluginFlagsLazy>(), |
| 633 |
Ok(MyPluginFlagsLazy::B) |
| 634 |
); |
| 635 |
assert_eq!( |
| 636 |
MyPluginFlagsLazy::AB.to_value().get::<MyPluginFlagsLazy>(), |
| 637 |
Ok(MyPluginFlagsLazy::AB) |
| 638 |
); |
| 639 |
|
| 640 |
assert!(e.value_by_name("Flag A").is_some()); |
| 641 |
assert!(e.value_by_name("Flag B").is_some()); |
| 642 |
assert!(e.value_by_name("AB").is_none()); |
| 643 |
assert!(e.value_by_name("C").is_some()); |
| 644 |
|
| 645 |
assert!(e.value_by_nick("nick-a").is_some()); |
| 646 |
assert!(e.value_by_nick("b").is_some()); |
| 647 |
assert!(e.value_by_nick("ab").is_none()); |
| 648 |
assert!(e.value_by_nick("c").is_some()); |
| 649 |
|
| 650 |
// simulates the GLib type system to unuse the plugin. |
| 651 |
TypePluginExt::unuse(plugin); |
| 652 |
} |
| 653 |
} |