Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1134)

Unified Diff: chrome/browser/chromeos/gdata/gdata_operations_unittest.cc

Issue 10808027: gdrive: Get JSON feeds parsing off the UI thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..70e12a1f590f42648836b98d190267e36ec21c29
--- /dev/null
+++ b/chrome/browser/chromeos/gdata/gdata_operations_unittest.cc
@@ -0,0 +1,179 @@
+// 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/gdata/gdata_operations.h"
+#include "chrome/browser/chromeos/gdata/gdata_operation_runner.h"
+#include "chrome/browser/chromeos/gdata/gdata_test_util.h"
+#include "chrome/test/base/testing_profile.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/test/test_browser_thread.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace gdata {
+
+namespace {
+
+class JsonParseTestGetDataOperation : public GetDataOperation {
+ public:
+ JsonParseTestGetDataOperation(GDataOperationRegistry* registry,
+ Profile* profile,
+ const GetDataCallback& callback)
+ : GetDataOperation(registry, profile, callback) {
+ }
+
+ virtual ~JsonParseTestGetDataOperation() {
+ }
+
+ void NotifyStart() {
+ NotifyStartToOperationRegistry();
+ }
+
+ void NotifySuccess() {
+ NotifySuccessToOperationRegistry();
+ }
+
+ protected:
+ // GetDataOperation overrides:
+ virtual GURL GetURL() const OVERRIDE {
+ // This method is never called because this test does not fetch json from
+ // network.
+ NOTREACHED();
+ return GURL();
+ }
+};
+
+} // namespace
+
+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,
hashimoto 2012/07/19 03:57:57 nit: Move this function into the anonymous namespa
yoshiki 2012/07/19 04:58:45 Done.
+ 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);
+ JsonParseTestGetDataOperation* getData =
+ new JsonParseTestGetDataOperation(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(HTTP_SUCCESS, valid_json_str);
+ test_util::RunBlockingPoolTask();
+ message_loop_.RunAllPending();
hashimoto 2012/07/19 03:57:57 Is this line needed?
yoshiki 2012/07/19 04:58:45 Done. RunAllPending() is called already in test_ut
+
+ EXPECT_EQ(HTTP_SUCCESS, 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]));
hashimoto 2012/07/19 03:57:57 Could you use Get() instead of Remove()? This way,
yoshiki 2012/07/19 04:58:45 Done.
+ 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]));
+
+ delete dict_literals[0];
+ delete dict_literals[1];
+ delete dict_strings[0];
+ delete dict_strings[1];
+ delete list_values[0];
+ delete list_values[1];
+ }
+
+ getData->NotifySuccess();
+}
+
+TEST_F(GDataOperationsTest, GetDataOperationParseInvalidJson) {
+ scoped_ptr<base::Value> value;
+ GDataErrorCode error;
+ gdata::GetDataCallback cb = base::Bind(&GetDataOperationParseJsonCallback,
+ &error,
+ &value);
+ JsonParseTestGetDataOperation* getData =
+ new JsonParseTestGetDataOperation(runner_->operation_registry(),
+ profile_.get(),
+ cb);
+ getData->NotifyStart();
+
+ // Parses an invalid json string.
+ {
+ std::string invalid_json_str =
+ "/* hogehoge *"
+ " \"test\": {"
+ " \"moo\": \"cow"
+ " "
+ " \"list\": ["
+ " \"foo\","
+ " \"bar\""
+ " ]";
+
+ getData->ParseResponse(HTTP_SUCCESS, invalid_json_str);
+ test_util::RunBlockingPoolTask();
+ message_loop_.RunAllPending();
hashimoto 2012/07/19 03:57:57 Is this line needed?
yoshiki 2012/07/19 04:58:45 Done.
+
+ ASSERT_TRUE(value.get() == NULL);
+ EXPECT_EQ(GDATA_PARSE_ERROR, error);
+ }
+
+ getData->NotifySuccess();
hashimoto 2012/07/19 03:57:57 Why notifying success after parse failure?
yoshiki 2012/07/19 04:58:45 Done.
+}
+
+} // namespace gdata

Powered by Google App Engine
This is Rietveld 408576698