fixup tp: strategy changes (FIX unit tests!)

Change-Id: I1fe1bb8913ed98b3dec9ef33b95c8ef22f4efad5
diff --git a/src/trace_processor/importers/proto/winscope/windowmanager_parser.cc b/src/trace_processor/importers/proto/winscope/windowmanager_parser.cc
index 8c2df7d..0b2d28e 100644
--- a/src/trace_processor/importers/proto/winscope/windowmanager_parser.cc
+++ b/src/trace_processor/importers/proto/winscope/windowmanager_parser.cc
@@ -28,6 +28,8 @@
 #include "src/trace_processor/types/trace_processor_context.h"
 #include "src/trace_processor/util/winscope_proto_mapping.h"
 
+#include <fstream>
+
 namespace perfetto::trace_processor::winscope {
 
 WindowManagerParser::WindowManagerParser(WinscopeContext* context)
@@ -37,6 +39,12 @@
       args_parser_{*context->trace_processor_context_->descriptor_pool_} {}
 
 void WindowManagerParser::Parse(int64_t timestamp, protozero::ConstBytes blob) {
+  std::ofstream outFile("trace_packet.pb", std::ios::binary);
+  PERFETTO_DCHECK(outFile.is_open());
+  outFile.write(reinterpret_cast<const char*>(blob.data),
+                static_cast<long>(blob.size));
+  outFile.close();
+
   protos::pbzero::WindowManagerTraceEntry::Decoder entry_decoder(blob);
   protos::pbzero::WindowManagerServiceDumpProto::Decoder service_decoder(
       entry_decoder.window_manager_service());
diff --git a/src/trace_processor/importers/proto/winscope/windowmanager_walk_strategy.cc b/src/trace_processor/importers/proto/winscope/windowmanager_walk_strategy.cc
index 3661bc0..81b84d0 100644
--- a/src/trace_processor/importers/proto/winscope/windowmanager_walk_strategy.cc
+++ b/src/trace_processor/importers/proto/winscope/windowmanager_walk_strategy.cc
@@ -15,6 +15,7 @@
  */
 
 #include "src/trace_processor/importers/proto/winscope/windowmanager_walk_strategy.h"
+#include "protos/perfetto/trace/android/server/windowmanagerservice.pbzero.h"
 
 namespace perfetto::trace_processor::winscope {
 
@@ -232,20 +233,39 @@
     const {
   base::FlatHashMap<int32_t, ParentLink> child_to_parent;
 
+  // TODO: adapt unit test (RootWindowContainerProto info entirely contained in
+  // root_window_container field, no longer partially in window_containers flat
+  // list (element without parent))
+  protos::pbzero::RootWindowContainerProto::Decoder root(
+      service.root_window_container());
+  CollectParentLinks(
+      protos::pbzero::WindowContainerProto::Decoder(root.window_container()),
+      &child_to_parent);
+
   for (auto it = service.window_containers(); it; ++it) {
     protos::pbzero::WindowContainerChildProto::Decoder child(*it);
-    int32_t token = child.token();
-
-    auto window_container = GetWindowContainer(child);
-    uint32_t index = 0;
-    for (auto cit = window_container.child_tokens(); cit; ++cit) {
-      int32_t child_token = *cit;
-      child_to_parent[child_token] = {token, index++};
-    }
+    CollectParentLinks(GetWindowContainer(child), &child_to_parent);
   }
   return child_to_parent;
 }
 
+void IterateWalkStrategy::CollectParentLinks(
+    const protos::pbzero::WindowContainerProto::Decoder& window_container,
+    base::FlatHashMap<int32_t, ParentLink>* child_to_parent) const {
+  protos::pbzero::IdentifierProto::Decoder identifier(
+      window_container.identifier());
+  if (!identifier.has_hash_code()) {
+    PERFETTO_DCHECK(false);  // TODO(keanmariotti): return
+                             // base::ErrStatus(kErrorMessageMissingField);
+  }
+  int32_t token = identifier.hash_code();
+  uint32_t index = 0;
+  for (auto cit = window_container.child_tokens(); cit; ++cit) {
+    int32_t child_token = *cit;
+    (*child_to_parent)[child_token] = {token, index++};
+  }
+}
+
 void IterateWalkStrategy::DispatchToCallbacks(
     const protos::pbzero::WindowManagerServiceDumpProto::Decoder& service,
     const base::FlatHashMap<int32_t, ParentLink>& child_to_parent,
@@ -256,32 +276,23 @@
         void(const protos::pbzero::WindowContainerChildProto::Decoder&,
              int32_t parent_token,
              uint32_t child_index)>& onChild) const {
-  bool root_found = false;
+  // Root
+  protos::pbzero::RootWindowContainerProto::Decoder root(
+      service.root_window_container());
+  // TODO(keanmariotti): get rid of second argument
+  onRoot(root, protos::pbzero::WindowContainerProto::Decoder(
+                   root.window_container()));
+
+  // Children
   for (auto it = service.window_containers(); it; ++it) {
     protos::pbzero::WindowContainerChildProto::Decoder child(*it);
     int32_t token = child.token();
 
     auto* parent_info = child_to_parent.Find(token);
-    if (parent_info) {
-      onChild(child, parent_info->parent_token, parent_info->child_index);
-    } else {
-      // Root candidate
-      root_found = true;
-      auto window_container = GetWindowContainer(child);
-      protos::pbzero::RootWindowContainerProto::Decoder root(
-          service.root_window_container());
-      onRoot(root, window_container);
+    if (!parent_info) {
+      PERFETTO_DCHECK(false);  // TODO(keanmariotti): return error instead
     }
-  }
-
-  // TODO(keanmariotti): return base::Status instead
-  //  Fallback for EmptyHierarchy
-  if (!root_found) {
-    protos::pbzero::RootWindowContainerProto::Decoder root(
-        service.root_window_container());
-    protos::pbzero::WindowContainerProto::Decoder empty_window_container(
-        nullptr, 0);
-    onRoot(root, empty_window_container);
+    onChild(child, parent_info->parent_token, parent_info->child_index);
   }
 }
 
diff --git a/src/trace_processor/importers/proto/winscope/windowmanager_walk_strategy.h b/src/trace_processor/importers/proto/winscope/windowmanager_walk_strategy.h
index c766025..4a8e102 100644
--- a/src/trace_processor/importers/proto/winscope/windowmanager_walk_strategy.h
+++ b/src/trace_processor/importers/proto/winscope/windowmanager_walk_strategy.h
@@ -99,6 +99,10 @@
       const protos::pbzero::WindowManagerServiceDumpProto::Decoder& service)
       const;
 
+  void CollectParentLinks(
+      const protos::pbzero::WindowContainerProto::Decoder& window_container,
+      base::FlatHashMap<int32_t, ParentLink>* child_to_parent) const;
+
   void DispatchToCallbacks(
       const protos::pbzero::WindowManagerServiceDumpProto::Decoder& service,
       const base::FlatHashMap<int32_t, ParentLink>& child_to_parent,