Skip to content

Commit 347a3f6

Browse files
author
Josh Deffibaugh
committed
Hooks up tests for using Optly in service and fragment
1 parent 5a9dfc8 commit 347a3f6

File tree

12 files changed

+283
-97
lines changed

12 files changed

+283
-97
lines changed

test-app/src/androidTest/java/com/optimizely/ab/android/test_app/MainActivityEspressoTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,25 @@ public void experimentActivationForWhitelistUser() throws InterruptedException {
130130
// These tests are pointed at a real project.
131131
// The user 'test_user` is in the whitelist for variation 1 for experiment 0 and experiment 1.
132132
onView(withId(R.id.button_1))
133-
.check(matches(withText(context.getString(R.string.button_1_text_var_1))));
133+
.check(matches(withText(context.getString(R.string.main_act_button_1_text_var_1))));
134134

135135
// Espresso will wait for Optimizely to start due to the registered idling resources
136136
onView(withId(R.id.text_view_1))
137-
.check(matches(withText(context.getString(R.string.text_view_1_var_1))));
137+
.check(matches(withText(context.getString(R.string.main_act_text_view_1_var_1))));
138138

139139
assertTrue(serviceScheduler.isScheduled(dataFileServiceIntent));
140140

141+
onView(withId(R.id.button_1)) // withId(R.id.my_view) is a ViewMatcher
142+
.perform(click()); // click() is a ViewAction
143+
144+
onView(withId(R.id.text_view_1))
145+
.check(matches(withText(context.getString(R.string.secondary_frag_text_view_1_var_1))));
146+
141147
onView(withId(R.id.button_1)) // withId(R.id.my_view) is a ViewMatcher
142148
.perform(click()); // click() is a ViewAction
143149

144150
List<Pair<String, String>> events = CountingIdlingResourceManager.getEvents();
145-
assertTrue(events.size() == 4);
151+
assertTrue(events.size() == 6);
146152
Iterator<Pair<String, String>> iterator = events.iterator();
147153
while (iterator.hasNext()) {
148154
Pair<String, String> event = iterator.next();
@@ -151,7 +157,9 @@ public void experimentActivationForWhitelistUser() throws InterruptedException {
151157
if (url.equals("https://p13nlog.dz.optimizely.com/log/decision") && payload.contains("7676481120") && payload.contains("7661891902")
152158
|| url.equals("https://p13nlog.dz.optimizely.com/log/decision") && payload.contains("7651112186") && payload.contains("7674261140")
153159
|| url.equals("https://p13nlog.dz.optimizely.com/log/event") && payload.contains("experiment_0")
154-
|| url.equals("https://p13nlog.dz.optimizely.com/log/event") && payload.contains("experiment_1")) {
160+
|| url.equals("https://p13nlog.dz.optimizely.com/log/event") && payload.contains("experiment_1")
161+
|| url.equals("https://p13nlog.dz.optimizely.com/log/decision") && payload.contains("7680080715") && payload.contains("7685562539")
162+
|| url.equals("https://p13nlog.dz.optimizely.com/log/event") && payload.contains("experiment_2")) {
155163
iterator.remove();
156164
}
157165
}

test-app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
<category android:name="android.intent.category.LAUNCHER"/>
1717
</intent-filter>
1818
</activity>
19-
<activity android:name=".SecondaryActivity">
19+
<activity
20+
android:name=".SecondaryActivity"
21+
android:parentActivityName=".MainActivity">
2022
</activity>
2123

2224
<service
23-
android:name=".MyIntentService"
25+
android:name=".NotificationService"
2426
android:exported="false">
2527
</service>
2628
</application>

test-app/src/main/java/com/optimizely/ab/android/test_app/MainActivity.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import android.content.Intent;
1919
import android.os.Bundle;
2020
import android.support.annotation.Nullable;
21-
import android.support.annotation.VisibleForTesting;
22-
import android.support.test.espresso.idling.CountingIdlingResource;
2321
import android.support.v7.app.AppCompatActivity;
2422
import android.view.View;
2523
import android.widget.Button;
@@ -54,12 +52,12 @@ protected void onCreate(Bundle savedInstanceState) {
5452
Variation variation = optimizely.activate("experiment_0", myApplication.getAnonUserId(), myApplication.getAttributes());
5553
if (variation != null) {
5654
if (variation.is("variation_1")) {
57-
button.setText(R.string.button_1_text_var_1);
55+
button.setText(R.string.main_act_button_1_text_var_1);
5856
} else if (variation.is("variation_2")) {
59-
button.setText(R.string.button_1_text_var_2);
57+
button.setText(R.string.main_act_button_1_text_var_2);
6058
}
6159
} else {
62-
button.setText(R.string.button_1_text_default);
60+
button.setText(R.string.main_act_button_1_text_default);
6361
}
6462

6563
button.setOnClickListener(new View.OnClickListener() {
@@ -72,9 +70,6 @@ public void onClick(View v) {
7270
v.getContext().getResources().getString(R.string.app_name);
7371
Intent intent = new Intent(v.getContext(), SecondaryActivity.class);
7472
startActivity(intent);
75-
76-
intent.setClass(v.getContext(), MyIntentService.class);
77-
startService(intent);
7873
}
7974
});
8075
}
@@ -94,12 +89,12 @@ public void onStart(AndroidOptimizely optimizely) {
9489
Variation variation = optimizely.activate("experiment_1", myApplication.getAnonUserId());
9590
if (variation != null) {
9691
if (variation.is("variation_1")) {
97-
textView1.setText(R.string.text_view_1_var_1);
92+
textView1.setText(R.string.main_act_text_view_1_var_1);
9893
} else if (variation.is("variation_2")) {
99-
textView1.setText(R.string.text_view_1_var_2);
94+
textView1.setText(R.string.main_act_text_view_1_var_2);
10095
}
10196
} else {
102-
textView1.setText(R.string.text_view_1_default);
97+
textView1.setText(R.string.main_act_text_view_1_default);
10398
}
10499
}
105100
});

test-app/src/main/java/com/optimizely/ab/android/test_app/MyIntentService.java

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright 2016, Optimizely
3+
* <p/>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p/>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p/>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.optimizely.ab.android.test_app;
17+
18+
import android.app.IntentService;
19+
import android.app.NotificationManager;
20+
import android.app.PendingIntent;
21+
import android.content.Context;
22+
import android.content.Intent;
23+
import android.support.v4.app.NotificationCompat;
24+
import android.support.v4.app.TaskStackBuilder;
25+
26+
import com.optimizely.ab.android.sdk.AndroidOptimizely;
27+
import com.optimizely.ab.android.sdk.OptimizelyManager;
28+
import com.optimizely.ab.android.shared.CountingIdlingResourceManager;
29+
30+
public class NotificationService extends IntentService {
31+
public NotificationService() {
32+
super("NotificationService");
33+
}
34+
35+
@Override
36+
protected void onHandleIntent(Intent intent) {
37+
if (intent != null) {
38+
showNotification();
39+
40+
// Get Optimizely from the Intent that started this Service
41+
final MyApplication myApplication = (MyApplication) getApplication();
42+
final OptimizelyManager optimizelyManager = myApplication.getOptimizelyManager();
43+
AndroidOptimizely optimizely = optimizelyManager.getOptimizely();
44+
CountingIdlingResourceManager.increment();
45+
optimizely.track("experiment_2", myApplication.getAnonUserId());
46+
}
47+
}
48+
49+
private void showNotification() {
50+
NotificationCompat.Builder mBuilder =
51+
new NotificationCompat.Builder(this)
52+
.setSmallIcon(android.R.drawable.ic_notification_clear_all)
53+
.setContentTitle(getString(R.string.notification_service_notification_title))
54+
.setContentText(getString(R.string.notification_service_notification_text));
55+
// Creates an explicit intent for an Activity in your app
56+
Intent resultIntent = new Intent(this, MainActivity.class);
57+
58+
// The stack builder object will contain an artificial back stack for the
59+
// started Activity.
60+
// This ensures that navigating backward from the Activity leads out of
61+
// your application to the Home screen.
62+
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
63+
// Adds the back stack for the Intent (but not the Intent itself)
64+
stackBuilder.addParentStack(SecondaryActivity.class);
65+
// Adds the Intent that starts the Activity to the top of the stack
66+
stackBuilder.addNextIntent(resultIntent);
67+
PendingIntent resultPendingIntent =
68+
stackBuilder.getPendingIntent(
69+
0,
70+
PendingIntent.FLAG_UPDATE_CURRENT
71+
);
72+
mBuilder.setContentIntent(resultPendingIntent);
73+
NotificationManager mNotificationManager =
74+
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
75+
// mId allows you to update the notification later on.
76+
mNotificationManager.notify(0, mBuilder.build());
77+
}
78+
}

test-app/src/main/java/com/optimizely/ab/android/test_app/SecondaryActivity.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
package com.optimizely.ab.android.test_app;
1717

1818
import android.os.Bundle;
19-
import android.support.v4.app.Fragment;
20-
import android.support.v4.app.FragmentManager;
21-
import android.support.v4.app.FragmentTransaction;
2219
import android.support.v7.app.AppCompatActivity;
2320

2421
import com.optimizely.ab.android.sdk.AndroidOptimizely;
@@ -38,27 +35,6 @@ protected void onCreate(Bundle savedInstanceState) {
3835
AndroidOptimizely optimizely = optimizelyManager.getOptimizely();
3936
CountingIdlingResourceManager.increment(); // For track event
4037
optimizely.track("experiment_1", myApplication.getAnonUserId());
41-
42-
FragmentManager fragmentManager = getSupportFragmentManager();
43-
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
44-
45-
SecondaryFragment secondaryFragment = new SecondaryFragment();
46-
Bundle arguments = new Bundle();
47-
secondaryFragment.setArguments(arguments);
48-
49-
fragmentTransaction.add(secondaryFragment, "frag");
50-
51-
fragmentTransaction.commit();
5238
}
5339

54-
public static class SecondaryFragment extends Fragment {
55-
56-
@Override
57-
public void onStart() {
58-
super.onStart();
59-
final OptimizelyManager optimizelyManager = ((MyApplication) getActivity().getApplication()).getOptimizelyManager();
60-
AndroidOptimizely optimizely = optimizelyManager.getOptimizely();
61-
// TODO Setup an Optimizely experiment here and test with Espresso.
62-
}
63-
}
6440
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2016, Optimizely
3+
* <p/>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p/>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p/>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.optimizely.ab.android.test_app;
18+
19+
import android.content.Intent;
20+
import android.os.Bundle;
21+
import android.support.annotation.Nullable;
22+
import android.support.v4.app.Fragment;
23+
import android.support.v4.app.FragmentActivity;
24+
import android.view.LayoutInflater;
25+
import android.view.View;
26+
import android.view.ViewGroup;
27+
import android.widget.Button;
28+
import android.widget.TextView;
29+
30+
import com.optimizely.ab.android.sdk.AndroidOptimizely;
31+
import com.optimizely.ab.android.sdk.OptimizelyManager;
32+
import com.optimizely.ab.android.shared.CountingIdlingResourceManager;
33+
import com.optimizely.ab.config.Variation;
34+
35+
public class SecondaryFragment extends Fragment {
36+
37+
TextView textView1;
38+
Button button1;
39+
40+
@Nullable
41+
@Override
42+
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
43+
return inflater.inflate(R.layout.fragment_secondary, container, false);
44+
}
45+
46+
@Override
47+
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
48+
textView1 = (TextView) view.findViewById(R.id.text_view_1);
49+
button1 = (Button) view.findViewById(R.id.button_1);
50+
51+
final MyApplication myApplication = (MyApplication) getActivity().getApplication();
52+
final OptimizelyManager optimizelyManager = myApplication.getOptimizelyManager();
53+
54+
AndroidOptimizely optimizely = optimizelyManager.getOptimizely();
55+
CountingIdlingResourceManager.increment();
56+
Variation variation = optimizely.activate("experiment_2", myApplication.getAnonUserId());
57+
if (variation != null) {
58+
if (variation.is("variation_1")) {
59+
textView1.setText(R.string.secondary_frag_text_view_1_var_1);
60+
} else if (variation.is("variation_2")) {
61+
textView1.setText(R.string.secondary_frag_text_view_1_var_2);
62+
}
63+
} else {
64+
textView1.setText(R.string.secondary_frag_text_view_1_default);
65+
}
66+
67+
button1.setOnClickListener(new View.OnClickListener() {
68+
@Override
69+
public void onClick(View v) {
70+
final FragmentActivity activity = getActivity();
71+
Intent intent = new Intent(activity, NotificationService.class);
72+
activity.startService(intent);
73+
}
74+
});
75+
}
76+
}

test-app/src/main/res/layout/activity_main.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
android:layout_width="wrap_content"
1616
android:layout_height="wrap_content"
1717
android:layout_centerInParent="true"
18-
android:text="@string/button_1_text_default"/>
18+
android:text="@string/main_act_button_1_text_default"/>
1919

2020
<TextView
2121
android:id="@+id/text_view_1"
2222
android:layout_width="wrap_content"
2323
android:layout_height="wrap_content"
2424
android:layout_alignParentTop="true"
2525
android:layout_centerHorizontal="true"
26-
android:contentDescription="@string/text_view_1_cd"
26+
android:contentDescription="@string/main_act_text_view_1_cd"
2727
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
2828
android:visibility="invisible"
29-
tools:text="@string/text_view_1_default"
29+
tools:text="@string/main_act_text_view_1_default"
3030
tools:visibility="visible"/>
3131
</RelativeLayout>

test-app/src/main/res/layout/activity_secondary.xml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@
88
android:paddingLeft="@dimen/activity_horizontal_margin"
99
android:paddingRight="@dimen/activity_horizontal_margin"
1010
android:paddingTop="@dimen/activity_vertical_margin"
11-
tools:dataFileService="com.optimizely.ab.android.test_app.MainActivity">
11+
tools:dataFileService="com.optimizely.ab.android.test_app.SecondaryActivity">
12+
13+
<fragment
14+
android:id="@+id/secondaryFragment"
15+
android:name="com.optimizely.ab.android.test_app.SecondaryFragment"
16+
android:layout_width="match_parent"
17+
android:layout_height="match_parent"
18+
android:layout_alignParentBottom="true"
19+
android:layout_alignParentStart="true"
20+
android:layout_marginBottom="12dp"
21+
android:layout_marginStart="10dp"
22+
tools:layout="@layout/fragment_secondary"/>
1223

13-
<TextView
14-
android:id="@+id/text_view_1"
15-
android:layout_width="wrap_content"
16-
android:layout_height="wrap_content"
17-
android:text="@string/button_1_text_default"/>
1824
</RelativeLayout>

0 commit comments

Comments
 (0)