Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: ios/chrome/browser/ui/voice/text_to_speech_player_unittest.mm

Issue 2449593002: [ios] Adds support for parsing Text-to-Speech search results. (Closed)
Patch Set: Disabled Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "ios/chrome/browser/ui/voice/text_to_speech_player.h"
6
7 #import <UIKit/UIKit.h>
8
9 #include "base/mac/foundation_util.h"
10 #import "base/mac/scoped_nsobject.h"
11 #import "base/test/ios/wait_util.h"
12 #include "base/time/time.h"
13 #import "ios/chrome/browser/ui/voice/voice_search_notification_names.h"
14 #include "ios/web/public/test/test_web_thread_bundle.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/gtest_mac.h"
17 #include "testing/platform_test.h"
18 #include "url/gurl.h"
19
20 #pragma mark - TTSPlayerObserver
21
22 // Test object that listens for TTS notifications.
23 @interface TTSPlayerObserver : NSObject
24
25 // The TextToSpeechPlayer passed on initialization.
26 @property(nonatomic, retain) TextToSpeechPlayer* player;
27
28 // Whether notifications have been received.
29 @property(nonatomic, readonly) BOOL readyNotificationReceived;
30 @property(nonatomic, readonly) BOOL willStartNotificationReceived;
31 @property(nonatomic, readonly) BOOL didStopNotificationReceived;
32
33 // Notification handlers.
34 - (void)handleReadyNotification:(NSNotification*)notification;
35 - (void)handleWillStartNotification:(NSNotification*)notification;
36 - (void)handleDidStopNotification:(NSNotification*)notification;
37
38 @end
39
40 @implementation TTSPlayerObserver {
41 base::scoped_nsobject<TextToSpeechPlayer> _player;
42 }
43
44 @synthesize readyNotificationReceived = _readyNotificationReceived;
45 @synthesize willStartNotificationReceived = _willStartNotificationReceived;
46 @synthesize didStopNotificationReceived = _didStopNotificationReceived;
47
48 - (void)dealloc {
49 [[NSNotificationCenter defaultCenter] removeObserver:self];
50 [super dealloc];
51 }
52
53 - (void)setPlayer:(TextToSpeechPlayer*)player {
54 NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
55 [defaultCenter removeObserver:self];
56 _player.reset([player retain]);
57 if (player) {
58 [defaultCenter addObserver:self
59 selector:@selector(handleReadyNotification:)
60 name:kTTSAudioReadyForPlaybackNotification
61 object:player];
62 [defaultCenter addObserver:self
63 selector:@selector(handleWillStartNotification:)
64 name:kTTSWillStartPlayingNotification
65 object:player];
66 [defaultCenter addObserver:self
67 selector:@selector(handleDidStopNotification:)
68 name:kTTSDidStopPlayingNotification
69 object:player];
70 }
71 }
72
73 - (TextToSpeechPlayer*)player {
74 return _player;
75 }
76
77 - (void)handleReadyNotification:(NSNotification*)notification {
78 ASSERT_EQ(notification.object, self.player);
79 _readyNotificationReceived = YES;
80 }
81
82 - (void)handleWillStartNotification:(NSNotification*)notification {
83 ASSERT_EQ(notification.object, self.player);
84 _willStartNotificationReceived = YES;
85 }
86
87 - (void)handleDidStopNotification:(NSNotification*)notification {
88 ASSERT_EQ(notification.object, self.player);
89 _didStopNotificationReceived = YES;
90 }
91
92 @end
93
94 #pragma mark - TextToSpeechPlayerTest
95
96 class TextToSpeechPlayerTest : public PlatformTest {
97 protected:
98 void SetUp() override {
99 tts_player_.reset([[TextToSpeechPlayer alloc] init]);
100 tts_player_observer_.reset([[TTSPlayerObserver alloc] init]);
101 [tts_player_observer_ setPlayer:tts_player_];
102 }
103
104 base::scoped_nsobject<TextToSpeechPlayer> tts_player_;
105 base::scoped_nsobject<TTSPlayerObserver> tts_player_observer_;
106 web::TestWebThreadBundle web_thread_bundle_;
107 };
108
109 // Tests that kTTSAudioReadyForPlaybackNotification is received and that
110 // TTSPlayer state is updated.
111 TEST_F(TextToSpeechPlayerTest, ReadyForPlayback) {
112 base::scoped_nsobject<NSData> audio_data(
113 [[@"audio_data" dataUsingEncoding:NSUTF8StringEncoding] retain]);
114 [tts_player_ prepareToPlayAudioData:audio_data];
115 EXPECT_TRUE([tts_player_observer_ readyNotificationReceived]);
116 EXPECT_TRUE([tts_player_ isReadyForPlayback]);
117 }
118
119 // Tests that kTTSAudioReadyForPlaybackNotification is received and that
120 // TTSPlayer's |-readyForPlayback| is NO for empty data.
121 TEST_F(TextToSpeechPlayerTest, ReadyForPlaybackEmtpyData) {
122 base::scoped_nsobject<NSData> audio_data(
123 [[@"" dataUsingEncoding:NSUTF8StringEncoding] retain]);
124 [tts_player_ prepareToPlayAudioData:audio_data];
125 EXPECT_TRUE([tts_player_observer_ readyNotificationReceived]);
126 EXPECT_FALSE([tts_player_ isReadyForPlayback]);
127 }
128
129 // Tests that kTTSWillStartPlayingNotification is received when playback begins
130 // and kTTSDidStopPlayingNotification is received when it is cancelled.
131 // TODO(rohitrao): Disabled because the bots do not have a valid sound output
132 // device.
133 TEST_F(TextToSpeechPlayerTest, DISABLED_ValidPlaybackNotifications) {
134 NSString* path =
135 [[NSBundle mainBundle] pathForResource:@"test_sound"
136 ofType:@"m4a"
137 inDirectory:@"ios/chrome/test/data/voice"];
138 base::scoped_nsobject<NSData> audio_data(
139 [[NSData alloc] initWithContentsOfFile:path]);
140 [tts_player_ prepareToPlayAudioData:audio_data];
141 [tts_player_ beginPlayback];
142 EXPECT_TRUE([tts_player_observer_ willStartNotificationReceived]);
143 EXPECT_TRUE([tts_player_ isPlayingAudio]);
144 [tts_player_ cancelPlayback];
145 EXPECT_TRUE([tts_player_observer_ didStopNotificationReceived]);
146 EXPECT_FALSE([tts_player_ isPlayingAudio]);
147 }
148
149 // Tests that playback is cancelled when the application enters the background
150 // while playback is occurring.
151 // TODO(rohitrao): Disabled because the bots do not have a valid sound output
152 // device.
153 TEST_F(TextToSpeechPlayerTest, DISABLED_BackgroundNotification) {
154 NSString* path =
155 [[NSBundle mainBundle] pathForResource:@"test_sound"
156 ofType:@"m4a"
157 inDirectory:@"ios/chrome/test/data/voice"];
158 base::scoped_nsobject<NSData> audio_data(
159 [[NSData alloc] initWithContentsOfFile:path]);
160 [tts_player_ prepareToPlayAudioData:audio_data];
161 [tts_player_ beginPlayback];
162 EXPECT_TRUE([tts_player_observer_ willStartNotificationReceived]);
163 EXPECT_TRUE([tts_player_ isPlayingAudio]);
164 [[NSNotificationCenter defaultCenter]
165 postNotificationName:UIApplicationDidEnterBackgroundNotification
166 object:[UIApplication sharedApplication]];
167 EXPECT_TRUE([tts_player_observer_ didStopNotificationReceived]);
168 EXPECT_FALSE([tts_player_ isPlayingAudio]);
169 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698