Skip to content

Commit 2c2c6c6

Browse files
committed
Update example projects
1 parent ef086f3 commit 2c2c6c6

34 files changed

Lines changed: 530 additions & 46 deletions

File tree

com.vogella.android.robolectric/app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ android {
55
buildToolsVersion "25.0.2"
66
defaultConfig {
77
applicationId "com.vogella.android.robolectric"
8-
minSdkVersion 14
8+
minSdkVersion 24
99
targetSdkVersion 25
1010
versionCode 1
1111
versionName "1.0"
@@ -24,7 +24,7 @@ dependencies {
2424
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
2525
exclude group: 'com.android.support', module: 'support-annotations'
2626
})
27-
testCompile 'junit:junit:4.12'
2827
testCompile "org.robolectric:robolectric:3.3.2"
29-
28+
testCompile 'junit:junit:4.12'
29+
compile 'com.android.support.constraint:constraint-layout:1.0.2'
3030
}

com.vogella.android.robolectric/app/proguard-rules.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Add project specific ProGuard rules here.
22
# By default, the flags in this file are appended to flags specified
3-
# in /home/david/Android/Sdk/tools/proguard/proguard-android.txt
3+
# in /home/vogella/Android/Sdk/tools/proguard/proguard-android.txt
44
# You can edit the include path and order by changing the proguardFiles
55
# directive in build.gradle.
66
#
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.vogella.android.robolectric;
2+
3+
import android.content.Context;
4+
import android.support.test.rule.ActivityTestRule;
5+
import android.support.test.runner.AndroidJUnit4;
6+
import android.util.DisplayMetrics;
7+
import android.widget.LinearLayout;
8+
import android.widget.TextView;
9+
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
14+
import static org.junit.Assert.assertEquals;
15+
16+
@RunWith(AndroidJUnit4.class)
17+
public class AndroidActivityTest {
18+
19+
@Rule
20+
public ActivityTestRule<RobolectricActivity> rule = new ActivityTestRule<RobolectricActivity>(RobolectricActivity.class);
21+
22+
@Test
23+
public void shouldHaveDefaultMargin() {
24+
RobolectricActivity activity = rule.getActivity();
25+
TextView textView = (TextView) activity.findViewById(R.id.hello_textview);
26+
27+
int bottomMargin = ((LinearLayout.LayoutParams) textView.getLayoutParams()).bottomMargin;
28+
assertEquals(dpToPx(activity.getApplicationContext(),5), bottomMargin);
29+
int topMargin = ((LinearLayout.LayoutParams) textView.getLayoutParams()).topMargin;
30+
assertEquals(dpToPx(activity.getApplicationContext(),5), topMargin);
31+
int rightMargin = ((LinearLayout.LayoutParams) textView.getLayoutParams()).rightMargin;
32+
assertEquals(dpToPx(activity.getApplicationContext(),10), rightMargin);
33+
int leftMargin = ((LinearLayout.LayoutParams) textView.getLayoutParams()).leftMargin;
34+
assertEquals(dpToPx(activity.getApplicationContext(),10) , leftMargin);
35+
}
36+
37+
public static int dpToPx(Context context, int dp) {
38+
int px = Math.round(dp * getPixelScaleFactor(context));
39+
return px;
40+
}
41+
private static float getPixelScaleFactor(Context context) {
42+
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
43+
return (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT);
44+
}
45+
46+
}

com.vogella.android.test.juntexamples/app/src/androidTest/java/com/vogella/android/test/juntexamples/ExampleInstrumentationTest.java renamed to com.vogella.android.robolectric/app/src/androidTest/java/com/vogella/android/robolectric/ExampleInstrumentedTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
package com.vogella.android.test.juntexamples;
1+
package com.vogella.android.robolectric;
22

33
import android.content.Context;
44
import android.support.test.InstrumentationRegistry;
5-
import android.support.test.filters.MediumTest;
65
import android.support.test.runner.AndroidJUnit4;
76

87
import org.junit.Test;
98
import org.junit.runner.RunWith;
109

11-
1210
import static org.junit.Assert.*;
1311

1412
/**
1513
* Instrumentation test, which will execute on an Android device.
1614
*
1715
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
1816
*/
19-
@MediumTest
2017
@RunWith(AndroidJUnit4.class)
21-
public class ExampleInstrumentationTest {
18+
public class ExampleInstrumentedTest {
2219
@Test
2320
public void useAppContext() throws Exception {
2421
// Context of the app under test.
2522
Context appContext = InstrumentationRegistry.getTargetContext();
2623

27-
assertEquals("com.vogella.android.test.juntexamples", appContext.getPackageName());
24+
assertEquals("com.vogella.android.robolectric", appContext.getPackageName());
2825
}
29-
}
26+
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.vogella.android.robolectric">
3+
package="com.vogella.android.robolectric">
44

55
<application
66
android:allowBackup="true"
@@ -9,13 +9,15 @@
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
1111
android:theme="@style/AppTheme">
12-
<activity android:name=".MainActivity">
12+
<activity android:name=".RobolectricActivity">
1313
<intent-filter>
14-
<action android:name="android.intent.action.MAIN" />
14+
<action android:name="android.intent.action.MAIN"/>
1515

16-
<category android:name="android.intent.category.LAUNCHER" />
16+
<category android:name="android.intent.category.LAUNCHER"/>
1717
</intent-filter>
1818
</activity>
19+
<activity android:name=".RobolectricSecondActivity">
20+
</activity>
1921
</application>
2022

2123
</manifest>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.vogella.android.robolectric;
2+
3+
import android.app.Activity;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
import android.view.View;
7+
import android.widget.Toast;
8+
9+
public class RobolectricActivity extends Activity {
10+
11+
@Override
12+
protected void onCreate(Bundle savedInstanceState) {
13+
super.onCreate(savedInstanceState);
14+
setContentView(R.layout.activity_robolectric);
15+
}
16+
17+
public void onClick(View view) {
18+
switch (view.getId()) {
19+
case R.id.startNextActivity:
20+
Intent intent = new Intent(this, RobolectricSecondActivity.class);
21+
startActivity(intent);
22+
break;
23+
case R.id.showToast:
24+
Toast.makeText(this, "Lala", Toast.LENGTH_SHORT).show();
25+
}
26+
27+
}
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.vogella.android.robolectric;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
6+
public class RobolectricSecondActivity extends Activity {
7+
8+
@Override
9+
protected void onCreate(Bundle savedInstanceState) {
10+
super.onCreate(savedInstanceState);
11+
setContentView(R.layout.activity_robolectric);
12+
}
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:orientation="vertical"
7+
tools:context="com.vogella.android.robolectric.RobolectricActivity">
8+
<TextView
9+
android:id="@+id/hello_textview"
10+
android:layout_width="100dp"
11+
android:layout_height="100dp"
12+
android:text="Hello World!"
13+
android:gravity="center"
14+
android:layout_marginLeft="10dp"
15+
android:layout_marginRight="10dp"
16+
android:layout_marginTop="5dp"
17+
android:layout_marginBottom="5dp"
18+
android:background="@android:color/holo_green_dark"/>
19+
<Button
20+
android:id="@+id/startNextActivity"
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:onClick="onClick"
24+
/>
25+
<Button
26+
android:id="@+id/showToast"
27+
android:layout_width="wrap_content"
28+
android:layout_height="wrap_content"
29+
android:onClick="onClick"
30+
/>
31+
</LinearLayout>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<resources>
2-
<string name="app_name">com.vogella.android.robolectric</string>
2+
<string name="app_name">Hello world!</string>
33
</resources>

com.vogella.android.robolectric/app/src/main/res/values/styles.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<resources>
22

33
<!-- Base application theme. -->
4-
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
4+
<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
55
<!-- Customize your theme here. -->
66
</style>
77

0 commit comments

Comments
 (0)