AAPT2: Change XmlDom to exclude Namespace as a node

In preparation for exporting an XML proto format for UAM to consume,
this change brings the XML DOM API more in line with other APIs that
do not make the Namespace a separate node.

Treating Namespace declarations as just properties of an Element
node makes the implementation of algorithms much simpler, as
the constraints that Namespace nodes have only one child
are now built in and traversing to find Element nodes
is much simpler.

Also made a bunch of quality of life improvements, like formatting and
comment style.

Test: make aapt2_tests
Change-Id: Ib97ff1c4252b7907e2cc1f13a448dc4ca3b809a4
diff --git a/tools/aapt2/test/Common.h b/tools/aapt2/test/Common.h
index d7b46ca..e6b38c0 100644
--- a/tools/aapt2/test/Common.h
+++ b/tools/aapt2/test/Common.h
@@ -142,10 +142,97 @@
   return android::StringPiece16(arg) == a;
 }
 
-MATCHER_P(ValueEq, a,
-          std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {
-  return arg.Equals(&a);
-}
+class ValueEq {
+ public:
+  template <typename arg_type>
+  class BaseImpl : public ::testing::MatcherInterface<arg_type> {
+    BaseImpl(const BaseImpl&) = default;
+
+    void DescribeTo(::std::ostream* os) const override {
+      *os << "is equal to " << *expected_;
+    }
+
+    void DescribeNegationTo(::std::ostream* os) const override {
+      *os << "is not equal to " << *expected_;
+    }
+
+   protected:
+    BaseImpl(const Value* expected) : expected_(expected) {
+    }
+
+    const Value* expected_;
+  };
+
+  template <typename T, bool>
+  class Impl {};
+
+  template <typename T>
+  class Impl<T, false> : public ::testing::MatcherInterface<T> {
+   public:
+    explicit Impl(const Value* expected) : expected_(expected) {
+    }
+
+    bool MatchAndExplain(T x, ::testing::MatchResultListener* listener) const override {
+      return expected_->Equals(&x);
+    }
+
+    void DescribeTo(::std::ostream* os) const override {
+      *os << "is equal to " << *expected_;
+    }
+
+    void DescribeNegationTo(::std::ostream* os) const override {
+      *os << "is not equal to " << *expected_;
+    }
+
+   private:
+    DISALLOW_COPY_AND_ASSIGN(Impl);
+
+    const Value* expected_;
+  };
+
+  template <typename T>
+  class Impl<T, true> : public ::testing::MatcherInterface<T> {
+   public:
+    explicit Impl(const Value* expected) : expected_(expected) {
+    }
+
+    bool MatchAndExplain(T x, ::testing::MatchResultListener* listener) const override {
+      return expected_->Equals(x);
+    }
+
+    void DescribeTo(::std::ostream* os) const override {
+      *os << "is equal to " << *expected_;
+    }
+
+    void DescribeNegationTo(::std::ostream* os) const override {
+      *os << "is not equal to " << *expected_;
+    }
+
+   private:
+    DISALLOW_COPY_AND_ASSIGN(Impl);
+
+    const Value* expected_;
+  };
+
+  ValueEq(const Value& expected) : expected_(&expected) {
+  }
+  ValueEq(const Value* expected) : expected_(expected) {
+  }
+  ValueEq(const ValueEq&) = default;
+
+  template <typename T>
+  operator ::testing::Matcher<T>() const {
+    return ::testing::Matcher<T>(new Impl<T, std::is_pointer<T>::value>(expected_));
+  }
+
+ private:
+  const Value* expected_;
+};
+
+// MATCHER_P(ValueEq, a,
+//          std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {
+//  return arg.Equals(&a);
+//}
 
 MATCHER_P(StrValueEq, a,
           std::string(negation ? "isn't" : "is") + " equal to " + ::testing::PrintToString(a)) {