aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/androidTest/java/org/wordpress/android/PostUploadServiceTest.java
blob: 29c28f9d484e62aa1cc5c738fe9152ac45758476 (plain)
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
package org.wordpress.android;

import android.content.Context;
import android.content.Intent;
import android.test.RenamingDelegatingContext;
import android.test.ServiceTestCase;

import org.wordpress.android.mocks.RestClientFactoryTest;
import org.wordpress.android.mocks.XMLRPCFactoryTest;
import org.wordpress.android.ui.posts.services.PostUploadService;
import org.wordpress.android.util.AppLog;

public class PostUploadServiceTest extends ServiceTestCase<PostUploadService> {
    protected Context testContext;
    protected Context targetContext;

    public PostUploadServiceTest() {
        super(PostUploadService.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        FactoryUtils.initWithTestFactories();

        String namespace = BuildConfig.FLAVOR.equals("wasabi") ? "org.wordpress.android.beta"
                : "org.wordpress.android";
        testContext = getContext().createPackageContext(namespace, Context.CONTEXT_IGNORE_SECURITY);
        targetContext = new RenamingDelegatingContext(getContext(), "test_");

        // Init contexts
        XMLRPCFactoryTest.sContext = getContext();
        RestClientFactoryTest.sContext = getContext();
        AppLog.v(AppLog.T.TESTS, "Contexts set");

        // Set mode to Customizable
        XMLRPCFactoryTest.sMode = XMLRPCFactoryTest.Mode.CUSTOMIZABLE_XML;
        RestClientFactoryTest.sMode = RestClientFactoryTest.Mode.CUSTOMIZABLE;
        AppLog.v(AppLog.T.TESTS, "Modes set to customizable");
    }

    @Override
    protected void tearDown() throws Exception {
        FactoryUtils.clearFactories();
        super.tearDown();
    }

    public void testStartable() {
        Intent startIntent = new Intent();
        startIntent.setClass(getContext(), PostUploadService.class);
        startService(startIntent);
    }

    // test reproducing https://github.com/wordpress-mobile/WordPress-Android/issues/884
    // Following test is valid but won't be fixed now (it will with the post editor rewrite)
    /*
    public void testUploadMalformedPostNullPostId() throws Exception {
        // init a test db containing a few blogs and posts
        SQLiteDatabase db = TestUtils.loadDBFromDump(targetContext, testContext, "taliwutt-blogs-sample.sql");
        WordPressDB wpdb = WordPress.wpDB;

        // callback should be called 3 times
        final CountDownLatch countDownLatch = new CountDownLatch(3);

        // trick to have a mutable final int
        final int[] notifyCount = {0};
        final int[] cancelCount = {0};
        SystemServiceFactoryTest.sNotificationCallback = new Answer() {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                if ("notify".equals(invocation.getMethod().getName())) {
                    notifyCount[0] += 1;
                }
                if ("cancel".equals(invocation.getMethod().getName())) {
                    cancelCount[0] += 1;
                }
                countDownLatch.countDown();
                return null;
            }
        };

        // get an existing uploaded post (defined in the previously loaded db dump)
        int postId = 27;
        Post post = wpdb.getPostForLocalTablePostId(postId);

        // fake the remote post id to null
        post.setRemotePostId(null);

        // push it to the PostUploadService
        PostUploadService.addPostToUpload(post);
        startService(new Intent(getContext(), PostUploadService.class));

        // wait for the response
        countDownLatch.await(15, TimeUnit.SECONDS);
        assertTrue("NotificationManager.cancel must be called at least once - see #884",
                cancelCount[0] == 1 && notifyCount[0] == 2);
    }
    */
}