Chromium Code Reviews| Index: chrome/browser/chromeos/gdata/gdata_operations_unittest.cc |
| diff --git a/chrome/browser/chromeos/gdata/gdata_operations_unittest.cc b/chrome/browser/chromeos/gdata/gdata_operations_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..48c8fb2d59aa7180af1a51b6c195a6859788f90b |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/gdata/gdata_operations_unittest.cc |
| @@ -0,0 +1,153 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/chromeos/cros/cros_library.h" |
|
hashimoto
2012/07/10 03:48:39
Could you remove this?
yoshiki
2012/07/11 00:47:14
Done.
|
| +#include "chrome/browser/chromeos/gdata/gdata_operations.h" |
| +#include "chrome/browser/chromeos/gdata/gdata_operation_runner.h" |
| +#include "chrome/browser/chromeos/gdata/gdata_test_util.h" |
| +#include "chrome/browser/chromeos/gdata/gdata_util.h" |
|
hashimoto
2012/07/10 03:48:39
ditto.
yoshiki
2012/07/11 00:47:14
Done.
|
| +#include "chrome/test/base/testing_profile.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/test/test_browser_thread.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
|
hashimoto
2012/07/10 03:48:39
ditto.
yoshiki
2012/07/11 00:47:14
Done.
|
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace gdata { |
| + |
| +class GetDataOperationJsonParseTest : public GetDataOperation { |
|
hashimoto
2012/07/10 03:48:39
Two comments:
1) Could you move this class into a
yoshiki
2012/07/11 00:47:14
Done x2.
|
| + public: |
| + GetDataOperationJsonParseTest(GDataOperationRegistry* registry, |
| + Profile* profile, |
| + const GetDataCallback& callback) |
| + : GetDataOperation(registry, profile, callback) { |
| + } |
| + |
| + virtual ~GetDataOperationJsonParseTest() { |
| + } |
| + |
| + void NotifyStart() { |
| + NotifyStartToOperationRegistry(); |
| + } |
| + |
| + void NotifySuccess() { |
| + NotifySuccessToOperationRegistry(); |
| + } |
| + |
| + virtual GURL GetURL() const OVERRIDE { |
|
hashimoto
2012/07/10 03:48:39
Two comments:
1) Add method comment "GetDataOperat
yoshiki
2012/07/11 00:47:14
Done x2.
|
| + return GURL(); |
| + } |
| +}; |
| + |
| + |
| +class GDataOperationsTest : public testing::Test { |
| + protected: |
| + GDataOperationsTest() |
| + : ui_thread_(content::BrowserThread::UI, &message_loop_) { |
| + } |
| + |
| + virtual void SetUp() OVERRIDE { |
| + profile_.reset(new TestingProfile); |
| + runner_.reset(new GDataOperationRunner(profile_.get())); |
| + runner_->Initialize(); |
| + } |
| + |
| + protected: |
| + MessageLoopForUI message_loop_; |
| + content::TestBrowserThread ui_thread_; |
| + scoped_ptr<TestingProfile> profile_; |
| + scoped_ptr<GDataOperationRunner> runner_; |
| +}; |
| + |
| +void GetDataOperationParseJsonCallback(GDataErrorCode* error_out, |
| + scoped_ptr<base::Value>* value_out, |
| + GDataErrorCode error_in, |
| + scoped_ptr<base::Value> value_in) { |
| + value_out->swap(value_in); |
| + *error_out = error_in; |
| +} |
| + |
| + |
| +TEST_F(GDataOperationsTest, GetDataOperationParseJson) { |
| + |
| + scoped_ptr<base::Value> value; |
| + GDataErrorCode error; |
| + gdata::GetDataCallback cb = base::Bind(&GetDataOperationParseJsonCallback, |
| + &error, |
| + &value); |
| + GetDataOperationJsonParseTest* getData = |
| + new GetDataOperationJsonParseTest(runner_->operation_registry(), |
| + profile_.get(), |
| + cb); |
| + getData->NotifyStart(); |
| + |
| + // Parses a valid json string. |
| + { |
| + std::string valid_json_str = |
| + "{" |
| + " \"test\": {" |
| + " \"foo\": true," |
| + " \"bar\": 3.14," |
| + " \"baz\": \"bat\"," |
| + " \"moo\": \"cow\"" |
| + " }," |
| + " \"list\": [" |
| + " \"a\"," |
| + " \"b\"" |
| + " ]" |
| + "}"; |
| + |
| + getData->ParseResponse(GDATA_NO_STATUS, valid_json_str); |
| + test_util::RunBlockingPoolTask(); |
| + message_loop_.RunAllPending(); |
| + |
| + EXPECT_EQ(GDATA_NO_STATUS, error); |
| + ASSERT_TRUE(value.get()); |
| + |
| + DictionaryValue* root_dict = NULL; |
| + ASSERT_TRUE(value->GetAsDictionary(&root_dict)); |
| + |
| + DictionaryValue* dict = NULL; |
| + ListValue* list = NULL; |
| + ASSERT_TRUE(root_dict->GetDictionary("test", &dict)); |
| + ASSERT_TRUE(root_dict->GetList("list", &list)); |
| + |
| + Value* dict_literals[2] = {0}; |
| + Value* dict_strings[2] = {0}; |
| + Value* list_values[2] = {0}; |
| + EXPECT_TRUE(dict->Remove("foo", &dict_literals[0])); |
| + EXPECT_TRUE(dict->Remove("bar", &dict_literals[1])); |
| + EXPECT_TRUE(dict->Remove("baz", &dict_strings[0])); |
| + EXPECT_TRUE(dict->Remove("moo", &dict_strings[1])); |
| + ASSERT_EQ(2u, list->GetSize()); |
| + EXPECT_TRUE(list->Remove(0, &list_values[0])); |
| + EXPECT_TRUE(list->Remove(0, &list_values[1])); |
| + } |
| + |
| + // Parses an invalid json string. |
| + { |
| + std::string invalid_json_str = |
| + "/* hogehoge *" |
| + " \"test\": {" |
| + " \"moo\": \"cow" |
| + " " |
| + " \"list\": [" |
| + " \"foo\"," |
| + " \"bar\"" |
| + " ]"; |
| + |
| + getData->ParseResponse(GDATA_NO_STATUS, invalid_json_str); |
| + test_util::RunBlockingPoolTask(); |
| + message_loop_.RunAllPending(); |
| + |
| + ASSERT_TRUE(value.get() == NULL); |
| + EXPECT_EQ(GDATA_PARSE_ERROR, error); |
| + } |
| + |
| + getData->NotifySuccess(); |
| +} |
| + |
| +} // namespace gdata |