summaryrefslogtreecommitdiff
path: root/addons/mail/static/src/env/test_env.js
blob: 048bfc8d65fd5e32c8d733525161ca6084a76561 (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
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
147
148
odoo.define('mail/static/src/env/test_env.js', function (require) {
'use strict';

const { makeDeferred } = require('mail/static/src/utils/deferred/deferred.js');
const { nextTick } = require('mail/static/src/utils/utils.js');

const { Store } = owl;
const { EventBus } = owl.core;

/**
 * @param {Object} [providedEnv={}]
 * @returns {Object}
 */
function addMessagingToEnv(providedEnv = {}) {
    const env = Object.assign(providedEnv);

    /**
     * Messaging store
     */
    const store = new Store({
        env,
        state: {
            messagingRevNumber: 0,
        },
    });

    /**
     * Registry of models.
     */
    env.models = {};
    /**
     * Environment keys used in messaging.
     */
    Object.assign(env, {
        autofetchPartnerImStatus: false,
        browser: Object.assign({
            innerHeight: 1080,
            innerWidth: 1920,
            Notification: Object.assign({
                permission: 'denied',
                async requestPermission() {
                    return this.permission;
                },
            }, (env.browser && env.browser.Notification) || {}),
        }, env.browser),
        destroyMessaging() {
            if (env.modelManager) {
                env.modelManager.deleteAll();
                env.messaging = undefined;
            }
        },
        disableAnimation: true,
        isMessagingInitialized() {
            if (!this.messaging) {
                return false;
            }
            return this.messaging.isInitialized;
        },
        /**
         * States whether the environment is in QUnit test or not.
         *
         * Useful to prevent some behaviour in QUnit tests, like applying
         * style of attachment that uses url.
         */
        isQUnitTest: true,
        loadingBaseDelayDuration: providedEnv.loadingBaseDelayDuration || 0,
        messaging: undefined,
        messagingCreatedPromise: makeDeferred(),
        messagingInitializedDeferred: makeDeferred(),
        messagingBus: new EventBus(),
        modelManager: undefined,
        store,
    });

    return env;
}

/**
 * @param {Object} [providedEnv={}]
 * @returns {Object}
 */
function addTimeControlToEnv(providedEnv = {}) {

    let env = Object.assign({}, providedEnv);

    if (!env.browser) {
        env.browser = {};
    }
    // list of timeout ids that have timed out.
    let timedOutIds = [];
    // key: timeoutId, value: func + remaining duration
    const timeouts = new Map();
    Object.assign(env.browser, {
        clearTimeout: id => {
            timeouts.delete(id);
            timedOutIds = timedOutIds.filter(i => i !== id);
        },
        setTimeout: (func, duration) => {
            const timeoutId = _.uniqueId('timeout_');
            const timeout = {
                id: timeoutId,
                isTimedOut: false,
                func,
                duration,
            };
            timeouts.set(timeoutId, timeout);
            if (duration === 0) {
                timedOutIds.push(timeoutId);
                timeout.isTimedOut = true;
            }
            return timeoutId;
        },
    });
    if (!env.testUtils) {
        env.testUtils = {};
    }
    Object.assign(env.testUtils, {
        advanceTime: async duration => {
            await nextTick();
            for (const id of timeouts.keys()) {
                const timeout = timeouts.get(id);
                if (timeout.isTimedOut) {
                    continue;
                }
                timeout.duration = Math.max(timeout.duration - duration, 0);
                if (timeout.duration === 0) {
                    timedOutIds.push(id);
                }
            }
            while (timedOutIds.length > 0) {
                const id = timedOutIds.shift();
                const timeout = timeouts.get(id);
                timeouts.delete(id);
                timeout.func();
                await nextTick();
            }
            await nextTick();
        },
    });
    return env;
}

return {
    addMessagingToEnv,
    addTimeControlToEnv,
};

});