/
NotificationsTests.java
146 lines (104 loc) · 4.05 KB
/
NotificationsTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* poor tests checking if the service is running in the background
*
*/
package com.applicake.beanstalkclient.test;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.test.ServiceTestCase;
import android.test.mock.MockApplication;
import android.util.Log;
import com.applicake.beanstalkclient.Constants;
import com.applicake.beanstalkclient.SyncService;
public class NotificationsTests extends ServiceTestCase<SyncService> {
private SharedPreferences prefs;
private Context context;
public NotificationsTests() {
super(SyncService.class);
// TODO Auto-generated constructor stub
}
public static class Tester {
private static int numberOfRuns = 0;
public static int getNumebrOfRuns() {
return numberOfRuns;
}
public static void reset() {
numberOfRuns = 0;
}
public static void increment() {
numberOfRuns++;
}
}
@Override
protected void setUp() throws Exception {
final SharedPreferencesMockContext mockContext = new SharedPreferencesMockContext(getContext());
MockApplication mockApplication = new MockApplication(){
@Override
public Context getApplicationContext() {
Log.d("tests", "Im here");
return mockContext;
}
};
context = mockContext;
setApplication(mockApplication);
prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().clear().commit();
super.setUp();
}
@Override
protected void tearDown() throws Exception {
// clearing preferences
prefs.edit().clear().commit();
super.tearDown();
}
public void testServiceInitializationAndStopCredsStoredNotificationsEnabled() throws InterruptedException {
notificationPreferencesSetter(prefs, true, "1", true);
// starting service
SyncService.initializeService(context);
// checking if the service is scheduled
Thread.sleep(1000);
assertTrue(SyncService.isServiceRunning(context));
// stopping service
SyncService.stopService(context);
Thread.sleep(1000);
// checking if the service is not scheduled any more
assertFalse(SyncService.isServiceRunning(context));
}
public void testServiceInitializationAndStopCredsNotStoredNotificationsEnabled() throws InterruptedException {
notificationPreferencesSetter(prefs, true, "1", false);
// starting service
SyncService.initializeService(context);
Thread.sleep(1000);
// checking if the service is scheduled
assertFalse(SyncService.isServiceRunning(context));
// stopping service
SyncService.stopService(context);
Thread.sleep(1000);
// checking if the service is not scheduled any more
assertFalse(SyncService.isServiceRunning(context));
}
public void testServiceInitializationAndStopCredsNotStoredNotificationsNotEnabled() throws InterruptedException {
notificationPreferencesSetter(prefs, false, "1", false);
// starting service
SyncService.initializeService(context);
Thread.sleep(1000);
// checking if the service is scheduled
assertFalse(SyncService.isServiceRunning(context));
// stopping service
SyncService.stopService(context);
Thread.sleep(1000);
// checking if the service is not scheduled any more
assertFalse(SyncService.isServiceRunning(context));
}
public static void notificationPreferencesSetter(SharedPreferences prefs, boolean isOn, String delay, boolean credentailsStored){
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(Constants.CREDENTIALS_STORED, credentailsStored);
editor.putBoolean(Constants.AUTO_UPDATE_NOTIFICATION_SERVICE, isOn);
editor.putString(Constants.AUTO_UPDATE_NOTIFICATION_SERVICE_DELAY, delay);
// setup credentials
editor.putString(Constants.USER_ACCOUNT_DOMAIN, SecretData.USER_ACCOUNT_DOMAIN);
editor.putString(Constants.USER_LOGIN, SecretData.USER_LOGIN);
editor.putString(Constants.USER_PASSWORD, SecretData.PASSWORD);
editor.commit();
}
}