OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/safe_json/testing_json_parser.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/values.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace safe_json { |
| 15 namespace { |
| 16 |
| 17 const char kTestJson[] = "{\"key\":2}"; |
| 18 |
| 19 class TestingJsonParserTest : public testing::Test { |
| 20 public: |
| 21 void Parse(const std::string& input) { |
| 22 base::RunLoop run_loop; |
| 23 SafeJsonParser::Parse(input, |
| 24 base::Bind(&SuccessCallback, base::Unretained(this), |
| 25 run_loop.QuitClosure()), |
| 26 base::Bind(&ErrorCallback, base::Unretained(this), |
| 27 run_loop.QuitClosure())); |
| 28 run_loop.Run(); |
| 29 } |
| 30 |
| 31 bool did_success() { return did_success_; } |
| 32 bool did_error() { return did_error_; } |
| 33 |
| 34 private: |
| 35 static void SuccessCallback(TestingJsonParserTest* test, |
| 36 base::Closure quit_closure, |
| 37 scoped_ptr<base::Value> value) { |
| 38 test->did_success_ = true; |
| 39 quit_closure.Run(); |
| 40 |
| 41 ASSERT_TRUE(value->IsType(base::Value::TYPE_DICTIONARY)); |
| 42 base::DictionaryValue* dict; |
| 43 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
| 44 int key_value = 0; |
| 45 EXPECT_TRUE(dict->GetInteger("key", &key_value)); |
| 46 EXPECT_EQ(2, key_value); |
| 47 } |
| 48 |
| 49 static void ErrorCallback(TestingJsonParserTest* test, |
| 50 base::Closure quit_closure, |
| 51 const std::string& error) { |
| 52 test->did_error_ = true; |
| 53 quit_closure.Run(); |
| 54 |
| 55 EXPECT_FALSE(error.empty()); |
| 56 } |
| 57 |
| 58 base::MessageLoop message_loop; |
| 59 TestingJsonParser::ScopedFactoryOverride factory_override_; |
| 60 bool did_success_ = false; |
| 61 bool did_error_ = false; |
| 62 }; |
| 63 |
| 64 TEST_F(TestingJsonParserTest, QuitLoopInSuccessCallback) { |
| 65 Parse(kTestJson); |
| 66 EXPECT_TRUE(did_success()); |
| 67 EXPECT_FALSE(did_error()); |
| 68 } |
| 69 |
| 70 TEST_F(TestingJsonParserTest, QuitLoopInErrorCallback) { |
| 71 Parse(&kTestJson[1]); |
| 72 EXPECT_FALSE(did_success()); |
| 73 EXPECT_TRUE(did_error()); |
| 74 } |
| 75 |
| 76 } // namespace |
| 77 } // namespace safe_json |
OLD | NEW |