1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chromeos/network/onc/onc_test_utils.h"
6
7#include "base/files/file_path.h"
8#include "base/files/file_util.h"
9#include "base/json/json_file_value_serializer.h"
10#include "base/logging.h"
11#include "base/path_service.h"
12#include "base/values.h"
13#include "chromeos/chromeos_test_utils.h"
14
15namespace chromeos {
16namespace onc {
17namespace test_utils {
18
19namespace {
20
21// The name of the component directory to get the test data from.
22const char kNetworkComponentDirectory[] = "network";
23
24}  // namespace
25
26std::string ReadTestData(const std::string& filename) {
27  base::FilePath path;
28  if (!chromeos::test_utils::GetTestDataPath(kNetworkComponentDirectory,
29                                             filename,
30                                             &path)) {
31    NOTREACHED() << "Unable to get test data path for "
32                 << kNetworkComponentDirectory << "/" << filename;
33    return "";
34  }
35  std::string result;
36  base::ReadFileToString(path, &result);
37  return result;
38}
39
40scoped_ptr<base::DictionaryValue> ReadTestDictionary(
41    const std::string& filename) {
42  base::DictionaryValue* dict = NULL;
43  base::FilePath path;
44  if (!chromeos::test_utils::GetTestDataPath(kNetworkComponentDirectory,
45                                             filename,
46                                             &path)) {
47    NOTREACHED() << "Unable to get test dictionary path for "
48                 << kNetworkComponentDirectory << "/" << filename;
49    return make_scoped_ptr(dict);
50  }
51
52  JSONFileValueSerializer serializer(path);
53  serializer.set_allow_trailing_comma(true);
54
55  std::string error_message;
56  base::Value* content = serializer.Deserialize(NULL, &error_message);
57  CHECK(content != NULL) << "Couldn't json-deserialize file '"
58                         << filename << "': " << error_message;
59
60  CHECK(content->GetAsDictionary(&dict))
61      << "File '" << filename
62      << "' does not contain a dictionary as expected, but type "
63      << content->GetType();
64  return make_scoped_ptr(dict);
65}
66
67::testing::AssertionResult Equals(const base::Value* expected,
68                                  const base::Value* actual) {
69  CHECK(expected != NULL);
70  if (actual == NULL)
71    return ::testing::AssertionFailure() << "Actual value pointer is NULL";
72
73  if (expected->Equals(actual))
74    return ::testing::AssertionSuccess() << "Values are equal";
75
76  return ::testing::AssertionFailure() << "Values are unequal.\n"
77                                       << "Expected value:\n" << *expected
78                                       << "Actual value:\n" << *actual;
79}
80
81}  // namespace test_utils
82}  // namespace onc
83}  // namespace chromeos
84