summaryrefslogtreecommitdiff
path: root/addons/mail_bot/static
diff options
context:
space:
mode:
authorChristophe Simonis <chs@odoo.com>2019-01-09 15:44:05 +0000
committerChristophe Simonis <chs@odoo.com>2019-01-09 15:56:53 +0000
commit8aa8548d8ab68feb13f3bde51c557db84877e809 (patch)
tree930790c1c84a4412db777986f6b2077808560d95 /addons/mail_bot/static
parent55a48e341580a6a400c36b6ba0d7b12ff9997f12 (diff)
parent3e4138deaa23b3631dc3fce33cd6d1b3be28ffbd (diff)
[MERGE] forward port branch 12.0 up to 3e4138deaa2
closes odoo/odoo#30045
Diffstat (limited to 'addons/mail_bot/static')
-rw-r--r--addons/mail_bot/static/src/js/mailbot_service.js36
-rw-r--r--addons/mail_bot/static/src/js/systray_messaging_menu.js1
-rw-r--r--addons/mail_bot/static/tests/systray_messaging_menu_tests.js97
3 files changed, 110 insertions, 24 deletions
diff --git a/addons/mail_bot/static/src/js/mailbot_service.js b/addons/mail_bot/static/src/js/mailbot_service.js
index 8863ea94ba0..5312e5d5129 100644
--- a/addons/mail_bot/static/src/js/mailbot_service.js
+++ b/addons/mail_bot/static/src/js/mailbot_service.js
@@ -12,15 +12,9 @@ var MailBotService = AbstractService.extend({
* @override
*/
start: function () {
- var self = this;
+ this._hasRequest = (window.Notification && window.Notification.permission === "default") || false;
if ('odoobot_initialized' in session && ! session.odoobot_initialized) {
- setTimeout(function () {
- session.odoobot_initialized = true;
- self._rpc({
- model: 'mail.channel',
- method: 'init_odoobot',
- });
- }, 2*60*1000);
+ this._showOdoobotTimeout();
}
},
@@ -60,7 +54,31 @@ var MailBotService = AbstractService.extend({
* @returns {boolean}
*/
isRequestingForNativeNotifications: function () {
- return window.Notification && window.Notification.permission === "default";
+ return this._hasRequest;
+ },
+ /**
+ * Called when user either accepts or refuses push notifications.
+ */
+ removeRequest: function () {
+ this._hasRequest = false;
+ },
+
+ //--------------------------------------------------------------------------
+ // Private
+ //--------------------------------------------------------------------------
+
+ /**
+ * @private
+ */
+ _showOdoobotTimeout: function () {
+ var self = this;
+ setTimeout(function () {
+ session.odoobot_initialized = true;
+ self._rpc({
+ model: 'mail.channel',
+ method: 'init_odoobot',
+ });
+ }, 2*60*1000);
},
});
diff --git a/addons/mail_bot/static/src/js/systray_messaging_menu.js b/addons/mail_bot/static/src/js/systray_messaging_menu.js
index f7e64ff751e..3b7ee2f9e38 100644
--- a/addons/mail_bot/static/src/js/systray_messaging_menu.js
+++ b/addons/mail_bot/static/src/js/systray_messaging_menu.js
@@ -56,6 +56,7 @@ return MessagingMenu.include({
* @param {string} value
*/
_handleResponseNotificationPermission: function (value) {
+ this.call('mailbot_service', 'removeRequest');
if (value !== 'granted') {
this.call('bus_service', 'sendNotification', _t('Permission denied'),
_t('Odoo will not have the permission to send native notifications on this device.'));
diff --git a/addons/mail_bot/static/tests/systray_messaging_menu_tests.js b/addons/mail_bot/static/tests/systray_messaging_menu_tests.js
index 88f5c161f66..d22c9ca80da 100644
--- a/addons/mail_bot/static/tests/systray_messaging_menu_tests.js
+++ b/addons/mail_bot/static/tests/systray_messaging_menu_tests.js
@@ -24,31 +24,44 @@ QUnit.module('MessagingMenu', {
},
};
- // Patch mailbot_service so that it does do not do any RPC, and
- // the detection of push notifications permissions can be simulated in
- // the test cases. By default, shows 'OdooBot has a request'
+ this.services = _.extend({}, mailTestUtils.getMailServices(), {
+ mailbot_service: MailBotService
+ });
+
+ // By default, permission are to ask user for push notification ("default").
+ // Use requestPermissionDef to simulate permission change, e.g. "granted"
+ this.requestPermissionDef = $.Deferred();
+ this.ORIGINAL_WINDOW_NOTIFICATION = window.Notification;
+ window.Notification = {
+ permission: "default",
+ requestPermission: function () {
+ return self.requestPermissionDef;
+ },
+ };
+
+ // Patch mailbot_service so that it does do not do any RPC
this.isMailbotRequesting = true;
testUtils.mock.patch(MailBotService, {
/**
* @override
*/
- start: function () {},
+ _showOdoobotTimeout: function () {},
+ });
+
+ // Patch Bus Service so that it does not play any audio (may raise
+ // Uncaught rejected Promise due to Chrome autoplay policy: https://goo.gl/xX8pDD)
+ testUtils.patch(this.services.bus_service, {
/**
* @override
- * @returns {boolean}
*/
- isRequestingForNativeNotifications: function () {
- return self.isMailbotRequesting;
- },
- });
-
- this.services = _.extend({}, mailTestUtils.getMailServices(), {
- mailbot_service: MailBotService
+ _beep: function () {},
});
},
afterEach: function () {
- // unpatch MailBotService
+ // unpatch MailBotService and BusService
testUtils.mock.unpatch(MailBotService);
+ testUtils.mock.unpatch(this.services.bus_service);
+ window.Notification = this.ORIGINAL_WINDOW_NOTIFICATION;
}
});
@@ -79,10 +92,10 @@ QUnit.test('messaging menu widget: rendering with OdooBot has a request', functi
messagingMenu.destroy();
});
-QUnit.test('messaging menu widget: rendering without OdooBot has a request', function (assert) {
+QUnit.test('messaging menu widget: rendering without OdooBot has a request (denied)', function (assert) {
assert.expect(3);
- this.isMailbotRequesting = false;
+ window.Notification.permission = 'denied';
var messagingMenu = new MessagingMenu();
testUtils.mock.addMockEnvironment(messagingMenu, {
@@ -102,6 +115,60 @@ QUnit.test('messaging menu widget: rendering without OdooBot has a request', fun
messagingMenu.destroy();
});
+QUnit.test('messaging menu widget: rendering without OdooBot has a request (accepted)', function (assert) {
+ assert.expect(3);
+
+ window.Notification.permission = 'granted';
+
+ var messagingMenu = new MessagingMenu();
+ testUtils.addMockEnvironment(messagingMenu, {
+ data: this.data,
+ services: this.services,
+ });
+ messagingMenu.appendTo($('#qunit-fixture'));
+
+ assert.containsOnce(messagingMenu, '.o_notification_counter',
+ "should display a notification counter next to the messaging menu");
+ assert.strictEqual(messagingMenu.$('.o_notification_counter').text(), '0',
+ "should display a counter of '0' next to the messaging menu");
+ testUtils.dom.click(messagingMenu.$('.dropdown-toggle'));
+ assert.containsNone(messagingMenu, '.o_preview_info',
+ "should display no preview in the messaging menu");
+
+ messagingMenu.destroy();
+});
+
+QUnit.test('messaging menu widget: respond to notification prompt', function (assert) {
+ assert.expect(4);
+
+ var messagingMenu = new MessagingMenu();
+ testUtils.addMockEnvironment(messagingMenu, {
+ data: this.data,
+ services: this.services,
+ });
+ messagingMenu.appendTo($('#qunit-fixture'));
+
+ assert.containsOnce(messagingMenu, '.o_notification_counter',
+ "should display a notification counter next to the messaging menu");
+ assert.strictEqual(messagingMenu.$('.o_notification_counter').text(), '1',
+ "should display a counter of '1' next to the messaging menu");
+
+ testUtils.dom.click(messagingMenu.$('.dropdown-toggle'));
+ testUtils.dom.click(messagingMenu.$('.o_preview_info'));
+
+ // simulate "default" response, which is equivalent to "Not Now" in Firefox.
+ this.requestPermissionDef.resolve("default");
+
+ assert.strictEqual(messagingMenu.$('.o_notification_counter').text(), '0',
+ "should display a counter of '0' next to the messaging menu");
+
+ testUtils.dom.click(messagingMenu.$('.dropdown-toggle'));
+ assert.containsNone(messagingMenu, '.o_preview_info',
+ "should display no preview in the messaging menu");
+
+ messagingMenu.destroy();
+});
+
});
});