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

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: Fix AVFoundation dependency. 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 {
sdefresne 2016/10/25 13:20:30 nit: move the ivar declaration to @implementation
rohitrao (ping after 24h) 2016/10/25 15:23:12 Done.
24 base::scoped_nsobject<TextToSpeechPlayer> _player;
25 }
26
27 // The TextToSpeechPlayer passed on initialization.
28 @property(nonatomic, retain) TextToSpeechPlayer* player;
29
30 // Whether notifications have been received.
31 @property(nonatomic, readonly) BOOL readyNotificationReceived;
32 @property(nonatomic, readonly) BOOL willStartNotificationReceived;
33 @property(nonatomic, readonly) BOOL didStopNotificationReceived;
34
35 // Notification handlers.
36 - (void)handleReadyNotification:(NSNotification*)notification;
37 - (void)handleWillStartNotification:(NSNotification*)notification;
38 - (void)handleDidStopNotification:(NSNotification*)notification;
39
40 @end
41
42 @implementation TTSPlayerObserver
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 TEST_F(TextToSpeechPlayerTest, ValidPlaybackNotifications) {
132 NSString* path =
133 [[NSBundle mainBundle] pathForResource:@"test_sound"
134 ofType:@"m4a"
135 inDirectory:@"ios/chrome/test/data/voice"];
136 base::scoped_nsobject<NSData> audio_data(
137 [[NSData alloc] initWithContentsOfFile:path]);
138 [tts_player_ prepareToPlayAudioData:audio_data];
139 [tts_player_ beginPlayback];
140 EXPECT_TRUE([tts_player_observer_ willStartNotificationReceived]);
141 EXPECT_TRUE([tts_player_ isPlayingAudio]);
142 [tts_player_ cancelPlayback];
143 EXPECT_TRUE([tts_player_observer_ didStopNotificationReceived]);
144 EXPECT_FALSE([tts_player_ isPlayingAudio]);
145 }
146
147 // Tests that playback is cancelled when the application enters the background
148 // while playback is occurring.
149 TEST_F(TextToSpeechPlayerTest, BackgroundNotification) {
150 NSString* path =
151 [[NSBundle mainBundle] pathForResource:@"test_sound"
152 ofType:@"m4a"
153 inDirectory:@"ios/chrome/test/data/voice"];
154 base::scoped_nsobject<NSData> audio_data(
155 [[NSData alloc] initWithContentsOfFile:path]);
156 [tts_player_ prepareToPlayAudioData:audio_data];
157 [tts_player_ beginPlayback];
158 EXPECT_TRUE([tts_player_observer_ willStartNotificationReceived]);
159 EXPECT_TRUE([tts_player_ isPlayingAudio]);
160 [[NSNotificationCenter defaultCenter]
161 postNotificationName:UIApplicationDidEnterBackgroundNotification
162 object:[UIApplication sharedApplication]];
163 EXPECT_TRUE([tts_player_observer_ didStopNotificationReceived]);
164 EXPECT_FALSE([tts_player_ isPlayingAudio]);
165 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698