public class

RuleLoggingUtils

extends Object
java.lang.Object
   ↳ android.support.test.rule.logging.RuleLoggingUtils

Class Overview

Convenience methods to ensure logging rules perform certain actions in the same manner.

Summary

Constants
String LOGGING_SUB_DIR_NAME
Public Constructors
RuleLoggingUtils()
Public Methods
static void assertEmptyFile(String message, File file)
Test utility method to check if a file is empty.
static void assertFileContentContains(String message, File file, String contentString)
Test utility method to check if a file contains the specified content.
static void assertFileContentDoesNotContain(String message, File file, String contentString)
Test utility method to check if a file doesn't contain the specified content.
static void assertFileContentStartsWith(String message, File file, String contentString)
Test utility method to quickly check if a file begins with the specified content.
static File getTestDir(String className, String testName, int testRunNumber)
Retrieve the directory where logging rules logs should be written to.
static File getTestFile(String className, String testName, String filename, int testRunNumber)
Retrieve a file handle that is within the testing directory where tests should be written to.
static File getTestRunDir()
Retrieve the test run directory where tests should be written to.
static File getTestRunFile(String filename)
Retrieve a file handle within the testing directory where test data can be written for the complete test run.
static void printFileToLogcat(File logFile, String logcatTag)
Utility method to print file to logcat for debugging purposes.
static void startCmdAndLogOutputPostL(String[] commandParts, File logFile)
Start a Process on the system using a process compatible with all Android runtimes.
static Process startProcess(String[] commandParts)
Start a Process with the command and arguments specified in commandParts.
static void startProcessAndLogToFile(String[] commandParts, File logFile, int androidVersion)
Start a Process on the system using either startCmdAndLogOutputPostL(String[], File) or startProcessAndWriteOutputToFilePreL(String[], File) according to the Android version number passed in.
static void startProcessAndWriteOutputToFilePreL(String[] commandParts, File logFile)
Start a Process on the system using a process compatible with all Android runtimes.
static void writeErrorToFileAndLogcat(File file, String logTag, String errorMessage, Exception exception)
Utility method to write an error message to a file and logcat as an error.
static void writeProcessOutputToFile(Process process, File logFile)
Utility method to read a Process's output and write it to a file.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String LOGGING_SUB_DIR_NAME

Constant Value: "testdata"

Public Constructors

public RuleLoggingUtils ()

Public Methods

public static void assertEmptyFile (String message, File file)

Test utility method to check if a file is empty.

Parameters
message to be used when throwing an Assertion error if the content is not empty
file to inspect
Throws
AssertionError is thrown when the file isn't empty
IOException when the there are issues accessing the file parameter

public static void assertFileContentContains (String message, File file, String contentString)

Test utility method to check if a file contains the specified content.

Parameters
message to be used when throwing an Assertion error if the content does not match
file to inspect
contentString to compare against the content of the file
Throws
AssertionError is thrown when the content is not found
IOException when the there are issues accessing the file parameter

public static void assertFileContentDoesNotContain (String message, File file, String contentString)

Test utility method to check if a file doesn't contain the specified content.

Parameters
message to be used when throwing an Assertion error if the content does not match
file to inspect
contentString to compare against the content of the file
Throws
AssertionError is thrown when the content is not found
IOException when the there are issues accessing the file parameter

public static void assertFileContentStartsWith (String message, File file, String contentString)

Test utility method to quickly check if a file begins with the specified content.

Parameters
message to be used if the content does not match
file to inspect
contentString to compare against the content of the file
Throws
AssertionError is thrown when the content is not found
IOException when the there are issues accessing the file parameter

public static File getTestDir (String className, String testName, int testRunNumber)

Retrieve the directory where logging rules logs should be written to. This directory is on external storage so it is not removed when the app is uninstalled. This allows the files to be retrieved despite fatal (think OutOfMemory) exceptions. testRunNumber should be set whenever a test method is run more than one time in a single test run to indicate which iteration the logging is for. Use zero as a default.

public static File getTestFile (String className, String testName, String filename, int testRunNumber)

Retrieve a file handle that is within the testing directory where tests should be written to.

public static File getTestRunDir ()

Retrieve the test run directory where tests should be written to.

public static File getTestRunFile (String filename)

Retrieve a file handle within the testing directory where test data can be written for the complete test run.

public static void printFileToLogcat (File logFile, String logcatTag)

Utility method to print file to logcat for debugging purposes.

Throws
IOException

public static void startCmdAndLogOutputPostL (String[] commandParts, File logFile)

Start a Process on the system using a process compatible with all Android runtimes. Standard and error output is redirected to the specified file.

This command runs within the testing instrumentation and has some development permissions already granted.

Parameters
commandParts the command and parameters to execute on the system
logFile where comamnd output is written, or in the case of an error, the exception output is written

public static Process startProcess (String[] commandParts)

Start a Process with the command and arguments specified in commandParts.

You must call Process.destroy() on the object returned.

Throws
IOException

public static void startProcessAndLogToFile (String[] commandParts, File logFile, int androidVersion)

Start a Process on the system using either startCmdAndLogOutputPostL(String[], File) or startProcessAndWriteOutputToFilePreL(String[], File) according to the Android version number passed in.

This utility method eliminates the need to grant your app some system permissions when running on Android Lollipop or above by using the test instrumentation to run the specified command. If you are testing on pre-Lollipop devices you will need to ensure your test APK has been granted any permissions needed to execute the commands passed in.

Parameters
commandParts the command and parameters to execute on the system
logFile where comamnd output is written, or in the case of an error, where the exception output is written
androidVersion overrides the system Android version which is used to decide the best method to invoke the command with. This is useful for code that needs to write Android version specific tests.

public static void startProcessAndWriteOutputToFilePreL (String[] commandParts, File logFile)

Start a Process on the system using a process compatible with all Android runtimes. Standard and error output is redirected to the specified file.

This command runs as the current user and requires appropriate permissions be granted to the App/Test APK. If the runtime is Android M or greater use startCmdAndLogOutputPostL(String[], File) instead to run as with instrumentation permissions.

Parameters
commandParts the command and parameters to execute on the system
logFile where comamnd output is written, or in the case of an error, the exception output is written

public static void writeErrorToFileAndLogcat (File file, String logTag, String errorMessage, Exception exception)

Utility method to write an error message to a file and logcat as an error.

public static void writeProcessOutputToFile (Process process, File logFile)

Utility method to read a Process's output and write it to a file.

Throws
IOException