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

Unified Diff: ios/chrome/browser/voice/text_to_speech_listener_unittest.mm

Issue 2449593002: [ios] Adds support for parsing Text-to-Speech search results. (Closed)
Patch Set: Disabled Created 4 years, 2 months 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 side-by-side diff with in-line comments
Download patch
Index: ios/chrome/browser/voice/text_to_speech_listener_unittest.mm
diff --git a/ios/chrome/browser/voice/text_to_speech_listener_unittest.mm b/ios/chrome/browser/voice/text_to_speech_listener_unittest.mm
new file mode 100644
index 0000000000000000000000000000000000000000..3dfc88d64ab8df3bbf396c2eb9b5e57c0dbaa02b
--- /dev/null
+++ b/ios/chrome/browser/voice/text_to_speech_listener_unittest.mm
@@ -0,0 +1,99 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "ios/chrome/browser/voice/text_to_speech_listener.h"
+
+#import "base/mac/scoped_nsobject.h"
+#import "ios/web/public/web_state/web_state.h"
+#include "ios/web/public/test/web_test_with_web_state.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/gtest_mac.h"
+#import "third_party/google_toolbox_for_mac/src/Foundation/GTMStringEncoding.h"
+
+namespace {
+NSString* const kHTMLFormat =
+ @"<html><head><script>%@</script></head><body></body></html>";
+NSString* const kValidVoiceSearchScript =
+ @"(function(){var _a_tts='dGVzdGF1ZG8zMm9pbw==';var _m_tts= {}})";
+} // namespace
+
+#pragma mark - TestTTSListenerDelegate
+
+@interface TestTTSListenerDelegate : NSObject<TextToSpeechListenerDelegate> {
+ // Backing objects for properties of the same name.
+ base::scoped_nsobject<NSData> _expectedAudioData;
+}
+
+// The expected audio data to be returned by the TextToSpeechListener.
+@property(nonatomic, retain) NSData* expectedAudioData;
+
+// Whether |-textToSpeechListener:didReceiveResult:| was called.
+@property(nonatomic, assign) BOOL audioDataReceived;
+
+@end
+
+@implementation TestTTSListenerDelegate
+
+@synthesize audioDataReceived = _audioDataReceived;
+
+- (void)setExpectedAudioData:(NSData*)expectedAudioData {
+ _expectedAudioData.reset([expectedAudioData retain]);
+}
+
+- (NSData*)expectedAudioData {
+ return _expectedAudioData;
+}
+
+- (void)textToSpeechListener:(TextToSpeechListener*)listener
+ didReceiveResult:(NSData*)result {
+ EXPECT_NSEQ(self.expectedAudioData, result);
+ self.audioDataReceived = YES;
+}
+
+- (void)textToSpeechListenerWebStateWasDestroyed:
+ (TextToSpeechListener*)listener {
+}
+
+- (BOOL)shouldTextToSpeechListener:(TextToSpeechListener*)listener
+ parseDataFromURL:(const GURL&)URL {
+ return YES;
+}
+
+@end
+
+#pragma mark - TextToSpeechListenerTest
+
+class TextToSpeechListenerTest : public web::WebTestWithWebState {
+ public:
+ void SetUp() override {
+ web::WebTestWithWebState::SetUp();
+ delegate_.reset([[TestTTSListenerDelegate alloc] init]);
+ listener_.reset([[TextToSpeechListener alloc] initWithWebState:web_state()
+ delegate:delegate_]);
+ }
+
+ void TestExtraction(NSString* html, NSData* expected_audio_data) {
+ [delegate_ setExpectedAudioData:expected_audio_data];
+ LoadHtml(html);
+ WaitForCondition(^bool {
+ return [delegate_ audioDataReceived];
+ });
+ }
+
+ private:
+ base::scoped_nsobject<TestTTSListenerDelegate> delegate_;
+ base::scoped_nsobject<TextToSpeechListener> listener_;
+};
+
+TEST_F(TextToSpeechListenerTest, ValidAudioDataTest) {
+ GTMStringEncoding* encoder = [GTMStringEncoding rfc4648Base64StringEncoding];
+ NSString* html =
+ [NSString stringWithFormat:kHTMLFormat, kValidVoiceSearchScript];
+ NSData* expected_audio_data = [encoder decode:@"dGVzdGF1ZG8zMm9pbw=="];
+ TestExtraction(html, expected_audio_data);
+}
+
+TEST_F(TextToSpeechListenerTest, InvalidAudioDataTest) {
+ TestExtraction([NSString stringWithFormat:kHTMLFormat, @""], nil);
+}
« no previous file with comments | « ios/chrome/browser/voice/text_to_speech_listener.mm ('k') | ios/chrome/browser/voice/text_to_speech_parser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698