Fix nits in CPDF_NameTree.

- Rename GetRootForTest() to GetRootForTesting(), to be consistent with
  CreateForTesting().
- Make |m_pRoot| a const pointer. Move code to find the root dict into
  GetNameTreeRoot().

Change-Id: Ida68b2a9f75ed3ab0b4d430b406d3f3daddaa930
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/68193
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfdoc/cpdf_nametree.cpp b/core/fpdfdoc/cpdf_nametree.cpp
index a42376e..549fccb 100644
--- a/core/fpdfdoc/cpdf_nametree.cpp
+++ b/core/fpdfdoc/cpdf_nametree.cpp
@@ -19,6 +19,19 @@
 
 constexpr int kNameTreeMaxRecursion = 32;
 
+CPDF_Dictionary* GetNameTreeRoot(CPDF_Document* pDoc,
+                                 const ByteString& category) {
+  CPDF_Dictionary* pRoot = pDoc->GetRoot();
+  if (!pRoot)
+    return nullptr;
+
+  CPDF_Dictionary* pNames = pRoot->GetDictFor("Names");
+  if (!pNames)
+    return nullptr;
+
+  return pNames->GetDictFor(category);
+}
+
 std::pair<WideString, WideString> GetNodeLimitsMaybeSwap(CPDF_Array* pLimits) {
   ASSERT(pLimits);
   WideString csLeft = pLimits->GetUnicodeTextAt(0);
@@ -298,17 +311,8 @@
 
 CPDF_NameTree::CPDF_NameTree(CPDF_Dictionary* pRoot) : m_pRoot(pRoot) {}
 
-CPDF_NameTree::CPDF_NameTree(CPDF_Document* pDoc, const ByteString& category) {
-  CPDF_Dictionary* pRoot = pDoc->GetRoot();
-  if (!pRoot)
-    return;
-
-  CPDF_Dictionary* pNames = pRoot->GetDictFor("Names");
-  if (!pNames)
-    return;
-
-  m_pRoot.Reset(pNames->GetDictFor(category));
-}
+CPDF_NameTree::CPDF_NameTree(CPDF_Document* pDoc, const ByteString& category)
+    : CPDF_NameTree(GetNameTreeRoot(pDoc, category)) {}
 
 CPDF_NameTree::~CPDF_NameTree() = default;
 
diff --git a/core/fpdfdoc/cpdf_nametree.h b/core/fpdfdoc/cpdf_nametree.h
index 6ad1c1e..01d85aa 100644
--- a/core/fpdfdoc/cpdf_nametree.h
+++ b/core/fpdfdoc/cpdf_nametree.h
@@ -35,12 +35,12 @@
   CPDF_Array* LookupNamedDest(CPDF_Document* pDoc, const WideString& sName);
 
   size_t GetCount() const;
-  CPDF_Dictionary* GetRootForTest() const { return m_pRoot.Get(); }
+  CPDF_Dictionary* GetRootForTesting() const { return m_pRoot.Get(); }
 
  private:
   explicit CPDF_NameTree(CPDF_Dictionary* pRoot);
 
-  RetainPtr<CPDF_Dictionary> m_pRoot;
+  const RetainPtr<CPDF_Dictionary> m_pRoot;
 };
 
 #endif  // CORE_FPDFDOC_CPDF_NAMETREE_H_
diff --git a/core/fpdfdoc/cpdf_nametree_unittest.cpp b/core/fpdfdoc/cpdf_nametree_unittest.cpp
index 6ef0d1b..9e3f483 100644
--- a/core/fpdfdoc/cpdf_nametree_unittest.cpp
+++ b/core/fpdfdoc/cpdf_nametree_unittest.cpp
@@ -208,7 +208,7 @@
 
   // Check that the node on the first level has the expected limits.
   CPDF_Dictionary* pKid1 =
-      name_tree->GetRootForTest()->GetArrayFor("Kids")->GetDictAt(0);
+      name_tree->GetRootForTesting()->GetArrayFor("Kids")->GetDictAt(0);
   ASSERT_TRUE(pKid1);
   CheckLimitsArray(pKid1, "0.txt", "99.txt");
 
@@ -255,7 +255,7 @@
 
   // Retrieve the kid dictionaries.
   CPDF_Dictionary* pKid1 =
-      name_tree->GetRootForTest()->GetArrayFor("Kids")->GetDictAt(0);
+      name_tree->GetRootForTesting()->GetArrayFor("Kids")->GetDictAt(0);
   ASSERT_TRUE(pKid1);
   CPDF_Dictionary* pKid2 = pKid1->GetArrayFor("Kids")->GetDictAt(0);
   ASSERT_TRUE(pKid2);
@@ -325,9 +325,9 @@
   EXPECT_EQ(555, name_tree->LookupValue(L"5.txt")->GetInteger());
   EXPECT_TRUE(name_tree->LookupValueAndName(0, &csName));
   EXPECT_STREQ(L"5.txt", csName.c_str());
-  EXPECT_EQ(1u, name_tree->GetRootForTest()->GetArrayFor("Kids")->size());
+  EXPECT_EQ(1u, name_tree->GetRootForTesting()->GetArrayFor("Kids")->size());
   EXPECT_TRUE(name_tree->DeleteValueAndName(0));
-  EXPECT_EQ(0u, name_tree->GetRootForTest()->GetArrayFor("Kids")->size());
+  EXPECT_EQ(0u, name_tree->GetRootForTesting()->GetArrayFor("Kids")->size());
 
   // Check that the tree is now empty.
   EXPECT_EQ(0u, name_tree->GetCount());