diff options
author | Joe LaPenna <jlapenna@google.com> | 2017-02-13 17:45:19 -0800 |
---|---|---|
committer | Joe LaPenna <jlapenna@google.com> | 2017-02-21 17:47:42 -0800 |
commit | 197976bcaf5ac84447dbfdfb7ed6e5cab2654b14 (patch) | |
tree | e45f955650c93dfb11db0e1be81d953a96197438 | |
parent | 02076cabe11dd2ed395e84a399b42a58d7b12f75 (diff) | |
download | NetworkRecommendation-197976bcaf5ac84447dbfdfb7ed6e5cab2654b14.tar.gz |
Add an example robolectric test.
This alos provides an override for SystemApi objects that are not part
of the official robolectric distribution.
Test: mma NetworkRecommendation RunNetworkRecommendationRoboTests
Bug: 34944625
Change-Id: I4085cc7caef2d33c7d2e210b721499195a5b7b72
4 files changed, 127 insertions, 9 deletions
diff --git a/robotests/src/android/net/RecommendationRequest.java b/robotests/src/android/net/RecommendationRequest.java new file mode 100644 index 0000000..debf674 --- /dev/null +++ b/robotests/src/android/net/RecommendationRequest.java @@ -0,0 +1,43 @@ +/* + * 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. + */ +package android.net; + +import android.os.Parcel; +import android.os.Parcelable; + +/** + * A placeholder class to prevent ClassNotFound exceptions caused by lack of visibility. + */ +public class RecommendationRequest implements Parcelable { + public static final class Builder { + + public RecommendationRequest build() { + return null; + } + } + + protected RecommendationRequest(Parcel in) { + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + } +} diff --git a/robotests/src/com/android/networkrecommendation/ExampleTest.java b/robotests/src/com/android/networkrecommendation/ExampleTest.java index c81633a..10e0ccb 100644 --- a/robotests/src/com/android/networkrecommendation/ExampleTest.java +++ b/robotests/src/com/android/networkrecommendation/ExampleTest.java @@ -18,32 +18,32 @@ package com.android.networkrecommendation; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; -import static org.mockito.Mockito.times; + import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; -import android.app.job.JobInfo; import android.app.job.JobScheduler; import android.content.Context; import android.content.Intent; +import android.net.RecommendationRequest; + +import com.android.networkrecommendation.shadows.RecommendationRequestShadow; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -import java.util.List; @RunWith(RobolectricTestRunner.class) -@Config(manifest="packages/services/NetworkRecommendation/AndroidManifest.xml", sdk=23) +@Config(manifest="packages/services/NetworkRecommendation/AndroidManifest.xml", sdk=23, + shadows={RecommendationRequestShadow.class}) public class ExampleTest { @Mock private Context mMockContext; @Mock private JobScheduler mJobScheduler; @Mock private Intent mMockIntent; + @Mock private RecommendationRequest request; @Before public void setUp() { @@ -51,7 +51,17 @@ public class ExampleTest { } @Test - public void example() { - // pass + public void buildRecommendation() { + new RecommendationRequest.Builder().build(); + } + + @Test + public void build() { + new Example().buildRecommendationRequest(); + } + + @Test + public void reflect() { + new Example().reflectRecommendationRequest(); } } diff --git a/robotests/src/com/android/networkrecommendation/shadows/RecommendationRequestShadow.java b/robotests/src/com/android/networkrecommendation/shadows/RecommendationRequestShadow.java new file mode 100644 index 0000000..efdad3e --- /dev/null +++ b/robotests/src/com/android/networkrecommendation/shadows/RecommendationRequestShadow.java @@ -0,0 +1,24 @@ +/* + * 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. + */ +package com.android.networkrecommendation.shadows; + +import android.net.RecommendationRequest; + +import org.robolectric.annotation.Implements; + +@Implements(RecommendationRequest.class) +public class RecommendationRequestShadow { +} diff --git a/src/com/android/networkrecommendation/Example.java b/src/com/android/networkrecommendation/Example.java new file mode 100644 index 0000000..a85a112 --- /dev/null +++ b/src/com/android/networkrecommendation/Example.java @@ -0,0 +1,41 @@ +/* + * 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. + */ +package com.android.networkrecommendation; + +import android.net.RecommendationRequest; + +import java.lang.reflect.Method; + +public class Example { + + public Example() {} + + public void buildRecommendationRequest() { + new RecommendationRequest.Builder().build(); + } + + @SuppressWarnings("unchecked") + public RecommendationRequest reflectRecommendationRequest() { + try { + Class builder = RecommendationRequest.class.getClasses()[0]; + Object builderObj = builder.getConstructor().newInstance(); + Method build = builder.getDeclaredMethod("build"); + return (RecommendationRequest) build.invoke(builderObj); + } catch (Exception e) { + throw new RuntimeException("Noooo", e); + } + } +} |