Bug 1194424 - Part 5: gtest for DeserializedStackFrame ubi::StackFrames; r=sfink
authorNick Fitzgerald <fitzgen@gmail.com>
Mon, 24 Aug 2015 09:29:44 -0700
changeset 259064 2928078657ba3b761f2c5d7eaf860c21e0f49e29
parent 259063 fbca64555f063b0dfcc4217208ddaac4b0783770
child 259065 9b0aa46d9a9402158067620c8defd81b37222fa9
push id29268
push userryanvm@gmail.com
push dateTue, 25 Aug 2015 00:37:23 +0000
treeherdermozilla-central@08015770c9d6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssfink
bugs1194424
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1194424 - Part 5: gtest for DeserializedStackFrame ubi::StackFrames; r=sfink
toolkit/devtools/server/DeserializedNode.h
toolkit/devtools/server/tests/gtest/DeserializedStackFrameUbiStackFrames.cpp
toolkit/devtools/server/tests/gtest/DevTools.h
toolkit/devtools/server/tests/gtest/moz.build
--- a/toolkit/devtools/server/DeserializedNode.h
+++ b/toolkit/devtools/server/DeserializedNode.h
@@ -163,16 +163,30 @@ struct DeserializedStackFrame {
     , owner(&owner)
   {
     MOZ_ASSERT(source);
   }
 
   JS::ubi::StackFrame getParentStackFrame() const;
 
   struct HashPolicy;
+
+protected:
+  // This is exposed only for MockDeserializedStackFrame in the gtests.
+  explicit DeserializedStackFrame()
+    : id(0)
+    , parent(Nothing())
+    , line(0)
+    , column(0)
+    , source(nullptr)
+    , functionDisplayName(nullptr)
+    , isSystem(false)
+    , isSelfHosted(false)
+    , owner(nullptr)
+  { };
 };
 
 struct DeserializedStackFrame::HashPolicy {
   using Lookup = StackFrameId;
 
   static js::HashNumber hash(const Lookup& lookup) {
     return hashIdDerivedFromPtr(lookup);
   }
new file mode 100644
--- /dev/null
+++ b/toolkit/devtools/server/tests/gtest/DeserializedStackFrameUbiStackFrames.cpp
@@ -0,0 +1,91 @@
+/* -*-  Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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/. */
+
+// Test that the `JS::ubi::StackFrame`s we create from
+// `mozilla::devtools::DeserializedStackFrame` instances look and behave as we would
+// like.
+
+#include "DevTools.h"
+#include "js/TypeDecls.h"
+#include "mozilla/devtools/DeserializedNode.h"
+
+using testing::Field;
+using testing::ReturnRef;
+
+// A mock DeserializedStackFrame for testing.
+struct MockDeserializedStackFrame : public DeserializedStackFrame
+{
+  MockDeserializedStackFrame() : DeserializedStackFrame() { }
+};
+
+DEF_TEST(DeserializedStackFrameUbiStackFrames, {
+    StackFrameId id = 1L << 42;
+    uint32_t line = 1337;
+    uint32_t column = 9; // 3 space tabs!?
+    const char16_t* source = MOZ_UTF16("my-javascript-file.js");
+    const char16_t* functionDisplayName = MOZ_UTF16("myFunctionName");
+
+    MockDeserializedStackFrame mocked;
+    mocked.id = id;
+    mocked.line = line;
+    mocked.column = column;
+    mocked.source = source;
+    mocked.functionDisplayName = functionDisplayName;
+
+    DeserializedStackFrame& deserialized = mocked;
+    JS::ubi::StackFrame ubiFrame(&deserialized);
+
+    // Test the JS::ubi::StackFrame accessors.
+
+    EXPECT_EQ(id, ubiFrame.identifier());
+    EXPECT_EQ(JS::ubi::StackFrame(), ubiFrame.parent());
+    EXPECT_EQ(line, ubiFrame.line());
+    EXPECT_EQ(column, ubiFrame.column());
+    EXPECT_EQ(JS::ubi::AtomOrTwoByteChars(source), ubiFrame.source());
+    EXPECT_EQ(JS::ubi::AtomOrTwoByteChars(functionDisplayName),
+              ubiFrame.functionDisplayName());
+    EXPECT_FALSE(ubiFrame.isSelfHosted());
+    EXPECT_FALSE(ubiFrame.isSystem());
+
+    JS::RootedObject savedFrame(cx);
+    EXPECT_TRUE(ubiFrame.constructSavedFrameStack(cx, &savedFrame));
+
+    uint32_t frameLine;
+    ASSERT_EQ(JS::SavedFrameResult::Ok, JS::GetSavedFrameLine(cx, savedFrame, &frameLine));
+    EXPECT_EQ(line, frameLine);
+
+    uint32_t frameColumn;
+    ASSERT_EQ(JS::SavedFrameResult::Ok, JS::GetSavedFrameColumn(cx, savedFrame, &frameColumn));
+    EXPECT_EQ(column, frameColumn);
+
+    JS::RootedObject parent(cx);
+    ASSERT_EQ(JS::SavedFrameResult::Ok, JS::GetSavedFrameParent(cx, savedFrame, &parent));
+    EXPECT_EQ(nullptr, parent);
+
+    ASSERT_EQ(NS_strlen(source), 21U);
+    char16_t sourceBuf[21] = {};
+
+    // Test when the length is shorter than the string length.
+    auto written = ubiFrame.source(RangedPtr<char16_t>(sourceBuf), 3);
+    EXPECT_EQ(written, 3U);
+    for (size_t i = 0; i < 3; i++) {
+      EXPECT_EQ(sourceBuf[i], source[i]);
+    }
+
+    written = ubiFrame.source(RangedPtr<char16_t>(sourceBuf), 21);
+    EXPECT_EQ(written, 21U);
+    for (size_t i = 0; i < 21; i++) {
+      EXPECT_EQ(sourceBuf[i], source[i]);
+    }
+
+    ASSERT_EQ(NS_strlen(functionDisplayName), 14U);
+    char16_t nameBuf[14] = {};
+
+    written = ubiFrame.functionDisplayName(RangedPtr<char16_t>(nameBuf), 14);
+    EXPECT_EQ(written, 14U);
+    for (size_t i = 0; i < 14; i++) {
+      EXPECT_EQ(nameBuf[i], functionDisplayName[i]);
+    }
+});
--- a/toolkit/devtools/server/tests/gtest/DevTools.h
+++ b/toolkit/devtools/server/tests/gtest/DevTools.h
@@ -13,16 +13,17 @@
 
 #include "gtest/gtest.h"
 #include "gmock/gmock.h"
 #include "mozilla/devtools/HeapSnapshot.h"
 #include "mozilla/dom/ChromeUtils.h"
 #include "mozilla/CycleCollectedJSRuntime.h"
 #include "mozilla/Move.h"
 #include "mozilla/UniquePtr.h"
+#include "js/Principals.h"
 #include "js/UbiNode.h"
 
 using namespace mozilla;
 using namespace mozilla::devtools;
 using namespace mozilla::dom;
 using namespace testing;
 
 // GTest fixture class that all of our tests derive from.
--- a/toolkit/devtools/server/tests/gtest/moz.build
+++ b/toolkit/devtools/server/tests/gtest/moz.build
@@ -7,16 +7,17 @@
 Library('devtoolstests')
 
 LOCAL_INCLUDES += [
     '/toolkit/devtools/server',
 ]
 
 UNIFIED_SOURCES = [
     'DeserializedNodeUbiNodes.cpp',
+    'DeserializedStackFrameUbiStackFrames.cpp',
     'DoesCrossZoneBoundaries.cpp',
     'DoesntCrossZoneBoundaries.cpp',
     'SerializesEdgeNames.cpp',
     'SerializesEverythingInHeapGraphOnce.cpp',
     'SerializesTypeNames.cpp',
     'UniqueStringHashPolicy.cpp',
 ]