Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 1 | # Chromium Java Style Guide |
nyquist | 9d61f98 | 2017-02-10 00:29:08 | [diff] [blame] | 2 | |
| 3 | _For other languages, please see the [Chromium style |
John Palmer | be05130 | 2021-05-19 11:48:35 | [diff] [blame] | 4 | guides](https://chromium.googlesource.com/chromium/src/+/main/styleguide/styleguide.md)._ |
nyquist | 9d61f98 | 2017-02-10 00:29:08 | [diff] [blame] | 5 | |
| 6 | Chromium follows the [Android Open Source style |
| 7 | guide](http://source.android.com/source/code-style.html) unless an exception |
| 8 | is listed below. |
| 9 | |
nyquist | aae4c7c | 2017-02-15 20:41:42 | [diff] [blame] | 10 | You can propose changes to this style guide by sending an email to |
| 11 | `[email protected]`. Ideally, the list will arrive at some consensus and you can |
| 12 | request review for a change to this file. If there's no consensus, |
John Palmer | be05130 | 2021-05-19 11:48:35 | [diff] [blame] | 13 | [`//styleguide/java/OWNERS`](https://chromium.googlesource.com/chromium/src/+/main/styleguide/java/OWNERS) |
nyquist | aae4c7c | 2017-02-15 20:41:42 | [diff] [blame] | 14 | get to decide. |
| 15 | |
agrieve | 0e6bdf2 | 2018-08-03 14:25:24 | [diff] [blame] | 16 | [TOC] |
| 17 | |
Andrew Grieve | 99e388f | 2023-11-02 20:46:51 | [diff] [blame] | 18 | ## Java Language Features |
Nate Fischer | 03308e9 | 2022-11-07 18:14:59 | [diff] [blame] | 19 | |
Andrew Grieve | 99e388f | 2023-11-02 20:46:51 | [diff] [blame] | 20 | ### Type Deduction using "var" {#var} |
Nate Fischer | 03308e9 | 2022-11-07 18:14:59 | [diff] [blame] | 21 | |
| 22 | A variable declaration can use the `var` keyword in place of the type (similar |
| 23 | to the `auto` keyword in C++). In line with the [guidance for |
| 24 | C++](https://google.github.io/styleguide/cppguide.html#Type_deduction), the |
| 25 | `var` keyword may be used when it aids readability and the type of the value is |
| 26 | already clear (ex. `var bundle = new Bundle()` is OK, but `var something = |
| 27 | returnValueIsNotObvious()` may be unclear to readers who are new to this part of |
| 28 | the code). |
| 29 | |
| 30 | The `var` keyword may also be used in try-with-resources when the resource is |
| 31 | not directly accessed (or when it falls under the previous guidance), such as: |
| 32 | |
| 33 | ```java |
| 34 | try (var ignored = StrictModeContext.allowDiskWrites()) { |
| 35 | // 'var' is permitted so long as the 'ignored' variable is not used directly |
| 36 | // in the code. |
| 37 | } |
| 38 | ``` |
| 39 | |
agrieve | 398286b | 2018-08-15 01:44:45 | [diff] [blame] | 40 | ### Exceptions |
Andrew Grieve | 99e388f | 2023-11-02 20:46:51 | [diff] [blame] | 41 | |
Andrew Grieve | 318b3532 | 2023-01-13 16:03:23 | [diff] [blame] | 42 | We discourage overly broad catches via `Throwable`, `Exception`, or |
| 43 | `RuntimeException`, except when dealing with `RemoteException` or similar |
| 44 | system APIs. |
| 45 | * There have been many cases of crashes caused by `IllegalStateException` / |
| 46 | `IllegalArgumentException` / `SecurityException` being thrown where only |
| 47 | `RemoteException` was being caught. In these cases, use |
| 48 | `catch (RemoteException | RuntimeException e)`. |
| 49 | * For all broad catch expressions, add a comment to explain why. |
agrieve | 398286b | 2018-08-15 01:44:45 | [diff] [blame] | 50 | |
Andrew Grieve | 318b3532 | 2023-01-13 16:03:23 | [diff] [blame] | 51 | Avoid adding messages to exceptions that do not aid in debugging. For example: |
agrieve | 398286b | 2018-08-15 01:44:45 | [diff] [blame] | 52 | |
agrieve | 50430de | 2018-08-15 17:49:16 | [diff] [blame] | 53 | ```java |
| 54 | try { |
Nate Fischer | fc38dc81 | 2024-04-24 17:07:26 | [diff] [blame^] | 55 | somethingThatThrowsIOException(); |
agrieve | 50430de | 2018-08-15 17:49:16 | [diff] [blame] | 56 | } catch (IOException e) { |
Nate Fischer | fc38dc81 | 2024-04-24 17:07:26 | [diff] [blame^] | 57 | // Bad - message does not tell you more than the stack trace does: |
| 58 | throw new RuntimeException("Failed to parse a file.", e); |
| 59 | // Good - conveys that this block failed along with the "caused by" exception. |
| 60 | throw new RuntimeException(e); |
| 61 | // Good - adds useful information. |
| 62 | throw new RuntimeException(String.format("Failed to parse %s", fileName), e); |
agrieve | 50430de | 2018-08-15 17:49:16 | [diff] [blame] | 63 | } |
| 64 | ``` |
| 65 | |
agrieve | 398286b | 2018-08-15 01:44:45 | [diff] [blame] | 66 | ### Asserts |
Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 67 | |
Andrew Grieve | 08734276 | 2023-07-25 01:14:25 | [diff] [blame] | 68 | The build system: |
| 69 | * strips asserts in release builds (via R8), |
| 70 | * enables them in debug builds, |
| 71 | * and enables them in report-only mode for Canary builds. |
agrieve | 398286b | 2018-08-15 01:44:45 | [diff] [blame] | 72 | |
| 73 | ```java |
Andrew Grieve | 08734276 | 2023-07-25 01:14:25 | [diff] [blame] | 74 | // Code for assert expressions & messages is removed when asserts are disabled. |
| 75 | assert someCallWithoutSideEffects(param) : "Call failed with: " + param; |
agrieve | 398286b | 2018-08-15 01:44:45 | [diff] [blame] | 76 | ``` |
| 77 | |
Andrew Grieve | 08734276 | 2023-07-25 01:14:25 | [diff] [blame] | 78 | Use your judgement for when to use asserts vs exceptions. Generally speaking, |
| 79 | use asserts to check program invariants (e.g. parameter constraints) and |
| 80 | exceptions for unrecoverable error conditions (e.g. OS errors). You should tend |
| 81 | to use exceptions more in privacy / security-sensitive code. |
| 82 | |
| 83 | Do not add checks when the code will crash anyways. E.g.: |
| 84 | |
| 85 | ```java |
| 86 | // Don't do this. |
| 87 | assert(foo != null); |
| 88 | foo.method(); // This will throw anyways. |
| 89 | ``` |
| 90 | |
| 91 | For multi-statement asserts, use [`BuildConfig.ENABLE_ASSERTS`] to guard your |
| 92 | code (similar to `#if DCHECK_IS_ON()` in C++). E.g.: |
agrieve | 398286b | 2018-08-15 01:44:45 | [diff] [blame] | 93 | |
| 94 | ```java |
Nate Fischer | 4570ebc3 | 2021-06-04 00:44:45 | [diff] [blame] | 95 | import org.chromium.build.BuildConfig; |
| 96 | |
| 97 | ... |
| 98 | |
| 99 | if (BuildConfig.ENABLE_ASSERTS) { |
Nate Fischer | fc38dc81 | 2024-04-24 17:07:26 | [diff] [blame^] | 100 | // Any code here will be stripped in release builds by R8. |
| 101 | ... |
agrieve | 398286b | 2018-08-15 01:44:45 | [diff] [blame] | 102 | } |
| 103 | ``` |
| 104 | |
Andrew Grieve | 08734276 | 2023-07-25 01:14:25 | [diff] [blame] | 105 | [`BuildConfig.ENABLE_ASSERTS`]: https://source.chromium.org/search?q=symbol:BuildConfig%5C.ENABLE_ASSERTS |
| 106 | |
Andrew Grieve | 99e388f | 2023-11-02 20:46:51 | [diff] [blame] | 107 | #### DCHECKS vs Java Asserts {#asserts} |
Andrew Grieve | 08734276 | 2023-07-25 01:14:25 | [diff] [blame] | 108 | |
| 109 | `DCHECK` and `assert` are similar, but our guidance for them differs: |
| 110 | * CHECKs are preferred in C++, whereas asserts are preferred in Java. |
| 111 | |
| 112 | This is because as a memory-safe language, logic bugs in Java are much less |
| 113 | likely to be exploitable. |
| 114 | |
Andrew Grieve | 99e388f | 2023-11-02 20:46:51 | [diff] [blame] | 115 | ### toString() {#toString} |
Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 116 | |
Andrew Grieve | 99e388f | 2023-11-02 20:46:51 | [diff] [blame] | 117 | Use explicit serialization methods (e.g. `toDebugString()` or `getDescription()`) |
| 118 | instead of `toString()` when dynamic dispatch is not required. |
Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 119 | |
Andrew Grieve | 99e388f | 2023-11-02 20:46:51 | [diff] [blame] | 120 | 1. R8 cannot detect when `toString()` is unused, so overrides will not be stripped |
| 121 | when unused. |
| 122 | 2. R8 cannot optimize / inline these calls as well as non-overriding methods. |
Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 123 | |
Andrew Grieve | 99e388f | 2023-11-02 20:46:51 | [diff] [blame] | 124 | ### Records & AutoValue {#records} |
| 125 | |
| 126 | ```java |
| 127 | // Banned. |
| 128 | record Rectangle(float length, float width) {} |
| 129 | ``` |
| 130 | |
| 131 | **Rationale:** |
| 132 | * To avoid dead code: |
| 133 | * Records and `@AutoValue` generate `equals()`, `hashCode()`, and `toString()`, |
| 134 | which `R8` is unable to remove when unused. |
| 135 | * When these methods are required, implement them explicitly so that the |
| 136 | intention is clear. |
| 137 | * Also - supporting `record` requires build system work ([crbug/1493366]). |
| 138 | |
| 139 | Example with `equals()` and `hashCode()`: |
| 140 | |
| 141 | ```java |
| 142 | public class ValueClass { |
| 143 | private final SomeClass mObjMember; |
| 144 | private final int mIntMember; |
| 145 | |
| 146 | @Override |
| 147 | public boolean equals(Object o) { |
| 148 | return o instanceof ValueClass vc |
| 149 | && Objects.equals(mObjMember, vc.mObjMember) |
| 150 | && mIntMember == vc.mIntMember; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public int hashCode() { |
| 155 | return Objects.hash(mObjMember, mIntMember); |
| 156 | } |
| 157 | } |
| 158 | ``` |
| 159 | |
| 160 | [crbug/1493366]: https://crbug.com/1493366 |
| 161 | |
| 162 | ### Enums |
| 163 | |
| 164 | Banned. Use [`@IntDef`](#intdefs) instead. |
| 165 | |
| 166 | **Rationale:** |
| 167 | |
| 168 | Java enums generate a lot of bytecode. Use constants where possible. When a |
| 169 | custom type hierarchy is required, use explicit classes with inheritance. |
Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 170 | |
agrieve | 16c6fe8 | 2018-11-27 17:47:49 | [diff] [blame] | 171 | ### Finalizers |
Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 172 | |
Andrew Grieve | ec828460 | 2023-10-16 15:53:25 | [diff] [blame] | 173 | In line with [Google's Java style guide] and [Android's Java style guide], |
agrieve | 16c6fe8 | 2018-11-27 17:47:49 | [diff] [blame] | 174 | never override `Object.finalize()`. |
| 175 | |
| 176 | Custom finalizers: |
| 177 | * are called on a background thread, and at an unpredicatble point in time, |
| 178 | * swallow all exceptions (asserts won't work), |
| 179 | * causes additional garbage collector jank. |
| 180 | |
| 181 | Classes that need destructor logic should provide an explicit `destroy()` |
John Palmer | be05130 | 2021-05-19 11:48:35 | [diff] [blame] | 182 | method. Use [LifetimeAssert](https://chromium.googlesource.com/chromium/src/+/main/base/android/java/src/org/chromium/base/LifetimeAssert.java) |
Bo Liu | 9bb53ca | 2020-09-22 00:48:10 | [diff] [blame] | 183 | to ensure in debug builds and tests that `destroy()` is called. |
agrieve | 16c6fe8 | 2018-11-27 17:47:49 | [diff] [blame] | 184 | |
Andrew Grieve | ec828460 | 2023-10-16 15:53:25 | [diff] [blame] | 185 | [Google's Java style guide]: https://google.github.io/styleguide/javaguide.html#s6.4-finalizers |
| 186 | [Android's Java style guide]: https://source.android.com/docs/setup/contribute/code-style#dont-use-finalizers |
| 187 | |
Andrew Grieve | 99e388f | 2023-11-02 20:46:51 | [diff] [blame] | 188 | ## Java Library APIs |
Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 189 | |
Andrew Grieve | 99e388f | 2023-11-02 20:46:51 | [diff] [blame] | 190 | Android provides the ability to bundle copies of `java.*` APIs alongside |
| 191 | application code, known as [Java Library Desugaring]. However, since this |
| 192 | bundling comes with a performance cost, Chrome does not use it. Treat `java.*` |
| 193 | APIs the same as you would `android.*` ones and guard them with |
| 194 | `Build.VERSION.SDK_INT` checks [when necessary]. The one exception is if the |
| 195 | method is [directly backported by D8] (these are okay to use, since they are |
| 196 | lightweight). Android Lint will fail if you try to use an API without a |
| 197 | corresponding `Build.VERSION.SDK_INT` guard or `@RequiresApi` annotation. |
| 198 | |
| 199 | [Java Library Desugaring]: https://developer.android.com/studio/write/java8-support-table |
| 200 | [when necessary]: https://developer.android.com/reference/packages |
| 201 | [directly backported by D8]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/r8/backported_methods.txt |
| 202 | |
| 203 | ### Logging |
| 204 | |
| 205 | * Use `org.chromium.base.Log` instead of `android.util.Log`. |
| 206 | * It provides `%s` support, and ensures log stripping works correctly. |
| 207 | * Minimize the use of `Log.w()` and `Log.e()`. |
| 208 | * Debug and Info log levels are stripped by ProGuard in release builds, and |
| 209 | so have no performance impact for shipping builds. However, Warning and |
| 210 | Error log levels are not stripped. |
| 211 | * Function calls in log parameters are *not* stripped by ProGuard. |
| 212 | |
| 213 | ```java |
| 214 | Log.d(TAG, "There are %d cats", countCats()); // countCats() not stripped. |
| 215 | ``` |
| 216 | |
| 217 | ### Streams |
| 218 | |
| 219 | Most uses of [Java streams] are discouraged. If you can write your code as an |
| 220 | explicit loop, then do so. The primary reason for this guidance is because the |
| 221 | lambdas (and method references) needed for streams almost always result in |
| 222 | larger binary size ([example](https://chromium-review.googlesource.com/c/chromium/src/+/4329952). |
| 223 | |
| 224 | The `parallel()` and `parallelStream()` APIs are simpler than their loop |
| 225 | equivalents, but are are currently banned due to a lack of a compelling use case |
| 226 | in Chrome. If you find one, please discuss on `[email protected]`. |
| 227 | |
| 228 | [Java streams]: https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html |
| 229 | |
| 230 | ### AndroidX Annotations {#annotations} |
| 231 | |
| 232 | * Use them liberally. They are [documented here](https://developer.android.com/studio/write/annotations). |
agrieve | 398286b | 2018-08-15 01:44:45 | [diff] [blame] | 233 | * They generally improve readability. |
Andrew Grieve | 99e388f | 2023-11-02 20:46:51 | [diff] [blame] | 234 | * Many make lint more useful. |
Nate Fischer | 74cd25c | 2020-12-16 16:17:03 | [diff] [blame] | 235 | * `javax.annotation.Nullable` vs `androidx.annotation.Nullable` |
| 236 | * Always prefer `androidx.annotation.Nullable`. |
agrieve | 398286b | 2018-08-15 01:44:45 | [diff] [blame] | 237 | * It uses `@Retention(SOURCE)` rather than `@Retention(RUNTIME)`. |
| 238 | |
Andrew Grieve | 99e388f | 2023-11-02 20:46:51 | [diff] [blame] | 239 | #### IntDefs {#intdefs} |
Carlos Knippschild | f2e58c1 | 2021-06-03 01:43:37 | [diff] [blame] | 240 | |
Andrew Grieve | 99e388f | 2023-11-02 20:46:51 | [diff] [blame] | 241 | Values can be declared outside or inside the `@interface`. Chromium style is |
| 242 | to declare inside. |
Carlos Knippschild | f2e58c1 | 2021-06-03 01:43:37 | [diff] [blame] | 243 | |
| 244 | ```java |
| 245 | @IntDef({ContactsPickerAction.CANCEL, ContactsPickerAction.CONTACTS_SELECTED, |
| 246 | ContactsPickerAction.SELECT_ALL, ContactsPickerAction.UNDO_SELECT_ALL}) |
| 247 | @Retention(RetentionPolicy.SOURCE) |
| 248 | public @interface ContactsPickerAction { |
| 249 | int CANCEL = 0; |
| 250 | int CONTACTS_SELECTED = 1; |
| 251 | int SELECT_ALL = 2; |
| 252 | int UNDO_SELECT_ALL = 3; |
| 253 | int NUM_ENTRIES = 4; |
| 254 | } |
| 255 | // ... |
| 256 | void onContactsPickerUserAction(@ContactsPickerAction int action, ...); |
| 257 | ``` |
| 258 | |
| 259 | Values of `Integer` type are also supported, which allows using a sentinel |
| 260 | `null` if needed. |
| 261 | |
| 262 | [@IntDef annotation]: https://developer.android.com/studio/write/annotations#enum-annotations |
| 263 | [Android lint]: https://chromium.googlesource.com/chromium/src/+/HEAD/build/android/docs/lint.md |
| 264 | |
Andrew Grieve | 99e388f | 2023-11-02 20:46:51 | [diff] [blame] | 265 | |
| 266 | ## Style / Formatting {#style} |
nyquist | aae4c7c | 2017-02-15 20:41:42 | [diff] [blame] | 267 | |
agrieve | 398286b | 2018-08-15 01:44:45 | [diff] [blame] | 268 | ### File Headers |
John Palmer | be05130 | 2021-05-19 11:48:35 | [diff] [blame] | 269 | * Use the same format as in the [C++ style guide](https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++.md#File-headers). |
nyquist | aae4c7c | 2017-02-15 20:41:42 | [diff] [blame] | 270 | |
agrieve | 398286b | 2018-08-15 01:44:45 | [diff] [blame] | 271 | ### TODOs |
Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 272 | |
agrieve | 398286b | 2018-08-15 01:44:45 | [diff] [blame] | 273 | * TODO should follow chromium convention. Examples: |
| 274 | * `TODO(username): Some sentence here.` |
Alison Gale | 53c77f6 | 2024-04-22 15:16:27 | [diff] [blame] | 275 | * `TODO(crbug.com/40192027): Even better to use a bug for context.` |
nyquist | aae4c7c | 2017-02-15 20:41:42 | [diff] [blame] | 276 | |
Andrew Grieve | d99e3c0 | 2023-10-25 14:56:48 | [diff] [blame] | 277 | ### Parameter Comments |
| 278 | |
| 279 | Use [parameter comments] when they aid in the readability of a function call. |
| 280 | |
| 281 | E.g.: |
| 282 | |
| 283 | ```java |
| 284 | someMethod(/* enabled= */ true, /* target= */ null, defaultValue); |
| 285 | ``` |
| 286 | |
| 287 | [parameter comments]: https://errorprone.info/bugpattern/ParameterName |
| 288 | |
| 289 | ### Default Field Initializers |
Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 290 | |
nyquist | 9d61f98 | 2017-02-10 00:29:08 | [diff] [blame] | 291 | * Fields should not be explicitly initialized to default values (see |
| 292 | [here](https://groups.google.com/a/chromium.org/d/topic/chromium-dev/ylbLOvLs0bs/discussion)). |
nyquist | aae4c7c | 2017-02-15 20:41:42 | [diff] [blame] | 293 | |
Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 294 | ### Curly Braces |
| 295 | |
nyquist | aae4c7c | 2017-02-15 20:41:42 | [diff] [blame] | 296 | Conditional braces should be used, but are optional if the conditional and the |
| 297 | statement can be on a single line. |
| 298 | |
| 299 | Do: |
| 300 | |
| 301 | ```java |
| 302 | if (someConditional) return false; |
| 303 | for (int i = 0; i < 10; ++i) callThing(i); |
| 304 | ``` |
| 305 | |
| 306 | or |
| 307 | |
| 308 | ```java |
| 309 | if (someConditional) { |
Nate Fischer | fc38dc81 | 2024-04-24 17:07:26 | [diff] [blame^] | 310 | return false; |
nyquist | aae4c7c | 2017-02-15 20:41:42 | [diff] [blame] | 311 | } |
| 312 | ``` |
| 313 | |
| 314 | Do NOT do: |
| 315 | |
| 316 | ```java |
| 317 | if (someConditional) |
Nate Fischer | fc38dc81 | 2024-04-24 17:07:26 | [diff] [blame^] | 318 | return false; |
nyquist | aae4c7c | 2017-02-15 20:41:42 | [diff] [blame] | 319 | ``` |
| 320 | |
nyquist | 2d192c4c | 2017-03-06 21:36:51 | [diff] [blame] | 321 | ### Import Order |
Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 322 | |
nyquist | 2d192c4c | 2017-03-06 21:36:51 | [diff] [blame] | 323 | * Static imports go before other imports. |
| 324 | * Each import group must be separated by an empty line. |
| 325 | |
| 326 | This is the order of the import groups: |
| 327 | |
| 328 | 1. android |
Yun Liu | f40227d9 | 2019-04-04 17:37:46 | [diff] [blame] | 329 | 1. androidx |
nyquist | 2d192c4c | 2017-03-06 21:36:51 | [diff] [blame] | 330 | 1. com (except com.google.android.apps.chrome) |
| 331 | 1. dalvik |
| 332 | 1. junit |
| 333 | 1. org |
| 334 | 1. com.google.android.apps.chrome |
| 335 | 1. org.chromium |
| 336 | 1. java |
| 337 | 1. javax |
| 338 | |
Andrew Grieve | 11c37007 | 2023-07-18 13:46:46 | [diff] [blame] | 339 | ## Testing |
| 340 | |
| 341 | Googlers, see [go/clank-test-strategy](http://go/clank-test-strategy). |
| 342 | |
| 343 | In summary: |
| 344 | |
| 345 | * Use real dependencies when feasible and fast. Use Mockito’s `@Mock` most |
| 346 | of the time, but write fakes for frequently used dependencies. |
| 347 | |
| 348 | * Do not use Robolectric Shadows for Chromium code. Instead, use |
| 349 | `setForTesting()` methods so that it is clear that test hooks exist. |
| 350 | * When `setForTesting()` methods alter global state, use |
| 351 | [`ResettersForTesting.register()`] to ensure that the state is reset |
| 352 | between tests. Omit resetting them via `@After` methods. |
| 353 | |
| 354 | * Use Robolectric when possible (when tests do not require native). Other |
| 355 | times, use on-device tests with one of the following annotations: |
| 356 | * [`@Batch(UNIT_TESTS)`] for unit tests |
| 357 | * [`@Batch(PER_CLASS)`] for integration tests |
| 358 | * [`@DoNotBatch`] for when each test method requires an app restart |
| 359 | |
| 360 | [`ResettersForTesting.register()`]: https://source.chromium.org/search?q=symbol:ResettersForTesting.register |
| 361 | [`@Batch(UNIT_TESTS)`]: https://source.chromium.org/search?q=symbol:Batch.UNIT_TESTS |
| 362 | [`@Batch(PER_CLASS)`]: https://source.chromium.org/search?q=symbol:Batch.PER_CLASS |
| 363 | [`@DoNotBatch`]: https://source.chromium.org/search?q=symbol:DoNotBatch |
| 364 | |
| 365 | ### Test-only Code |
Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 366 | |
Andrew Grieve | 0872aad | 2023-06-26 14:16:31 | [diff] [blame] | 367 | Functions and fields used only for testing should have `ForTesting` as a |
| 368 | suffix so that: |
Caitlin Fischer | 210cfab | 2020-05-07 20:04:30 | [diff] [blame] | 369 | |
Andrew Grieve | 0872aad | 2023-06-26 14:16:31 | [diff] [blame] | 370 | 1. The `android-binary-size` trybot can [ensure they are removed] in |
| 371 | non-test optimized builds (by R8). |
| 372 | 2. [`PRESUMBIT.py`] can ensure no calls are made to such methods outside of |
| 373 | tests, and |
| 374 | |
| 375 | `ForTesting` methods that are `@CalledByNative` should use |
| 376 | `@CalledByNativeForTesting` instead. |
| 377 | |
| 378 | Symbols that are made public (or package-private) for the sake of tests |
| 379 | should be annotated with [`@VisibleForTesting`]. Android Lint will check |
| 380 | that calls from non-test code respect the "otherwise" visibility. |
| 381 | |
Andrew Grieve | e27a4f71 | 2023-07-04 15:16:35 | [diff] [blame] | 382 | Symbols with a `ForTesting` suffix **should not** be annotated with |
Andrew Grieve | 0872aad | 2023-06-26 14:16:31 | [diff] [blame] | 383 | `@VisibleForTesting`. While `otherwise=VisibleForTesting.NONE` exists, it |
| 384 | is redundant given the "ForTesting" suffix and the associated lint check |
Andrew Grieve | e27a4f71 | 2023-07-04 15:16:35 | [diff] [blame] | 385 | is redundant given our trybot check. You should, however, use it for |
| 386 | test-only constructors. |
Andrew Grieve | 0872aad | 2023-06-26 14:16:31 | [diff] [blame] | 387 | |
| 388 | [ensure they are removed]: /docs/speed/binary_size/android_binary_size_trybot.md#Added-Symbols-named-ForTest |
| 389 | [`PRESUMBIT.py`]: https://chromium.googlesource.com/chromium/src/+/main/PRESUBMIT.py |
| 390 | [`@VisibleForTesting`]: https://developer.android.com/reference/androidx/annotation/VisibleForTesting |
Sam Maier | 7452a0d | 2022-07-20 18:24:35 | [diff] [blame] | 391 | |
nyquist | 9d61f98 | 2017-02-10 00:29:08 | [diff] [blame] | 392 | ## Location |
Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 393 | |
nyquist | 9d61f98 | 2017-02-10 00:29:08 | [diff] [blame] | 394 | "Top level directories" are defined as directories with a GN file, such as |
John Palmer | be05130 | 2021-05-19 11:48:35 | [diff] [blame] | 395 | [//base](https://chromium.googlesource.com/chromium/src/+/main/base/) |
nyquist | 9d61f98 | 2017-02-10 00:29:08 | [diff] [blame] | 396 | and |
John Palmer | be05130 | 2021-05-19 11:48:35 | [diff] [blame] | 397 | [//content](https://chromium.googlesource.com/chromium/src/+/main/content/), |
nyquist | 9d61f98 | 2017-02-10 00:29:08 | [diff] [blame] | 398 | Chromium Java should live in a directory named |
| 399 | `<top level directory>/android/java`, with a package name |
| 400 | `org.chromium.<top level directory>`. Each top level directory's Java should |
| 401 | build into a distinct JAR that honors the abstraction specified in a native |
John Palmer | be05130 | 2021-05-19 11:48:35 | [diff] [blame] | 402 | [checkdeps](https://chromium.googlesource.com/chromium/buildtools/+/main/checkdeps/checkdeps.py) |
nyquist | 9d61f98 | 2017-02-10 00:29:08 | [diff] [blame] | 403 | (e.g. `org.chromium.base` does not import `org.chromium.content`). The full |
| 404 | path of any java file should contain the complete package name. |
| 405 | |
| 406 | For example, top level directory `//base` might contain a file named |
| 407 | `base/android/java/org/chromium/base/Class.java`. This would get compiled into a |
| 408 | `chromium_base.jar` (final JAR name TBD). |
| 409 | |
| 410 | `org.chromium.chrome.browser.foo.Class` would live in |
| 411 | `chrome/android/java/org/chromium/chrome/browser/foo/Class.java`. |
| 412 | |
| 413 | New `<top level directory>/android` directories should have an `OWNERS` file |
| 414 | much like |
John Palmer | be05130 | 2021-05-19 11:48:35 | [diff] [blame] | 415 | [//base/android/OWNERS](https://chromium.googlesource.com/chromium/src/+/main/base/android/OWNERS). |
nyquist | 9d61f98 | 2017-02-10 00:29:08 | [diff] [blame] | 416 | |
Andrew Grieve | 0872aad | 2023-06-26 14:16:31 | [diff] [blame] | 417 | ## Tools |
| 418 | |
Andrew Grieve | ec828460 | 2023-10-16 15:53:25 | [diff] [blame] | 419 | `google-java-format` is used to auto-format Java files. Formatting of its code |
| 420 | should be accepted in code reviews. |
Andrew Grieve | 0872aad | 2023-06-26 14:16:31 | [diff] [blame] | 421 | |
| 422 | You can run `git cl format` to apply the automatic formatting. |
| 423 | |
Andrew Grieve | ec828460 | 2023-10-16 15:53:25 | [diff] [blame] | 424 | Chromium also makes use of several [static analysis] tools. |
Andrew Grieve | 0872aad | 2023-06-26 14:16:31 | [diff] [blame] | 425 | |
Andrew Grieve | ec828460 | 2023-10-16 15:53:25 | [diff] [blame] | 426 | [static analysis]: /build/android/docs/static_analysis.md |
Andrew Grieve | 0872aad | 2023-06-26 14:16:31 | [diff] [blame] | 427 | |
nyquist | aae4c7c | 2017-02-15 20:41:42 | [diff] [blame] | 428 | ## Miscellany |
Andrew Grieve | 8d9e40f | 2023-03-15 21:04:42 | [diff] [blame] | 429 | |
nyquist | aae4c7c | 2017-02-15 20:41:42 | [diff] [blame] | 430 | * Use UTF-8 file encodings and LF line endings. |