├── .gitignore ├── LICENSE ├── README.md ├── androidunittest ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── github │ │ └── florent37 │ │ └── androidunittest │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── florent37 │ │ │ └── androidunittest │ │ │ ├── AndroidUnitTest.java │ │ │ ├── AndroidUnitTestAnnotations.java │ │ │ ├── AndroidUnitTestRunner.java │ │ │ ├── annotations │ │ │ ├── RActivity.java │ │ │ ├── RContext.java │ │ │ ├── RFragment.java │ │ │ └── RView.java │ │ │ ├── controllers │ │ │ ├── ControllerActivity.java │ │ │ └── ControllerFragment.java │ │ │ ├── managers │ │ │ ├── AbstractAnnotationManager.java │ │ │ ├── AnnotationActivityManager.java │ │ │ ├── AnnotationContextManager.java │ │ │ ├── AnnotationFragmentManager.java │ │ │ └── AnnotationViewManager.java │ │ │ └── states │ │ │ └── ActivityState.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── github │ └── florent37 │ └── androidunittest │ └── managers │ ├── AnnotationActivityManagerTest.java │ ├── AnnotationContextManagerTest.java │ ├── AnnotationFragmentManagerTest.java │ └── AnnotationViewManagerTest.java ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── github │ │ └── florent37 │ │ └── androidunittest │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── florent37 │ │ │ └── testsample │ │ │ ├── MainActivity.java │ │ │ ├── MainFragment.java │ │ │ ├── MainView.java │ │ │ ├── MyApplication.java │ │ │ └── model │ │ │ └── User.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ └── fragment_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── github │ └── florent37 │ └── testsample │ ├── BasicActivityTest.java │ ├── CustomTestRunner.java │ ├── MainActivityTest.java │ ├── MainFragmentTest.java │ ├── MainFragmentWithActivityTest.java │ ├── MainViewTest.java │ ├── StateActivityTest.java │ └── TestMyApplication.java ├── build.gradle ├── gradle.properties ├── gradle ├── bintray-android-v1.gradle ├── bintray-java-v1.gradle ├── install-v1.gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | # Android Studio Navigation editor temp files 29 | .navigation/ 30 | 31 | # Android Studio captures folder 32 | captures/ 33 | 34 | .gradle 35 | .idea 36 | *.iml 37 | **/build/* -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android Unit Test 2 | 3 | Save time & clear your unit tests on Android ! 4 | 5 | Use annotations to inject Context, Activities, Fragments and Views into your tests 6 | 7 | 8 | 9 | Android app on Google Play 10 | 11 | 12 | 13 | # Usage 14 | 15 | ```java 16 | @RunWith(CustomTestRunner.class) 17 | public class MainActivityTest { 18 | @Rule public AndroidUnitTest androidUnitTest = AndroidUnitTest.rule(); 19 | 20 | @RContect Context context; //inject the app context 21 | @RActivity MainActivity activity; //generates the tested activity 22 | @Mock User user; //mock an user 23 | 24 | @Test 25 | public void testDisplayUser() throws Exception { 26 | // Given 27 | given(user.getName()).willReturn("florent"); 28 | 29 | // When 30 | activity.display(user); 31 | 32 | // Then 33 | assertThat(activity.textView.getText()).isEqualTo("florent"); 34 | } 35 | } 36 | ``` 37 | 38 | ## TestRunner 39 | 40 | Simplify Robolectric Integration 41 | 42 | ```java 43 | public class CustomTestRunner extends AndroidUnitTestRunner { 44 | public CustomTestRunner(Class testClass) throws InitializationError { 45 | super(testClass, BuildConfig.FLAVOR, BuildConfig.BUILD_TYPE, BuildConfig.APPLICATION_ID, TestMyApplication.class); 46 | } 47 | } 48 | ``` 49 | 50 | ## Activity 51 | 52 | Set initial activity state (by default activity is created()) 53 | 54 | ```java 55 | @RunWith(CustomTestRunner.class) 56 | public class MyTest { 57 | @Rule public AndroidUnitTest androidUnitTest = AndroidUnitTest.rule(); 58 | 59 | @RActivity(state = CREATED / STARTED / RESUMED / PAUSED / STOPPED / DESTROYED) 60 | MainActivity activity; 61 | 62 | @Test 63 | public void testMyFunction(){ 64 | androidUnitTest.activity().resume(); 65 | } 66 | 67 | } 68 | ``` 69 | 70 | Note that the injected activity is a spy ! 71 | 72 | ```java 73 | verify(activity, times(2)).someMethod(anyInt()); 74 | ``` 75 | 76 | ## Context 77 | 78 | Retrieve Context easily 79 | 80 | ```java 81 | @RunWith(CustomTestRunner.class) 82 | public class MyTest { 83 | @Rule public AndroidUnitTest androidUnitTest = AndroidUnitTest.rule(); 84 | 85 | @RContext Context context; 86 | } 87 | ``` 88 | 89 | Note that the injected context is a spy ! 90 | 91 | ```java 92 | verify(context, times(2)).someMethod(anyInt()); 93 | ``` 94 | 95 | ## View 96 | 97 | ```java 98 | @RunWith(CustomTestRunner.class) 99 | public class MyTest { 100 | @Rule public AndroidUnitTest androidUnitTest = AndroidUnitTest.rule(); 101 | 102 | @RView CustomView customView; 103 | 104 | @Test 105 | public void testDisplayUser() throws Exception { 106 | // Given 107 | given(user.getName()).willReturn("florent"); 108 | 109 | // When 110 | mainView.display(user); 111 | 112 | // Then 113 | verify(customView).displayText("florent"); 114 | } 115 | } 116 | ``` 117 | 118 | Note that the injected view is a spy ! 119 | 120 | ## Fragment 121 | 122 | ```java 123 | @RunWith(CustomTestRunner.class) 124 | public class MyTest { 125 | @Rule public AndroidUnitTest androidUnitTest = AndroidUnitTest.rule(); 126 | 127 | @RFragment MyFragment myFragment; 128 | @Mock User user; 129 | 130 | @Test 131 | public void testDisplayUser() throws Exception { 132 | // Given 133 | given(user.getName()).willReturn("florent"); 134 | 135 | // When 136 | myFragment.display(user); 137 | 138 | // Then 139 | verify(myFragment).displayText("florent"); 140 | } 141 | } 142 | ``` 143 | 144 | ```java 145 | @RunWith(CustomTestRunner.class) 146 | public class MyTest { 147 | @Rule public AndroidUnitTest androidUnitTest = AndroidUnitTest.rule(); 148 | 149 | @RFragment( 150 | attached = true / false, 151 | tag = "fragmentTag" 152 | ) 153 | MyFragment myFragment; 154 | 155 | @Test 156 | public void testMyFunction() throws Exception { 157 | androidUnitTest.fragment().addToActivity(myFragment) 158 | } 159 | } 160 | ``` 161 | 162 | Note that the injected fragment is a spy ! 163 | 164 | # Download 165 | 166 | Buy Me a Coffee at ko-fi.com 167 | 168 | [ ![Download](https://api.bintray.com/packages/florent37/maven/AndroidUnitTest/images/download.svg) ](https://bintray.com/florent37/maven/AndroidUnitTest/_latestVersion) 169 | 170 | ```java 171 | testCompile 'com.github.florent37:androidunittest:(last version)' 172 | 173 | testCompile 'junit:junit:4.12' 174 | testCompile 'org.mockito:mockito-core:1.10.19' 175 | testCompile 'org.robolectric:robolectric:3.0' 176 | ``` 177 | 178 | # Credits 179 | 180 | Author: Florent Champigny 181 | 182 | 183 | 184 | Android app on Google Play 185 | 186 | 187 | 188 | Follow me on Google+ 190 | 191 | 192 | Follow me on Twitter 194 | 195 | 196 | Follow me on LinkedIn 198 | 199 | 200 | License 201 | -------- 202 | 203 | Copyright 2016 florent37, Inc. 204 | 205 | Licensed under the Apache License, Version 2.0 (the "License"); 206 | you may not use this file except in compliance with the License. 207 | You may obtain a copy of the License at 208 | 209 | http://www.apache.org/licenses/LICENSE-2.0 210 | 211 | Unless required by applicable law or agreed to in writing, software 212 | distributed under the License is distributed on an "AS IS" BASIS, 213 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 214 | See the License for the specific language governing permissions and 215 | limitations under the License. 216 | -------------------------------------------------------------------------------- /androidunittest/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /androidunittest/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion project.sdk 5 | buildToolsVersion project.buildTools 6 | 7 | defaultConfig { 8 | minSdkVersion project.minSdk 9 | targetSdkVersion project.sdk 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | } 14 | 15 | dependencies { 16 | compile( 17 | 'junit:junit:4.12', 18 | 'org.mockito:mockito-core:1.10.19', 19 | 'org.robolectric:robolectric:3.0', 20 | 'com.android.support:support-annotations:'+project.supportVersion, 21 | 'com.android.support:appcompat-v7:'+project.supportVersion 22 | ) 23 | testCompile 'com.google.truth:truth:0.28' 24 | } 25 | 26 | 27 | ext { 28 | bintrayRepo = 'maven' 29 | bintrayName = 'AndroidUnitTest' 30 | orgName = 'florent37' 31 | 32 | publishedGroupId = 'com.github.florent37' 33 | libraryName = 'AndroidUnitTest' 34 | artifact = 'androidunittest' 35 | 36 | libraryDescription = 'AndroidUnitTest' 37 | 38 | siteUrl = 'https://github.com/florent37/AndroidUnitTest' 39 | gitUrl = 'https://github.com/florent37/AndroidUnitTest.git' 40 | 41 | libraryVersion = rootProject.ext.libraryVersion 42 | 43 | developerId = 'florent37' 44 | developerName = 'Florent Champigny' 45 | developerEmail = 'champigny.florent@gmail.com' 46 | 47 | licenseName = 'The Apache Software License, Version 2.0' 48 | licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt' 49 | allLicenses = ["Apache-2.0"] 50 | } 51 | 52 | if (project.rootProject.file('local.properties').exists()) { 53 | apply from: rootProject.file('gradle/install-v1.gradle') 54 | apply from: rootProject.file('gradle/bintray-android-v1.gradle') 55 | } -------------------------------------------------------------------------------- /androidunittest/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/florentchampigny/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /androidunittest/src/androidTest/java/com/github/florent37/androidunittest/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /androidunittest/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/AndroidUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest; 2 | 3 | import android.support.annotation.Nullable; 4 | 5 | import com.github.florent37.androidunittest.controllers.ControllerActivity; 6 | import com.github.florent37.androidunittest.controllers.ControllerFragment; 7 | 8 | import org.junit.rules.MethodRule; 9 | import org.junit.runners.model.FrameworkMethod; 10 | import org.junit.runners.model.Statement; 11 | import org.mockito.Mockito; 12 | import org.mockito.MockitoAnnotations; 13 | import org.robolectric.util.ActivityController; 14 | 15 | /** 16 | * Created by florentchampigny on 07/05/2016. 17 | */ 18 | public class AndroidUnitTest implements MethodRule { 19 | 20 | ControllerActivity controllerActivity; 21 | ControllerFragment controllerFragment; 22 | @Nullable ActivityController activityController; 23 | private AndroidUnitTestAnnotations androidUnitTestAnnotations; 24 | 25 | protected AndroidUnitTest() { 26 | androidUnitTestAnnotations = new AndroidUnitTestAnnotations(AndroidUnitTest.this); 27 | controllerActivity = new ControllerActivity(this); 28 | controllerFragment = new ControllerFragment(this); 29 | } 30 | 31 | public static AndroidUnitTest rule() { 32 | return new AndroidUnitTest(); 33 | } 34 | 35 | @Override 36 | public Statement apply(final Statement base, FrameworkMethod method, final Object target) { 37 | return new Statement() { 38 | @Override 39 | public void evaluate() throws Throwable { 40 | MockitoAnnotations.initMocks(target); 41 | androidUnitTestAnnotations.init(target); 42 | base.evaluate(); 43 | Mockito.validateMockitoUsage(); 44 | } 45 | }; 46 | } 47 | 48 | @Nullable 49 | public ActivityController getActivityController() { 50 | return activityController; 51 | } 52 | 53 | public void setActivityController(@Nullable ActivityController activityController) { 54 | this.activityController = activityController; 55 | } 56 | 57 | public AndroidUnitTestAnnotations getAndroidUnitTestAnnotations() { 58 | return androidUnitTestAnnotations; 59 | } 60 | 61 | public ControllerActivity activity() { 62 | return controllerActivity; 63 | } 64 | 65 | public ControllerFragment fragment() { 66 | return controllerFragment; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/AndroidUnitTestAnnotations.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.support.v4.app.Fragment; 6 | 7 | import com.github.florent37.androidunittest.managers.AbstractAnnotationManager; 8 | import com.github.florent37.androidunittest.managers.AnnotationActivityManager; 9 | import com.github.florent37.androidunittest.managers.AnnotationContextManager; 10 | import com.github.florent37.androidunittest.managers.AnnotationFragmentManager; 11 | import com.github.florent37.androidunittest.managers.AnnotationViewManager; 12 | 13 | import org.robolectric.RuntimeEnvironment; 14 | 15 | import java.lang.reflect.Field; 16 | 17 | /** 18 | * Created by florentchampigny on 07/05/2016. 19 | */ 20 | public class AndroidUnitTestAnnotations { 21 | final Context context; 22 | 23 | AndroidUnitTest androidUnitTest; 24 | Object target; 25 | 26 | AbstractAnnotationManager managers[]; 27 | AnnotationActivityManager activityManager; 28 | AnnotationFragmentManager fragmentManager; 29 | 30 | 31 | private AndroidUnitTestAnnotations() { 32 | context = RuntimeEnvironment.application; 33 | } 34 | 35 | public AndroidUnitTestAnnotations(AndroidUnitTest androidUnitTest) { 36 | this(); 37 | 38 | this.androidUnitTest = androidUnitTest; 39 | } 40 | 41 | 42 | public AndroidUnitTestAnnotations init(Object target) { 43 | //find @Activity 44 | //find @Fragment 45 | //find @RContext -> RuntimeEnvironment.application; 46 | this.target = target; 47 | 48 | instantiate(); 49 | scan(); 50 | execute(); 51 | return this; 52 | } 53 | 54 | 55 | public void updateActivity() { 56 | activityManager.updateActivity(target); 57 | } 58 | 59 | public void addToActivity(@NonNull Fragment fragment) { 60 | fragmentManager.addToActivity(target, fragment); 61 | } 62 | 63 | public void removeFromActivity(@NonNull Fragment fragment) { 64 | fragmentManager.removeFromActivity(fragment); 65 | } 66 | 67 | 68 | /** 69 | * Instantiate the list of abstract annotation managers 70 | */ 71 | private void instantiate() { 72 | managers = new AbstractAnnotationManager[]{ 73 | new AnnotationContextManager(androidUnitTest), 74 | new AnnotationActivityManager(androidUnitTest), 75 | new AnnotationFragmentManager(androidUnitTest), 76 | new AnnotationViewManager(androidUnitTest) 77 | }; 78 | 79 | activityManager = (AnnotationActivityManager) managers[1]; 80 | fragmentManager = (AnnotationFragmentManager) managers[2]; 81 | } 82 | 83 | /** 84 | * Scan the target to populate the managers 85 | */ 86 | private void scan() { 87 | for (Field field : target.getClass().getDeclaredFields()) { 88 | for (AbstractAnnotationManager manager : managers) 89 | if (manager.canManage(field)) 90 | manager.scanned(field); 91 | } 92 | } 93 | 94 | /** 95 | * Execute the different managers 96 | * 97 | * given their positions, it represents their dependencies 98 | */ 99 | private void execute() { 100 | for (AbstractAnnotationManager manager : managers) 101 | manager.execute(target, context); 102 | } 103 | } -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/AndroidUnitTestRunner.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest; 2 | 3 | import android.app.Application; 4 | import android.os.Build; 5 | import android.support.annotation.NonNull; 6 | import android.support.annotation.Nullable; 7 | 8 | import org.junit.runners.model.InitializationError; 9 | import org.robolectric.RobolectricTestRunner; 10 | import org.robolectric.annotation.Config; 11 | import org.robolectric.internal.bytecode.InstrumentationConfiguration; 12 | import org.robolectric.internal.bytecode.ShadowMap; 13 | import org.robolectric.manifest.AndroidManifest; 14 | 15 | import java.io.File; 16 | import java.util.Properties; 17 | 18 | public class AndroidUnitTestRunner extends RobolectricTestRunner { 19 | 20 | public static final String PATH_ASSET = "../../../assets/"; 21 | public static final String PATH_RESOURCE = "../../../res/merged/"; 22 | public static final String PATH_MANIFEST = "build/intermediates/manifests/full/%s/AndroidManifest.xml"; 23 | 24 | public static final String CONFIG_APPLICATION = "application"; 25 | public static final String CONFIG_MANIFEST = "manifest"; 26 | public static final String CONFIG_ASSET_DIR = "assetDir"; 27 | public static final String CONFIG_RESOURCE_DIR = "resourceDir"; 28 | public static final String CONFIG_PACKAGE_NAME = "packageName"; 29 | public static final String CONFIG_SDK = "sdk"; 30 | 31 | public static final String PATH_PREFIX = "app/"; 32 | final String packageName; 33 | final String flavor; 34 | final String buildType; 35 | final Class applicationClass; 36 | 37 | public AndroidUnitTestRunner(Class testClass, @NonNull String flavor, @NonNull String buildType, String packageName, @Nullable Class applicationClass) throws InitializationError { 38 | super(testClass); 39 | this.packageName = packageName; 40 | this.applicationClass = applicationClass; 41 | this.flavor = flavor; 42 | this.buildType = buildType; 43 | } 44 | 45 | public String getPathManifest() { 46 | String path = buildType; 47 | if (flavor != null && !flavor.isEmpty()) { 48 | path = String.format("%s/%s", flavor, path); 49 | } 50 | return String.format(PATH_MANIFEST, path); 51 | } 52 | 53 | public String getPathAssets() { 54 | String prePath = ""; 55 | String postPath = buildType; 56 | if (flavor != null && !flavor.isEmpty()) { 57 | prePath = "../"; 58 | postPath = String.format("%s/%s", flavor, postPath); 59 | } 60 | return prePath + PATH_ASSET + postPath; 61 | } 62 | 63 | public String getPathResources() { 64 | String prePath = ""; 65 | String postPath = buildType; 66 | if (flavor != null && !flavor.isEmpty()) { 67 | prePath = "../"; 68 | postPath = String.format("%s/%s", flavor, postPath); 69 | } 70 | return prePath + PATH_RESOURCE + postPath; 71 | } 72 | 73 | @Override 74 | public InstrumentationConfiguration createClassLoaderConfig() { 75 | InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder(); 76 | return builder.build(); 77 | } 78 | 79 | @Override 80 | protected AndroidManifest getAppManifest(Config config) { 81 | String path = getPathManifest(); 82 | 83 | // android studio has a different execution root for tests than pure gradle 84 | // so we avoid here manual effort to get them running inside android studio 85 | if (!new File(path).exists()) { 86 | path = PATH_PREFIX + path; 87 | } 88 | 89 | config = overwriteConfig(config, CONFIG_MANIFEST, path); 90 | config = overwriteConfig(config, CONFIG_ASSET_DIR, getPathAssets()); 91 | config = overwriteConfig(config, CONFIG_RESOURCE_DIR, getPathResources()); 92 | if (packageName != null) { 93 | config = overwriteConfig(config, CONFIG_PACKAGE_NAME, packageName); 94 | } 95 | if (applicationClass != null) { 96 | config = overwriteConfig(config, CONFIG_APPLICATION, applicationClass.getCanonicalName()); 97 | } 98 | return super.getAppManifest(config); 99 | } 100 | 101 | @Override 102 | protected int pickSdkVersion(Config config, AndroidManifest manifest) { 103 | config = overwriteConfig(config, CONFIG_SDK, String.valueOf(Build.VERSION_CODES.JELLY_BEAN)); 104 | return super.pickSdkVersion(config, manifest); 105 | } 106 | 107 | @Override 108 | protected ShadowMap createShadowMap() { 109 | return super.createShadowMap() 110 | .newBuilder() 111 | .build(); 112 | } 113 | 114 | protected Config.Implementation overwriteConfig(Config config, String key, String value) { 115 | Properties properties = new Properties(); 116 | properties.setProperty(key, value); 117 | return new Config.Implementation(config, Config.Implementation.fromProperties(properties)); 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/annotations/RActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.annotations; 2 | 3 | import com.github.florent37.androidunittest.states.ActivityState; 4 | 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | 8 | /** 9 | * Created by florentchampigny on 07/05/2016. 10 | */ 11 | @Retention(RetentionPolicy.RUNTIME) 12 | public @interface RActivity { 13 | ActivityState state() default ActivityState.CREATED; 14 | } 15 | -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/annotations/RContext.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.annotations; 2 | 3 | import java.lang.annotation.Retention; 4 | import java.lang.annotation.RetentionPolicy; 5 | 6 | /** 7 | * Created by florentchampigny on 07/05/2016. 8 | */ 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface RContext { 11 | } 12 | -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/annotations/RFragment.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.annotations; 2 | 3 | import java.lang.annotation.Retention; 4 | import java.lang.annotation.RetentionPolicy; 5 | 6 | /** 7 | * Created by florentchampigny on 07/05/2016. 8 | */ 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface RFragment { 11 | boolean attached() default true; 12 | String tag() default ""; 13 | } 14 | -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/annotations/RView.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.annotations; 2 | 3 | import java.lang.annotation.Retention; 4 | import java.lang.annotation.RetentionPolicy; 5 | 6 | /** 7 | * Created by florentchampigny on 07/05/2016. 8 | */ 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface RView { 11 | boolean attached() default true; 12 | } 13 | -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/controllers/ControllerActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.controllers; 2 | 3 | import android.app.Activity; 4 | import android.support.annotation.Nullable; 5 | 6 | import com.github.florent37.androidunittest.AndroidUnitTest; 7 | import com.github.florent37.androidunittest.annotations.RActivity; 8 | import com.github.florent37.androidunittest.states.ActivityState; 9 | 10 | import org.mockito.Mockito; 11 | import org.robolectric.Robolectric; 12 | import org.robolectric.util.ActivityController; 13 | 14 | /** 15 | * Created by florentchampigny on 08/05/2016. 16 | */ 17 | public class ControllerActivity { 18 | 19 | private AndroidUnitTest androidUnitTest; 20 | 21 | public ControllerActivity(AndroidUnitTest androidUnitTest) { 22 | this.androidUnitTest = androidUnitTest; 23 | } 24 | 25 | @Nullable 26 | public Activity get() { 27 | if (androidUnitTest.getActivityController() != null) { 28 | return (Activity) androidUnitTest.getActivityController().get(); 29 | } 30 | return null; 31 | } 32 | 33 | @Nullable 34 | public ActivityController getActivityController() { 35 | if (androidUnitTest.getActivityController() != null) { 36 | return androidUnitTest.getActivityController(); 37 | } 38 | return null; 39 | } 40 | 41 | public Activity createAndInitActivity(Class activityClass, @Nullable RActivity activityAnnotation) { 42 | ActivityController activityController = ActivityController.of(Robolectric.getShadowsAdapter(), activityClass); 43 | androidUnitTest.setActivityController(activityController); 44 | if (activityAnnotation != null) { 45 | ActivityState activityState = activityAnnotation.state(); 46 | setActivityState(activityController, activityState); 47 | } 48 | Activity activity = (Activity) activityController.get(); 49 | return Mockito.spy(activity); 50 | } 51 | 52 | public ControllerActivity resume() { 53 | if (androidUnitTest.getActivityController() != null) { 54 | androidUnitTest.getActivityController().resume(); 55 | androidUnitTest.getAndroidUnitTestAnnotations().updateActivity(); 56 | } 57 | return this; 58 | } 59 | 60 | public ControllerActivity create() { 61 | if (androidUnitTest.getActivityController() != null) { 62 | androidUnitTest.getActivityController().create(); 63 | androidUnitTest.getAndroidUnitTestAnnotations().updateActivity(); 64 | } 65 | return this; 66 | } 67 | 68 | public ControllerActivity start() { 69 | if (androidUnitTest.getActivityController() != null) { 70 | androidUnitTest.getActivityController().start(); 71 | androidUnitTest.getAndroidUnitTestAnnotations().updateActivity(); 72 | } 73 | return this; 74 | } 75 | 76 | public ControllerActivity pause() { 77 | if (androidUnitTest.getActivityController() != null) { 78 | androidUnitTest.getActivityController().pause(); 79 | androidUnitTest.getAndroidUnitTestAnnotations().updateActivity(); 80 | } 81 | return this; 82 | } 83 | 84 | public ControllerActivity stop() { 85 | if (androidUnitTest.getActivityController() != null) { 86 | androidUnitTest.getActivityController().stop(); 87 | androidUnitTest.getAndroidUnitTestAnnotations().updateActivity(); 88 | } 89 | return this; 90 | } 91 | 92 | public ControllerActivity destroy() { 93 | if (androidUnitTest.getActivityController() != null) { 94 | androidUnitTest.getActivityController().destroy(); 95 | androidUnitTest.getAndroidUnitTestAnnotations().updateActivity(); 96 | } 97 | return this; 98 | } 99 | 100 | public ControllerActivity setActivityState(ActivityState activityState) { 101 | if (androidUnitTest.getActivityController() != null) { 102 | setActivityState(getActivityController(), activityState); 103 | } 104 | return this; 105 | } 106 | 107 | public ControllerActivity setActivityState(ActivityController activityController, ActivityState activityState) { 108 | ActivityState init = ActivityState.CREATED; 109 | 110 | while (init != null && init.isLowerOrEquals(activityState)) { 111 | System.out.println(init); 112 | applyState(init, activityController); 113 | init = init.next(); 114 | } 115 | 116 | return this; 117 | } 118 | 119 | private void applyState(ActivityState state, 120 | ActivityController controller) { 121 | switch (state) { 122 | case STARTED: 123 | controller.start(); 124 | break; 125 | case RESUMED: 126 | controller.resume(); 127 | break; 128 | case PAUSED: 129 | controller.pause(); 130 | break; 131 | case STOPPED: 132 | controller.stop(); 133 | break; 134 | case DESTROYED: 135 | controller.destroy(); 136 | break; 137 | case CREATED: 138 | default: 139 | controller.create(); 140 | break; 141 | } 142 | } 143 | 144 | } 145 | -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/controllers/ControllerFragment.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.controllers; 2 | 3 | import android.support.v4.app.Fragment; 4 | 5 | import com.github.florent37.androidunittest.AndroidUnitTest; 6 | 7 | /** 8 | * Created by florentchampigny on 08/05/2016. 9 | */ 10 | public class ControllerFragment { 11 | private AndroidUnitTest androidUnitTest; 12 | public ControllerFragment(AndroidUnitTest androidUnitTest) { 13 | this.androidUnitTest = androidUnitTest; 14 | } 15 | 16 | public ControllerFragment attachToActivity(Fragment fragment){ 17 | if (androidUnitTest.activity().get() == null) { 18 | 19 | } 20 | androidUnitTest.getAndroidUnitTestAnnotations().addToActivity(fragment); 21 | return this; 22 | } 23 | 24 | public ControllerFragment removeFromActivity(Fragment fragment){ 25 | androidUnitTest.getAndroidUnitTestAnnotations().removeFromActivity(fragment); 26 | return this; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/managers/AbstractAnnotationManager.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.managers; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | 6 | import com.github.florent37.androidunittest.AndroidUnitTest; 7 | 8 | import java.lang.annotation.Annotation; 9 | import java.lang.reflect.Field; 10 | 11 | /** 12 | * Created by kevinleperf on 18/05/16. 13 | */ 14 | public abstract class AbstractAnnotationManager { 15 | private AndroidUnitTest mAndroidUnitTest; 16 | 17 | private AbstractAnnotationManager() { 18 | 19 | } 20 | 21 | public AbstractAnnotationManager(AndroidUnitTest parent) { 22 | this(); 23 | 24 | mAndroidUnitTest = parent; 25 | } 26 | 27 | public final boolean canManage(@NonNull Field field) { 28 | return field.isAnnotationPresent(canManagerInternal()); 29 | } 30 | 31 | @NonNull 32 | protected abstract Class canManagerInternal(); 33 | 34 | /** 35 | * Method called by the AndroidUnitTestAnnotations class 36 | * during the scan() method 37 | * 38 | * @param field the scanned field to manage internally 39 | */ 40 | public abstract void scanned(@NonNull Field field); 41 | 42 | /** 43 | * Method called by the AndroidUnitTestAnnotations class 44 | * during the execute() method 45 | * 46 | * @param object the object to be applied 47 | * @param context the current app context 48 | */ 49 | public abstract void execute(@NonNull Object object, 50 | @NonNull Context context); 51 | 52 | @NonNull 53 | protected AndroidUnitTest getAndroidUnitTest() { 54 | return mAndroidUnitTest; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/managers/AnnotationActivityManager.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.managers; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.support.annotation.NonNull; 6 | import android.support.annotation.Nullable; 7 | import android.support.annotation.VisibleForTesting; 8 | 9 | import com.github.florent37.androidunittest.AndroidUnitTest; 10 | import com.github.florent37.androidunittest.annotations.RActivity; 11 | 12 | import org.mockito.internal.util.reflection.FieldSetter; 13 | 14 | import java.lang.annotation.Annotation; 15 | import java.lang.reflect.Field; 16 | 17 | /** 18 | * Created by kevinleperf on 18/05/16. 19 | */ 20 | public class AnnotationActivityManager extends AbstractAnnotationManager { 21 | 22 | @VisibleForTesting Field activityField; 23 | 24 | public AnnotationActivityManager(AndroidUnitTest parent) { 25 | super(parent); 26 | 27 | //createAndInitActivity(null, FragmentActivity.class, null); 28 | //parent.activity().setActivityState(ActivityState.CREATED); 29 | //useless create() here since in createAndInitActivity 30 | //parent.getActivityController().create(); 31 | } 32 | 33 | @Override 34 | public void scanned(@NonNull Field field) { 35 | activityField = field; 36 | } 37 | 38 | @Override 39 | public void execute(@NonNull Object object, @NonNull Context context) { 40 | if (activityField != null) { 41 | createAndInitActivity(object, 42 | activityField.getType(), 43 | activityField.getAnnotation(RActivity.class)); 44 | }//activityField is null, using standard FragmentActivity from constructor 45 | } 46 | 47 | public Field getScanned() { 48 | return activityField; 49 | } 50 | 51 | public void createAndInitActivity(@Nullable Object target, Class activityClass, @Nullable RActivity activityAnnotation) { 52 | Activity activity = getAndroidUnitTest().activity().createAndInitActivity(activityClass, activityAnnotation); 53 | if (this.activityField != null && target != null) { 54 | new FieldSetter(target, this.activityField).set(activity); 55 | } 56 | } 57 | 58 | public void updateActivity(Object target) { 59 | new FieldSetter(target, this.activityField) 60 | .set(getAndroidUnitTest().getActivityController().get()); 61 | } 62 | 63 | @NonNull 64 | @Override 65 | protected Class canManagerInternal() { 66 | return RActivity.class; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/managers/AnnotationContextManager.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.managers; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.support.annotation.VisibleForTesting; 6 | 7 | import com.github.florent37.androidunittest.AndroidUnitTest; 8 | import com.github.florent37.androidunittest.annotations.RContext; 9 | 10 | import org.mockito.Mockito; 11 | import org.mockito.internal.util.reflection.FieldSetter; 12 | 13 | import java.lang.annotation.Annotation; 14 | import java.lang.reflect.Field; 15 | 16 | /** 17 | * Created by kevinleperf on 18/05/16. 18 | */ 19 | public class AnnotationContextManager extends AbstractAnnotationManager { 20 | 21 | @VisibleForTesting Field contextField; 22 | 23 | public AnnotationContextManager(AndroidUnitTest parent) { 24 | super(parent); 25 | } 26 | 27 | @Override 28 | public void scanned(@NonNull Field field) { 29 | contextField = field; 30 | } 31 | 32 | @Override 33 | public void execute(@NonNull Object target, @NonNull Context context) { 34 | if (contextField != null) { 35 | Context appContext = context; 36 | appContext = Mockito.spy(appContext); 37 | new FieldSetter(target, this.contextField).set(appContext); 38 | } 39 | } 40 | 41 | @NonNull 42 | @Override 43 | protected Class canManagerInternal() { 44 | return RContext.class; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/managers/AnnotationFragmentManager.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.managers; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.support.annotation.NonNull; 6 | import android.support.annotation.VisibleForTesting; 7 | import android.support.v4.app.Fragment; 8 | import android.support.v4.app.FragmentActivity; 9 | 10 | import com.github.florent37.androidunittest.AndroidUnitTest; 11 | import com.github.florent37.androidunittest.annotations.RFragment; 12 | import com.github.florent37.androidunittest.controllers.ControllerActivity; 13 | import com.github.florent37.androidunittest.states.ActivityState; 14 | 15 | import org.mockito.Mockito; 16 | import org.mockito.internal.util.reflection.FieldReader; 17 | import org.mockito.internal.util.reflection.FieldSetter; 18 | 19 | import java.lang.annotation.Annotation; 20 | import java.lang.reflect.Field; 21 | import java.util.HashSet; 22 | 23 | /** 24 | * Created by kevinleperf on 18/05/16. 25 | */ 26 | public class AnnotationFragmentManager extends AbstractAnnotationManager { 27 | @VisibleForTesting final HashSet fragmentFields; 28 | 29 | public AnnotationFragmentManager(AndroidUnitTest parent) { 30 | super(parent); 31 | 32 | fragmentFields = new HashSet<>(); 33 | } 34 | 35 | public static void addToActivity(Activity activity, Fragment fragment, String tag) { 36 | if (activity instanceof FragmentActivity) { 37 | FragmentActivity fragmentActivity = (FragmentActivity) activity; 38 | fragmentActivity.getSupportFragmentManager().beginTransaction() 39 | .add(fragment, tag) 40 | .commit(); 41 | } 42 | } 43 | 44 | public static void removeFromActivity(Activity activity, Fragment fragment) { 45 | if (activity instanceof FragmentActivity) { 46 | FragmentActivity fragmentActivity = (FragmentActivity) activity; 47 | fragmentActivity.getSupportFragmentManager().beginTransaction() 48 | .remove(fragment) 49 | .commit(); 50 | } 51 | } 52 | 53 | @Override 54 | public void scanned(@NonNull Field field) { 55 | fragmentFields.add(field); 56 | } 57 | 58 | @Override 59 | public void execute(@NonNull Object object, @NonNull Context context) { 60 | for (Field fragment : fragmentFields) { 61 | initFragment(object, fragment); 62 | } 63 | } 64 | 65 | public void initFragment(@NonNull Object target, @NonNull Field fragmentField) { 66 | RFragment fragmentAnnotation = fragmentField.getAnnotation(RFragment.class); 67 | 68 | Class fragmentClass = fragmentField.getType(); 69 | Fragment fragment = null; 70 | try { 71 | fragment = (Fragment) fragmentClass.newInstance(); 72 | } catch (Exception e) { 73 | e.printStackTrace(); 74 | throw new IllegalStateException("Impossible to instantiate a fragment using the default constructor"); 75 | } 76 | 77 | fragment = Mockito.spy(fragment); 78 | new FieldSetter(target, fragmentField).set(fragment); 79 | 80 | //if no activity is create, the default activity manager behaviour is to create one 81 | /*if (this.activityField == null) { 82 | createActivity(FragmentActivity.class, null); 83 | androidUnitTest.activity().setActivityState(ActivityState.CREATED); 84 | }*/ 85 | 86 | if (fragmentAnnotation.attached()) { 87 | String tag = fragmentAnnotation.tag(); 88 | if (tag == null || tag.isEmpty()) { 89 | tag = fragment.getClass().toString(); 90 | } 91 | addToActivity(getActivity(), fragment, tag); 92 | } 93 | } 94 | 95 | public void addToActivity(@NonNull Object target, Fragment fragment) { 96 | String tag = null; 97 | for (Field fragmentField : fragmentFields) { 98 | Fragment fragmentOfField = (Fragment) new FieldReader(target, fragmentField).read(); 99 | if (fragment == fragmentOfField) { 100 | RFragment fragmentAnnotation = fragmentField.getAnnotation(RFragment.class); 101 | tag = fragmentAnnotation.tag(); 102 | } 103 | } 104 | if (tag == null || tag.isEmpty()) { 105 | tag = fragment.getClass().toString(); 106 | } 107 | 108 | ControllerActivity controllerActivity = getAndroidUnitTest().activity(); 109 | //if no activity is create, the default activity manager behaviour is to create one 110 | if (controllerActivity.get() == null) { 111 | controllerActivity.createAndInitActivity(FragmentActivity.class, null); 112 | controllerActivity.setActivityState(ActivityState.CREATED); 113 | } 114 | 115 | AnnotationFragmentManager.addToActivity(getActivity(), fragment, tag); 116 | } 117 | 118 | public void removeFromActivity(Fragment fragment) { 119 | AnnotationFragmentManager.removeFromActivity(getActivity(), fragment); 120 | } 121 | 122 | @NonNull 123 | @Override 124 | protected Class canManagerInternal() { 125 | return RFragment.class; 126 | } 127 | 128 | private Activity getActivity() { 129 | return getAndroidUnitTest().activity().get(); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/managers/AnnotationViewManager.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.managers; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.support.annotation.VisibleForTesting; 6 | import android.view.View; 7 | 8 | import com.github.florent37.androidunittest.AndroidUnitTest; 9 | import com.github.florent37.androidunittest.annotations.RView; 10 | 11 | import org.mockito.Mockito; 12 | import org.mockito.internal.util.reflection.FieldSetter; 13 | 14 | import java.lang.annotation.Annotation; 15 | import java.lang.reflect.Field; 16 | import java.util.HashSet; 17 | import java.util.Set; 18 | 19 | /** 20 | * Created by kevinleperf on 18/05/16. 21 | */ 22 | public class AnnotationViewManager extends AbstractAnnotationManager { 23 | 24 | @VisibleForTesting Set fields; 25 | 26 | public AnnotationViewManager(AndroidUnitTest androidUnitTest) { 27 | super(androidUnitTest); 28 | 29 | fields = new HashSet<>(); 30 | } 31 | 32 | @Override 33 | public void scanned(@NonNull Field field) { 34 | fields.add(field); 35 | } 36 | 37 | @Override 38 | public void execute(@NonNull Object object, @NonNull Context context) { 39 | for (Field view : fields) { 40 | initView(object, context, view); 41 | } 42 | } 43 | 44 | @NonNull 45 | @Override 46 | protected Class canManagerInternal() { 47 | return RView.class; 48 | } 49 | 50 | private void initView(@NonNull Object target, @NonNull Context context, @NonNull Field viewField) { 51 | Class viewClass = viewField.getType(); 52 | View view = null; 53 | try { 54 | view = (View) viewClass.getDeclaredConstructor(Context.class).newInstance(context); 55 | } catch (Exception e) { 56 | e.printStackTrace(); 57 | } 58 | view = Mockito.spy(view); 59 | new FieldSetter(target, viewField).set(view); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /androidunittest/src/main/java/com/github/florent37/androidunittest/states/ActivityState.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.states; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | /** 6 | * Created by kevinleperf on 14/05/16. 7 | *

8 | * A ActivityState describe the initial state of an activity at binding 9 | */ 10 | public enum ActivityState { 11 | //reverse ordered to prevent illegal forward reference 12 | DESTROYED(null), 13 | STOPPED(ActivityState.DESTROYED), 14 | PAUSED(ActivityState.STOPPED), 15 | RESUMED(ActivityState.PAUSED), 16 | STARTED(ActivityState.RESUMED), 17 | CREATED(ActivityState.STARTED); 18 | 19 | 20 | private ActivityState mNext; 21 | 22 | ActivityState(ActivityState next) { 23 | mNext = next; 24 | } 25 | 26 | public ActivityState next() { 27 | return mNext; 28 | } 29 | 30 | /** 31 | * Check wether the given ActivityState is before or at least the same element as this instance 32 | * 33 | * commonly used to check if the previous state can be call 34 | * for instance, checking if Resume can be used before Paused, it will returns true 35 | * whereas, testing destroy against resume will return false 36 | * @param state 37 | * @return 38 | */ 39 | public boolean isLowerOrEquals(@NonNull ActivityState state) { 40 | return ordinal() >= state.ordinal(); //since we are in the reverse order 41 | //it is not <= but => 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /androidunittest/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AndroidUnitTest 3 | 4 | -------------------------------------------------------------------------------- /androidunittest/src/test/java/com/github/florent37/androidunittest/managers/AnnotationActivityManagerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.managers; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import android.content.Context; 6 | import android.os.Bundle; 7 | 8 | import com.github.florent37.androidunittest.AndroidUnitTest; 9 | import com.github.florent37.androidunittest.annotations.RActivity; 10 | import com.github.florent37.androidunittest.controllers.ControllerActivity; 11 | import com.github.florent37.androidunittest.states.ActivityState; 12 | 13 | import org.junit.Before; 14 | import org.junit.Rule; 15 | import org.junit.Test; 16 | import org.junit.runner.RunWith; 17 | import org.mockito.InjectMocks; 18 | import org.mockito.Mock; 19 | import org.mockito.junit.MockitoJUnit; 20 | import org.mockito.junit.MockitoRule; 21 | import org.robolectric.RobolectricTestRunner; 22 | import org.robolectric.annotation.Config; 23 | import org.robolectric.util.ActivityController; 24 | 25 | import java.lang.reflect.Field; 26 | 27 | import static com.google.common.truth.Truth.assertThat; 28 | import static org.mockito.BDDMockito.given; 29 | import static org.mockito.Matchers.any; 30 | import static org.mockito.Mockito.verify; 31 | 32 | /** 33 | * Created by florentchampigny on 19/05/2016. 34 | */ 35 | @RunWith(RobolectricTestRunner.class) 36 | @Config(manifest = Config.NONE) 37 | public class AnnotationActivityManagerTest { 38 | 39 | @Rule public MockitoRule rule = MockitoJUnit.rule(); 40 | 41 | @Mock AndroidUnitTest androidUnitTest; 42 | ControllerActivity controllerActivity; 43 | @Mock Activity activity; 44 | @InjectMocks AnnotationActivityManager manager; 45 | 46 | @Before 47 | public void setUp() throws Exception { 48 | controllerActivity = new ControllerActivity(androidUnitTest); 49 | given(androidUnitTest.activity()).willReturn(controllerActivity); 50 | } 51 | 52 | @Test 53 | public void testCanManagerInternal() throws Exception { 54 | // Given 55 | // When 56 | Object canManage = manager.canManagerInternal(); 57 | // Then 58 | assertThat(canManage).isNotNull(); 59 | assertThat(canManage).isEqualTo(RActivity.class); 60 | } 61 | 62 | @Test 63 | public void testScanned() throws Exception { 64 | // Given 65 | TestObject testObject = new TestObject(); 66 | Field field = testObject.getClass().getDeclaredField("activity"); 67 | // When 68 | manager.scanned(field); 69 | // Then 70 | assertThat(manager.activityField).isEqualTo(field); 71 | } 72 | 73 | @Test 74 | public void testExecute() throws Exception { 75 | // Given 76 | TestObject testObject = new TestObject(); 77 | Context context = new Application(); 78 | manager.activityField = testObject.getClass().getDeclaredField("activity"); 79 | // When 80 | manager.execute(testObject, context); 81 | //Then 82 | assertThat(testObject.activity).isNotNull(); 83 | assertThat(testObject.activity.created).isTrue(); 84 | verify(androidUnitTest).setActivityController(any(ActivityController.class)); 85 | } 86 | 87 | @Test 88 | public void testExecuteState() throws Exception { 89 | // Given 90 | TestObjectState testObject = new TestObjectState(); 91 | Context context = new Application(); 92 | manager.activityField = testObject.getClass().getDeclaredField("activity"); 93 | // When 94 | manager.execute(testObject, context); 95 | //Then 96 | assertThat(testObject.activity).isNotNull(); 97 | assertThat(testObject.activity.created).isTrue(); 98 | assertThat(testObject.activity.started).isTrue(); 99 | assertThat(testObject.activity.resumed).isTrue(); 100 | verify(androidUnitTest).setActivityController(any(ActivityController.class)); 101 | } 102 | 103 | static class TestObject { 104 | @RActivity TestActivity activity; 105 | } 106 | 107 | static class TestObjectState { 108 | @RActivity(state = ActivityState.RESUMED) TestActivity activity; 109 | } 110 | 111 | static class TestActivity extends Activity { 112 | boolean created = false; 113 | boolean resumed = false; 114 | boolean started = false; 115 | 116 | @Override 117 | protected void onCreate(Bundle savedInstanceState) { 118 | super.onCreate(savedInstanceState); 119 | created = true; 120 | } 121 | 122 | @Override 123 | protected void onStart() { 124 | super.onStart(); 125 | started = true; 126 | } 127 | 128 | @Override 129 | protected void onResume() { 130 | super.onResume(); 131 | resumed = true; 132 | } 133 | } 134 | } -------------------------------------------------------------------------------- /androidunittest/src/test/java/com/github/florent37/androidunittest/managers/AnnotationContextManagerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.managers; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | 6 | import com.github.florent37.androidunittest.AndroidUnitTest; 7 | import com.github.florent37.androidunittest.annotations.RContext; 8 | 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.mockito.InjectMocks; 12 | import org.mockito.Mock; 13 | import org.mockito.runners.MockitoJUnitRunner; 14 | 15 | import java.lang.reflect.Field; 16 | 17 | import static com.google.common.truth.Truth.assertThat; 18 | 19 | /** 20 | * Created by florentchampigny on 19/05/2016. 21 | */ 22 | @RunWith(MockitoJUnitRunner.class) 23 | public class AnnotationContextManagerTest { 24 | 25 | @Mock AndroidUnitTest androidUnitTest; 26 | @InjectMocks AnnotationContextManager manager; 27 | 28 | @Test 29 | public void testCanManagerInternal() throws Exception { 30 | // Given 31 | // When 32 | Object canManage = manager.canManagerInternal(); 33 | // Then 34 | assertThat(canManage).isNotNull(); 35 | assertThat(canManage).isEqualTo(RContext.class); 36 | } 37 | 38 | @Test 39 | public void testScanned() throws Exception { 40 | // Given 41 | TestObject testObject = new TestObject(); 42 | Field field = testObject.getClass().getDeclaredField("context"); 43 | // When 44 | manager.scanned(field); 45 | // Then 46 | assertThat(manager.contextField).isEqualTo(field); 47 | } 48 | 49 | @Test 50 | public void testExecute() throws Exception { 51 | // Given 52 | TestObject testObject = new TestObject(); 53 | Context context = new Application(); 54 | manager.contextField = testObject.getClass().getDeclaredField("context"); 55 | // When 56 | manager.execute(testObject, context); 57 | //Then 58 | assertThat(testObject.context).isNotNull(); 59 | } 60 | 61 | static class TestObject { 62 | @RContext Context context; 63 | } 64 | } -------------------------------------------------------------------------------- /androidunittest/src/test/java/com/github/florent37/androidunittest/managers/AnnotationFragmentManagerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.managers; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v4.app.FragmentActivity; 7 | 8 | import com.github.florent37.androidunittest.AndroidUnitTest; 9 | import com.github.florent37.androidunittest.annotations.RFragment; 10 | 11 | import org.junit.Before; 12 | import org.junit.Rule; 13 | import org.junit.Test; 14 | import org.junit.runner.RunWith; 15 | import org.mockito.junit.MockitoJUnit; 16 | import org.mockito.junit.MockitoRule; 17 | import org.robolectric.Robolectric; 18 | import org.robolectric.RobolectricTestRunner; 19 | import org.robolectric.annotation.Config; 20 | 21 | import java.lang.reflect.Field; 22 | 23 | import static com.google.common.truth.Truth.assertThat; 24 | 25 | /** 26 | * Created by florentchampigny on 19/05/2016. 27 | */ 28 | @RunWith(RobolectricTestRunner.class) 29 | @Config(manifest = Config.NONE) 30 | public class AnnotationFragmentManagerTest { 31 | 32 | @Rule public MockitoRule rule = MockitoJUnit.rule(); 33 | 34 | AndroidUnitTest androidUnitTest; 35 | AnnotationFragmentManager manager; 36 | 37 | @Before 38 | public void setUp() throws Exception { 39 | androidUnitTest = AndroidUnitTest.rule(); 40 | manager = new AnnotationFragmentManager(androidUnitTest); 41 | } 42 | 43 | @Test 44 | public void testCanManagerInternal() throws Exception { 45 | // Given 46 | // When 47 | Object canManage = manager.canManagerInternal(); 48 | // Then 49 | assertThat(canManage).isNotNull(); 50 | assertThat(canManage).isEqualTo(RFragment.class); 51 | } 52 | 53 | @Test 54 | public void testScanned() throws Exception { 55 | // Given 56 | TestObject testObject = new TestObject(); 57 | Field field = testObject.getClass().getDeclaredField("fragment1"); 58 | // When 59 | manager.scanned(field); 60 | // Then 61 | assertThat(manager.fragmentFields).contains(field); 62 | } 63 | 64 | @Test 65 | public void testExecute() throws Exception { 66 | // Given 67 | TestObject testObject = new TestObject(); 68 | Context context = new Application(); 69 | manager.fragmentFields.add(testObject.getClass().getDeclaredField("fragment1")); 70 | manager.fragmentFields.add(testObject.getClass().getDeclaredField("fragment2")); 71 | // When 72 | manager.execute(testObject, context); 73 | //Then 74 | assertThat(testObject.fragment1).isNotNull(); 75 | assertThat(testObject.fragment2).isNotNull(); 76 | } 77 | 78 | @Test 79 | public void testAddToActivity() throws Exception { 80 | // Given 81 | TestObject target = new TestObject(); 82 | target.fragment1 = new Fragment(); 83 | // When 84 | manager.addToActivity(target, target.fragment1); 85 | // Then 86 | assertThat(target.fragment1.getActivity()).isNotNull(); 87 | } 88 | 89 | @Test 90 | public void testRemoveFromActivity() throws Exception { 91 | // Given 92 | TestObject target = new TestObject(); 93 | target.fragment1 = new Fragment(); 94 | manager.addToActivity(target, target.fragment1); 95 | // When 96 | manager.removeFromActivity(target.fragment1); 97 | // Then 98 | assertThat(target.fragment1.getActivity()).isNull(); 99 | } 100 | 101 | @Test 102 | public void testAddToActivityWithTag() throws Exception { 103 | // Given 104 | TestObject target = new TestObject(); 105 | FragmentActivity activity = Robolectric.buildActivity(FragmentActivity.class).create().get(); 106 | target.fragment1 = new Fragment(); 107 | String tag = "TAG"; 108 | // When 109 | AnnotationFragmentManager.addToActivity(activity, target.fragment1, tag); 110 | // Then 111 | assertThat(target.fragment1.getActivity()).isNotNull(); 112 | } 113 | 114 | static class TestObject { 115 | @RFragment Fragment fragment1; 116 | @RFragment Fragment fragment2; 117 | } 118 | 119 | } -------------------------------------------------------------------------------- /androidunittest/src/test/java/com/github/florent37/androidunittest/managers/AnnotationViewManagerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest.managers; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | import android.view.View; 6 | import android.widget.TextView; 7 | 8 | import com.github.florent37.androidunittest.AndroidUnitTest; 9 | import com.github.florent37.androidunittest.annotations.RView; 10 | 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | import org.mockito.InjectMocks; 14 | import org.mockito.Mock; 15 | import org.mockito.runners.MockitoJUnitRunner; 16 | 17 | import java.lang.reflect.Field; 18 | 19 | import static com.google.common.truth.Truth.assertThat; 20 | 21 | /** 22 | * Created by florentchampigny on 19/05/2016. 23 | */ 24 | @RunWith(MockitoJUnitRunner.class) 25 | public class AnnotationViewManagerTest { 26 | 27 | @Mock AndroidUnitTest androidUnitTest; 28 | @InjectMocks AnnotationViewManager manager; 29 | 30 | @Test 31 | public void testCanManagerInternal() throws Exception { 32 | // Given 33 | // When 34 | Object canManage = manager.canManagerInternal(); 35 | // Then 36 | assertThat(canManage).isNotNull(); 37 | assertThat(canManage).isEqualTo(RView.class); 38 | } 39 | 40 | @Test 41 | public void testScanned() throws Exception { 42 | // Given 43 | TestObject testObject = new TestObject(); 44 | Field field = testObject.getClass().getDeclaredField("view1"); 45 | // When 46 | manager.scanned(field); 47 | // Then 48 | assertThat(manager.fields).contains(field); 49 | } 50 | 51 | @Test 52 | public void testExecute() throws Exception { 53 | // Given 54 | TestObject testObject = new TestObject(); 55 | Context context = new Application(); 56 | manager.fields.add(testObject.getClass().getDeclaredField("view1")); 57 | manager.fields.add(testObject.getClass().getDeclaredField("view2")); 58 | // When 59 | manager.execute(testObject, context); 60 | //Then 61 | assertThat(testObject.view1).isNotNull(); 62 | assertThat(testObject.view2).isNotNull(); 63 | } 64 | 65 | static class TestObject { 66 | @RView View view1; 67 | @RView TextView view2; 68 | } 69 | } -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion project.sdk 5 | buildToolsVersion project.buildTools 6 | 7 | defaultConfig { 8 | applicationId "com.github.florent37.testsample" 9 | minSdkVersion project.minSdk 10 | targetSdkVersion project.sdk 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile 'com.android.support:appcompat-v7:'+project.supportVersion 25 | compile 'com.android.support:support-annotations:'+project.supportVersion 26 | 27 | testCompile project(":androidunittest") 28 | testCompile 'com.google.truth:truth:0.28' 29 | } 30 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/florentchampigny/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/github/florent37/androidunittest/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.androidunittest; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/florent37/testsample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.testsample; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.widget.TextView; 6 | 7 | import com.github.florent37.testsample.model.User; 8 | 9 | public class MainActivity extends AppCompatActivity { 10 | 11 | TextView textView; 12 | 13 | boolean created = false; 14 | boolean resumed = false; 15 | boolean started = false; 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_main); 21 | 22 | this.textView = (TextView) findViewById(R.id.textView); 23 | created = true; 24 | } 25 | 26 | @Override 27 | protected void onStart() { 28 | super.onStart(); 29 | started = true; 30 | } 31 | 32 | @Override 33 | protected void onResume() { 34 | super.onResume(); 35 | resumed = true; 36 | } 37 | 38 | public void display(User user) { 39 | this.textView.setText(user.getName()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/florent37/testsample/MainFragment.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.testsample; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | /** 11 | * Created by florentchampigny on 08/05/2016. 12 | */ 13 | public class MainFragment extends Fragment{ 14 | 15 | @Nullable 16 | @Override 17 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 18 | return inflater.inflate(R.layout.fragment_main, container, false); 19 | } 20 | 21 | @Override 22 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 23 | super.onViewCreated(view, savedInstanceState); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/florent37/testsample/MainView.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.testsample; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.widget.FrameLayout; 6 | 7 | import com.github.florent37.testsample.model.User; 8 | 9 | /** 10 | * Created by florentchampigny on 08/05/2016. 11 | */ 12 | public class MainView extends FrameLayout { 13 | public MainView(Context context) { 14 | this(context, null); 15 | } 16 | 17 | public MainView(Context context, AttributeSet attrs) { 18 | this(context, attrs, 0); 19 | } 20 | 21 | public MainView(Context context, AttributeSet attrs, int defStyleAttr) { 22 | super(context, attrs, defStyleAttr); 23 | } 24 | 25 | protected void displayText(String text){ 26 | 27 | } 28 | 29 | public void display(User user){ 30 | displayText(user.getName()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/florent37/testsample/MyApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.testsample; 2 | 3 | import android.app.Application; 4 | 5 | /** 6 | * Created by florentchampigny on 07/05/2016. 7 | */ 8 | public class MyApplication extends Application { 9 | } 10 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/florent37/testsample/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.testsample.model; 2 | 3 | /** 4 | * Created by florentchampigny on 08/05/2016. 5 | */ 6 | public class User { 7 | String name; 8 | 9 | public String getName() { 10 | return name; 11 | } 12 | 13 | public void setName(String name) { 14 | this.name = name; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/AndroidUnitTest/9f6e63d6cb5e3ae22ffca36d412096b63fa9d634/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/AndroidUnitTest/9f6e63d6cb5e3ae22ffca36d412096b63fa9d634/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/AndroidUnitTest/9f6e63d6cb5e3ae22ffca36d412096b63fa9d634/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/AndroidUnitTest/9f6e63d6cb5e3ae22ffca36d412096b63fa9d634/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/AndroidUnitTest/9f6e63d6cb5e3ae22ffca36d412096b63fa9d634/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AndroidUnitTest 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/github/florent37/testsample/BasicActivityTest.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.testsample; 2 | 3 | import android.content.Context; 4 | 5 | import com.github.florent37.androidunittest.AndroidUnitTest; 6 | import com.github.florent37.androidunittest.annotations.RActivity; 7 | import com.github.florent37.androidunittest.annotations.RContext; 8 | import com.github.florent37.androidunittest.annotations.RView; 9 | import com.github.florent37.testsample.model.User; 10 | 11 | import org.junit.Rule; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | import org.mockito.Mock; 15 | 16 | import static com.google.common.truth.Truth.assertThat; 17 | 18 | @RunWith(CustomTestRunner.class) 19 | public class BasicActivityTest { 20 | @Rule public AndroidUnitTest androidUnitTest = AndroidUnitTest.rule(); 21 | 22 | @RContext Context context; 23 | @RActivity MainActivity activity; 24 | @RView MainView view; 25 | 26 | @Mock User user; 27 | 28 | @Test 29 | public void testAnnotations() throws Exception { 30 | assertThat(context).isNotNull(); 31 | assertThat(activity).isNotNull(); 32 | assertThat(view).isNotNull(); 33 | assertThat(user).isNotNull(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /app/src/test/java/com/github/florent37/testsample/CustomTestRunner.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.testsample; 2 | 3 | import com.github.florent37.androidunittest.AndroidUnitTestRunner; 4 | 5 | import org.junit.runners.model.InitializationError; 6 | 7 | /** 8 | * Created by florentchampigny on 07/05/2016. 9 | */ 10 | public class CustomTestRunner extends AndroidUnitTestRunner { 11 | public CustomTestRunner(Class testClass) throws InitializationError { 12 | super(testClass, BuildConfig.FLAVOR, BuildConfig.BUILD_TYPE, BuildConfig.APPLICATION_ID, TestMyApplication.class); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /app/src/test/java/com/github/florent37/testsample/MainActivityTest.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.testsample; 2 | 3 | import com.github.florent37.androidunittest.AndroidUnitTest; 4 | import com.github.florent37.androidunittest.annotations.RActivity; 5 | import com.github.florent37.testsample.model.User; 6 | 7 | import org.junit.Rule; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.mockito.Mock; 11 | 12 | import static com.google.common.truth.Truth.assertThat; 13 | import static org.mockito.BDDMockito.given; 14 | 15 | @RunWith(CustomTestRunner.class) 16 | public class MainActivityTest { 17 | @Rule public AndroidUnitTest androidUnitTest = AndroidUnitTest.rule(); 18 | 19 | @RActivity MainActivity activity; 20 | 21 | @Mock User user; 22 | 23 | @Test 24 | public void testDisplayUser() throws Exception { 25 | // Given 26 | given(user.getName()).willReturn("florent"); 27 | // When 28 | activity.display(user); 29 | // Then 30 | assertThat(activity.textView.getText()).isEqualTo("florent"); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /app/src/test/java/com/github/florent37/testsample/MainFragmentTest.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.testsample; 2 | 3 | import android.content.Context; 4 | import android.support.v4.app.FragmentActivity; 5 | 6 | import com.github.florent37.androidunittest.AndroidUnitTest; 7 | import com.github.florent37.androidunittest.annotations.RActivity; 8 | import com.github.florent37.androidunittest.annotations.RContext; 9 | import com.github.florent37.androidunittest.annotations.RFragment; 10 | 11 | import org.junit.Rule; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | 15 | import static com.google.common.truth.Truth.assertThat; 16 | import static org.mockito.Mockito.spy; 17 | 18 | @RunWith(CustomTestRunner.class) 19 | public class MainFragmentTest { 20 | @Rule public AndroidUnitTest androidUnitTest = AndroidUnitTest.rule(); 21 | 22 | @RContext Context context; 23 | @RFragment(attached = false) MainFragment fragment; 24 | 25 | @Test 26 | public void testAnnotations() throws Exception { 27 | assertThat(context).isNotNull(); 28 | assertThat(fragment).isNotNull(); 29 | } 30 | 31 | @Test 32 | public void testAttach() throws Exception { 33 | // Given 34 | assertThat(fragment.getActivity()).isNull(); 35 | 36 | // When 37 | androidUnitTest.fragment().attachToActivity(fragment); 38 | 39 | // Then 40 | assertThat(fragment.getActivity()).isNotNull(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /app/src/test/java/com/github/florent37/testsample/MainFragmentWithActivityTest.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.testsample; 2 | 3 | import android.content.Context; 4 | import android.support.v4.app.FragmentActivity; 5 | 6 | import com.github.florent37.androidunittest.AndroidUnitTest; 7 | import com.github.florent37.androidunittest.annotations.RActivity; 8 | import com.github.florent37.androidunittest.annotations.RContext; 9 | import com.github.florent37.androidunittest.annotations.RFragment; 10 | 11 | import org.junit.Rule; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | 15 | import static com.google.common.truth.Truth.assertThat; 16 | 17 | @RunWith(CustomTestRunner.class) 18 | public class MainFragmentWithActivityTest { 19 | @Rule public AndroidUnitTest androidUnitTest = AndroidUnitTest.rule(); 20 | 21 | @RContext Context context; 22 | @RActivity FragmentActivity fragmentActivity; 23 | @RFragment(attached = false, tag = "monFragment") MainFragment fragment; 24 | 25 | @Test 26 | public void testAnnotations() throws Exception { 27 | assertThat(context).isNotNull(); 28 | assertThat(fragment).isNotNull(); 29 | } 30 | 31 | @Test 32 | public void testAttach() throws Exception { 33 | // Given 34 | assertThat(fragment.getActivity()).isNull(); 35 | 36 | // When 37 | androidUnitTest.fragment().attachToActivity(fragment); 38 | 39 | // Then 40 | assertThat(fragment.getActivity()).isNotNull(); 41 | assertThat(fragmentActivity.getSupportFragmentManager().findFragmentByTag("monFragment")).isNotNull(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /app/src/test/java/com/github/florent37/testsample/MainViewTest.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.testsample; 2 | 3 | import android.content.Context; 4 | 5 | import com.github.florent37.androidunittest.AndroidUnitTest; 6 | import com.github.florent37.androidunittest.annotations.RContext; 7 | import com.github.florent37.androidunittest.annotations.RFragment; 8 | import com.github.florent37.androidunittest.annotations.RView; 9 | import com.github.florent37.testsample.model.User; 10 | 11 | import org.junit.Rule; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | import org.mockito.Mock; 15 | 16 | import static com.google.common.truth.Truth.assertThat; 17 | import static org.mockito.BDDMockito.given; 18 | import static org.mockito.Mockito.verify; 19 | 20 | @RunWith(CustomTestRunner.class) 21 | public class MainViewTest { 22 | @Rule public AndroidUnitTest androidUnitTest = AndroidUnitTest.rule(); 23 | 24 | @RView MainView mainView; 25 | @Mock User user; 26 | 27 | @Test 28 | public void testDisplayUser() throws Exception { 29 | // Given 30 | given(user.getName()).willReturn("florent"); 31 | // When 32 | mainView.display(user); 33 | // Then 34 | verify(mainView).displayText("florent"); 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /app/src/test/java/com/github/florent37/testsample/StateActivityTest.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.testsample; 2 | 3 | import com.github.florent37.androidunittest.AndroidUnitTest; 4 | import com.github.florent37.androidunittest.annotations.RActivity; 5 | import com.github.florent37.androidunittest.states.ActivityState; 6 | 7 | import org.junit.Rule; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static com.google.common.truth.Truth.assertThat; 12 | 13 | @RunWith(CustomTestRunner.class) 14 | public class StateActivityTest { 15 | @Rule public AndroidUnitTest androidUnitTest = AndroidUnitTest.rule(); 16 | 17 | @RActivity(state = ActivityState.STARTED) MainActivity activity; 18 | 19 | @Test 20 | public void testAnnotations() throws Exception { 21 | assertThat(activity.created).isTrue(); 22 | assertThat(activity.started).isTrue(); 23 | assertThat(activity.resumed).isFalse(); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /app/src/test/java/com/github/florent37/testsample/TestMyApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.florent37.testsample; 2 | 3 | /** 4 | * Created by florentchampigny on 07/05/2016. 5 | */ 6 | public class TestMyApplication extends MyApplication { 7 | } 8 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.1.0' 9 | classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1" 10 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' 11 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0' 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | ext { 19 | sdk = 23 20 | buildTools = "23.0.3" 21 | minSdk = 10 22 | libraryVersion = "1.0.1" 23 | supportVersion = "23.3.0" 24 | } 25 | 26 | allprojects { 27 | repositories { 28 | jcenter() 29 | } 30 | } 31 | 32 | task clean(type: Delete) { 33 | delete rootProject.buildDir 34 | } 35 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/bintray-android-v1.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.jfrog.bintray' 2 | 3 | version = libraryVersion 4 | 5 | task sourcesJar(type: Jar) { 6 | from android.sourceSets.main.java.srcDirs 7 | classifier = 'sources' 8 | } 9 | 10 | task javadoc(type: Javadoc) { 11 | source = android.sourceSets.main.java.srcDirs 12 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 13 | } 14 | 15 | task javadocJar(type: Jar, dependsOn: javadoc) { 16 | classifier = 'javadoc' 17 | from javadoc.destinationDir 18 | } 19 | artifacts { 20 | archives javadocJar 21 | archives sourcesJar 22 | } 23 | 24 | // Bintray 25 | Properties properties = new Properties() 26 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 27 | 28 | bintray { 29 | user = properties.getProperty("bintray.user") 30 | key = properties.getProperty("bintray.apikey") 31 | 32 | configurations = ['archives'] 33 | pkg { 34 | repo = bintrayRepo 35 | name = bintrayName 36 | desc = libraryDescription 37 | userOrg = orgName 38 | websiteUrl = siteUrl 39 | vcsUrl = gitUrl 40 | licenses = allLicenses 41 | publish = true 42 | publicDownloadNumbers = true 43 | version { 44 | desc = libraryDescription 45 | gpg { 46 | sign = true //Determines whether to GPG sign the files. The default is false 47 | passphrase = properties.getProperty("bintray.gpg.password") 48 | //Optional. The passphrase for GPG signing' 49 | } 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /gradle/bintray-java-v1.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.jfrog.bintray' 2 | 3 | version = libraryVersion 4 | 5 | task sourcesJar(type: Jar) { 6 | from sourceSets.main.allSource 7 | classifier = 'sources' 8 | } 9 | 10 | task javadocJar(type: Jar, dependsOn: javadoc) { 11 | classifier = 'javadoc' 12 | from javadoc.destinationDir 13 | } 14 | artifacts { 15 | archives javadocJar 16 | archives sourcesJar 17 | } 18 | 19 | // Bintray 20 | Properties properties = new Properties() 21 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 22 | 23 | bintray { 24 | user = properties.getProperty("bintray.user") 25 | key = properties.getProperty("bintray.apikey") 26 | 27 | configurations = ['archives'] 28 | pkg { 29 | repo = bintrayRepo 30 | name = bintrayName 31 | desc = libraryDescription 32 | userOrg = orgName 33 | websiteUrl = siteUrl 34 | vcsUrl = gitUrl 35 | licenses = ['Apache-2.0'] 36 | publish = true 37 | publicDownloadNumbers = true 38 | version { 39 | desc = libraryDescription 40 | gpg { 41 | sign = true //Determines whether to GPG sign the files. The default is false 42 | passphrase = properties.getProperty("bintray.gpg.password") 43 | //Optional. The passphrase for GPG signing' 44 | } 45 | } 46 | } 47 | } 48 | 49 | //from https://github.com/workarounds/bundler/blob/master/gradle/bintray-java-v1.gradle -------------------------------------------------------------------------------- /gradle/install-v1.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.github.dcendents.android-maven' 2 | 3 | group = publishedGroupId // Maven Group ID for the artifact 4 | 5 | install { 6 | repositories.mavenInstaller { 7 | // This generates POM.xml with proper parameters 8 | pom { 9 | project { 10 | packaging 'aar' 11 | groupId publishedGroupId 12 | artifactId artifact 13 | 14 | // Add your description here 15 | name libraryName 16 | description libraryDescription 17 | url siteUrl 18 | 19 | // Set your license 20 | licenses { 21 | license { 22 | name licenseName 23 | url licenseUrl 24 | } 25 | } 26 | developers { 27 | developer { 28 | id developerId 29 | name developerName 30 | email developerEmail 31 | } 32 | } 33 | scm { 34 | connection gitUrl 35 | developerConnection gitUrl 36 | url siteUrl 37 | 38 | } 39 | } 40 | } 41 | } 42 | } 43 | 44 | //from https://github.com/workarounds/bundler/blob/master/gradle/install-v1.gradle -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florent37/AndroidUnitTest/9f6e63d6cb5e3ae22ffca36d412096b63fa9d634/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':androidunittest' 2 | --------------------------------------------------------------------------------