mirror of https://github.com/tasks/tasks
Convert remaining robolectric tests
parent
66452a64e6
commit
5317dca490
@ -1,82 +1,64 @@
|
|||||||
package com.todoroo.andlib.utility;
|
package com.todoroo.andlib.utility;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.test.AndroidTestCase;
|
||||||
|
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.robolectric.RobolectricTestRunner;
|
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import static com.todoroo.andlib.utility.DateUtilities.getRelativeDay;
|
import static com.todoroo.andlib.utility.DateUtilities.getRelativeDay;
|
||||||
import static org.joda.time.DateTime.now;
|
import static org.joda.time.DateTime.now;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.robolectric.Robolectric.getShadowApplication;
|
|
||||||
import static org.tasks.Freeze.freezeAt;
|
import static org.tasks.Freeze.freezeAt;
|
||||||
import static org.tasks.Freeze.thaw;
|
import static org.tasks.Freeze.thaw;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
public class RelativeDayTest extends AndroidTestCase {
|
||||||
public class RelativeDayTest {
|
|
||||||
|
|
||||||
private static Locale defaultLocale;
|
private static Locale defaultLocale;
|
||||||
private static final DateTime now = new DateTime(2013, 12, 31, 11, 9, 42, 357);
|
private static final DateTime now = new DateTime(2013, 12, 31, 11, 9, 42, 357);
|
||||||
|
|
||||||
@Before
|
public void setUp() {
|
||||||
public void before() {
|
|
||||||
defaultLocale = Locale.getDefault();
|
defaultLocale = Locale.getDefault();
|
||||||
Locale.setDefault(Locale.US);
|
Locale.setDefault(Locale.US);
|
||||||
freezeAt(now);
|
freezeAt(now);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
public void tearDown() {
|
||||||
public void after() {
|
|
||||||
Locale.setDefault(defaultLocale);
|
Locale.setDefault(defaultLocale);
|
||||||
thaw();
|
thaw();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
public void testRelativeDayIsToday() {
|
||||||
public void relativeDayIsToday() {
|
|
||||||
checkRelativeDay(now(), "today", "today");
|
checkRelativeDay(now(), "today", "today");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
public void testRelativeDayIsTomorrow() {
|
||||||
public void relativeDayIsTomorrow() {
|
|
||||||
checkRelativeDay(now().plusDays(1), "tomorrow", "tmrw");
|
checkRelativeDay(now().plusDays(1), "tomorrow", "tmrw");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
public void testRelativeDayIsYesterday() {
|
||||||
public void relativeDayIsYesterday() {
|
|
||||||
checkRelativeDay(now().minusDays(1), "yesterday", "yest");
|
checkRelativeDay(now().minusDays(1), "yesterday", "yest");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
public void testRelativeDayTwo() {
|
||||||
public void relativeDayTwo() {
|
|
||||||
checkRelativeDay(now().minusDays(2), "Sunday", "Sun");
|
checkRelativeDay(now().minusDays(2), "Sunday", "Sun");
|
||||||
checkRelativeDay(now().plusDays(2), "Thursday", "Thu");
|
checkRelativeDay(now().plusDays(2), "Thursday", "Thu");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
public void testRelativeDaySix() {
|
||||||
public void relativeDaySix() {
|
|
||||||
checkRelativeDay(now().minusDays(6), "Wednesday", "Wed");
|
checkRelativeDay(now().minusDays(6), "Wednesday", "Wed");
|
||||||
checkRelativeDay(now().plusDays(6), "Monday", "Mon");
|
checkRelativeDay(now().plusDays(6), "Monday", "Mon");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
public void testRelativeDayOneWeek() {
|
||||||
public void relativeDayOneWeek() {
|
|
||||||
checkRelativeDay(now().minusDays(7), "Dec 24", "Dec 24");
|
checkRelativeDay(now().minusDays(7), "Dec 24", "Dec 24");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
public void testRelativeDayOneWeekNextYear() {
|
||||||
public void relativeDayOneWeekNextYear() {
|
|
||||||
checkRelativeDay(now().plusDays(7), "Jan 7\n2014", "Jan 7\n2014");
|
checkRelativeDay(now().plusDays(7), "Jan 7\n2014", "Jan 7\n2014");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkRelativeDay(DateTime now, String full, String abbreviated) {
|
private void checkRelativeDay(DateTime now, String full, String abbreviated) {
|
||||||
final Context context = getShadowApplication().getApplicationContext();
|
assertEquals(full, getRelativeDay(getContext(), now.getMillis(), false));
|
||||||
assertEquals(full, getRelativeDay(context, now.getMillis(), false));
|
assertEquals(abbreviated, getRelativeDay(getContext(), now.getMillis()));
|
||||||
assertEquals(abbreviated, getRelativeDay(context, now.getMillis()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,249 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2012 Todoroo Inc
|
|
||||||
*
|
|
||||||
* See the file "LICENSE" for the full license governing this code.
|
|
||||||
*/
|
|
||||||
package com.todoroo.andlib.service;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.robolectric.RobolectricTestRunner;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
|
||||||
public class DependencyInjectionTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNoAutowire() {
|
|
||||||
DependencyInjectionService service = new DependencyInjectionService();
|
|
||||||
|
|
||||||
Object test = new Object();
|
|
||||||
service.inject(test);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSimpleStringInjectionAutowire() {
|
|
||||||
DependencyInjectionService service = new DependencyInjectionService();
|
|
||||||
service.addInjector(
|
|
||||||
new AbstractDependencyInjector() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getInjection(Field field) {
|
|
||||||
if(field.getName().equals("foo"))
|
|
||||||
return "bar";
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// test various permissions
|
|
||||||
Object test = new Object() {
|
|
||||||
@Autowired public String foo;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return foo;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
service.inject(test);
|
|
||||||
assertEquals("bar", test.toString());
|
|
||||||
|
|
||||||
test = new Object() {
|
|
||||||
@Autowired String foo;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return foo;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
service.inject(test);
|
|
||||||
assertEquals("bar", test.toString());
|
|
||||||
|
|
||||||
test = new Object() {
|
|
||||||
@Autowired protected String foo;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return foo;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
service.inject(test);
|
|
||||||
assertEquals("bar", test.toString());
|
|
||||||
|
|
||||||
test = new Object() {
|
|
||||||
@Autowired private String foo;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return foo;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
service.inject(test);
|
|
||||||
assertEquals("bar", test.toString());
|
|
||||||
|
|
||||||
// test no annotation
|
|
||||||
test = new Object() {
|
|
||||||
public String foo;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return foo;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
service.inject(test);
|
|
||||||
assertNull( test.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testHierarchicalStringInjectionAutowire() {
|
|
||||||
DependencyInjectionService service = new DependencyInjectionService();
|
|
||||||
service.addInjector(
|
|
||||||
new AbstractDependencyInjector() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getInjection(Field field) {
|
|
||||||
return "malarkey";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
service.addInjector(
|
|
||||||
new AbstractDependencyInjector() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getInjection(Field field) {
|
|
||||||
if(field.getName().equals("foo"))
|
|
||||||
return "bar";
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Object test = new Object() {
|
|
||||||
@Autowired public String foo;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return foo;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
service.inject(test);
|
|
||||||
assertEquals("bar", test.toString());
|
|
||||||
|
|
||||||
test = new Object() {
|
|
||||||
@Autowired public String forks;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return forks;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
service.inject(test);
|
|
||||||
assertEquals("malarkey", test.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testMissingInjection() {
|
|
||||||
DependencyInjectionService service = new DependencyInjectionService();
|
|
||||||
service.addInjector(
|
|
||||||
new AbstractDependencyInjector() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getInjection(Field field) {
|
|
||||||
if(field.getName().equals("wozzle"))
|
|
||||||
return "bar";
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
Object test = new Object() {
|
|
||||||
@Autowired public String foo;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return foo;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
service.inject(test);
|
|
||||||
fail("could inject with missing injector");
|
|
||||||
} catch (RuntimeException e) {
|
|
||||||
// expected
|
|
||||||
}
|
|
||||||
|
|
||||||
assertNull(test.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testMultipleInjection() {
|
|
||||||
DependencyInjectionService service = new DependencyInjectionService();
|
|
||||||
service.addInjector(
|
|
||||||
new AbstractDependencyInjector() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getInjection(Field field) {
|
|
||||||
if(field.getName().equals("foo"))
|
|
||||||
return "bar";
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
Object test1 = new Object() {
|
|
||||||
@Autowired public String foo;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return foo;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Object test2 = new Object() {
|
|
||||||
@Autowired public String foo;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return foo;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
service.inject(test1);
|
|
||||||
service.inject(test2);
|
|
||||||
assertEquals("bar", test1.toString());
|
|
||||||
assertEquals("bar", test2.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class ParentInjectee {
|
|
||||||
@Autowired protected String foo;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class ChildInjectee extends ParentInjectee {
|
|
||||||
@Autowired protected String bar;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testInheritedInjection() {
|
|
||||||
DependencyInjectionService service = new DependencyInjectionService();
|
|
||||||
service.addInjector(
|
|
||||||
new AbstractDependencyInjector() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getInjection(Field field) {
|
|
||||||
if(field.getName().equals("foo"))
|
|
||||||
return "gotfoo";
|
|
||||||
else if(field.getName().equals("bar"))
|
|
||||||
return "hasbar";
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
ChildInjectee child = new ChildInjectee();
|
|
||||||
service.inject(child);
|
|
||||||
assertEquals("gotfoo", child.foo);
|
|
||||||
assertEquals("hasbar", child.bar);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,30 +0,0 @@
|
|||||||
package com.todoroo.andlib.service;
|
|
||||||
|
|
||||||
public class RobolectricTestDependencyInjector extends AbstractDependencyInjector {
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
public RobolectricTestDependencyInjector(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addInjectable(String field, Object injection) {
|
|
||||||
injectables.put(field, injection);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "TestDI:" + name;
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- static stuff
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Install TestDependencyInjector above other injectors
|
|
||||||
*/
|
|
||||||
public synchronized static RobolectricTestDependencyInjector initialize(String name) {
|
|
||||||
RobolectricTestDependencyInjector instance = new RobolectricTestDependencyInjector(name);
|
|
||||||
DependencyInjectionService.getInstance().addInjector(instance);
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,117 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2012 Todoroo Inc
|
|
||||||
*
|
|
||||||
* See the file "LICENSE" for the full license governing this code.
|
|
||||||
*/
|
|
||||||
package com.todoroo.andlib.sql;
|
|
||||||
|
|
||||||
import com.todoroo.andlib.sql.Query.QueryTemplateHelper;
|
|
||||||
import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
|
|
||||||
import com.todoroo.astrid.data.Task;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.robolectric.RobolectricTestRunner;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
|
||||||
public class QueryTemplateHelperTest {
|
|
||||||
|
|
||||||
public StringBuilder selection = new StringBuilder();
|
|
||||||
public StringBuilder order = new StringBuilder();
|
|
||||||
public StringBuilder groupBy = new StringBuilder();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testBasic() {
|
|
||||||
QueryTemplateHelper.queryForContentResolver("",
|
|
||||||
selection, order, groupBy);
|
|
||||||
assertEquals(selection.toString(), "");
|
|
||||||
assertEquals(order.toString(), "");
|
|
||||||
assertEquals(groupBy.toString(), "");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSelection() {
|
|
||||||
QueryTemplateHelper.queryForContentResolver("WHERE foo = bar",
|
|
||||||
selection, order, groupBy);
|
|
||||||
assertEquals(selection.toString(), "foo = bar");
|
|
||||||
assertEquals(order.toString(), "");
|
|
||||||
assertEquals(groupBy.toString(), "");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testOrder() {
|
|
||||||
QueryTemplateHelper.queryForContentResolver("ORDER BY cats",
|
|
||||||
selection, order, groupBy);
|
|
||||||
assertEquals(selection.toString(), "");
|
|
||||||
assertEquals(order.toString(), "cats");
|
|
||||||
assertEquals(groupBy.toString(), "");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testWhereOrder() {
|
|
||||||
QueryTemplateHelper.queryForContentResolver("WHERE foo = bar ORDER BY cats",
|
|
||||||
selection, order, groupBy);
|
|
||||||
assertEquals(selection.toString(), "foo = bar");
|
|
||||||
assertEquals(order.toString(), "cats");
|
|
||||||
assertEquals(groupBy.toString(), "");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGroupBy() {
|
|
||||||
QueryTemplateHelper.queryForContentResolver("GROUP BY dogs",
|
|
||||||
selection, order, groupBy);
|
|
||||||
assertEquals(selection.toString(), "");
|
|
||||||
assertEquals(order.toString(), "");
|
|
||||||
assertEquals(groupBy.toString(), "dogs");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testWhereGroupBy() {
|
|
||||||
QueryTemplateHelper.queryForContentResolver("WHERE foo = bar GROUP BY dogs",
|
|
||||||
selection, order, groupBy);
|
|
||||||
assertEquals(selection.toString(), "foo = bar");
|
|
||||||
assertEquals(order.toString(), "");
|
|
||||||
assertEquals(groupBy.toString(), "dogs");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testOrderGroupBy() {
|
|
||||||
QueryTemplateHelper.queryForContentResolver("GROUP BY dogs ORDER BY cats",
|
|
||||||
selection, order, groupBy);
|
|
||||||
assertEquals(order.toString(), "cats");
|
|
||||||
assertEquals(groupBy.toString(), "dogs");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testWhereGroupByAndOrder() {
|
|
||||||
QueryTemplateHelper.queryForContentResolver("WHERE foo = bar GROUP BY dogs ORDER BY cats",
|
|
||||||
selection, order, groupBy);
|
|
||||||
assertEquals(selection.toString(), "foo = bar");
|
|
||||||
assertEquals(order.toString(), "cats");
|
|
||||||
assertEquals(groupBy.toString(), "dogs");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testRealQueryTemplate() {
|
|
||||||
QueryTemplateHelper.queryForContentResolver(
|
|
||||||
new QueryTemplate().where(TaskCriteria.completed()).
|
|
||||||
orderBy(Order.asc(Task.DUE_DATE)).toString(),
|
|
||||||
selection, order, groupBy);
|
|
||||||
assertEquals(TaskCriteria.completed().toString(), selection.toString());
|
|
||||||
assertEquals(Order.asc(Task.DUE_DATE).toString(), order.toString());
|
|
||||||
assertEquals("", groupBy.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testRealQueryTemplateTwo() {
|
|
||||||
QueryTemplateHelper.queryForContentResolver(
|
|
||||||
new QueryTemplate().where(TaskCriteria.isActive()).
|
|
||||||
orderBy(Order.asc(Task.ELAPSED_SECONDS)).groupBy(Task.NOTES).toString(),
|
|
||||||
selection, order, groupBy);
|
|
||||||
assertEquals(TaskCriteria.isActive().toString(), selection.toString());
|
|
||||||
assertEquals(Order.asc(Task.ELAPSED_SECONDS).toString(), order.toString());
|
|
||||||
assertEquals(Task.NOTES.toString(), groupBy.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,59 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2012 Todoroo Inc
|
|
||||||
*
|
|
||||||
* See the file "LICENSE" for the full license governing this code.
|
|
||||||
*/
|
|
||||||
package com.todoroo.andlib.test;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.res.Configuration;
|
|
||||||
import android.util.DisplayMetrics;
|
|
||||||
|
|
||||||
import com.todoroo.andlib.service.ContextManager;
|
|
||||||
import com.todoroo.andlib.service.DependencyInjectionService;
|
|
||||||
import com.todoroo.andlib.service.RobolectricTestDependencyInjector;
|
|
||||||
import com.todoroo.astrid.service.AstridDependencyInjector;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.robolectric.Robolectric;
|
|
||||||
import org.tasks.Broadcaster;
|
|
||||||
|
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.tasks.TestUtilities.resetPreferences;
|
|
||||||
|
|
||||||
public class TodorooRobolectricTestCase {
|
|
||||||
|
|
||||||
private RobolectricTestDependencyInjector testInjector;
|
|
||||||
|
|
||||||
protected <T> T addInjectable(String name, T object) {
|
|
||||||
testInjector.addInjectable(name, object);
|
|
||||||
return object;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void before() {
|
|
||||||
ContextManager.setContext(getRobolectricContext());
|
|
||||||
resetPreferences();
|
|
||||||
AstridDependencyInjector.reset();
|
|
||||||
testInjector = RobolectricTestDependencyInjector.initialize("test");
|
|
||||||
addInjectable("broadcaster", mock(Broadcaster.class));
|
|
||||||
DependencyInjectionService.getInstance().inject(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets locale
|
|
||||||
*/
|
|
||||||
private static void setLocale(Locale locale) {
|
|
||||||
Locale.setDefault(locale);
|
|
||||||
Configuration config = new Configuration();
|
|
||||||
config.locale = locale;
|
|
||||||
DisplayMetrics metrics = getRobolectricContext().getResources().getDisplayMetrics();
|
|
||||||
getRobolectricContext().getResources().updateConfiguration(config, metrics);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Context getRobolectricContext() {
|
|
||||||
return Robolectric.getShadowApplication().getApplicationContext();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,120 +0,0 @@
|
|||||||
package com.todoroo.astrid.gtasks;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
|
|
||||||
import com.google.api.services.tasks.model.TaskList;
|
|
||||||
import com.todoroo.andlib.service.Autowired;
|
|
||||||
import com.todoroo.andlib.test.TodorooRobolectricTestCase;
|
|
||||||
import com.todoroo.astrid.api.AstridApiConstants;
|
|
||||||
import com.todoroo.astrid.data.Metadata;
|
|
||||||
import com.todoroo.astrid.data.Task;
|
|
||||||
import com.todoroo.astrid.service.MetadataService;
|
|
||||||
import com.todoroo.astrid.service.TaskService;
|
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.robolectric.RobolectricTestRunner;
|
|
||||||
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
|
||||||
public class GtasksDetailExposerTest extends TodorooRobolectricTestCase {
|
|
||||||
|
|
||||||
private Context context;
|
|
||||||
private RobolectricGtasksPreferenceService gtasksPreferenceService;
|
|
||||||
|
|
||||||
@Autowired private GtasksListService gtasksListService;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void before() {
|
|
||||||
context = mock(Context.class);
|
|
||||||
|
|
||||||
super.before();
|
|
||||||
|
|
||||||
gtasksPreferenceService = addInjectable("gtasksPreferenceService", new RobolectricGtasksPreferenceService());
|
|
||||||
gtasksListService.addNewList(
|
|
||||||
new TaskList().setId("list_id").setTitle("list_title"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void after() throws Exception {
|
|
||||||
verify(context).getApplicationContext();
|
|
||||||
verifyNoMoreInteractions(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void dontBroadcastDetailsWhenNotLoggedIn() {
|
|
||||||
gtasksPreferenceService.logout();
|
|
||||||
|
|
||||||
getDetails(newTask("list_id"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void dontBroadcastDetailsForInvalidTaskId() {
|
|
||||||
getDetails(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void dontBroadcastDetailsWithNoList() {
|
|
||||||
getDetails(newTask());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void dontBroadcastDetailsWithNullList() {
|
|
||||||
getDetails(newTask(null));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void dontBroadcastDetailsWithDefaultList() {
|
|
||||||
getDetails(newTask(GtasksPreferenceService.PREF_DEFAULT_LIST));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void dontBroadcastDetailsForNonexistentList() {
|
|
||||||
getDetails(newTask("invalid_list_id"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void broadcastDetails() {
|
|
||||||
Task task = newTask("list_id");
|
|
||||||
|
|
||||||
getDetails(task);
|
|
||||||
|
|
||||||
verify(context).sendBroadcast(
|
|
||||||
new Intent(AstridApiConstants.BROADCAST_SEND_DETAILS)
|
|
||||||
.putExtra(AstridApiConstants.EXTRAS_ADDON, GtasksPreferenceService.IDENTIFIER)
|
|
||||||
.putExtra(AstridApiConstants.EXTRAS_TASK_ID, task.getId())
|
|
||||||
.putExtra(AstridApiConstants.EXTRAS_RESPONSE, "<img src='gtasks_detail'/> list_title"),
|
|
||||||
AstridApiConstants.PERMISSION_READ);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void getDetails(Task task) {
|
|
||||||
getDetails(task.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
private void getDetails(long taskId) {
|
|
||||||
Intent intent = new Intent().putExtra(AstridApiConstants.EXTRAS_TASK_ID, taskId);
|
|
||||||
new GtasksDetailExposer().onReceive(context, intent);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Task newTask() {
|
|
||||||
Task task = new Task();
|
|
||||||
new TaskService().save(task);
|
|
||||||
Metadata metadata = GtasksMetadata.createEmptyMetadata(task.getId());
|
|
||||||
new MetadataService().save(metadata);
|
|
||||||
return task;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Task newTask(String list) {
|
|
||||||
Task task = new Task();
|
|
||||||
new TaskService().save(task);
|
|
||||||
Metadata metadata = GtasksMetadata.createEmptyMetadata(task.getId());
|
|
||||||
metadata.setValue(GtasksMetadata.LIST_ID, list);
|
|
||||||
new MetadataService().save(metadata);
|
|
||||||
return task;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
package com.todoroo.astrid.gtasks;
|
|
||||||
|
|
||||||
public class RobolectricGtasksPreferenceService extends GtasksPreferenceService {
|
|
||||||
|
|
||||||
public RobolectricGtasksPreferenceService() {
|
|
||||||
setToken("");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getIdentifier() {
|
|
||||||
return "test";
|
|
||||||
}
|
|
||||||
|
|
||||||
public void logout() {
|
|
||||||
setToken(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,79 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2012 Todoroo Inc
|
|
||||||
*
|
|
||||||
* See the file "LICENSE" for the full license governing this code.
|
|
||||||
*/
|
|
||||||
package com.todoroo.astrid.service;
|
|
||||||
|
|
||||||
import com.todoroo.andlib.service.Autowired;
|
|
||||||
import com.todoroo.andlib.service.DependencyInjectionService;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.robolectric.RobolectricTestRunner;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
|
||||||
public class AstridDependencyInjectorTest {
|
|
||||||
|
|
||||||
protected static class Helper {
|
|
||||||
public Object getObject() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() throws Exception {
|
|
||||||
// in case some state from other unit tests overwrote injector
|
|
||||||
DependencyInjectionService.getInstance().addInjector(
|
|
||||||
new AstridDependencyInjector()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testWithString() {
|
|
||||||
Helper helper = new Helper() {
|
|
||||||
@Autowired public String applicationName;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getObject() {
|
|
||||||
return applicationName;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
DependencyInjectionService.getInstance().inject(helper);
|
|
||||||
assertTrue(((String)helper.getObject()).length() > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testWithClass() {
|
|
||||||
|
|
||||||
Helper helper = new Helper() {
|
|
||||||
@Autowired public TaskService taskService;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getObject() {
|
|
||||||
return taskService;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
DependencyInjectionService.getInstance().inject(helper);
|
|
||||||
assertTrue(helper.getObject() instanceof TaskService);
|
|
||||||
|
|
||||||
Helper helper2 = new Helper() {
|
|
||||||
@Autowired public TaskService taskService;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getObject() {
|
|
||||||
return taskService;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
DependencyInjectionService.getInstance().inject(helper2);
|
|
||||||
|
|
||||||
assertTrue(helper.getObject() == helper2.getObject());
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue