Bug 709568 - Part 0: Create SmsMessages from JS. r=smaug

This commit is contained in:
Philipp von Weitershausen 2011-12-24 06:02:52 +01:00
Родитель d6b6dbb160
Коммит 4419311470
12 изменённых файлов: 303 добавлений и 2 удалений

Просмотреть файл

@ -39,12 +39,15 @@ topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
relativesrcdir = dom/sms
include $(DEPTH)/config/autoconf.mk
PARALLEL_DIRS = interfaces src
ifdef ENABLE_TESTS
DIRS += tests
XPCSHELL_TESTS = tests
endif
include $(topsrcdir)/config/rules.mk

Просмотреть файл

@ -19,6 +19,7 @@
*
* Contributor(s):
* Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
* Philipp von Weitershausen <philipp@weitershausen.de>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -36,7 +37,7 @@
#include "nsISupports.idl"
[scriptable, function, uuid(0a0037ba-585e-41f4-b0a5-1d0224353105)]
[scriptable, builtinclass, uuid(20da0c51-2224-49ae-afe9-4b309c6d8f84)]
interface nsIDOMMozSmsMessage : nsISupports
{
// TODO: we should add SENT and RECEIVED DOMString constants, see bug 443316.

Просмотреть файл

@ -19,6 +19,7 @@
*
* Contributor(s):
* Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
* Philipp von Weitershausen <philipp@weitershausen.de>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -36,15 +37,25 @@
#include "nsISupports.idl"
interface nsIDOMMozSmsMessage;
%{C++
#define NS_SMSSERVICE_CID { 0xbada3cb8, 0xa568, 0x4dff, { 0xb5, 0x43, 0x52, 0xbb, 0xb3, 0x14, 0x31, 0x21 } }
#define SMSSERVICE_CONTRACTID "@mozilla.org/sms/smsservice;1"
%}
[scriptable, function, uuid(24edea1d-130a-4ae3-9522-0e2a7ee2885d)]
[scriptable, builtinclass, uuid(a0fbbe74-5d61-4b7e-b7ab-9b5224f9e5e9)]
interface nsISmsService : nsISupports
{
boolean hasSupport();
unsigned short getNumberOfMessagesForText(in DOMString text);
void send(in DOMString number, in DOMString message);
[implicit_jscontext]
nsIDOMMozSmsMessage createSmsMessage(in long id,
in DOMString delivery,
in DOMString sender,
in DOMString receiver,
in DOMString body,
in jsval timestamp);
};

Просмотреть файл

@ -44,6 +44,9 @@ namespace sms {
extern const char* kSmsReceivedObserverTopic; // Defined in the .cpp.
#define DELIVERY_RECEIVED NS_LITERAL_STRING("received")
#define DELIVERY_SENT NS_LITERAL_STRING("sent")
} // namespace sms
} // namespace dom
} // namespace mozilla

Просмотреть файл

@ -20,6 +20,7 @@
*
* Contributor(s):
* Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
* Philipp von Weitershausen <philipp@weitershausen.de>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
@ -38,6 +39,7 @@
#include "SmsMessage.h"
#include "nsIDOMClassInfo.h"
#include "jsapi.h" // For OBJECT_TO_JSVAL and JS_NewDateObjectMsec
#include "jsdate.h" // For js_DateGetMsecSinceEpoch
#include "Constants.h"
DOMCI_DATA(MozSmsMessage, mozilla::dom::sms::SmsMessage)
@ -60,6 +62,57 @@ SmsMessage::SmsMessage(const SmsMessageData& aData)
{
}
/* static */ nsresult
SmsMessage::Create(PRInt32 aId,
const nsAString& aDelivery,
const nsAString& aSender,
const nsAString& aReceiver,
const nsAString& aBody,
const jsval& aTimestamp,
JSContext* aCx,
nsIDOMMozSmsMessage** aMessage)
{
*aMessage = nsnull;
// SmsMessageData exposes these as references, so we can simply assign
// to them.
SmsMessageData data;
data.id() = aId;
data.sender() = nsString(aSender);
data.receiver() = nsString(aReceiver);
data.body() = nsString(aBody);
if (aDelivery.Equals(DELIVERY_RECEIVED)) {
data.delivery() = eDeliveryState_Received;
} else if (aDelivery.Equals(DELIVERY_SENT)) {
data.delivery() = eDeliveryState_Sent;
} else {
return NS_ERROR_INVALID_ARG;
}
// We support both a Date object and a millisecond timestamp as a number.
if (aTimestamp.isObject()) {
JSObject& obj = aTimestamp.toObject();
if (!JS_ObjectIsDate(aCx, &obj)) {
return NS_ERROR_INVALID_ARG;
}
data.timestamp() = js_DateGetMsecSinceEpoch(aCx, &obj);
} else {
if (!aTimestamp.isNumber()) {
return NS_ERROR_INVALID_ARG;
}
jsdouble number = aTimestamp.toNumber();
if (static_cast<PRUint64>(number) != number) {
return NS_ERROR_INVALID_ARG;
}
data.timestamp() = static_cast<PRUint64>(number);
}
nsCOMPtr<nsIDOMMozSmsMessage> message = new SmsMessage(data);
message.swap(*aMessage);
return NS_OK;
}
const SmsMessageData&
SmsMessage::GetData() const
{

Просмотреть файл

@ -41,6 +41,7 @@
#include "mozilla/dom/sms/PSms.h"
#include "nsIDOMSmsMessage.h"
#include "nsString.h"
#include "jspubtd.h"
namespace mozilla {
namespace dom {
@ -54,6 +55,14 @@ public:
SmsMessage(const SmsMessageData& aData);
static nsresult Create(PRInt32 aId,
const nsAString& aDelivery,
const nsAString& aSender,
const nsAString& aReceiver,
const nsAString& aBody,
const JS::Value& aTimestamp,
JSContext* aCx,
nsIDOMMozSmsMessage** aMessage);
const SmsMessageData& GetData() const;
private:

Просмотреть файл

@ -20,6 +20,7 @@
*
* Contributor(s):
* Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
* Philipp von Weitershausen <philipp@weitershausen.de>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
@ -35,8 +36,10 @@
*
* ***** END LICENSE BLOCK ***** */
#include "mozilla/dom/sms/SmsMessage.h"
#include "SmsService.h"
#include "AndroidBridge.h"
#include "jsapi.h"
namespace mozilla {
namespace dom {
@ -74,6 +77,20 @@ SmsService::Send(const nsAString& aNumber, const nsAString& aMessage)
return NS_OK;
}
NS_IMETHODIMP
SmsService::CreateSmsMessage(PRInt32 aId,
const nsAString& aDelivery,
const nsAString& aSender,
const nsAString& aReceiver,
const nsAString& aBody,
const jsval& aTimestamp,
JSContext* aCx,
nsIDOMMozSmsMessage** aMessage)
{
return SmsMessage::Create(
aId, aDelivery, aSender, aReceiver, aBody, aTimestamp, aCx, aMessage);
}
} // namespace sms
} // namespace dom
} // namespace mozilla

Просмотреть файл

@ -20,6 +20,7 @@
*
* Contributor(s):
* Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
* Philipp von Weitershausen <philipp@weitershausen.de>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
@ -35,7 +36,9 @@
*
* ***** END LICENSE BLOCK ***** */
#include "mozilla/dom/sms/SmsMessage.h"
#include "SmsService.h"
#include "jsapi.h"
namespace mozilla {
namespace dom {
@ -65,6 +68,20 @@ SmsService::Send(const nsAString& aNumber, const nsAString& aMessage)
return NS_OK;
}
NS_IMETHODIMP
SmsService::CreateSmsMessage(PRInt32 aId,
const nsAString& aDelivery,
const nsAString& aSender,
const nsAString& aReceiver,
const nsAString& aBody,
const jsval& aTimestamp,
JSContext* aCx,
nsIDOMMozSmsMessage** aMessage)
{
return SmsMessage::Create(
aId, aDelivery, aSender, aReceiver, aBody, aTimestamp, aCx, aMessage);
}
} // namespace sms
} // namespace dom
} // namespace mozilla

Просмотреть файл

@ -38,7 +38,9 @@
#include "mozilla/dom/ContentChild.h"
#include "SmsIPCService.h"
#include "nsXULAppAPI.h"
#include "jsapi.h"
#include "mozilla/dom/sms/SmsChild.h"
#include "mozilla/dom/sms/SmsMessage.h"
namespace mozilla {
namespace dom {
@ -82,6 +84,20 @@ SmsIPCService::Send(const nsAString& aNumber, const nsAString& aMessage)
return NS_OK;
}
NS_IMETHODIMP
SmsIPCService::CreateSmsMessage(PRInt32 aId,
const nsAString& aDelivery,
const nsAString& aSender,
const nsAString& aReceiver,
const nsAString& aBody,
const jsval& aTimestamp,
JSContext* aCx,
nsIDOMMozSmsMessage** aMessage)
{
return SmsMessage::Create(
aId, aDelivery, aSender, aReceiver, aBody, aTimestamp, aCx, aMessage);
}
} // namespace sms
} // namespace dom
} // namespace mozilla

Просмотреть файл

@ -0,0 +1,165 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
function do_check_throws(f, result, stack) {
if (!stack) {
stack = Components.stack.caller;
}
try {
f();
} catch (exc) {
if (exc.result == result)
return;
do_throw("expected result " + result + ", caught " + exc, stack);
}
do_throw("expected result " + result + ", none thrown", stack);
}
let gSmsService = Cc["@mozilla.org/sms/smsservice;1"]
.getService(Ci.nsISmsService);
function newMessage() {
return gSmsService.createSmsMessage.apply(gSmsService, arguments);
}
function run_test() {
run_next_test();
}
/**
* Ensure an SmsMessage object created has sensible initial values.
*/
add_test(function test_interface() {
let sms = newMessage(null, "sent", null, null, null, new Date());
do_check_true(sms instanceof Ci.nsIDOMMozSmsMessage);
do_check_eq(sms.id, 0);
do_check_eq(sms.delivery, "sent");
do_check_eq(sms.receiver, null);
do_check_eq(sms.sender, null);
do_check_eq(sms.body, null);
do_check_true(sms.timestamp instanceof Date);
run_next_test();
});
/**
* Verify that attributes are read-only.
*/
add_test(function test_readonly_attributes() {
let sms = newMessage(null, "received", null, null, null, new Date());
sms.id = 1;
do_check_eq(sms.id, 0);
sms.delivery = "sent";
do_check_eq(sms.delivery, "received");
sms.receiver = "a receiver";
do_check_eq(sms.receiver, null);
sms.sender = "a sender";
do_check_eq(sms.sender, null);
sms.body = "a body";
do_check_eq(sms.body, null);
let oldTimestamp = sms.timestamp.getTime();
sms.timestamp = new Date();
do_check_eq(sms.timestamp.getTime(), oldTimestamp);
run_next_test();
});
/**
* Test supplying the timestamp as a number of milliseconds.
*/
add_test(function test_timestamp_number() {
let ts = Date.now();
let sms = newMessage(42, "sent", "the sender", "the receiver", "the body", ts);
do_check_eq(sms.id, 42);
do_check_eq(sms.delivery, "sent");
do_check_eq(sms.sender, "the sender");
do_check_eq(sms.receiver, "the receiver");
do_check_eq(sms.body, "the body");
do_check_true(sms.timestamp instanceof Date);
do_check_eq(sms.timestamp.getTime(), ts);
run_next_test();
});
/**
* Test supplying the timestamp as a Date object.
*/
add_test(function test_timestamp_date() {
let date = new Date();
let sms = newMessage(42, "sent", "the sender", "the receiver", "the body", date);
do_check_eq(sms.id, 42);
do_check_eq(sms.delivery, "sent");
do_check_eq(sms.sender, "the sender");
do_check_eq(sms.receiver, "the receiver");
do_check_eq(sms.body, "the body");
do_check_true(sms.timestamp instanceof Date);
do_check_eq(sms.timestamp.getTime(), date.getTime());
run_next_test();
});
/**
* Test that a floating point number for the timestamp is not allowed.
*/
add_test(function test_invalid_timestamp_float() {
do_check_throws(function() {
newMessage(42, "sent", "the sender", "the receiver", "the body", 3.1415);
}, Cr.NS_ERROR_INVALID_ARG);
run_next_test();
});
/**
* Test that a null value for the timestamp is not allowed.
*/
add_test(function test_invalid_timestamp_null() {
do_check_throws(function() {
newMessage(42, "sent", "the sender", "the receiver", "the body", null);
}, Cr.NS_ERROR_INVALID_ARG);
run_next_test();
});
/**
* Test that undefined for the timestamp is not allowed.
*/
add_test(function test_invalid_timestamp_undefined() {
do_check_throws(function() {
newMessage(42, "sent", "the sender", "the receiver", "the body", undefined);
}, Cr.NS_ERROR_INVALID_ARG);
run_next_test();
});
/**
* Test that a random object for the timestamp is not allowed.
*/
add_test(function test_invalid_timestamp_object() {
do_check_throws(function() {
newMessage(42, "sent", "the sender", "the receiver", "the body", {});
}, Cr.NS_ERROR_INVALID_ARG);
run_next_test();
});
/**
* Test that an invalid delivery string is not accepted.
*/
add_test(function test_invalid_delivery_string() {
do_check_throws(function() {
newMessage(42, "this is invalid", "the sender", "the receiver", "the body",
new Date());
}, Cr.NS_ERROR_INVALID_ARG);
run_next_test();
});
/**
* Test that a number is not accepted for the 'delivery' argument.
*/
add_test(function test_invalid_delivery_string() {
do_check_throws(function() {
newMessage(42, 1, "the sender", "the receiver", "the body", new Date());
}, Cr.NS_ERROR_INVALID_ARG);
run_next_test();
});

Просмотреть файл

@ -0,0 +1,5 @@
[DEFAULT]
head =
tail =
[test_smsservice_createsmsmessage.js]

Просмотреть файл

@ -6,6 +6,7 @@
[include:parser/xml/test/unit/xpcshell.ini]
[include:image/test/unit/xpcshell.ini]
[include:dom/plugins/test/unit/xpcshell.ini]
[include:dom/sms/tests/xpcshell.ini]
[include:dom/src/json/test/unit/xpcshell.ini]
[include:dom/tests/unit/xpcshell.ini]
[include:content/xtf/test/unit/xpcshell.ini]