-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add robolectric basic sample #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fbertault
wants to merge
10
commits into
android:main
Choose a base branch
from
fbertault:add_robolectric_basic_sample
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
61e7e45
Add robolectric basic sample
fbertault 9fb578a
Add robolectric basic sample
fbertault 9a7c29b
Removed unused deps.
fbertault a2d5df5
Fix robolectricVersion
fbertault e6921ad
Gradle changes, using android 3.0+ config.
fbertault 0859b12
Addressed comments.
fbertault 4518de9
Moved to unit/BasicRobolectricSample + addressed comments.
fbertault 2d8dda1
Moved to unit/BasicRobolectricSample + addressed comments.
fbertault 93aa0cd
Moved to unit/BasicRobolectricSample + addressed comments.
fbertault 9f9f26b
Addressed comments, updated versions and launcher icon.
fbertault File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| .gradle | ||
| local.properties | ||
| .idea | ||
| .DS_Store | ||
| build | ||
| *.iml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Basic sample for Robolectric | ||
|
|
||
| *If you are new to Robolectric, try this sample first.* | ||
|
|
||
| This project uses the Gradle build system. You don't need an IDE to build and execute it but Android Studio is recommended. | ||
|
|
||
| 1. Download the project code, preferably using `git clone`. | ||
| 1. In Android Studio, select *File* | *Open...* and point to the `./build.gradle` file. | ||
| 1. Check out the relevant code: | ||
| * The application under test is located in `src/main/java` | ||
| * Tests are in `src/test/java` | ||
| 1. Create the test configuration: | ||
| * Open *Run* menu | *Edit Configurations* | ||
| * Add a new *Android JUnit Tests* configuration | ||
| * Choose a module | ||
| * Set the working directory to the module directory. | ||
| 1. Run the newly created configuration | ||
|
|
||
| If you are using Android Studio, the *Run* window will show the test results. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| apply plugin: 'com.android.application' | ||
|
|
||
| android { | ||
| compileSdkVersion 26 | ||
| buildToolsVersion rootProject.buildToolsVersion | ||
| defaultConfig { | ||
| applicationId "com.example.android.testing.robolectric.BasicRobolectricSample" | ||
| minSdkVersion 14 | ||
| targetSdkVersion 26 | ||
| versionCode 1 | ||
| versionName "1.0" | ||
| } | ||
| lintOptions { | ||
| abortOnError false | ||
| } | ||
|
|
||
| // Include resources in Robolectric tests (requires Android Studio 3.0+). | ||
| testOptions { | ||
| unitTests { | ||
| includeAndroidResources = true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation 'com.google.guava:guava:20.0' | ||
|
|
||
| // Testing-only dependencies | ||
| testImplementation "junit:junit:4.12" | ||
|
|
||
| // Robolectric dependencies | ||
| testImplementation "org.robolectric:robolectric:" + rootProject.robolectricVersion | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
unit/BasicRobolectricSample/app/src/main/AndroidManifest.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- | ||
| ~ Copyright (C) 2017 The Android Open Source Project | ||
| ~ | ||
| ~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
| ~ you may not use this file except in compliance with the License. | ||
| ~ You may obtain a copy of the License at | ||
| ~ | ||
| ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| ~ | ||
| ~ Unless required by applicable law or agreed to in writing, software | ||
| ~ distributed under the License is distributed on an "AS IS" BASIS, | ||
| ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| ~ See the License for the specific language governing permissions and | ||
| ~ limitations under the License. | ||
| --> | ||
|
|
||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| package="com.example.android.testing.robolectric.BasicRobolectricSample" > | ||
|
|
||
| <application | ||
| android:icon="@drawable/ic_launcher" | ||
| android:label="@string/app_name" | ||
| android:theme="@style/AppTheme" > | ||
| <activity | ||
| android:name="com.example.android.testing.robolectric.BasicRobolectricSample.MainActivity" | ||
| android:label="@string/app_name" > | ||
| <intent-filter> | ||
| <action android:name="android.intent.action.MAIN" /> | ||
| <category android:name="android.intent.category.LAUNCHER" /> | ||
| </intent-filter> | ||
| </activity> | ||
| <activity android:name="com.example.android.testing.robolectric.BasicRobolectricSample.ShowTextActivity"/> | ||
| </application> | ||
|
|
||
| </manifest> |
69 changes: 69 additions & 0 deletions
69
...ain/java/com/example/android/testing/robolectric/BasicRobolectricSample/MainActivity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Copyright 2017, The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.android.testing.robolectric.BasicRobolectricSample; | ||
|
|
||
| import android.app.Activity; | ||
| import android.content.Intent; | ||
| import android.os.Bundle; | ||
| import android.view.View; | ||
| import android.widget.EditText; | ||
| import android.widget.TextView; | ||
|
|
||
| /** | ||
| * An {@link Activity} that gets a text string from the user and displays it back when the user | ||
| * clicks on one of the two buttons. The first one shows it in the same activity and the second | ||
| * one opens another activity and displays the message. | ||
| */ | ||
| public class MainActivity extends Activity implements View.OnClickListener { | ||
|
|
||
| // The TextView used to display the message inside the Activity. | ||
| private TextView mTextView; | ||
|
|
||
| // The EditText where the user types the message. | ||
| private EditText mEditText; | ||
|
|
||
| @Override | ||
| protected void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_main); | ||
|
|
||
| // Set the listeners for the buttons. | ||
| findViewById(R.id.changeTextBt).setOnClickListener(this); | ||
| findViewById(R.id.activityChangeTextBtn).setOnClickListener(this); | ||
|
|
||
| mTextView = findViewById(R.id.textToBeChanged); | ||
| mEditText = findViewById(R.id.editTextUserInput); | ||
| } | ||
|
|
||
| @Override | ||
| public void onClick(View view) { | ||
| // Get the text from the EditText view. | ||
| final String text = mEditText.getText().toString(); | ||
|
|
||
| switch (view.getId()) { | ||
| case R.id.changeTextBt: | ||
| // First button's interaction: set a text in a text view. | ||
| mTextView.setText(text); | ||
| break; | ||
| case R.id.activityChangeTextBtn: | ||
| // Second button's interaction: start an activity and send a message to it. | ||
| Intent intent = ShowTextActivity.newStartIntent(this, text); | ||
| startActivity(intent); | ||
| break; | ||
| } | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
...java/com/example/android/testing/robolectric/BasicRobolectricSample/ShowTextActivity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright 2017, The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.android.testing.robolectric.BasicRobolectricSample; | ||
|
|
||
| import com.google.common.base.Strings; | ||
|
|
||
| import android.app.Activity; | ||
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.os.Bundle; | ||
| import android.widget.TextView; | ||
|
|
||
| /** | ||
| * A simple {@link Activity} that shows a message. | ||
| */ | ||
| public class ShowTextActivity extends Activity { | ||
|
|
||
| // The name of the extra data sent through an {@link Intent}. | ||
| public final static String KEY_EXTRA_MESSAGE = | ||
| "com.example.android.testing.robolectric.basicsample.MESSAGE"; | ||
|
|
||
| @Override | ||
| protected void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_show_text); | ||
|
|
||
| // Get the message from the Intent. | ||
| Intent intent = getIntent(); | ||
| String message = Strings.nullToEmpty(intent.getStringExtra(KEY_EXTRA_MESSAGE)); | ||
|
|
||
| // Show message. | ||
| ((TextView)findViewById(R.id.show_text_view)).setText(message); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an {@link Intent} for {@link ShowTextActivity} with the message to be displayed. | ||
| * @param context the {@link Context} where the {@link Intent} will be used | ||
| * @param message a {@link String} with text to be displayed | ||
| * @return an {@link Intent} used to start {@link ShowTextActivity} | ||
| */ | ||
| static protected Intent newStartIntent(Context context, String message) { | ||
| Intent newIntent = new Intent(context, ShowTextActivity.class); | ||
| newIntent.putExtra(KEY_EXTRA_MESSAGE, message); | ||
| return newIntent; | ||
| } | ||
| } |
Binary file added
BIN
+4.03 KB
unit/BasicRobolectricSample/app/src/main/res/drawable-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.99 KB
unit/BasicRobolectricSample/app/src/main/res/drawable-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.28 KB
unit/BasicRobolectricSample/app/src/main/res/drawable-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+12 KB
unit/BasicRobolectricSample/app/src/main/res/drawable-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+19.3 KB
unit/BasicRobolectricSample/app/src/main/res/drawable-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions
60
unit/BasicRobolectricSample/app/src/main/res/layout/activity_main.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- | ||
| ~ Copyright (C) 2017 The Android Open Source Project | ||
| ~ | ||
| ~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
| ~ you may not use this file except in compliance with the License. | ||
| ~ You may obtain a copy of the License at | ||
| ~ | ||
| ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| ~ | ||
| ~ Unless required by applicable law or agreed to in writing, software | ||
| ~ distributed under the License is distributed on an "AS IS" BASIS, | ||
| ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| ~ See the License for the specific language governing permissions and | ||
| ~ limitations under the License. | ||
| --> | ||
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:tools="http://schemas.android.com/tools" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| android:orientation="vertical" | ||
| android:padding="@dimen/activity_horizontal_margin" | ||
| tools:context=".MainActivity"> | ||
|
|
||
|
|
||
| <TextView | ||
| android:id="@+id/textToBeChanged" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:layout_gravity="center_horizontal" | ||
| android:layout_marginBottom="@dimen/header_margin" | ||
| android:layout_marginTop="@dimen/header_margin" | ||
| android:text="@string/hello_world" | ||
| android:textAppearance="?android:attr/textAppearanceLarge"/> | ||
|
|
||
|
|
||
| <EditText | ||
| android:id="@+id/editTextUserInput" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:layout_gravity="center_horizontal" | ||
| android:hint="@string/type_something"/> | ||
|
|
||
| <Button | ||
| style="?android:attr/buttonStyleSmall" | ||
| android:id="@+id/changeTextBt" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="@string/change_text" | ||
| android:layout_gravity="center_horizontal"/> | ||
|
|
||
|
|
||
| <Button | ||
| style="?android:attr/buttonStyleSmall" | ||
| android:id="@+id/activityChangeTextBtn" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:layout_gravity="center_horizontal" | ||
| android:text="@string/open_activity_and_change_text"/> | ||
| </LinearLayout> |
30 changes: 30 additions & 0 deletions
30
unit/BasicRobolectricSample/app/src/main/res/layout/activity_show_text.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- | ||
| ~ Copyright (C) 2017 The Android Open Source Project | ||
| ~ | ||
| ~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
| ~ you may not use this file except in compliance with the License. | ||
| ~ You may obtain a copy of the License at | ||
| ~ | ||
| ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| ~ | ||
| ~ Unless required by applicable law or agreed to in writing, software | ||
| ~ distributed under the License is distributed on an "AS IS" BASIS, | ||
| ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| ~ See the License for the specific language governing permissions and | ||
| ~ limitations under the License. | ||
| --> | ||
| <merge xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:tools="http://schemas.android.com/tools" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| tools:context=".ShowTextActivity"> | ||
|
|
||
| <TextView | ||
| android:id="@+id/show_text_view" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="@string/hello_world" | ||
| android:layout_gravity="center"/> | ||
|
|
||
| </merge> |
19 changes: 19 additions & 0 deletions
19
unit/BasicRobolectricSample/app/src/main/res/values-v13/styles.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- | ||
| ~ Copyright (C) 2017 The Android Open Source Project | ||
| ~ | ||
| ~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
| ~ you may not use this file except in compliance with the License. | ||
| ~ You may obtain a copy of the License at | ||
| ~ | ||
| ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| ~ | ||
| ~ Unless required by applicable law or agreed to in writing, software | ||
| ~ distributed under the License is distributed on an "AS IS" BASIS, | ||
| ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| ~ See the License for the specific language governing permissions and | ||
| ~ limitations under the License. | ||
| --> | ||
| <resources> | ||
| <style name="AppTheme" parent="android:Theme.Holo.Light"/> | ||
| </resources> |
19 changes: 19 additions & 0 deletions
19
unit/BasicRobolectricSample/app/src/main/res/values-v21/styles.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- | ||
| ~ Copyright (C) 2017 The Android Open Source Project | ||
| ~ | ||
| ~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
| ~ you may not use this file except in compliance with the License. | ||
| ~ You may obtain a copy of the License at | ||
| ~ | ||
| ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| ~ | ||
| ~ Unless required by applicable law or agreed to in writing, software | ||
| ~ distributed under the License is distributed on an "AS IS" BASIS, | ||
| ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| ~ See the License for the specific language governing permissions and | ||
| ~ limitations under the License. | ||
| --> | ||
| <resources> | ||
| <style name="AppTheme" parent="android:Theme.Material"/> | ||
| </resources> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment to indicate that this is required in order for Robolectric to work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done