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

Unified Diff: base/test/test_support_ios.mm

Issue 12328083: When running iOS tests on devices, write stdout to a file, then dump to NSLog. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: also redirect stderr Created 7 years, 9 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/test/test_support_ios.mm
diff --git a/base/test/test_support_ios.mm b/base/test/test_support_ios.mm
index 7d5fa7348334dded7059b44b1df545dfe36ef6d5..118ee321b3ba70a6b5cd4f7b51a5d2bfdb1c8c51 100644
--- a/base/test/test_support_ios.mm
+++ b/base/test/test_support_ios.mm
@@ -58,6 +58,9 @@ static char** g_argv;
label.textAlignment = UITextAlignmentCenter;
[window_ addSubview:label];
+ if ([self shouldRedirectOutputToFile])
+ [self redirectOutput];
+
// Queue up the test run.
[self performSelector:@selector(runTests)
withObject:nil
@@ -65,9 +68,66 @@ static char** g_argv;
return YES;
}
+// Returns true if the gtest output should be redirected to a file, then sent
+// to NSLog when compleete. This redirection is used because gtest only writes
+// output to stdout, but results must be written to NSLog in order to show up in
+// the device log that is retrieved from the device by the host.
+- (BOOL)shouldRedirectOutputToFile {
+#if !TARGET_IPHONE_SIMULATOR
+ return !base::debug::BeingDebugged();
+#endif // TARGET_IPHONE_SIMULATOR
+ return NO;
+}
+
+// Returns the path to the directory to store gtest output files.
+- (NSString*)outputPath {
+ NSArray* searchPath =
+ NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
+ NSUserDomainMask,
+ YES);
+ CHECK([searchPath count] > 0) << "Failed to get the Documents folder";
+ return [searchPath objectAtIndex:0];
+}
+
+// Returns the path to file that stdout is redirected to.
+- (NSString*)stdoutPath {
+ return [[self outputPath] stringByAppendingPathComponent:@"stdout.log"];
+}
+
+// Returns the path to file that stderr is redirected to.
+- (NSString*)stderrPath {
+ return [[self outputPath] stringByAppendingPathComponent:@"stderr.log"];
+}
+
+// Redirects stdout and stderr to files in the Documents folder in the app's
+// sandbox.
+- (void)redirectOutput {
+ freopen([[self stdoutPath] UTF8String], "w+", stdout);
+ freopen([[self stderrPath] UTF8String], "w+", stderr);
+}
+
+// Reads the redirected gtest output from a file and writes it to NSLog.
+- (void)writeOutputToNSLog {
+ for (NSString* path in @[ [self stdoutPath], [self stderrPath]]) {
+ NSString* content = [NSString stringWithContentsOfFile:path
+ encoding:NSUTF8StringEncoding
+ error:NULL];
+ NSArray* lines = [content componentsSeparatedByCharactersInSet:
+ [NSCharacterSet newlineCharacterSet]];
+
+ NSLog(@"Writing contents of %@ to NSLog", path);
+ for (NSString* line in lines) {
+ NSLog(@"%@", line);
+ }
+ }
+}
+
- (void)runTests {
int exitStatus = g_test_suite->Run();
+ if ([self shouldRedirectOutputToFile])
+ [self writeOutputToNSLog];
+
// If a test app is too fast, it will exit before Instruments has has a
// a chance to initialize and no test results will be seen.
// TODO(ios): crbug.com/137010 Figure out how much time is actually needed,
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698