| 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 enums as dynamic types. |
| 30 |
let my_module = self.obj(); |
| 31 |
let type_module: &glib::TypeModule = my_module.upcast_ref(); |
| 32 |
super::MyModuleEnum::on_implementation_load(type_module) |
| 33 |
&& super::MyModuleEnumLazy::on_implementation_load(type_module) |
| 34 |
} |
| 35 |
|
| 36 |
fn unload(&self) { |
| 37 |
// marks the enums as unregistered. |
| 38 |
let my_module = self.obj(); |
| 39 |
let type_module: &glib::TypeModule = my_module.upcast_ref(); |
| 40 |
super::MyModuleEnumLazy::on_implementation_unload(type_module); |
| 41 |
super::MyModuleEnum::on_implementation_unload(type_module); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// an enum to register as a dynamic type. |
| 47 |
#[derive(Debug, Eq, PartialEq, Clone, Copy, glib::Enum)] |
| 48 |
#[repr(u32)] |
| 49 |
#[enum_type(name = "MyModuleEnum")] |
| 50 |
#[enum_dynamic] |
| 51 |
pub enum MyModuleEnum { |
| 52 |
#[enum_value(name = "Foo")] |
| 53 |
Foo, |
| 54 |
Bar, |
| 55 |
} |
| 56 |
|
| 57 |
// an enum to lazy register as a dynamic type. |
| 58 |
#[derive(Debug, Eq, PartialEq, Clone, Copy, glib::Enum)] |
| 59 |
#[repr(u32)] |
| 60 |
#[enum_type(name = "MyModuleEnumLazy")] |
| 61 |
#[enum_dynamic(lazy_registration = true)] |
| 62 |
pub enum MyModuleEnumLazy { |
| 63 |
#[enum_value(name = "Foo")] |
| 64 |
Foo, |
| 65 |
Bar, |
| 66 |
} |
| 67 |
|
| 68 |
// a module (must extend `glib::TypeModule` and must implement `glib::TypePlugin`). |
| 69 |
glib::wrapper! { |
| 70 |
pub struct MyModule(ObjectSubclass<imp::MyModule>) |
| 71 |
@extends glib::TypeModule, @implements glib::TypePlugin; |
| 72 |
} |
| 73 |
|
| 74 |
#[test] |
| 75 |
fn dynamic_enums() { |
| 76 |
// 1st: creates a single module to test with. |
| 77 |
let module = glib::Object::new::<MyModule>(); |
| 78 |
// 1st: uses it to test lifecycle of enums registered as dynamic types. |
| 79 |
dynamic_enums_lifecycle(&module); |
| 80 |
// 2nd: uses it to test behavior of enums registered as dynamic types. |
| 81 |
dynamic_enums_behavior(&module); |
| 82 |
} |
| 83 |
|
| 84 |
// tests lifecycle of enums registered as dynamic types within a module. |
| 85 |
fn dynamic_enums_lifecycle(module: &MyModule) { |
| 86 |
// checks types of enums to register as dynamic types are invalid (module is not loaded yet). |
| 87 |
assert!(!MyModuleEnum::static_type().is_valid()); |
| 88 |
assert!(!MyModuleEnumLazy::static_type().is_valid()); |
| 89 |
|
| 90 |
// simulates the GLib type system to load/unload the module. |
| 91 |
TypeModuleExt::use_(module); |
| 92 |
TypeModuleExt::unuse(module); |
| 93 |
|
| 94 |
// checks types of enums registered as dynamic types are valid (module is unloaded). |
| 95 |
assert!(MyModuleEnum::static_type().is_valid()); |
| 96 |
// checks types of enums that are lazy registered as dynamic types are valid (module is unloaded). |
| 97 |
assert!(!MyModuleEnumLazy::static_type().is_valid()); |
| 98 |
|
| 99 |
// simulates the GLib type system to load the module. |
| 100 |
TypeModuleExt::use_(module); |
| 101 |
|
| 102 |
// checks types of enums registered as dynamic types are valid (module is loaded). |
| 103 |
let enum_type = MyModuleEnum::static_type(); |
| 104 |
assert!(enum_type.is_valid()); |
| 105 |
let enum_lazy_type = MyModuleEnumLazy::static_type(); |
| 106 |
assert!(enum_lazy_type.is_valid()); |
| 107 |
|
| 108 |
// checks plugin of enums registered as dynamic types is `MyModule`. |
| 109 |
assert_eq!( |
| 110 |
enum_type.plugin().as_ref(), |
| 111 |
Some(module.upcast_ref::<glib::TypePlugin>()) |
| 112 |
); |
| 113 |
assert_eq!( |
| 114 |
enum_lazy_type.plugin().as_ref(), |
| 115 |
Some(module.upcast_ref::<glib::TypePlugin>()) |
| 116 |
); |
| 117 |
|
| 118 |
// simulates the GLib type system to unload the module. |
| 119 |
TypeModuleExt::unuse(module); |
| 120 |
|
| 121 |
// checks types of enums registered as dynamic types are still valid (should have been marked as unloaded by the GLib type system but this cannot be checked). |
| 122 |
assert!(MyModuleEnum::static_type().is_valid()); |
| 123 |
assert!(MyModuleEnumLazy::static_type().is_valid()); |
| 124 |
|
| 125 |
// simulates the GLib type system to reload the module. |
| 126 |
TypeModuleExt::use_(module); |
| 127 |
|
| 128 |
// checks types of enums registered as dynamic types are still valid (should have been marked as loaded by the GLib type system but this cannot be checked). |
| 129 |
assert!(MyModuleEnum::static_type().is_valid()); |
| 130 |
assert!(MyModuleEnumLazy::static_type().is_valid()); |
| 131 |
|
| 132 |
// simulates the GLib type system to unload the module. |
| 133 |
TypeModuleExt::unuse(module); |
| 134 |
} |
| 135 |
|
| 136 |
// tests behavior of enums registered as dynamic types within a module. |
| 137 |
fn dynamic_enums_behavior(module: &MyModule) { |
| 138 |
use glib::prelude::*; |
| 139 |
use glib::translate::{FromGlib, IntoGlib}; |
| 140 |
|
| 141 |
// simulates the GLib type system to load the module. |
| 142 |
TypeModuleExt::use_(module); |
| 143 |
|
| 144 |
assert_eq!(MyModuleEnum::Foo.into_glib(), 0); |
| 145 |
assert_eq!(MyModuleEnum::Bar.into_glib(), 1); |
| 146 |
|
| 147 |
assert_eq!(unsafe { MyModuleEnum::from_glib(0) }, MyModuleEnum::Foo); |
| 148 |
assert_eq!(unsafe { MyModuleEnum::from_glib(1) }, MyModuleEnum::Bar); |
| 149 |
|
| 150 |
let t = MyModuleEnum::static_type(); |
| 151 |
assert!(t.is_a(glib::Type::ENUM)); |
| 152 |
assert_eq!(t.name(), "MyModuleEnum"); |
| 153 |
|
| 154 |
let e = glib::EnumClass::with_type(t).expect("EnumClass::new failed"); |
| 155 |
|
| 156 |
let values = e.values(); |
| 157 |
assert_eq!(values.len(), 2); |
| 158 |
assert_eq!(values[0].name(), "Foo"); |
| 159 |
assert_eq!(values[0].nick(), "foo"); |
| 160 |
assert_eq!(values[1].name(), "Bar"); |
| 161 |
assert_eq!(values[1].nick(), "bar"); |
| 162 |
|
| 163 |
let v = e.value(0).expect("EnumClass::get_value(0) failed"); |
| 164 |
assert_eq!(v.name(), "Foo"); |
| 165 |
assert_eq!(v.nick(), "foo"); |
| 166 |
let v = e.value(1).expect("EnumClass::get_value(1) failed"); |
| 167 |
assert_eq!(v.name(), "Bar"); |
| 168 |
assert_eq!(v.nick(), "bar"); |
| 169 |
assert_eq!(e.value(2), None); |
| 170 |
|
| 171 |
// within enums registered as dynamic types, values are usables only if |
| 172 |
// at least one type class ref exists (see `glib::EnumClass`). |
| 173 |
assert_eq!( |
| 174 |
MyModuleEnum::Foo.to_value().get::<MyModuleEnum>(), |
| 175 |
Ok(MyModuleEnum::Foo) |
| 176 |
); |
| 177 |
assert_eq!( |
| 178 |
MyModuleEnum::Bar.to_value().get::<MyModuleEnum>(), |
| 179 |
Ok(MyModuleEnum::Bar) |
| 180 |
); |
| 181 |
|
| 182 |
assert_eq!(MyModuleEnumLazy::Foo.into_glib(), 0); |
| 183 |
assert_eq!(MyModuleEnumLazy::Bar.into_glib(), 1); |
| 184 |
|
| 185 |
assert_eq!( |
| 186 |
unsafe { MyModuleEnumLazy::from_glib(0) }, |
| 187 |
MyModuleEnumLazy::Foo |
| 188 |
); |
| 189 |
assert_eq!( |
| 190 |
unsafe { MyModuleEnumLazy::from_glib(1) }, |
| 191 |
MyModuleEnumLazy::Bar |
| 192 |
); |
| 193 |
|
| 194 |
let t = MyModuleEnumLazy::static_type(); |
| 195 |
assert!(t.is_a(glib::Type::ENUM)); |
| 196 |
assert_eq!(t.name(), "MyModuleEnumLazy"); |
| 197 |
|
| 198 |
let e = glib::EnumClass::with_type(t).expect("EnumClass::new failed"); |
| 199 |
|
| 200 |
let values = e.values(); |
| 201 |
assert_eq!(values.len(), 2); |
| 202 |
assert_eq!(values[0].name(), "Foo"); |
| 203 |
assert_eq!(values[0].nick(), "foo"); |
| 204 |
assert_eq!(values[1].name(), "Bar"); |
| 205 |
assert_eq!(values[1].nick(), "bar"); |
| 206 |
|
| 207 |
let v = e.value(0).expect("EnumClass::get_value(0) failed"); |
| 208 |
assert_eq!(v.name(), "Foo"); |
| 209 |
assert_eq!(v.nick(), "foo"); |
| 210 |
let v = e.value(1).expect("EnumClass::get_value(1) failed"); |
| 211 |
assert_eq!(v.name(), "Bar"); |
| 212 |
assert_eq!(v.nick(), "bar"); |
| 213 |
assert_eq!(e.value(2), None); |
| 214 |
|
| 215 |
// within enums registered as dynamic types, values are usables only if |
| 216 |
// at least one type class ref exists (see `glib::EnumClass`). |
| 217 |
assert_eq!( |
| 218 |
MyModuleEnumLazy::Foo.to_value().get::<MyModuleEnumLazy>(), |
| 219 |
Ok(MyModuleEnumLazy::Foo) |
| 220 |
); |
| 221 |
assert_eq!( |
| 222 |
MyModuleEnumLazy::Bar.to_value().get::<MyModuleEnumLazy>(), |
| 223 |
Ok(MyModuleEnumLazy::Bar) |
| 224 |
); |
| 225 |
|
| 226 |
// simulates the GLib type system to unload the module. |
| 227 |
TypeModuleExt::unuse(module); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
pub mod plugin { |
| 232 |
use super::*; |
| 233 |
|
| 234 |
pub mod imp { |
| 235 |
use glib::EnumClass; |
| 236 |
|
| 237 |
use super::*; |
| 238 |
use std::cell::Cell; |
| 239 |
|
| 240 |
// impl for a type plugin (must implement `glib::TypePlugin`). |
| 241 |
#[derive(Default)] |
| 242 |
pub struct MyPlugin { |
| 243 |
my_enum_type_values: Cell<Option<&'static glib::enums::EnumValues>>, |
| 244 |
my_enum_lazy_type_values: Cell<Option<&'static glib::enums::EnumValues>>, |
| 245 |
} |
| 246 |
|
| 247 |
#[glib::object_subclass] |
| 248 |
impl ObjectSubclass for MyPlugin { |
| 249 |
const NAME: &'static str = "MyPlugin"; |
| 250 |
type Type = super::MyPlugin; |
| 251 |
type Interfaces = (glib::TypePlugin,); |
| 252 |
} |
| 253 |
|
| 254 |
impl ObjectImpl for MyPlugin {} |
| 255 |
|
| 256 |
impl TypePluginImpl for MyPlugin { |
| 257 |
fn use_plugin(&self) { |
| 258 |
// register enums as dynamic types. |
| 259 |
let my_plugin = self.obj(); |
| 260 |
super::MyPluginEnum::on_implementation_load(my_plugin.as_ref()); |
| 261 |
super::MyPluginEnumLazy::on_implementation_load(my_plugin.as_ref()); |
| 262 |
} |
| 263 |
|
| 264 |
fn unuse_plugin(&self) { |
| 265 |
// marks enums as unregistered. |
| 266 |
let my_plugin = self.obj(); |
| 267 |
super::MyPluginEnumLazy::on_implementation_unload(my_plugin.as_ref()); |
| 268 |
super::MyPluginEnum::on_implementation_unload(my_plugin.as_ref()); |
| 269 |
} |
| 270 |
|
| 271 |
fn complete_type_info( |
| 272 |
&self, |
| 273 |
type_: glib::Type, |
| 274 |
) -> (glib::TypeInfo, glib::TypeValueTable) { |
| 275 |
let enum_type_values = match type_ { |
| 276 |
type_ if type_ == super::MyPluginEnum::static_type() => { |
| 277 |
self.my_enum_type_values.get() |
| 278 |
} |
| 279 |
type_ if type_ == super::MyPluginEnumLazy::static_type() => { |
| 280 |
self.my_enum_lazy_type_values.get() |
| 281 |
} |
| 282 |
_ => panic!("unexpected type"), |
| 283 |
} |
| 284 |
.expect("enum type values"); |
| 285 |
let type_info = EnumClass::complete_type_info(type_, enum_type_values) |
| 286 |
.expect("EnumClass::complete_type_info failed"); |
| 287 |
(type_info, glib::TypeValueTable::default()) |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
impl TypePluginRegisterImpl for MyPlugin { |
| 292 |
fn register_dynamic_enum( |
| 293 |
&self, |
| 294 |
type_name: &str, |
| 295 |
const_static_values: &'static glib::enums::EnumValues, |
| 296 |
) -> glib::Type { |
| 297 |
let type_ = glib::Type::from_name(type_name).unwrap_or_else(|| { |
| 298 |
glib::Type::register_dynamic( |
| 299 |
glib::Type::ENUM, |
| 300 |
type_name, |
| 301 |
self.obj().upcast_ref::<glib::TypePlugin>(), |
| 302 |
glib::TypeFlags::NONE, |
| 303 |
) |
| 304 |
}); |
| 305 |
if type_.is_valid() { |
| 306 |
match type_name { |
| 307 |
"MyPluginEnum" => self.my_enum_type_values.set(Some(const_static_values)), |
| 308 |
"MyPluginEnumLazy" => { |
| 309 |
self.my_enum_lazy_type_values.set(Some(const_static_values)) |
| 310 |
} |
| 311 |
_ => panic!("unexpected"), |
| 312 |
}; |
| 313 |
} |
| 314 |
type_ |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
// an enum to register as a dynamic type. |
| 320 |
#[derive(Debug, Eq, PartialEq, Clone, Copy, glib::Enum)] |
| 321 |
#[repr(u32)] |
| 322 |
#[enum_type(name = "MyPluginEnum")] |
| 323 |
#[enum_dynamic(plugin_type = MyPlugin)] |
| 324 |
pub enum MyPluginEnum { |
| 325 |
#[enum_value(name = "Foo")] |
| 326 |
Foo, |
| 327 |
Bar, |
| 328 |
} |
| 329 |
|
| 330 |
// an enum to lazy register as a dynamic type. |
| 331 |
#[derive(Debug, Eq, PartialEq, Clone, Copy, glib::Enum)] |
| 332 |
#[repr(u32)] |
| 333 |
#[enum_type(name = "MyPluginEnumLazy")] |
| 334 |
#[enum_dynamic(plugin_type = MyPlugin, lazy_registration = true)] |
| 335 |
pub enum MyPluginEnumLazy { |
| 336 |
#[enum_value(name = "Foo")] |
| 337 |
Foo, |
| 338 |
Bar, |
| 339 |
} |
| 340 |
|
| 341 |
// a plugin (must implement `glib::TypePlugin`). |
| 342 |
glib::wrapper! { |
| 343 |
pub struct MyPlugin(ObjectSubclass<imp::MyPlugin>) @implements glib::TypePlugin; |
| 344 |
} |
| 345 |
|
| 346 |
#[test] |
| 347 |
fn dynamic_enums() { |
| 348 |
// 1st: creates a single plugin to test with. |
| 349 |
let plugin = glib::Object::new::<MyPlugin>(); |
| 350 |
// 1st: uses it to test lifecycle of enums registered as dynamic types. |
| 351 |
dynamic_enums_lifecycle(&plugin); |
| 352 |
// 2nd: uses it to test behavior of enums registered as dynamic types. |
| 353 |
dynamic_enums_behavior(&plugin); |
| 354 |
} |
| 355 |
|
| 356 |
// tests lifecycle of enums registered as dynamic types within a plugin. |
| 357 |
fn dynamic_enums_lifecycle(plugin: &MyPlugin) { |
| 358 |
use glib::prelude::*; |
| 359 |
|
| 360 |
// checks types of enums to register as dynamic types are invalid (plugin is not used yet). |
| 361 |
assert!(!MyPluginEnum::static_type().is_valid()); |
| 362 |
assert!(!MyPluginEnumLazy::static_type().is_valid()); |
| 363 |
|
| 364 |
// simulates the GLib type system to use/unuse the plugin. |
| 365 |
TypePluginExt::use_(plugin); |
| 366 |
TypePluginExt::unuse(plugin); |
| 367 |
|
| 368 |
// checks types of enums registered as dynamic types are valid (plugin is unused). |
| 369 |
assert!(MyPluginEnum::static_type().is_valid()); |
| 370 |
// checks types of enums that are lazy registered as dynamic types are still invalid (plugin is unused). |
| 371 |
assert!(!MyPluginEnumLazy::static_type().is_valid()); |
| 372 |
|
| 373 |
// simulates the GLib type system to use the plugin. |
| 374 |
TypePluginExt::use_(plugin); |
| 375 |
|
| 376 |
// checks types of enums registered as dynamic types are valid (plugin is used). |
| 377 |
let enum_type = MyPluginEnum::static_type(); |
| 378 |
assert!(enum_type.is_valid()); |
| 379 |
let enum_lazy_type = MyPluginEnumLazy::static_type(); |
| 380 |
assert!(enum_lazy_type.is_valid()); |
| 381 |
|
| 382 |
// checks plugin of enums registered as dynamic types is `MyPlugin`. |
| 383 |
assert_eq!( |
| 384 |
enum_type.plugin().as_ref(), |
| 385 |
Some(plugin.upcast_ref::<glib::TypePlugin>()) |
| 386 |
); |
| 387 |
assert_eq!( |
| 388 |
enum_lazy_type.plugin().as_ref(), |
| 389 |
Some(plugin.upcast_ref::<glib::TypePlugin>()) |
| 390 |
); |
| 391 |
|
| 392 |
// simulates the GLib type system to unuse the plugin. |
| 393 |
TypePluginExt::unuse(plugin); |
| 394 |
|
| 395 |
// checks types of enums registered as dynamic types are still valid. |
| 396 |
assert!(MyPluginEnum::static_type().is_valid()); |
| 397 |
assert!(MyPluginEnumLazy::static_type().is_valid()); |
| 398 |
|
| 399 |
// simulates the GLib type system to reuse the plugin. |
| 400 |
TypePluginExt::use_(plugin); |
| 401 |
|
| 402 |
// checks types of enums registered as dynamic types are still valid. |
| 403 |
assert!(MyPluginEnum::static_type().is_valid()); |
| 404 |
assert!(MyPluginEnumLazy::static_type().is_valid()); |
| 405 |
|
| 406 |
// simulates the GLib type system to unuse the plugin. |
| 407 |
TypePluginExt::unuse(plugin); |
| 408 |
} |
| 409 |
|
| 410 |
// tests behavior of enums registered as dynamic types within a plugin. |
| 411 |
fn dynamic_enums_behavior(plugin: &MyPlugin) { |
| 412 |
use glib::prelude::*; |
| 413 |
use glib::translate::{FromGlib, IntoGlib}; |
| 414 |
|
| 415 |
// simulates the GLib type system to use the plugin. |
| 416 |
TypePluginExt::use_(plugin); |
| 417 |
|
| 418 |
assert_eq!(MyPluginEnum::Foo.into_glib(), 0); |
| 419 |
assert_eq!(MyPluginEnum::Bar.into_glib(), 1); |
| 420 |
|
| 421 |
assert_eq!(unsafe { MyPluginEnum::from_glib(0) }, MyPluginEnum::Foo); |
| 422 |
assert_eq!(unsafe { MyPluginEnum::from_glib(1) }, MyPluginEnum::Bar); |
| 423 |
|
| 424 |
let t = MyPluginEnum::static_type(); |
| 425 |
assert!(t.is_a(glib::Type::ENUM)); |
| 426 |
assert_eq!(t.name(), "MyPluginEnum"); |
| 427 |
|
| 428 |
let e = glib::EnumClass::with_type(t).expect("EnumClass::new failed"); |
| 429 |
|
| 430 |
let values = e.values(); |
| 431 |
assert_eq!(values.len(), 2); |
| 432 |
assert_eq!(values[0].name(), "Foo"); |
| 433 |
assert_eq!(values[0].nick(), "foo"); |
| 434 |
assert_eq!(values[1].name(), "Bar"); |
| 435 |
assert_eq!(values[1].nick(), "bar"); |
| 436 |
|
| 437 |
let v = e.value(0).expect("EnumClass::get_value(0) failed"); |
| 438 |
assert_eq!(v.name(), "Foo"); |
| 439 |
assert_eq!(v.nick(), "foo"); |
| 440 |
let v = e.value(1).expect("EnumClass::get_value(1) failed"); |
| 441 |
assert_eq!(v.name(), "Bar"); |
| 442 |
assert_eq!(v.nick(), "bar"); |
| 443 |
assert_eq!(e.value(2), None); |
| 444 |
|
| 445 |
// within enums registered as dynamic types, values are usables only if |
| 446 |
// at least one type class ref exists (see `glib::EnumClass`). |
| 447 |
assert_eq!( |
| 448 |
MyPluginEnum::Foo.to_value().get::<MyPluginEnum>(), |
| 449 |
Ok(MyPluginEnum::Foo) |
| 450 |
); |
| 451 |
assert_eq!( |
| 452 |
MyPluginEnum::Bar.to_value().get::<MyPluginEnum>(), |
| 453 |
Ok(MyPluginEnum::Bar) |
| 454 |
); |
| 455 |
|
| 456 |
assert_eq!(MyPluginEnumLazy::Foo.into_glib(), 0); |
| 457 |
assert_eq!(MyPluginEnumLazy::Bar.into_glib(), 1); |
| 458 |
|
| 459 |
assert_eq!( |
| 460 |
unsafe { MyPluginEnumLazy::from_glib(0) }, |
| 461 |
MyPluginEnumLazy::Foo |
| 462 |
); |
| 463 |
assert_eq!( |
| 464 |
unsafe { MyPluginEnumLazy::from_glib(1) }, |
| 465 |
MyPluginEnumLazy::Bar |
| 466 |
); |
| 467 |
|
| 468 |
let t = MyPluginEnumLazy::static_type(); |
| 469 |
assert!(t.is_a(glib::Type::ENUM)); |
| 470 |
assert_eq!(t.name(), "MyPluginEnumLazy"); |
| 471 |
|
| 472 |
let e = glib::EnumClass::with_type(t).expect("EnumClass::new failed"); |
| 473 |
|
| 474 |
let values = e.values(); |
| 475 |
assert_eq!(values.len(), 2); |
| 476 |
assert_eq!(values[0].name(), "Foo"); |
| 477 |
assert_eq!(values[0].nick(), "foo"); |
| 478 |
assert_eq!(values[1].name(), "Bar"); |
| 479 |
assert_eq!(values[1].nick(), "bar"); |
| 480 |
|
| 481 |
let v = e.value(0).expect("EnumClass::get_value(0) failed"); |
| 482 |
assert_eq!(v.name(), "Foo"); |
| 483 |
assert_eq!(v.nick(), "foo"); |
| 484 |
let v = e.value(1).expect("EnumClass::get_value(1) failed"); |
| 485 |
assert_eq!(v.name(), "Bar"); |
| 486 |
assert_eq!(v.nick(), "bar"); |
| 487 |
assert_eq!(e.value(2), None); |
| 488 |
|
| 489 |
// within enums registered as dynamic types, values are usables only if |
| 490 |
// at least one type class ref exists (see `glib::EnumClass`). |
| 491 |
assert_eq!( |
| 492 |
MyPluginEnumLazy::Foo.to_value().get::<MyPluginEnumLazy>(), |
| 493 |
Ok(MyPluginEnumLazy::Foo) |
| 494 |
); |
| 495 |
assert_eq!( |
| 496 |
MyPluginEnumLazy::Bar.to_value().get::<MyPluginEnumLazy>(), |
| 497 |
Ok(MyPluginEnumLazy::Bar) |
| 498 |
); |
| 499 |
|
| 500 |
// simulates the GLib type system to unuse the plugin. |
| 501 |
TypePluginExt::unuse(plugin); |
| 502 |
} |
| 503 |
} |