pub struct MockBrowserFactory { /* private fields */ }Expand description
Mock browser factory for testing without Chrome.
This factory can be configured to:
- Always succeed (creates real browsers if Chrome available)
- Always fail with a specific error
- Fail after N successful creations
- Track creation count for verification
§Thread Safety
This factory is Send + Sync and tracks state using atomic operations.
§Example
use html2pdf_api::factory::mock::MockBrowserFactory;
// Create a factory that always fails
let factory = MockBrowserFactory::always_fails("Test error");
assert!(factory.create().is_err());
assert_eq!(factory.creation_count(), 1);
// Create a factory that fails after 2 successful creations
let factory = MockBrowserFactory::fail_after_n(2, "Exhausted");Implementations§
Source§impl MockBrowserFactory
impl MockBrowserFactory
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a mock factory that attempts real browser creation.
Note: This still requires Chrome to be installed to actually
create browsers. For pure mocking without Chrome, use
always_fails.
§Example
let factory = MockBrowserFactory::new();
// Will attempt real browser creation
let result = factory.create();Sourcepub fn always_fails<S: Into<String>>(message: S) -> Self
pub fn always_fails<S: Into<String>>(message: S) -> Self
Create a mock factory that always fails with the given message.
This is useful for testing error handling paths without requiring Chrome to be installed.
§Parameters
message- Error message to return on creation attempts.
§Example
let factory = MockBrowserFactory::always_fails("Chrome not installed");
let result = factory.create();
assert!(result.is_err());Sourcepub fn fail_after_n<S: Into<String>>(n: usize, message: S) -> Self
pub fn fail_after_n<S: Into<String>>(n: usize, message: S) -> Self
Create a mock factory that fails after N successful creations.
Useful for testing pool behavior when browsers start failing after some have been successfully created (e.g., resource exhaustion).
§Parameters
n- Number of successful creations before failing.message- Error message after failures begin.
§Example
let factory = MockBrowserFactory::fail_after_n(3, "Resource exhausted");
// First 3 calls may succeed (if Chrome installed), subsequent calls failSourcepub fn creation_count(&self) -> usize
pub fn creation_count(&self) -> usize
Sourcepub fn reset_count(&self)
pub fn reset_count(&self)
Reset the creation counter to zero.
Useful when reusing a factory across multiple tests.
Sourcepub fn counter(&self) -> Arc<AtomicUsize>
pub fn counter(&self) -> Arc<AtomicUsize>
Get a clone of the creation counter for external tracking.
This allows test code to monitor creation count even after the factory has been moved into a pool.
§Example
let factory = MockBrowserFactory::new();
let counter = factory.counter();
// Move factory into pool
let pool = BrowserPool::builder()
.factory(Box::new(factory))
.build()?;
// Can still check count via cloned counter
println!("Created: {}", counter.load(Ordering::SeqCst));Trait Implementations§
Source§impl BrowserFactory for MockBrowserFactory
impl BrowserFactory for MockBrowserFactory
Source§fn create(&self) -> Result<Browser>
fn create(&self) -> Result<Browser>
Create a browser or return a mock error.
Behavior depends on factory configuration:
- If
should_failis true, always returns error - If
fail_afteris set and count exceeded, returns error - Otherwise, attempts real browser creation
§Errors
Returns BrowserPoolError::BrowserCreation when configured to fail.
Source§impl Debug for MockBrowserFactory
impl Debug for MockBrowserFactory
Auto Trait Implementations§
impl Freeze for MockBrowserFactory
impl RefUnwindSafe for MockBrowserFactory
impl Send for MockBrowserFactory
impl Sync for MockBrowserFactory
impl Unpin for MockBrowserFactory
impl UnwindSafe for MockBrowserFactory
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);