Skip to content

Commit

Permalink
#384 Improving Timeline object concept
Browse files Browse the repository at this point in the history
  • Loading branch information
yvolk committed Jun 22, 2016
1 parent bab4cc2 commit 9a6edf9
Show file tree
Hide file tree
Showing 43 changed files with 388 additions and 342 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void testFriends() throws ConnectionException {
String messageOid = "https://identi.ca/api/comment/dasdjfdaskdjlkewjz1EhSrTRB";
MessageInserter.deleteOldMessage(TestSuite.getConversationOriginId(), messageOid);

CommandExecutionContext counters = new CommandExecutionContext(CommandData.newCommand(CommandEnum.EMPTY,
TestSuite.getConversationMyAccount())).setTimelineType(TimelineType.HOME);
CommandExecutionContext counters = new CommandExecutionContext(
CommandData.newAccountCommand(CommandEnum.EMPTY, TestSuite.getConversationMyAccount()));
DataInserter di = new DataInserter(counters);
String username = "somebody@identi.ca";
String userOid = "acct:" + username;
Expand Down Expand Up @@ -238,7 +238,10 @@ public void testMessageFavoritedByOtherUser() throws ConnectionException {
Cursor cursor = context.getContentResolver().query(contentUri, PROJECTION, sa.selection,
sa.selectionArgs, sortOrder);
assertTrue("Cursor returned", cursor != null);
assertEquals("MsgOfUser was not created, msgId=" + messageId, 0, cursor.getCount());
assertEquals("Message is not in a Home timeline, msgId=" + messageId, true, cursor.moveToNext());
assertEquals("Linked some other user, not " + TestSuite.getConversationMyAccount(),
TestSuite.getConversationMyAccount().getUserId(), cursor.getLong(1));
assertEquals("Message is favorited by " + TestSuite.getConversationMyAccount(), 0, cursor.getLong(0));
cursor.close();
}

Expand Down Expand Up @@ -306,7 +309,7 @@ public void testMessageWithAttachment() throws Exception {
.getInstrumentation().getContext());

MyAccount ma = MyContextHolder.get().persistentAccounts()
.getFirstSucceededMyAccountByOriginId(message.originId);
.getFirstSucceededForOriginId(message.originId);
DataInserter di = new DataInserter(ma);
long messageId = di.insertOrUpdateMsg(message);
assertTrue("Message added", messageId != 0);
Expand All @@ -318,7 +321,7 @@ public void testMessageWithAttachment() throws Exception {

public void testUnsentMessageWithAttachment() throws Exception {
MyAccount ma = MyContextHolder.get().persistentAccounts()
.getFirstSucceededMyAccountByOriginId(0);
.getFirstSucceededForOriginId(0);
MbMessage message = MbMessage.fromOriginAndOid(ma.getOriginId(), "",
DownloadStatus.SENDING);
message.actor = MbUser.fromOriginAndUserOid(ma.getOriginId(), ma.getUserOid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private MbMessage buildMessage(MbUser author, String body, MbMessage inReplyToMe

private long addMessage(MbMessage message) {
DataInserter di = new DataInserter(new CommandExecutionContext(
CommandData.newCommand(CommandEnum.EMPTY, ma)).setTimelineType(TimelineType.HOME));
CommandData.newAccountCommand(CommandEnum.EMPTY, ma)));
long messageId = di.insertOrUpdateMsg(message);
assertTrue( "Message added " + message.oid, messageId != 0);
return messageId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,9 @@ public static long addMessage(MyAccount ma, MbMessage message) {
}

public long addMessage(MbMessage messageIn) {
TimelineType tt = TimelineType.HOME;
if (messageIn.isPublic() ) {
tt = TimelineType.PUBLIC;
}
DataInserter di = new DataInserter(new CommandExecutionContext(
CommandData.newCommand(CommandEnum.EMPTY, ma)).setTimelineType(tt));
CommandData.newTimelineCommand(CommandEnum.EMPTY, ma,
messageIn.isPublic() ? TimelineType.PUBLIC : TimelineType.HOME)));
long messageId = di.insertOrUpdateMsg(messageIn);
assertTrue( "Message added " + messageIn.oid, messageId != 0);

Expand Down Expand Up @@ -190,9 +187,8 @@ public static void deleteOldMessage(long originId, String messageOid) {
}
}

public static long addMessageForAccount(String accountName, String body, String messageOid, DownloadStatus messageStatus) {
MyAccount ma = MyContextHolder.get().persistentAccounts().fromAccountName(accountName);
assertTrue(accountName + " exists", ma.isValid());
public static long addMessageForAccount(MyAccount ma, String body, String messageOid, DownloadStatus messageStatus) {
assertTrue("Is not valid: " + ma, ma.isValid());
MessageInserter mi = new MessageInserter(ma);
return mi.addMessage(mi.buildMessage(mi.buildUser(), body, null, messageOid, messageStatus));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void testShareHtml() throws Exception {

public void testSharePlainText() {
String body = "Posting as a plain Text " + TestSuite.TESTRUN_UID;
long msgId = MessageInserter.addMessageForAccount(TestSuite.TWITTER_TEST_ACCOUNT_NAME, body,
long msgId = MessageInserter.addMessageForAccount(TestSuite.getMyAccount(TestSuite.TWITTER_TEST_ACCOUNT_NAME), body,
TestSuite.PLAIN_TEXT_MESSAGE_OID, DownloadStatus.LOADED);
MessageShare messageShare = new MessageShare(msgId);
Intent intent = messageShare.intentForShare();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ protected void setUp() throws Exception {
MyLog.i(this, "setUp started");
TestSuite.initializeWithData(this);

MyAccount ma = MyContextHolder.get().persistentAccounts().fromAccountName(TestSuite.GNUSOCIAL_TEST_ACCOUNT_NAME);
assertTrue(ma.isValid());
MyAccount ma = MyContextHolder.get().persistentAccounts().getFirstSucceededForOriginId(
MyContextHolder.get().persistentOrigins().fromName(TestSuite.GNUSOCIAL_TEST_ORIGIN_NAME).getId());
assertTrue(ma.isValidAndSucceeded());
MyContextHolder.get().persistentAccounts().setCurrentAccount(ma);

assertEquals(ma.getUserId(), MyContextHolder.get().persistentAccounts().getCurrentAccountUserId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void run() {
}

private void broadcastCommandExecuted() {
CommandData commandData = CommandData.newCommand(CommandEnum.CREATE_FAVORITE,
CommandData commandData = CommandData.newAccountCommand(CommandEnum.CREATE_FAVORITE,
TestSuite.getConversationMyAccount());
MyServiceEventsBroadcaster.newInstance(MyContextHolder.get(), MyServiceState.RUNNING)
.setCommandData(commandData).setEvent(MyServiceEvent.AFTER_EXECUTING_COMMAND)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void testVerifyCredentials() throws IOException {

String msgOid = "383296535213002752";
long msgId = MyQuery.oidToId(OidEnum.MSG_OID, origin.getId(), msgOid) ;
assertTrue("Message found", msgId !=0);
assertTrue("Message not found", msgId !=0);
long userIdM = MyQuery.msgIdToUserId(MsgTable.AUTHOR_ID, msgId);
assertEquals("Message is by " + mbUser.getUserName() + " found", userId, userIdM);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ public void testPermalink() {
assertEquals(origin.originType, OriginType.TWITTER);
String body = "Posting to Twitter " + TestSuite.TESTRUN_UID;
String messageOid = "2578909845023" + TestSuite.TESTRUN_UID;
long msgId = MessageInserter.addMessageForAccount(TestSuite.TWITTER_TEST_ACCOUNT_NAME,
long msgId = MessageInserter.addMessageForAccount(
TestSuite.getMyAccount(TestSuite.TWITTER_TEST_ACCOUNT_NAME),
body, messageOid, DownloadStatus.LOADED);
assertTrue(msgId != 0);
String userName = MyQuery.msgIdToUsername(MsgTable.AUTHOR_ID, msgId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void testEquals() {
public void testPriority() {
Queue<CommandData> queue = new PriorityBlockingQueue<CommandData>(100);
queue.add(CommandData.newSearch(
TestSuite.getMyAccount(TestSuite.GNUSOCIAL_TEST_ACCOUNT_NAME), "q1"));
TestSuite.getMyAccount(TestSuite.GNUSOCIAL_TEST_ACCOUNT_NAME).getOrigin(), "q1"));
queue.add(CommandData.newUpdateStatus(null, 2));
queue.add(CommandData.newCommand(CommandEnum.FETCH_TIMELINE));
queue.add(CommandData.newUpdateStatus(null, 3));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ public class CommandExecutionContextTest extends InstrumentationTestCase {

@Override
protected void setUp() throws Exception {
TestSuite.initialize(this);
ma = MyAccount.Builder.newOrExistingFromAccountName(MyContextHolder.get(), "temp/",
TriState.UNKNOWN).getAccount();
TestSuite.initializeWithData(this);
ma = MyContextHolder.get().persistentAccounts().getFirstSucceededForOriginId(0);
}

public void testHomeAccumulation() {
CommandExecutionContext execContext = new CommandExecutionContext(
CommandData.newCommand(CommandEnum.EMPTY, ma)).setTimelineType(TimelineType.HOME);
CommandData.newAccountCommand(CommandEnum.EMPTY, ma));
assertEquals(execContext.getTimelineType(), TimelineType.HOME);

final int MESSAGES = 4;
Expand All @@ -31,20 +30,20 @@ public void testHomeAccumulation() {
for (int ind=0; ind < MENTIONS; ind++) {
execContext.getResult().incrementMentionsCount();
}
assertEquals(4, execContext.getResult().getMessagesAdded());
assertEquals(2, execContext.getResult().getMentionsAdded());
assertEquals(MESSAGES, execContext.getResult().getMessagesAdded());
assertEquals(MENTIONS, execContext.getResult().getMentionsAdded());
assertEquals(0, execContext.getResult().getDirectedAdded());
}

public void testDirectAccumulation() {
CommandExecutionContext execContext = new CommandExecutionContext(
CommandData.newCommand(CommandEnum.EMPTY, ma)).setTimelineType(TimelineType.DIRECT);
CommandData.newTimelineCommand(CommandEnum.EMPTY, ma, TimelineType.DIRECT));
final int MESSAGES = 4;
for (int ind=0; ind < MESSAGES; ind++) {
execContext.getResult().incrementMessagesCount(execContext.getTimelineType());
}
assertEquals(0, execContext.getResult().getMessagesAdded());
assertEquals(0, execContext.getResult().getMentionsAdded());
assertEquals(4, execContext.getResult().getDirectedAdded());
assertEquals(MESSAGES, execContext.getResult().getDirectedAdded());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.andstatus.app.net.http.HttpConnectionMock;
import org.andstatus.app.origin.DiscoveredOrigins;
import org.andstatus.app.origin.OriginType;
import org.andstatus.app.timeline.TimelineType;
import org.andstatus.app.util.RawResourceUtils;
import org.andstatus.app.util.TriState;

import java.io.IOException;
import java.util.Arrays;
Expand All @@ -44,22 +44,22 @@ protected void setUp() throws Exception {
TestSuite.initializeWithData(this);

TestSuite.setHttpConnectionMockClass(HttpConnectionMock.class);
// In order the mocked connection to have effect:
// In order for the the mocked connection to have effect:
MyContextHolder.get().persistentAccounts().initialize();
ma = MyAccount.Builder.newOrExistingFromAccountName(
MyContextHolder.get(),
TestSuite.GNUSOCIAL_TEST_ACCOUNT_NAME, TriState.UNKNOWN).getAccount();
assertTrue(ma.getUserId() != 0);
MyContextHolder.get().persistentTimelines().initialize();
ma = MyContextHolder.get().persistentAccounts().getFirstSucceededForOriginId(
MyContextHolder.get().persistentOrigins().fromName(TestSuite.GNUSOCIAL_TEST_ORIGIN_NAME).getId());
assertTrue(ma.toString(), ma.isValidAndSucceeded());
assertTrue("HttpConnection mocked", ma.getConnection().getHttp() instanceof HttpConnectionMock);
httpConnectionMock = (HttpConnectionMock) ma.getConnection().getHttp();
}

public void testFetchTimeline() {
CommandData commandData = CommandData.newCommand(CommandEnum.FETCH_TIMELINE);
CommandData commandData = CommandData.newTimelineCommand(CommandEnum.FETCH_TIMELINE, null, TimelineType.HOME);
CommandExecutorStrategy strategy = CommandExecutorStrategy.getStrategy(commandData, null);
assertEquals(CommandExecutorStrategy.class, strategy.getClass());

commandData = CommandData.newCommand(CommandEnum.FETCH_TIMELINE, ma);
commandData = CommandData.newTimelineCommand(CommandEnum.FETCH_TIMELINE, ma, TimelineType.HOME);
strategy = CommandExecutorStrategy.getStrategy(commandData, null);
assertEquals(CommandExecutorLoadTimeline.class, strategy.getClass());
}
Expand All @@ -69,7 +69,7 @@ public void testSearch() {
CommandExecutorStrategy strategy = CommandExecutorStrategy.getStrategy(commandData, null);
assertEquals(CommandExecutorStrategy.class, strategy.getClass());

commandData = CommandData.newSearch(ma, TestSuite.GLOBAL_PUBLIC_MESSAGE_TEXT);
commandData = CommandData.newSearch(ma.getOrigin(), TestSuite.GLOBAL_PUBLIC_MESSAGE_TEXT);
strategy = CommandExecutorStrategy.getStrategy(commandData, null);
assertEquals(CommandExecutorSearch.class, strategy.getClass());
strategy.execute();
Expand Down Expand Up @@ -143,10 +143,8 @@ public void testUpdateDestroyStatus() throws IOException {
private CommandData getCommandDataForUnsentMessage(String suffix) {
String body = "Some text " + suffix + " to send " + System.currentTimeMillis() + "ms";
long unsentMessageId = MessageInserter.addMessageForAccount(
TestSuite.TWITTER_TEST_ACCOUNT_NAME, body, "", DownloadStatus.SENDING);
return CommandData.newUpdateStatus(
TestSuite.getMyAccount(TestSuite.GNUSOCIAL_TEST_ACCOUNT_NAME),
unsentMessageId);
ma, body, "", DownloadStatus.SENDING);
return CommandData.newUpdateStatus(ma, unsentMessageId);
}

public void testDiscoverOrigins() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ protected void setUp() throws Exception {
TestSuite.initializeWithData(this);

mService.setUp(null);
ma = MyContextHolder.get().persistentAccounts()
.fromAccountName(TestSuite.CONVERSATION_ACCOUNT_NAME);
assertTrue(TestSuite.CONVERSATION_ACCOUNT_NAME + " exists", ma.isValid());
ma = MyContextHolder.get().persistentAccounts().getFirstSucceededForOriginId(0);
assertTrue("No successfully verified accounts", ma.isValidAndSucceeded());

MyLog.i(this, "setUp ended instanceId=" + mService.connectionInstanceId);
}
Expand All @@ -55,11 +54,11 @@ public void testRepeatingFailingCommand() throws MalformedURLException {
String urlString = "http://andstatus.org/nonexistent2_avatar_" + System.currentTimeMillis() + ".png";
AvatarDownloaderTest.changeAvatarUrl(ma, urlString);

mService.listenedCommand = CommandData.newUserCommand(
mService.setListenedCommand(CommandData.newUserCommand(
CommandEnum.FETCH_AVATAR,
ma.getOrigin(),
ma.getUserId(),
"");
""));

long startCount = mService.executionStartCount;
long endCount = mService.executionEndCount;
Expand All @@ -72,7 +71,7 @@ public void testRepeatingFailingCommand() throws MalformedURLException {
mService.sendListenedToCommand();
assertFalse("Duplicated command didn't start executing",
mService.waitForCommandExecutionStarted(startCount + 1));
mService.listenedCommand.setManuallyLaunched(true);
mService.getListenedCommand().setManuallyLaunched(true);
mService.sendListenedToCommand();
assertTrue("Manually launched duplicated command started executing",
mService.waitForCommandExecutionStarted(startCount + 1));
Expand All @@ -85,7 +84,7 @@ public void testAccountSync() {
final String method = "testAccountSync";
MyLog.v(this, method + " started");

MyAccount myAccount = MyContextHolder.get().persistentAccounts().getFirstSucceededMyAccountByOriginId(0);
MyAccount myAccount = MyContextHolder.get().persistentAccounts().getFirstSucceededForOriginId(0);
assertTrue("No successful account", myAccount != null);

SyncResult syncResult = new SyncResult();
Expand Down Expand Up @@ -113,8 +112,8 @@ public void testHomeTimeline() {
final String method = "testHomeTimeline";
MyLog.v(this, method + " started");

mService.listenedCommand = CommandData.newTimelineCommand(
CommandEnum.FETCH_TIMELINE, ma, TimelineType.HOME);
mService.setListenedCommand(CommandData.newTimelineCommand(
CommandEnum.FETCH_TIMELINE, ma, TimelineType.HOME));
long startCount = mService.executionStartCount;
long endCount = mService.executionEndCount;

Expand All @@ -132,10 +131,9 @@ public void testHomeTimeline() {
public void testRateLimitStatus() {
MyLog.v(this, "testRateLimitStatus started");

mService.listenedCommand = CommandData.newTimelineCommand(
mService.setListenedCommand(CommandData.newAccountCommand(
CommandEnum.RATE_LIMIT_STATUS,
TestSuite.getMyAccount(TestSuite.GNUSOCIAL_TEST_ACCOUNT_NAME),
TimelineType.EVERYTHING);
TestSuite.getMyAccount(TestSuite.GNUSOCIAL_TEST_ACCOUNT_NAME)));
long startCount = mService.executionStartCount;
long endCount = mService.executionEndCount;

Expand All @@ -156,7 +154,7 @@ public void testSyncInForeground() throws InterruptedException {
CommandData cd1 = CommandData.newTimelineCommand(CommandEnum.FETCH_TIMELINE,
TestSuite.getMyAccount(TestSuite.TWITTER_TEST_ACCOUNT_NAME),
TimelineType.DIRECT);
mService.listenedCommand = cd1;
mService.setListenedCommand(cd1);

long startCount = mService.executionStartCount;
long endCount = mService.executionEndCount;
Expand All @@ -165,15 +163,15 @@ public void testSyncInForeground() throws InterruptedException {
assertTrue("First command didn't start executing", mService.waitForCommandExecutionStarted(startCount));
assertTrue("First command didn't end executing", mService.waitForCommandExecutionEnded(endCount));
assertEquals(mService.httpConnectionMock.toString(),
mService.httpConnectionMock.getRequestsCounter(), 1);
1, mService.httpConnectionMock.getRequestsCounter());

assertTrue(TestSuite.setAndWaitForIsInForeground(true));
MyLog.v(this, method + "; we are in a foreground");

CommandData cd2 = CommandData.newTimelineCommand(CommandEnum.FETCH_TIMELINE,
TestSuite.getMyAccount(TestSuite.TWITTER_TEST_ACCOUNT_NAME),
TimelineType.MENTIONS);
mService.listenedCommand = cd2;
mService.setListenedCommand(cd2);

startCount = mService.executionStartCount;
endCount = mService.executionEndCount;
Expand All @@ -195,7 +193,7 @@ public void testSyncInForeground() throws InterruptedException {
TestSuite.getMyAccount(TestSuite.TWITTER_TEST_ACCOUNT_NAME),
TimelineType.HOME)
.setInForeground(true);
mService.listenedCommand = cd3;
mService.setListenedCommand(cd3);

startCount = mService.executionStartCount;
endCount = mService.executionEndCount;
Expand Down Expand Up @@ -233,9 +231,9 @@ private void myTestDeleteCommand(CommandData cd2) {
null,
cd2.getCommandId());
cdDelete.setInForeground(true);
mService.listenedCommand = cdDelete;
mService.setListenedCommand(cdDelete);

assertEquals(cd2.getCommandId(), mService.listenedCommand.itemId);
assertEquals(cd2.getCommandId(), mService.getListenedCommand().itemId);

long endCount = mService.executionEndCount;

Expand Down

0 comments on commit 9a6edf9

Please sign in to comment.