Summary: Constants | Ctors | Methods | Inherited Methods | [Expand All]

# RuleLoggingUtils

`public class RuleLoggingUtils`
`extends Object` ``

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

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.

### Inherited methods
From class `java.lang.Object`
`Object` | `clone()`
`boolean` | `equals(Object arg0)`
`void` | `finalize()`
`final Class<?>` | `getClass()`
`int` | `hashCode()`
`final void` | `notify()`
`final void` | `notifyAll()`
`String` | `toString()`
`final void` | `wait(long arg0, int arg1)`
`final void` | `wait(long arg0)`
`final void` | `wait()`

## Constants

### LOGGING_SUB_DIR_NAME

    String LOGGING_SUB_DIR_NAME

Constant Value: "testdata"

## Public constructors

### RuleLoggingUtils

    RuleLoggingUtils ()

## Public methods

### assertEmptyFile

    void assertEmptyFile (String message,
                    File file)

Test utility method to check if a file is empty.

Parameters
`message` | `String`: to be used when throwing an Assertion error if the content is not empty
`file` | `File`: to inspect

Throws
`AssertionError` | is thrown when the file isn't empty
`IOException` | when the there are issues accessing the `file` parameter

### assertFileContentContains

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

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

Parameters
`message` | `String`: to be used when throwing an Assertion error if the content does not match
`file` | `File`: to inspect
`contentString` | `String`: 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

### assertFileContentDoesNotContain

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

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

Parameters
`message` | `String`: to be used when throwing an Assertion error if the content does not match
`file` | `File`: to inspect
`contentString` | `String`: 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

### assertFileContentStartsWith

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

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

Parameters
`message` | `String`: to be used if the content does not match
`file` | `File`: to inspect
`contentString` | `String`: 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

### getTestDir

    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.

Parameters
`className` | `String`
`testName` | `String`
`testRunNumber` | `int`

Returns
`File` |

### getTestFile

    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.

Parameters
`className` | `String`
`testName` | `String`
`filename` | `String`
`testRunNumber` | `int`

Returns
`File` |

### getTestRunDir

    File getTestRunDir ()

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

Returns
`File` |

### getTestRunFile

    File getTestRunFile (String filename)

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

Parameters
`filename` | `String`

Returns
`File` |

### printFileToLogcat

    void printFileToLogcat (File logFile,
                    String logcatTag)

Utility method to print file to logcat for debugging purposes.

Parameters
`logFile` | `File`
`logcatTag` | `String`

Throws
`IOException` |

### startCmdAndLogOutputPostL

    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` | `String`: the command and parameters to execute on the system
`logFile` | `File`: where comamnd output is written, or in the case of an error, the exception output is written

### startProcess

    Process startProcess (String[] commandParts)

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

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

Parameters
`commandParts` | `String`

Returns
`Process` |

Throws
`IOException` |

### startProcessAndLogToFile

    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` | `String`: the command and parameters to execute on the system
`logFile` | `File`: where comamnd output is written, or in the case of an error, where the exception output is written
`androidVersion` | `int`: 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.

### startProcessAndWriteOutputToFilePreL

    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` | `String`: the command and parameters to execute on the system
`logFile` | `File`: where comamnd output is written, or in the case of an error, the exception output is written

### writeErrorToFileAndLogcat

    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.

Parameters
`file` | `File`
`logTag` | `String`
`errorMessage` | `String`
`exception` | `Exception`

### writeProcessOutputToFile

    void writeProcessOutputToFile (Process process,
                    File logFile)

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

Parameters
`process` | `Process`
`logFile` | `File`

Throws
`IOException` |

- ## Classes

  - AtraceLogger
  - EnableTestTracingRule
  - LogBatteryInformationRule
  - LogDeviceGetPropInfoRule
  - LoggingBaseRule
  - LogGraphicsStatsRule
  - LogLogcatRule
  - LogNetStatsRule
  - RuleLoggingUtils