DeserializedStackFrameUbiStackFrames.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. // Test that the `JS::ubi::StackFrame`s we create from
  6. // `mozilla::devtools::DeserializedStackFrame` instances look and behave as we would
  7. // like.
  8. #include "DevTools.h"
  9. #include "js/TypeDecls.h"
  10. #include "mozilla/devtools/DeserializedNode.h"
  11. using testing::Field;
  12. using testing::ReturnRef;
  13. // A mock DeserializedStackFrame for testing.
  14. struct MockDeserializedStackFrame : public DeserializedStackFrame
  15. {
  16. MockDeserializedStackFrame() : DeserializedStackFrame() { }
  17. };
  18. DEF_TEST(DeserializedStackFrameUbiStackFrames, {
  19. StackFrameId id = uint64_t(1) << 42;
  20. uint32_t line = 1337;
  21. uint32_t column = 9; // 3 space tabs!?
  22. const char16_t* source = u"my-javascript-file.js";
  23. const char16_t* functionDisplayName = u"myFunctionName";
  24. MockDeserializedStackFrame mocked;
  25. mocked.id = id;
  26. mocked.line = line;
  27. mocked.column = column;
  28. mocked.source = source;
  29. mocked.functionDisplayName = functionDisplayName;
  30. DeserializedStackFrame& deserialized = mocked;
  31. JS::ubi::StackFrame ubiFrame(&deserialized);
  32. // Test the JS::ubi::StackFrame accessors.
  33. EXPECT_EQ(id, ubiFrame.identifier());
  34. EXPECT_EQ(JS::ubi::StackFrame(), ubiFrame.parent());
  35. EXPECT_EQ(line, ubiFrame.line());
  36. EXPECT_EQ(column, ubiFrame.column());
  37. EXPECT_EQ(JS::ubi::AtomOrTwoByteChars(source), ubiFrame.source());
  38. EXPECT_EQ(JS::ubi::AtomOrTwoByteChars(functionDisplayName),
  39. ubiFrame.functionDisplayName());
  40. EXPECT_FALSE(ubiFrame.isSelfHosted(cx));
  41. EXPECT_FALSE(ubiFrame.isSystem());
  42. JS::RootedObject savedFrame(cx);
  43. EXPECT_TRUE(ubiFrame.constructSavedFrameStack(cx, &savedFrame));
  44. uint32_t frameLine;
  45. ASSERT_EQ(JS::SavedFrameResult::Ok, JS::GetSavedFrameLine(cx, savedFrame, &frameLine));
  46. EXPECT_EQ(line, frameLine);
  47. uint32_t frameColumn;
  48. ASSERT_EQ(JS::SavedFrameResult::Ok, JS::GetSavedFrameColumn(cx, savedFrame, &frameColumn));
  49. EXPECT_EQ(column, frameColumn);
  50. JS::RootedObject parent(cx);
  51. ASSERT_EQ(JS::SavedFrameResult::Ok, JS::GetSavedFrameParent(cx, savedFrame, &parent));
  52. EXPECT_EQ(nullptr, parent);
  53. ASSERT_EQ(NS_strlen(source), 21U);
  54. char16_t sourceBuf[21] = {};
  55. // Test when the length is shorter than the string length.
  56. auto written = ubiFrame.source(RangedPtr<char16_t>(sourceBuf), 3);
  57. EXPECT_EQ(written, 3U);
  58. for (size_t i = 0; i < 3; i++) {
  59. EXPECT_EQ(sourceBuf[i], source[i]);
  60. }
  61. written = ubiFrame.source(RangedPtr<char16_t>(sourceBuf), 21);
  62. EXPECT_EQ(written, 21U);
  63. for (size_t i = 0; i < 21; i++) {
  64. EXPECT_EQ(sourceBuf[i], source[i]);
  65. }
  66. ASSERT_EQ(NS_strlen(functionDisplayName), 14U);
  67. char16_t nameBuf[14] = {};
  68. written = ubiFrame.functionDisplayName(RangedPtr<char16_t>(nameBuf), 14);
  69. EXPECT_EQ(written, 14U);
  70. for (size_t i = 0; i < 14; i++) {
  71. EXPECT_EQ(nameBuf[i], functionDisplayName[i]);
  72. }
  73. });