| OLD | NEW |
| (Empty) |
| 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 "chrome/browser/google_apis/base_operations.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chrome/browser/google_apis/operation_runner.h" | |
| 12 #include "chrome/browser/google_apis/test_util.h" | |
| 13 #include "chrome/test/base/testing_profile.h" | |
| 14 #include "content/public/test/test_browser_thread.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace google_apis { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 const char kValidJsonString[] = "{ \"test\": 123 }"; | |
| 22 const char kInvalidJsonString[] = "$$$"; | |
| 23 | |
| 24 class FakeGetDataOperation : public GetDataOperation { | |
| 25 public: | |
| 26 explicit FakeGetDataOperation(OperationRunner* runner, | |
| 27 const GetDataCallback& callback) | |
| 28 : GetDataOperation(runner, NULL, callback) { | |
| 29 } | |
| 30 | |
| 31 virtual ~FakeGetDataOperation() { | |
| 32 } | |
| 33 | |
| 34 void NotifyStart() { | |
| 35 NotifyStartToOperationRegistry(); | |
| 36 } | |
| 37 | |
| 38 protected: | |
| 39 virtual GURL GetURL() const OVERRIDE { | |
| 40 NOTREACHED(); // This method is not called in tests. | |
| 41 return GURL(); | |
| 42 } | |
| 43 }; | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 class BaseOperationsTest : public testing::Test { | |
| 48 public: | |
| 49 BaseOperationsTest() | |
| 50 : ui_thread_(content::BrowserThread::UI, &message_loop_), | |
| 51 parse_json_callback_called_(false), | |
| 52 get_data_callback_called_(false) { | |
| 53 } | |
| 54 | |
| 55 void ParseJsonCallback(scoped_ptr<base::Value> value) { | |
| 56 parse_json_result_ = value.Pass();; | |
| 57 parse_json_callback_called_ = true; | |
| 58 } | |
| 59 | |
| 60 void GetDataCallback(GDataErrorCode error, scoped_ptr<base::Value> value) { | |
| 61 get_data_result_error_ = error; | |
| 62 get_data_result_value_ = value.Pass(); | |
| 63 get_data_callback_called_ = true; | |
| 64 } | |
| 65 | |
| 66 virtual void SetUp() OVERRIDE { | |
| 67 profile_.reset(new TestingProfile); | |
| 68 runner_.reset(new OperationRunner(profile_.get(), | |
| 69 NULL /* url_request_context_getter */, | |
| 70 std::vector<std::string>() /* scopes */, | |
| 71 std::string() /* custom user agent */)); | |
| 72 runner_->Initialize(); | |
| 73 LOG(ERROR) << "Initialized."; | |
| 74 } | |
| 75 | |
| 76 base::MessageLoopForUI message_loop_; | |
| 77 content::TestBrowserThread ui_thread_; | |
| 78 scoped_ptr<TestingProfile> profile_; | |
| 79 scoped_ptr<OperationRunner> runner_; | |
| 80 | |
| 81 // Following members stores data returned with callbacks to be verified | |
| 82 // by tests. | |
| 83 scoped_ptr<base::Value> parse_json_result_; | |
| 84 bool parse_json_callback_called_; | |
| 85 GDataErrorCode get_data_result_error_; | |
| 86 scoped_ptr<base::Value> get_data_result_value_; | |
| 87 bool get_data_callback_called_; | |
| 88 }; | |
| 89 | |
| 90 TEST_F(BaseOperationsTest, ParseValidJson) { | |
| 91 ParseJson(kValidJsonString, | |
| 92 base::Bind(&BaseOperationsTest::ParseJsonCallback, | |
| 93 base::Unretained(this))); | |
| 94 // Should wait for a blocking pool task, as the JSON parsing is done in the | |
| 95 // blocking pool. | |
| 96 test_util::RunBlockingPoolTask(); | |
| 97 | |
| 98 ASSERT_TRUE(parse_json_callback_called_); | |
| 99 ASSERT_TRUE(parse_json_result_.get()); | |
| 100 | |
| 101 DictionaryValue* root_dict = NULL; | |
| 102 ASSERT_TRUE(parse_json_result_->GetAsDictionary(&root_dict)); | |
| 103 | |
| 104 int int_value = 0; | |
| 105 ASSERT_TRUE(root_dict->GetInteger("test", &int_value)); | |
| 106 EXPECT_EQ(123, int_value); | |
| 107 } | |
| 108 | |
| 109 TEST_F(BaseOperationsTest, ParseInvalidJson) { | |
| 110 ParseJson(kInvalidJsonString, | |
| 111 base::Bind(&BaseOperationsTest::ParseJsonCallback, | |
| 112 base::Unretained(this))); | |
| 113 // Should wait for a blocking pool task, as the JSON parsing is done in the | |
| 114 // blocking pool. | |
| 115 test_util::RunBlockingPoolTask(); | |
| 116 | |
| 117 ASSERT_TRUE(parse_json_callback_called_); | |
| 118 ASSERT_FALSE(parse_json_result_.get()); | |
| 119 } | |
| 120 | |
| 121 TEST_F(BaseOperationsTest, GetDataOperationParseValidResponse) { | |
| 122 FakeGetDataOperation* get_data_operation = | |
| 123 new FakeGetDataOperation( | |
| 124 runner_.get(), | |
| 125 base::Bind(&BaseOperationsTest::GetDataCallback, | |
| 126 base::Unretained(this))); | |
| 127 get_data_operation->NotifyStart(); | |
| 128 | |
| 129 get_data_operation->ParseResponse(HTTP_SUCCESS, kValidJsonString); | |
| 130 // Should wait for a blocking pool task, as the JSON parsing is done in the | |
| 131 // blocking pool. | |
| 132 test_util::RunBlockingPoolTask(); | |
| 133 | |
| 134 ASSERT_TRUE(get_data_callback_called_); | |
| 135 ASSERT_EQ(HTTP_SUCCESS, get_data_result_error_); | |
| 136 ASSERT_TRUE(get_data_result_value_.get()); | |
| 137 } | |
| 138 | |
| 139 TEST_F(BaseOperationsTest, GetDataOperationParseInvalidResponse) { | |
| 140 FakeGetDataOperation* get_data_operation = | |
| 141 new FakeGetDataOperation( | |
| 142 runner_.get(), | |
| 143 base::Bind(&BaseOperationsTest::GetDataCallback, | |
| 144 base::Unretained(this))); | |
| 145 get_data_operation->NotifyStart(); | |
| 146 | |
| 147 get_data_operation->ParseResponse(HTTP_SUCCESS, kInvalidJsonString); | |
| 148 // Should wait for a blocking pool task, as the JSON parsing is done in the | |
| 149 // blocking pool. | |
| 150 test_util::RunBlockingPoolTask(); | |
| 151 | |
| 152 ASSERT_TRUE(get_data_callback_called_); | |
| 153 ASSERT_EQ(GDATA_PARSE_ERROR, get_data_result_error_); | |
| 154 ASSERT_FALSE(get_data_result_value_.get()); | |
| 155 } | |
| 156 | |
| 157 } // namespace google_apis | |
| OLD | NEW |