browser_dbg_controller-evaluate-01.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/ */
  4. /**
  5. * Tests the public evaluation API from the debugger controller.
  6. */
  7. const TAB_URL = EXAMPLE_URL + "doc_script-switching-01.html";
  8. function test() {
  9. Task.spawn(function* () {
  10. const options = {
  11. source: EXAMPLE_URL + "code_script-switching-01.js",
  12. line: 1
  13. };
  14. const [tab,, panel] = yield initDebugger(TAB_URL, options);
  15. const win = panel.panelWin;
  16. const frames = win.DebuggerController.StackFrames;
  17. const framesView = win.DebuggerView.StackFrames;
  18. const sourcesView = win.DebuggerView.Sources;
  19. const editorView = win.DebuggerView.editor;
  20. const events = win.EVENTS;
  21. const queries = win.require("./content/queries");
  22. const constants = win.require("./content/constants");
  23. const actions = bindActionCreators(panel);
  24. const getState = win.DebuggerController.getState;
  25. function checkView(frameDepth, selectedSource, caretLine, editorText) {
  26. is(win.gThreadClient.state, "paused",
  27. "Should only be getting stack frames while paused.");
  28. is(framesView.itemCount, 2,
  29. "Should have four frames.");
  30. is(framesView.selectedDepth, frameDepth,
  31. "The correct frame is selected in the widget.");
  32. is(sourcesView.selectedIndex, selectedSource,
  33. "The correct source is selected in the widget.");
  34. ok(isCaretPos(panel, caretLine),
  35. "Editor caret location is correct.");
  36. is(editorView.getText().search(editorText[0]), editorText[1],
  37. "The correct source is not displayed.");
  38. }
  39. // Cache the sources text to avoid having to wait for their
  40. // retrieval.
  41. const sources = queries.getSources(getState());
  42. yield promise.all(Object.keys(sources).map(k => {
  43. return actions.loadSourceText(sources[k]);
  44. }));
  45. is(Object.keys(getState().sources.sourcesText).length, 2,
  46. "There should be two cached sources in the cache.");
  47. // Eval while not paused.
  48. try {
  49. yield frames.evaluate("foo");
  50. } catch (error) {
  51. is(error.message, "No stack frame available.",
  52. "Evaluating shouldn't work while the debuggee isn't paused.");
  53. }
  54. callInTab(tab, "firstCall");
  55. yield waitForSourceAndCaretAndScopes(panel, "-02.js", 6);
  56. checkView(0, 1, 6, [/secondCall/, 118]);
  57. // Eval in the topmost frame, while paused.
  58. let updatedView = waitForDebuggerEvents(panel, events.FETCHED_SCOPES);
  59. let result = yield frames.evaluate("foo");
  60. ok(!result.throw, "The evaluation hasn't thrown.");
  61. is(result.return.type, "object", "The evaluation return type is correct.");
  62. is(result.return.class, "Function", "The evaluation return class is correct.");
  63. yield updatedView;
  64. checkView(0, 1, 6, [/secondCall/, 118]);
  65. ok(true, "Evaluating in the topmost frame works properly.");
  66. // Eval in a different frame, while paused.
  67. updatedView = waitForDebuggerEvents(panel, events.FETCHED_SCOPES);
  68. try {
  69. yield frames.evaluate("foo", { depth: 1 }); // oldest frame
  70. } catch (result) {
  71. is(result.return.type, "object", "The evaluation thrown type is correct.");
  72. is(result.return.class, "Error", "The evaluation thrown class is correct.");
  73. ok(!result.return, "The evaluation hasn't returned.");
  74. }
  75. yield updatedView;
  76. checkView(0, 1, 6, [/secondCall/, 118]);
  77. ok(true, "Evaluating in a custom frame works properly.");
  78. // Eval in a non-existent frame, while paused.
  79. waitForDebuggerEvents(panel, events.FETCHED_SCOPES).then(() => {
  80. ok(false, "Shouldn't have updated the view when trying to evaluate " +
  81. "an expression in a non-existent stack frame.");
  82. });
  83. try {
  84. yield frames.evaluate("foo", { depth: 4 }); // non-existent frame
  85. } catch (error) {
  86. is(error.message, "No stack frame available.",
  87. "Evaluating shouldn't work if the specified frame doesn't exist.");
  88. }
  89. yield resumeDebuggerThenCloseAndFinish(panel);
  90. });
  91. }