Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chromeos / network / onc / onc_test_utils.cc
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/values.h"
12 #include "chromeos/chromeos_test_utils.h"
13
14 namespace chromeos {
15 namespace onc {
16 namespace test_utils {
17
18 namespace {
19
20 // The name of the component directory to get the test data from.
21 const char kNetworkComponentDirectory[] = "network";
22
23 }  // namespace
24
25 std::string ReadTestData(const std::string& filename) {
26   base::FilePath path;
27   if (!chromeos::test_utils::GetTestDataPath(kNetworkComponentDirectory,
28                                              filename,
29                                              &path)) {
30     NOTREACHED() << "Unable to get test data path for "
31                  << kNetworkComponentDirectory << "/" << filename;
32     return "";
33   }
34   std::string result;
35   base::ReadFileToString(path, &result);
36   return result;
37 }
38
39 scoped_ptr<base::DictionaryValue> ReadTestDictionary(
40     const std::string& filename) {
41   base::DictionaryValue* dict = NULL;
42   base::FilePath path;
43   if (!chromeos::test_utils::GetTestDataPath(kNetworkComponentDirectory,
44                                              filename,
45                                              &path)) {
46     NOTREACHED() << "Unable to get test dictionary path for "
47                  << kNetworkComponentDirectory << "/" << filename;
48     return make_scoped_ptr(dict);
49   }
50
51   JSONFileValueSerializer serializer(path);
52   serializer.set_allow_trailing_comma(true);
53
54   std::string error_message;
55   base::Value* content = serializer.Deserialize(NULL, &error_message);
56   CHECK(content != NULL) << "Couldn't json-deserialize file '"
57                          << filename << "': " << error_message;
58
59   CHECK(content->GetAsDictionary(&dict))
60       << "File '" << filename
61       << "' does not contain a dictionary as expected, but type "
62       << content->GetType();
63   return make_scoped_ptr(dict);
64 }
65
66 ::testing::AssertionResult Equals(const base::Value* expected,
67                                   const base::Value* actual) {
68   CHECK(expected != NULL);
69   if (actual == NULL)
70     return ::testing::AssertionFailure() << "Actual value pointer is NULL";
71
72   if (expected->Equals(actual))
73     return ::testing::AssertionSuccess() << "Values are equal";
74
75   return ::testing::AssertionFailure() << "Values are unequal.\n"
76                                        << "Expected value:\n" << *expected
77                                        << "Actual value:\n" << *actual;
78 }
79
80 }  // namespace test_utils
81 }  // namespace onc
82 }  // namespace chromeos