blob: 5d7f5a10d8e05111bb79b3fc1caa8a71a7dfe4bf [file] [log] [blame]
bauerb4c2863e2015-01-06 00:26:301// Copyright 2014 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 "base/bind.h"
6#include "base/json/json_reader.h"
7#include "base/json/json_writer.h"
8#include "base/memory/scoped_ptr.h"
9#include "base/values.h"
10#include "chrome/browser/safe_json_parser.h"
11#include "chrome/test/base/in_process_browser_test.h"
12#include "content/public/test/test_browser_thread_bundle.h"
13#include "content/public/test/test_utils.h"
14
15namespace {
16
17std::string MaybeToJson(const base::Value* value) {
18 if (!value)
19 return "(null)";
20
21 std::string json;
22 if (!base::JSONWriter::Write(value, &json))
23 return "(invalid value)";
24
25 return json;
26}
27
28} // namespace
29
30class SafeJsonParserTest : public InProcessBrowserTest {
31 protected:
32 void TestParse(const std::string& json) {
33 SCOPED_TRACE(json);
34 DCHECK(!message_loop_runner_);
35 message_loop_runner_ = new content::MessageLoopRunner;
36
37 std::string error;
38 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError(
39 json, base::JSON_PARSE_RFC, nullptr, &error));
40
41 SafeJsonParser::SuccessCallback success_callback;
42 SafeJsonParser::ErrorCallback error_callback;
43 if (value) {
44 success_callback =
45 base::Bind(&SafeJsonParserTest::ExpectValue, base::Unretained(this),
46 base::Passed(&value));
47 error_callback = base::Bind(&SafeJsonParserTest::FailWithError,
48 base::Unretained(this));
49 } else {
50 success_callback = base::Bind(&SafeJsonParserTest::FailWithValue,
51 base::Unretained(this));
52 error_callback = base::Bind(&SafeJsonParserTest::ExpectError,
53 base::Unretained(this), error);
54 }
55 scoped_refptr<SafeJsonParser> parser =
56 new SafeJsonParser(json, success_callback, error_callback);
57 parser->Start();
58
59 message_loop_runner_->Run();
60 message_loop_runner_ = nullptr;
61 }
62
63 private:
64 void ExpectValue(scoped_ptr<base::Value> expected_value,
65 scoped_ptr<base::Value> actual_value) {
66 EXPECT_TRUE(base::Value::Equals(actual_value.get(), expected_value.get()))
67 << "Expected: " << MaybeToJson(expected_value.get())
68 << " Actual: " << MaybeToJson(actual_value.get());
69 message_loop_runner_->Quit();
70 }
71
72 void ExpectError(const std::string& expected_error,
73 const std::string& actual_error) {
74 EXPECT_EQ(expected_error, actual_error);
75 message_loop_runner_->Quit();
76 }
77
78 void FailWithValue(scoped_ptr<base::Value> value) {
79 ADD_FAILURE() << MaybeToJson(value.get());
80 message_loop_runner_->Quit();
81 }
82
83 void FailWithError(const std::string& error) {
84 ADD_FAILURE() << error;
85 message_loop_runner_->Quit();
86 }
87
88 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
89};
90
91IN_PROC_BROWSER_TEST_F(SafeJsonParserTest, Parse) {
92 TestParse("{}");
93 TestParse("choke");
94 TestParse("{\"awesome\": true}");
95 TestParse("\"laser\"");
96 TestParse("false");
97 TestParse("null");
98 TestParse("3.14");
99 TestParse("[");
100 TestParse("\"");
101 TestParse(std::string());
102 TestParse("☃");
103 TestParse("\"☃\"");
104}