Skip to content

Commit 7bc0767

Browse files
committed
Updates from last Android testing and expert training
1 parent 7031aab commit 7bc0767

File tree

49 files changed

+864
-84
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+864
-84
lines changed

com.vogella.android.databinding/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.4.0-alpha6'
8+
classpath 'com.android.tools.build:gradle:2.3.3'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 26
5+
buildToolsVersion "26.0.0"
6+
defaultConfig {
7+
applicationId "com.vogella.android.persistence"
8+
minSdkVersion 25
9+
targetSdkVersion 26
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
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+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25+
exclude group: 'com.android.support', module: 'support-annotations'
26+
})
27+
compile 'com.android.support.constraint:constraint-layout:1.0.2'
28+
testCompile 'junit:junit:4.12'
29+
compile 'android.arch.lifecycle:extensions:1.0.0-alpha3';
30+
compile 'android.arch.persistence.room:runtime:1.0.0-alpha3';
31+
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0-alpha3';
32+
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-alpha3';
33+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /home/vogella/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+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.vogella.android.persistence;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.vogella.android.persistence", appContext.getPackageName());
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.vogella.android.persistence">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN"/>
15+
16+
<category android:name="android.intent.category.LAUNCHER"/>
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.vogella.android.persistence;
2+
3+
import android.content.Context;
4+
5+
import android.arch.persistence.room.Database;
6+
import android.arch.persistence.room.Room;
7+
import android.arch.persistence.room.RoomDatabase;
8+
9+
@Database(entities = {Task.class}, version = 1, exportSchema = false)
10+
public abstract class AppDatabase extends RoomDatabase {
11+
12+
private static AppDatabase INSTANCE;
13+
14+
public abstract TaskDao taskModel();
15+
16+
public static AppDatabase getInMemoryDatabase(Context context) {
17+
if (INSTANCE == null) {
18+
INSTANCE =
19+
Room.databaseBuilder(context, AppDatabase.class, "tasks")
20+
// Room.inMemoryDatabaseBuilder(context.getApplicationContext(), AppDatabase.class)
21+
// To simplify the exercise, allow queries on the main thread.
22+
// Don't do this on a real app!
23+
.allowMainThreadQueries()
24+
.build();
25+
}
26+
return INSTANCE;
27+
}
28+
29+
public static void destroyInstance() {
30+
INSTANCE = null;
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.vogella.android.persistence;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
import android.widget.TextView;
6+
7+
import java.util.List;
8+
9+
public class MainActivity extends Activity {
10+
11+
@Override
12+
protected void onCreate(Bundle savedInstanceState) {
13+
super.onCreate(savedInstanceState);
14+
setContentView(R.layout.activity_main);
15+
AppDatabase database = AppDatabase.getInMemoryDatabase(getApplicationContext());
16+
for (int i = 0; i <10; i++) {
17+
Task build = Task.builder().setId(i).setSummary("Testing " + i).setDescription("More ..." + i).build();
18+
database.taskModel().addTask(build);
19+
}
20+
List<Task> allTasks = database.taskModel().getAllTasks();
21+
TextView textView = findViewById(R.id.result);
22+
textView.setText(allTasks.toString());
23+
24+
}
25+
26+
@Override
27+
protected void onDestroy() {
28+
AppDatabase.destroyInstance();
29+
super.onDestroy();
30+
}
31+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.vogella.android.persistence;
2+
3+
import android.arch.persistence.room.Entity;
4+
import android.arch.persistence.room.PrimaryKey;
5+
6+
import java.util.Date;
7+
8+
@Entity
9+
public class Task {
10+
11+
@PrimaryKey
12+
public final long id;
13+
public String summary;
14+
public String description;
15+
public boolean done;
16+
// public Date dueDate;
17+
18+
19+
public Task(long id, String summary, String description, boolean done) {
20+
this.id = id;
21+
this.summary = summary;
22+
this.description = description;
23+
this.done = done;
24+
// this.dueDate = dueDate;
25+
}
26+
27+
public static TaskBuilder builder(){
28+
return new TaskBuilder();
29+
}
30+
31+
public static class TaskBuilder {
32+
private long id;
33+
private String summary = "";
34+
private String description = "";
35+
private boolean done = false;
36+
private Date dueDate;
37+
38+
public TaskBuilder setId(long id) {
39+
this.id = id;
40+
return this;
41+
}
42+
43+
public TaskBuilder setSummary(String summary) {
44+
this.summary = summary;
45+
return this;
46+
}
47+
48+
public TaskBuilder setDescription(String description) {
49+
this.description = description;
50+
return this;
51+
}
52+
53+
public TaskBuilder setDone(boolean done) {
54+
this.done = done;
55+
return this;
56+
}
57+
58+
public TaskBuilder setDueDate(Date dueDate) {
59+
this.dueDate = new Date(dueDate.getTime());
60+
return this;
61+
}
62+
63+
public Task build() {
64+
return new Task(id, summary, description, done);
65+
}
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "Task{" +
71+
"id=" + id +
72+
", summary='" + summary + '\'' +
73+
", description='" + description + '\'' +
74+
", done=" + done +
75+
'}';
76+
}
77+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.vogella.android.persistence;
2+
3+
import android.arch.persistence.room.Dao;
4+
import android.arch.persistence.room.Insert;
5+
import android.arch.persistence.room.OnConflictStrategy;
6+
import android.arch.persistence.room.Query;
7+
import android.arch.persistence.room.Update;
8+
9+
import java.util.List;
10+
11+
@Dao
12+
public interface TaskDao {
13+
14+
@Insert(onConflict = OnConflictStrategy.REPLACE)
15+
void addTask(Task task);
16+
17+
@Query("select * from task")
18+
public List<Task> getAllTasks();
19+
20+
@Query("select * from task where id = :id")
21+
public List<Task> getTask(long id);
22+
23+
@Update(onConflict = OnConflictStrategy.REPLACE)
24+
void updateTask(Task task);
25+
26+
@Query("delete from task")
27+
void removeAllTasks();
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.constraint.ConstraintLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context="com.vogella.android.persistence.MainActivity"
9+
>
10+
11+
<TextView
12+
android:id="@+id/result"
13+
android:layout_width="wrap_content"
14+
android:layout_height="wrap_content"
15+
android:text="Hello World!"
16+
app:layout_constraintBottom_toBottomOf="parent"
17+
app:layout_constraintLeft_toLeftOf="parent"
18+
app:layout_constraintRight_toRightOf="parent"
19+
app:layout_constraintTop_toTopOf="parent"
20+
/>
21+
22+
</android.support.constraint.ConstraintLayout>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="colorPrimary">#3F51B5</color>
4+
<color name="colorPrimaryDark">#303F9F</color>
5+
<color name="colorAccent">#FF4081</color>
6+
</resources>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">Persistence</string>
3+
</resources>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<resources>
2+
3+
<!-- Base application theme. -->
4+
<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
5+
<!-- Customize your theme here. -->
6+
</style>
7+
8+
</resources>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.vogella.android.persistence;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* Example local unit test, which will execute on the development machine (host).
9+
*
10+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
11+
*/
12+
public class ExampleUnitTest {
13+
@Test
14+
public void addition_isCorrect() throws Exception {
15+
assertEquals(4, 2 + 2);
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}
8+
dependencies {
9+
classpath 'com.android.tools.build:gradle:2.3.3'
10+
11+
// NOTE: Do not place your application dependencies here; they belong
12+
// in the individual module build.gradle files
13+
}
14+
}
15+
16+
allprojects {
17+
repositories {
18+
jcenter()
19+
maven {
20+
url "https://maven.google.com"
21+
}
22+
}
23+
}
24+
25+
task clean(type: Delete) {
26+
delete rootProject.buildDir
27+
}

0 commit comments

Comments
 (0)