forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test observing r8 behavior with HiltViewModel.
RELNOTES=n/a PiperOrigin-RevId: 578956204
- Loading branch information
1 parent
ed47d4b
commit 8801fd2
Showing
19 changed files
with
788 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
javatests/artifacts/hilt-android/viewmodel/app/build.gradle
This file contains 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,65 @@ | ||
/* | ||
* Copyright (C) 2023 The Dagger Authors. | ||
* | ||
* 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. | ||
*/ | ||
|
||
plugins { | ||
id 'com.android.application' | ||
id 'com.google.dagger.hilt.android' | ||
} | ||
|
||
android { | ||
|
||
namespace 'dagger.hilt.viewmodel' | ||
compileSdkVersion 33 | ||
defaultConfig { | ||
applicationId 'dagger.hilt.viewmodel' | ||
minSdk 21 | ||
targetSdk 33 | ||
versionCode 1 | ||
versionName "1.0" | ||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
debug { | ||
minifyEnabled true | ||
shrinkResources true | ||
proguardFiles getDefaultProguardFile( | ||
'proguard-android-optimize.txt'), | ||
'proguard-rules.pro' | ||
|
||
} | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_11 | ||
targetCompatibility JavaVersion.VERSION_11 | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation 'androidx.appcompat:appcompat:1.2.0' | ||
implementation 'com.google.errorprone:error_prone_annotations:2.15.0' | ||
|
||
androidTestImplementation 'androidx.test:core:1.5.0-alpha02' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.3' | ||
androidTestImplementation "androidx.test:runner:1.5.2" | ||
androidTestImplementation "androidx.test:rules:1.5.0" | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' | ||
|
||
// Hilt dependencies | ||
implementation "com.google.dagger:hilt-android:$hilt_version" | ||
annotationProcessor "com.google.dagger:hilt-compiler:$hilt_version" | ||
} |
2 changes: 2 additions & 0 deletions
2
javatests/artifacts/hilt-android/viewmodel/app/proguard-rules.pro
This file contains 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,2 @@ | ||
-dontwarn com.google.errorprone.annotations.MustBeClosed | ||
-keep class kotlin.** |
43 changes: 43 additions & 0 deletions
43
...droid/viewmodel/app/src/androidTest/java/dagger/hilt/viewmodel/SimpleApplicationTest.java
This file contains 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,43 @@ | ||
/* | ||
* Copyright (C) 2023 The Dagger Authors. | ||
* | ||
* 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 dagger.hilt.viewmodel; | ||
|
||
import static androidx.test.espresso.Espresso.onView; | ||
import static androidx.test.espresso.assertion.ViewAssertions.matches; | ||
import static androidx.test.espresso.matcher.ViewMatchers.withResourceName; | ||
import static androidx.test.espresso.matcher.ViewMatchers.withText; | ||
import static org.hamcrest.Matchers.startsWith; | ||
|
||
import android.content.Intent; | ||
import androidx.test.core.app.ActivityScenario; | ||
import androidx.test.core.app.ApplicationProvider; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class SimpleApplicationTest { | ||
@Test | ||
public void testHiltViewModelWithR8DoesNotCrash() { | ||
Intent mainIntent = | ||
new Intent(ApplicationProvider.getApplicationContext(), SimpleActivity.class); | ||
try (ActivityScenario<SimpleActivity> scenario = ActivityScenario.launch(mainIntent)) { | ||
onView(withResourceName("greeting")) | ||
.check(matches(withText(startsWith("Hello")))); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
javatests/artifacts/hilt-android/viewmodel/app/src/main/AndroidManifest.xml
This file contains 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,29 @@ | ||
<!-- | ||
~ Copyright (C) 2023 The Dagger Authors. | ||
~ | ||
~ 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"> | ||
|
||
<application android:name=".SimpleApplication" android:label="@string/appName"> | ||
<activity | ||
android:name=".SimpleActivity" | ||
android:theme="@style/Theme.AppCompat.Light" | ||
android:exported="true"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
</manifest> |
40 changes: 40 additions & 0 deletions
40
...ifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleActivity.java
This file contains 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,40 @@ | ||
/* | ||
* Copyright (C) 2023 The Dagger Authors. | ||
* | ||
* 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 dagger.hilt.viewmodel; | ||
|
||
import android.os.Bundle; | ||
import android.widget.TextView; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.lifecycle.ViewModelProvider; | ||
import dagger.hilt.android.AndroidEntryPoint; | ||
|
||
/** The main activity of the application. */ | ||
@AndroidEntryPoint | ||
public class SimpleActivity extends AppCompatActivity { | ||
private static final String TAG = SimpleActivity.class.getSimpleName(); | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
SimpleViewModel viewModel = new ViewModelProvider(this).get(SimpleViewModel.class); | ||
|
||
setContentView(R.layout.activity_main); | ||
|
||
((TextView) findViewById(R.id.greeting)) | ||
.setText(getResources().getString(R.string.welcome, viewModel.userName)); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...cts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleApplication.java
This file contains 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,27 @@ | ||
/* | ||
* Copyright (C) 2023 The Dagger Authors. | ||
* | ||
* 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 dagger.hilt.viewmodel; | ||
|
||
import android.app.Application; | ||
import dagger.hilt.android.HiltAndroidApp; | ||
|
||
/** | ||
* A simple, skeletal application that demonstrates a dependency-injected application using the | ||
* utilities in {@code Hilt} in Android. | ||
*/ | ||
@HiltAndroidApp | ||
public class SimpleApplication extends Application {} |
32 changes: 32 additions & 0 deletions
32
...facts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleViewModel.java
This file contains 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,32 @@ | ||
/* | ||
* Copyright (C) 2023 The Dagger Authors. | ||
* | ||
* 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 dagger.hilt.viewmodel; | ||
|
||
import androidx.lifecycle.ViewModel; | ||
import dagger.hilt.android.lifecycle.HiltViewModel; | ||
import javax.inject.Inject; | ||
|
||
/** The view model requires creation by hilt. */ | ||
@HiltViewModel | ||
public final class SimpleViewModel extends ViewModel { | ||
String userName; | ||
|
||
@Inject | ||
SimpleViewModel(@UserName String userName) { | ||
this.userName = userName; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ts/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/UserName.java
This file contains 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,29 @@ | ||
/* | ||
* Copyright (C) 2023 The Dagger Authors. | ||
* | ||
* 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 dagger.hilt.viewmodel; | ||
|
||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import javax.inject.Qualifier; | ||
|
||
/** Qualifies bindings relating to the user name. */ | ||
@Qualifier | ||
@Retention(RUNTIME) | ||
@Documented | ||
@interface UserName {} |
35 changes: 35 additions & 0 deletions
35
...ifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/UserNameModule.java
This file contains 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,35 @@ | ||
/* | ||
* Copyright (C) 2023 The Dagger Authors. | ||
* | ||
* 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 dagger.hilt.viewmodel; | ||
|
||
import dagger.Module; | ||
import dagger.Provides; | ||
import dagger.hilt.InstallIn; | ||
import dagger.hilt.android.components.ViewModelComponent; | ||
import java.util.Random; | ||
|
||
@Module | ||
@InstallIn(ViewModelComponent.class) | ||
final class UserNameModule { | ||
@UserName | ||
@Provides | ||
static String provideUserName() { | ||
return "ProdUser-" + new Random().nextInt(); | ||
} | ||
|
||
private UserNameModule() {} | ||
} |
38 changes: 38 additions & 0 deletions
38
javatests/artifacts/hilt-android/viewmodel/app/src/main/res/layout/activity_main.xml
This file contains 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,38 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
~ Copyright (C) 2023 The Dagger Authors. | ||
~ | ||
~ 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. | ||
--> | ||
|
||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:background="@android:color/background_light"> | ||
|
||
<TextView | ||
android:id="@+id/greeting" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentTop="true" | ||
android:textColor="@android:color/primary_text_light" | ||
/> | ||
|
||
<Button | ||
android:id="@+id/goto_feature" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_below="@id/greeting" | ||
android:text="@string/navigateToFeature" | ||
/> | ||
</RelativeLayout> |
27 changes: 27 additions & 0 deletions
27
javatests/artifacts/hilt-android/viewmodel/app/src/main/res/values/strings.xml
This file contains 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,27 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
~ Copyright (C) 2023 The Dagger Authors. | ||
~ | ||
~ 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> | ||
<!--The app name [CHAR_LIMIT=20]--> | ||
<string name="appName">Simple Hilt Android</string> | ||
|
||
<!--The greeting message [CHAR_LIMIT=100]--> | ||
<string name="welcome">Hello, %1$s!</string> | ||
|
||
<!--The feature button message [CHAR_LIMIT=100]--> | ||
<string name="navigateToFeature">Navigate to a feature!</string> | ||
</resources> |
Oops, something went wrong.