1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Provides useful publishers for testing specifically. These should not be used in normal code.
16 
17 use crate::{subscriptions::SharedBufferSubscription, *};
18 
19 /// A [BufferPublisher] specifically for testing.
20 ///
21 /// Provides users the ability to send events and read the state of the subscription.
22 pub struct TestPublisher {
23     config: StreamConfig,
24     subscriber: Option<Box<dyn BufferSubscriber>>,
25     subscription: SharedBufferSubscription,
26 }
27 
28 impl TestPublisher {
29     /// Create a new [TestPublisher].
new(config: StreamConfig) -> Self30     pub fn new(config: StreamConfig) -> Self {
31         Self { config, subscriber: None, subscription: SharedBufferSubscription::new() }
32     }
33 
34     /// Send a [BufferSubscriber::on_next] event to an owned [BufferSubscriber] if it has any
35     /// requested and returns true. Drops the frame and returns false otherwise.
36     ///
37     /// # Panics
38     ///
39     /// This will panic if there is no owned subscriber.
send_frame(&mut self, frame: Frame) -> bool40     pub fn send_frame(&mut self, frame: Frame) -> bool {
41         let subscriber =
42             self.subscriber.as_deref_mut().expect("Tried to send_frame with no subscriber");
43 
44         if self.subscription.take_request() {
45             subscriber.on_next(frame);
46             true
47         } else {
48             false
49         }
50     }
51 
52     /// Send a [BufferSubscriber::on_complete] event to an owned [BufferSubscriber].
53     ///
54     /// # Panics
55     ///
56     /// This will panic if there is no owned subscriber.
send_complete(&mut self)57     pub fn send_complete(&mut self) {
58         let subscriber =
59             self.subscriber.as_deref_mut().expect("Tried to send_complete with no subscriber");
60         subscriber.on_complete();
61     }
62 
63     /// Send a [BufferSubscriber::on_error] event to an owned [BufferSubscriber].
64     ///
65     /// # Panics
66     ///
67     /// This will panic if there is no owned subscriber.
send_error(&mut self, error: BufferError)68     pub fn send_error(&mut self, error: BufferError) {
69         let subscriber =
70             self.subscriber.as_deref_mut().expect("Tried to send_error with no subscriber");
71         subscriber.on_error(error);
72     }
73 
74     /// Returns whether this [BufferPublisher] owns a subscriber.
has_subscriber(&self) -> bool75     pub fn has_subscriber(&self) -> bool {
76         self.subscriber.is_some()
77     }
78 
79     /// Returns the nummber of frames requested by the [BufferSubscriber].
pending_requests(&self) -> u6480     pub fn pending_requests(&self) -> u64 {
81         self.subscription.pending_requests()
82     }
83 
84     /// Returns whether the [BufferSubscriber] has cancelled the subscription.
is_cancelled(&self) -> bool85     pub fn is_cancelled(&self) -> bool {
86         self.subscription.is_cancelled()
87     }
88 }
89 
90 impl BufferPublisher for TestPublisher {
get_publisher_stream_config(&self) -> crate::StreamConfig91     fn get_publisher_stream_config(&self) -> crate::StreamConfig {
92         self.config
93     }
94 
subscribe(&mut self, subscriber: impl BufferSubscriber + 'static)95     fn subscribe(&mut self, subscriber: impl BufferSubscriber + 'static) {
96         assert!(self.subscriber.is_none(), "TestingPublishers can only take one subscriber");
97         self.subscriber = Some(Box::new(subscriber));
98 
99         if let Some(ref mut subscriber) = self.subscriber {
100             subscriber.on_subscribe(self.subscription.clone_for_subscriber());
101         }
102     }
103 }
104