Add converter utility for the time limits processor consistency test.

It will be used by the test engine later on to convert goldens to and from
the processor-specific structures.

Bug: 935711
Change-Id: I84d2ea9f8eb742b1d7f1a062eeea88dfb7f30d23
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1492091
Commit-Queue: G. Silva <gfaus@chromium.org>
Reviewed-by: Aga Wronska <agawronska@chromium.org>
Reviewed-by: Henrique Grandinetti <hgrandinetti@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638605}
diff --git a/chrome/browser/chromeos/BUILD.gn b/chrome/browser/chromeos/BUILD.gn
index f823f12..9b5206c 100644
--- a/chrome/browser/chromeos/BUILD.gn
+++ b/chrome/browser/chromeos/BUILD.gn
@@ -2767,6 +2767,8 @@
   check_includes = false
 
   sources = [
+    "child_accounts/time_limit_consistency_test/consistency_golden_converter.cc",
+    "child_accounts/time_limit_consistency_test/consistency_golden_converter_unittest.cc",
     "child_accounts/time_limit_consistency_test/consistency_golden_loader.cc",
     "child_accounts/time_limit_consistency_test/consistency_golden_loader_unittest.cc",
   ]
diff --git a/chrome/browser/chromeos/child_accounts/time_limit_consistency_test/consistency_golden_converter.cc b/chrome/browser/chromeos/child_accounts/time_limit_consistency_test/consistency_golden_converter.cc
new file mode 100644
index 0000000..802d286
--- /dev/null
+++ b/chrome/browser/chromeos/child_accounts/time_limit_consistency_test/consistency_golden_converter.cc
@@ -0,0 +1,110 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/chromeos/child_accounts/time_limit_consistency_test/consistency_golden_converter.h"
+
+#include "base/time/time.h"
+#include "base/values.h"
+#include "chrome/browser/chromeos/child_accounts/time_limit_test_utils.h"
+
+namespace chromeos {
+
+namespace utils = time_limit_test_utils;
+
+namespace time_limit_consistency {
+namespace {
+
+// Converts a ActivePolicies object from the time limit processor to a
+// ConsistencyGoldenPolicy used by the goldens.
+ConsistencyGoldenPolicy ConvertProcessorPolicyToGoldenPolicy(
+    usage_time_limit::ActivePolicies processor_policy) {
+  switch (processor_policy) {
+    case usage_time_limit::ActivePolicies::kOverride:
+      return OVERRIDE;
+    case usage_time_limit::ActivePolicies::kFixedLimit:
+      return FIXED_LIMIT;
+    case usage_time_limit::ActivePolicies::kUsageLimit:
+      return USAGE_LIMIT;
+    case usage_time_limit::ActivePolicies::kNoActivePolicy:
+      return NO_ACTIVE_POLICY;
+  }
+
+  NOTREACHED();
+  return UNSPECIFIED_POLICY;
+}
+
+// Converts the representation of a day of week used by the goldens to the one
+// used by the time limit processor.
+const char* ConvertGoldenDayToProcessorDay(ConsistencyGoldenEffectiveDay day) {
+  switch (day) {
+    case MONDAY:
+      return utils::kMonday;
+    case TUESDAY:
+      return utils::kTuesday;
+    case WEDNESDAY:
+      return utils::kWednesday;
+    case THURSDAY:
+      return utils::kThursday;
+    case FRIDAY:
+      return utils::kFriday;
+    case SATURDAY:
+      return utils::kSaturday;
+    case SUNDAY:
+      return utils::kSunday;
+    default:
+      NOTREACHED();
+      return nullptr;
+  }
+}
+
+}  // namespace
+
+std::unique_ptr<base::DictionaryValue> ConvertGoldenInputToProcessorInput(
+    ConsistencyGoldenInput input) {
+  // Random date representing the last time the policies were updated,
+  // since the tests won't take this into account for now.
+  base::Time last_updated = utils::TimeFromString("1 Jan 2018 10:00 GMT+0300");
+
+  std::unique_ptr<base::DictionaryValue> policy =
+      utils::CreateTimeLimitPolicy(base::TimeDelta::FromHours(6));
+
+  /* Begin Window Limits data */
+
+  if (input.window_limits_size() > 0) {
+    for (ConsistencyGoldenWindowLimitEntry window_limit :
+         input.window_limits()) {
+      utils::AddTimeWindowLimit(
+          policy.get(),
+          ConvertGoldenDayToProcessorDay(window_limit.effective_day()),
+          utils::CreateTime(window_limit.starts_at().hour(),
+                            window_limit.starts_at().minute()),
+          utils::CreateTime(window_limit.ends_at().hour(),
+                            window_limit.ends_at().minute()),
+          last_updated);
+    }
+  }
+
+  /* End Window Limits data */
+
+  return policy;
+}
+
+ConsistencyGoldenOutput ConvertProcessorOutputToGoldenOutput(
+    usage_time_limit::State state) {
+  ConsistencyGoldenOutput golden_output;
+
+  golden_output.set_is_locked(state.is_locked);
+  golden_output.set_active_policy(
+      ConvertProcessorPolicyToGoldenPolicy(state.active_policy));
+
+  if (state.is_locked) {
+    golden_output.set_next_unlocking_time_millis(
+        state.next_unlock_time.ToJavaTime());
+  }
+
+  return golden_output;
+}
+
+}  // namespace time_limit_consistency
+}  // namespace chromeos
diff --git a/chrome/browser/chromeos/child_accounts/time_limit_consistency_test/consistency_golden_converter.h b/chrome/browser/chromeos/child_accounts/time_limit_consistency_test/consistency_golden_converter.h
new file mode 100644
index 0000000..5d55e1a
--- /dev/null
+++ b/chrome/browser/chromeos/child_accounts/time_limit_consistency_test/consistency_golden_converter.h
@@ -0,0 +1,36 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+// Utilities for converting goldens to and from the structures used by the time
+// limit processor.
+
+#ifndef CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMIT_CONSISTENCY_TEST_CONSISTENCY_GOLDEN_CONVERTER_H_
+#define CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMIT_CONSISTENCY_TEST_CONSISTENCY_GOLDEN_CONVERTER_H_
+
+#include <memory>
+
+#include "chrome/browser/chromeos/child_accounts/time_limit_consistency_test/goldens/consistency_golden.pb.h"
+#include "chrome/browser/chromeos/child_accounts/usage_time_limit_processor.h"
+
+namespace base {
+class DictionaryValue;
+}  // namespace base
+
+namespace chromeos {
+namespace time_limit_consistency {
+
+// Converts the input part of a consistency golden case to the structure used by
+// the time limit processor.
+std::unique_ptr<base::DictionaryValue> ConvertGoldenInputToProcessorInput(
+    ConsistencyGoldenInput input);
+
+// Converts the output struct generated by the time limit processor to the
+// consistency golden output proto.
+ConsistencyGoldenOutput ConvertProcessorOutputToGoldenOutput(
+    usage_time_limit::State state);
+
+}  // namespace time_limit_consistency
+}  // namespace chromeos
+
+#endif  // CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_TIME_LIMIT_CONSISTENCY_TEST_CONSISTENCY_GOLDEN_CONVERTER_H_
diff --git a/chrome/browser/chromeos/child_accounts/time_limit_consistency_test/consistency_golden_converter_unittest.cc b/chrome/browser/chromeos/child_accounts/time_limit_consistency_test/consistency_golden_converter_unittest.cc
new file mode 100644
index 0000000..ea9ce85
--- /dev/null
+++ b/chrome/browser/chromeos/child_accounts/time_limit_consistency_test/consistency_golden_converter_unittest.cc
@@ -0,0 +1,106 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/chromeos/child_accounts/time_limit_consistency_test/consistency_golden_converter.h"
+
+#include "base/time/time.h"
+#include "chrome/browser/chromeos/child_accounts/time_limit_consistency_test/proto_matcher.h"
+#include "chrome/browser/chromeos/child_accounts/time_limit_test_utils.h"
+#include "chrome/browser/chromeos/child_accounts/usage_time_limit_processor.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace chromeos {
+
+namespace utils = time_limit_test_utils;
+
+namespace time_limit_consistency {
+
+using ConsistencyGoldenConverterTest = testing::Test;
+
+// A timestamp used during the tests. Nothing special about the date it
+// points to.
+constexpr int64_t kTestTimestamp = 1548709200000L;
+
+TEST_F(ConsistencyGoldenConverterTest, ConvertInputWhenEmpty) {
+  ConsistencyGoldenInput input;
+
+  std::unique_ptr<base::DictionaryValue> actual_output =
+      ConvertGoldenInputToProcessorInput(input);
+
+  std::unique_ptr<base::DictionaryValue> expected_output =
+      utils::CreateTimeLimitPolicy(base::TimeDelta::FromHours(6));
+
+  EXPECT_TRUE(*actual_output == *expected_output);
+}
+
+TEST_F(ConsistencyGoldenConverterTest, ConvertInputWithBedtimes) {
+  base::Time last_updated = utils::TimeFromString("1 Jan 2018 10:00 GMT+0300");
+
+  ConsistencyGoldenInput input;
+  std::unique_ptr<base::DictionaryValue> expected_output =
+      utils::CreateTimeLimitPolicy(base::TimeDelta::FromHours(6));
+
+  // First window: Wednesday, 22:30 to 8:00
+  ConsistencyGoldenWindowLimitEntry* window_one = input.add_window_limits();
+  window_one->mutable_starts_at()->set_hour(22);
+  window_one->mutable_starts_at()->set_minute(30);
+  window_one->mutable_ends_at()->set_hour(8);
+  window_one->mutable_ends_at()->set_minute(0);
+  window_one->set_effective_day(WEDNESDAY);
+  utils::AddTimeWindowLimit(expected_output.get(), utils::kWednesday,
+                            utils::CreateTime(22, 30), utils::CreateTime(8, 0),
+                            last_updated);
+
+  // Second window: Saturday, 18:45 to 22:30
+  ConsistencyGoldenWindowLimitEntry* window_two = input.add_window_limits();
+  window_two->mutable_starts_at()->set_hour(18);
+  window_two->mutable_starts_at()->set_minute(45);
+  window_two->mutable_ends_at()->set_hour(22);
+  window_two->mutable_ends_at()->set_minute(30);
+  window_two->set_effective_day(SATURDAY);
+  utils::AddTimeWindowLimit(expected_output.get(), utils::kSaturday,
+                            utils::CreateTime(18, 45),
+                            utils::CreateTime(22, 30), last_updated);
+
+  std::unique_ptr<base::DictionaryValue> actual_output =
+      ConvertGoldenInputToProcessorInput(input);
+
+  EXPECT_TRUE(*actual_output == *expected_output);
+}
+
+TEST_F(ConsistencyGoldenConverterTest, ConvertOutputWhenUnlocked) {
+  usage_time_limit::State state;
+  state.is_locked = false;
+  state.active_policy = usage_time_limit::ActivePolicies::kNoActivePolicy;
+  state.next_unlock_time = base::Time::FromJavaTime(kTestTimestamp);
+
+  ConsistencyGoldenOutput actual_output =
+      ConvertProcessorOutputToGoldenOutput(state);
+
+  ConsistencyGoldenOutput expected_output;
+  expected_output.set_is_locked(false);
+  expected_output.set_active_policy(NO_ACTIVE_POLICY);
+
+  EXPECT_THAT(actual_output, EqualsProto(expected_output));
+}
+
+TEST_F(ConsistencyGoldenConverterTest, ConvertOutputWhenLocked) {
+  usage_time_limit::State state;
+  state.is_locked = true;
+  state.active_policy = usage_time_limit::ActivePolicies::kFixedLimit;
+  state.next_unlock_time = base::Time::FromJavaTime(kTestTimestamp);
+
+  ConsistencyGoldenOutput actual_output =
+      ConvertProcessorOutputToGoldenOutput(state);
+
+  ConsistencyGoldenOutput expected_output;
+  expected_output.set_is_locked(true);
+  expected_output.set_active_policy(FIXED_LIMIT);
+  expected_output.set_next_unlocking_time_millis(kTestTimestamp);
+
+  EXPECT_THAT(actual_output, EqualsProto(expected_output));
+}
+
+}  // namespace time_limit_consistency
+}  // namespace chromeos