1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/server/tests/unit/test_longstringactor.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,107 @@ 1.4 +/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; js-indent-level: 2; -*- */ 1.5 +/* Any copyright is dedicated to the Public Domain. 1.6 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.7 + 1.8 +function run_test() 1.9 +{ 1.10 + Cu.import("resource://gre/modules/jsdebugger.jsm"); 1.11 + addDebuggerToGlobal(this); 1.12 + let loader = Cc["@mozilla.org/moz/jssubscript-loader;1"] 1.13 + .getService(Components.interfaces.mozIJSSubScriptLoader); 1.14 + loader.loadSubScript("resource://gre/modules/devtools/server/actors/script.js"); 1.15 + 1.16 + test_LSA_disconnect(); 1.17 + test_LSA_grip(); 1.18 + test_LSA_onSubstring(); 1.19 +} 1.20 + 1.21 +const TEST_STRING = "This is a very long string!"; 1.22 + 1.23 +function makeMockLongStringActor() 1.24 +{ 1.25 + let string = TEST_STRING; 1.26 + let actor = new LongStringActor(string); 1.27 + actor.actorID = "longString1"; 1.28 + actor.registeredPool = { 1.29 + longStringActors: { 1.30 + longString1: actor 1.31 + } 1.32 + }; 1.33 + return actor; 1.34 +} 1.35 + 1.36 +function test_LSA_disconnect() 1.37 +{ 1.38 + let actor = makeMockLongStringActor(); 1.39 + do_check_eq(actor.registeredPool.longStringActors[actor.actorID], actor); 1.40 + 1.41 + actor.disconnect(); 1.42 + do_check_eq(actor.registeredPool.longStringActors[actor.actorID], void 0); 1.43 +} 1.44 + 1.45 +function test_LSA_substring() 1.46 +{ 1.47 + let actor = makeMockLongStringActor(); 1.48 + do_check_eq(actor._substring(0, 4), TEST_STRING.substring(0, 4)); 1.49 + do_check_eq(actor._substring(6, 9), TEST_STRING.substring(6, 9)); 1.50 + do_check_eq(actor._substring(0, TEST_STRING.length), TEST_STRING); 1.51 +} 1.52 + 1.53 +function test_LSA_grip() 1.54 +{ 1.55 + let actor = makeMockLongStringActor(); 1.56 + 1.57 + let grip = actor.grip(); 1.58 + do_check_eq(grip.type, "longString"); 1.59 + do_check_eq(grip.initial, TEST_STRING.substring(0, DebuggerServer.LONG_STRING_INITIAL_LENGTH)); 1.60 + do_check_eq(grip.length, TEST_STRING.length); 1.61 + do_check_eq(grip.actor, actor.actorID); 1.62 +} 1.63 + 1.64 +function test_LSA_onSubstring() 1.65 +{ 1.66 + let actor = makeMockLongStringActor(); 1.67 + let response; 1.68 + 1.69 + // From the start 1.70 + response = actor.onSubstring({ 1.71 + start: 0, 1.72 + end: 4 1.73 + }); 1.74 + do_check_eq(response.from, actor.actorID); 1.75 + do_check_eq(response.substring, TEST_STRING.substring(0, 4)); 1.76 + 1.77 + // In the middle 1.78 + response = actor.onSubstring({ 1.79 + start: 5, 1.80 + end: 8 1.81 + }); 1.82 + do_check_eq(response.from, actor.actorID); 1.83 + do_check_eq(response.substring, TEST_STRING.substring(5, 8)); 1.84 + 1.85 + // Whole string 1.86 + response = actor.onSubstring({ 1.87 + start: 0, 1.88 + end: TEST_STRING.length 1.89 + }); 1.90 + do_check_eq(response.from, actor.actorID); 1.91 + do_check_eq(response.substring, TEST_STRING); 1.92 + 1.93 + // Negative index 1.94 + response = actor.onSubstring({ 1.95 + start: -5, 1.96 + end: TEST_STRING.length 1.97 + }); 1.98 + do_check_eq(response.from, actor.actorID); 1.99 + do_check_eq(response.substring, 1.100 + TEST_STRING.substring(-5, TEST_STRING.length)); 1.101 + 1.102 + // Past the end 1.103 + response = actor.onSubstring({ 1.104 + start: TEST_STRING.length - 5, 1.105 + end: 100 1.106 + }); 1.107 + do_check_eq(response.from, actor.actorID); 1.108 + do_check_eq(response.substring, 1.109 + TEST_STRING.substring(TEST_STRING.length - 5, 100)); 1.110 +}