Bug 1046156 - "bind" entire AndroidLog module; r=mfinkle

--HG--
extra : rebase_source : 7131cfcb7b5bca9ee8b3af822cf9c4a9f1ae8028
This commit is contained in:
Myk Melez 2014-07-30 08:35:03 -07:00
parent f29eef6be6
commit c7fa0bb48d
2 changed files with 56 additions and 13 deletions

View File

@ -3,6 +3,24 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
const TAG = "AndroidLogTest";
const VERBOSE_MESSAGE = "This is a verbose message.";
const DEBUG_MESSAGE = "This is a debug message.";
const INFO_MESSAGE = "This is an info message.";
const WARNING_MESSAGE = "This is a warning message.";
const ERROR_MESSAGE = "This is an error message.";
// Number of bytes we expect to log. This isn't equivalent to the number
// of characters, although the difference is consistent, so we can calculate it
// from the lengths of the messages and tag. We include the length of "Gecko"
// because the module prepends it to the tag.
const VERBOSE_BYTES = "Gecko".length + TAG.length + VERBOSE_MESSAGE.length + 3;
const DEBUG_BYTES = "Gecko".length + TAG.length + DEBUG_MESSAGE.length + 3;
const INFO_BYTES = "Gecko".length + TAG.length + INFO_MESSAGE.length + 3;
const WARNING_BYTES = "Gecko".length + TAG.length + WARNING_MESSAGE.length + 3;
const ERROR_BYTES = "Gecko".length + TAG.length + ERROR_MESSAGE.length + 3;
add_task(function test_AndroidLog() {
Components.utils.import("resource://gre/modules/AndroidLog.jsm");
@ -14,22 +32,30 @@ add_task(function test_AndroidLog() {
do_check_true("w" in AndroidLog && typeof AndroidLog.w == "function");
do_check_true("e" in AndroidLog && typeof AndroidLog.e == "function");
// I don't know how to check that these messages actually make it to the log,
// but at least we can ensure that they don't cause the test process to crash
// Ensure that the functions don't cause the test process to crash
// (because of some change to the native object being accessed via ctypes)
// and return the right values (the number of bytes--not characters--logged).
do_check_eq(48, AndroidLog.v("AndroidLogTest", "This is a verbose message."));
do_check_eq(46, AndroidLog.d("AndroidLogTest", "This is a debug message."));
do_check_eq(46, AndroidLog.i("AndroidLogTest", "This is an info message."));
do_check_eq(48, AndroidLog.w("AndroidLogTest", "This is a warning message."));
do_check_eq(47, AndroidLog.e("AndroidLogTest", "This is an error message."));
// and return the right values (the number of bytes logged).
// XXX Ensure that these messages actually make it to the log (bug 1046096).
do_check_eq(VERBOSE_BYTES, AndroidLog.v(TAG, VERBOSE_MESSAGE));
do_check_eq(DEBUG_BYTES, AndroidLog.d(TAG, DEBUG_MESSAGE));
do_check_eq(INFO_BYTES, AndroidLog.i(TAG, INFO_MESSAGE));
do_check_eq(WARNING_BYTES, AndroidLog.w(TAG, WARNING_MESSAGE));
do_check_eq(ERROR_BYTES, AndroidLog.e(TAG, ERROR_MESSAGE));
// Ensure the functions work when bound with null value for thisArg parameter.
do_check_eq(48, AndroidLog.v.bind(null, "AndroidLogTest")("This is a verbose message."));
do_check_eq(46, AndroidLog.d.bind(null, "AndroidLogTest")("This is a debug message."));
do_check_eq(46, AndroidLog.i.bind(null, "AndroidLogTest")("This is an info message."));
do_check_eq(48, AndroidLog.w.bind(null, "AndroidLogTest")("This is a warning message."));
do_check_eq(47, AndroidLog.e.bind(null, "AndroidLogTest")("This is an error message."));
do_check_eq(VERBOSE_BYTES, AndroidLog.v.bind(null, TAG)(VERBOSE_MESSAGE));
do_check_eq(DEBUG_BYTES, AndroidLog.d.bind(null, TAG)(DEBUG_MESSAGE));
do_check_eq(INFO_BYTES, AndroidLog.i.bind(null, TAG)(INFO_MESSAGE));
do_check_eq(WARNING_BYTES, AndroidLog.w.bind(null, TAG)(WARNING_MESSAGE));
do_check_eq(ERROR_BYTES, AndroidLog.e.bind(null, TAG)(ERROR_MESSAGE));
// Ensure the functions work when the module object is "bound" to a tag.
let Log = AndroidLog.bind(TAG);
do_check_eq(VERBOSE_BYTES, Log.v(VERBOSE_MESSAGE));
do_check_eq(DEBUG_BYTES, Log.d(DEBUG_MESSAGE));
do_check_eq(INFO_BYTES, Log.i(INFO_MESSAGE));
do_check_eq(WARNING_BYTES, Log.w(WARNING_MESSAGE));
do_check_eq(ERROR_BYTES, Log.e(ERROR_MESSAGE));
// Ensure the functions work when the tag length is greater than the maximum
// tag length.

View File

@ -27,6 +27,12 @@
* // Bind a function with a tag to replace a bespoke dump/log/debug function:
* let debug = Log.d.bind(null, "MyModule");
* debug("This is a debug message.");
* // Outputs "D/GeckoMyModule(#####): This is a debug message."
*
* // Or "bind" the module object to a tag to automatically tag messages:
* Log = Log.bind("MyModule");
* Log.d("This is a debug message.");
* // Outputs "D/GeckoMyModule(#####): This is a debug message."
*
* Note: the module automatically prepends "Gecko" to the tag you specify,
* since all tags used by Fennec code should start with that string; and it
@ -67,6 +73,17 @@ let AndroidLog = {
i: (tag, msg) => __android_log_write(ANDROID_LOG_INFO, "Gecko" + tag.substring(0, MAX_TAG_LENGTH), msg),
w: (tag, msg) => __android_log_write(ANDROID_LOG_WARN, "Gecko" + tag.substring(0, MAX_TAG_LENGTH), msg),
e: (tag, msg) => __android_log_write(ANDROID_LOG_ERROR, "Gecko" + tag.substring(0, MAX_TAG_LENGTH), msg),
bind: function(tag) {
return {
MAX_TAG_LENGTH: MAX_TAG_LENGTH,
v: AndroidLog.v.bind(null, tag),
d: AndroidLog.d.bind(null, tag),
i: AndroidLog.i.bind(null, tag),
w: AndroidLog.w.bind(null, tag),
e: AndroidLog.e.bind(null, tag),
};
},
};
if (typeof Components == "undefined") {