Use app list item shadow for app list folders.
[chromium-blink-merge.git] / chrome / browser / safe_json_parser_browsertest.cc
blob97957f0eebbfd91a7bb0b8fa757e5eaeac465b69
1 // 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.
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/test/base/in_process_browser_test.h"
11 #include "components/safe_json_parser/safe_json_parser.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "content/public/test/test_utils.h"
15 namespace {
17 using safe_json_parser::SafeJsonParser;
19 std::string MaybeToJson(const base::Value* value) {
20 if (!value)
21 return "(null)";
23 std::string json;
24 if (!base::JSONWriter::Write(*value, &json))
25 return "(invalid value)";
27 return json;
30 } // namespace
32 class SafeJsonParserTest : public InProcessBrowserTest {
33 protected:
34 void TestParse(const std::string& json) {
35 SCOPED_TRACE(json);
36 DCHECK(!message_loop_runner_);
37 message_loop_runner_ = new content::MessageLoopRunner;
39 std::string error;
40 scoped_ptr<base::Value> value = base::JSONReader::ReadAndReturnError(
41 json, base::JSON_PARSE_RFC, nullptr, &error);
43 SafeJsonParser::SuccessCallback success_callback;
44 SafeJsonParser::ErrorCallback error_callback;
45 if (value) {
46 success_callback =
47 base::Bind(&SafeJsonParserTest::ExpectValue, base::Unretained(this),
48 base::Passed(&value));
49 error_callback = base::Bind(&SafeJsonParserTest::FailWithError,
50 base::Unretained(this));
51 } else {
52 success_callback = base::Bind(&SafeJsonParserTest::FailWithValue,
53 base::Unretained(this));
54 error_callback = base::Bind(&SafeJsonParserTest::ExpectError,
55 base::Unretained(this), error);
57 scoped_refptr<SafeJsonParser> parser =
58 new SafeJsonParser(json, success_callback, error_callback);
59 parser->Start();
61 message_loop_runner_->Run();
62 message_loop_runner_ = nullptr;
65 private:
66 void ExpectValue(scoped_ptr<base::Value> expected_value,
67 scoped_ptr<base::Value> actual_value) {
68 EXPECT_TRUE(base::Value::Equals(actual_value.get(), expected_value.get()))
69 << "Expected: " << MaybeToJson(expected_value.get())
70 << " Actual: " << MaybeToJson(actual_value.get());
71 message_loop_runner_->Quit();
74 void ExpectError(const std::string& expected_error,
75 const std::string& actual_error) {
76 EXPECT_EQ(expected_error, actual_error);
77 message_loop_runner_->Quit();
80 void FailWithValue(scoped_ptr<base::Value> value) {
81 ADD_FAILURE() << MaybeToJson(value.get());
82 message_loop_runner_->Quit();
85 void FailWithError(const std::string& error) {
86 ADD_FAILURE() << error;
87 message_loop_runner_->Quit();
90 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
93 IN_PROC_BROWSER_TEST_F(SafeJsonParserTest, Parse) {
94 TestParse("{}");
95 TestParse("choke");
96 TestParse("{\"awesome\": true}");
97 TestParse("\"laser\"");
98 TestParse("false");
99 TestParse("null");
100 TestParse("3.14");
101 TestParse("[");
102 TestParse("\"");
103 TestParse(std::string());
104 TestParse("☃");
105 TestParse("\"\"");