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

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_operations_unittest.cc

Issue 10749015: 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "base/memory/scoped_ptr.h"
6 #include "base/message_loop.h"
7 #include "base/values.h"
8 #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.
9 #include "chrome/browser/chromeos/gdata/gdata_operations.h"
10 #include "chrome/browser/chromeos/gdata/gdata_operation_runner.h"
11 #include "chrome/browser/chromeos/gdata/gdata_test_util.h"
12 #include "chrome/browser/chromeos/gdata/gdata_util.h"
hashimoto 2012/07/10 03:48:39 ditto.
yoshiki 2012/07/11 00:47:14 Done.
13 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "testing/gmock/include/gmock/gmock.h"
hashimoto 2012/07/10 03:48:39 ditto.
yoshiki 2012/07/11 00:47:14 Done.
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace gdata {
20
21 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.
22 public:
23 GetDataOperationJsonParseTest(GDataOperationRegistry* registry,
24 Profile* profile,
25 const GetDataCallback& callback)
26 : GetDataOperation(registry, profile, callback) {
27 }
28
29 virtual ~GetDataOperationJsonParseTest() {
30 }
31
32 void NotifyStart() {
33 NotifyStartToOperationRegistry();
34 }
35
36 void NotifySuccess() {
37 NotifySuccessToOperationRegistry();
38 }
39
40 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.
41 return GURL();
42 }
43 };
44
45
46 class GDataOperationsTest : public testing::Test {
47 protected:
48 GDataOperationsTest()
49 : ui_thread_(content::BrowserThread::UI, &message_loop_) {
50 }
51
52 virtual void SetUp() OVERRIDE {
53 profile_.reset(new TestingProfile);
54 runner_.reset(new GDataOperationRunner(profile_.get()));
55 runner_->Initialize();
56 }
57
58 protected:
59 MessageLoopForUI message_loop_;
60 content::TestBrowserThread ui_thread_;
61 scoped_ptr<TestingProfile> profile_;
62 scoped_ptr<GDataOperationRunner> runner_;
63 };
64
65 void GetDataOperationParseJsonCallback(GDataErrorCode* error_out,
66 scoped_ptr<base::Value>* value_out,
67 GDataErrorCode error_in,
68 scoped_ptr<base::Value> value_in) {
69 value_out->swap(value_in);
70 *error_out = error_in;
71 }
72
73
74 TEST_F(GDataOperationsTest, GetDataOperationParseJson) {
75
76 scoped_ptr<base::Value> value;
77 GDataErrorCode error;
78 gdata::GetDataCallback cb = base::Bind(&GetDataOperationParseJsonCallback,
79 &error,
80 &value);
81 GetDataOperationJsonParseTest* getData =
82 new GetDataOperationJsonParseTest(runner_->operation_registry(),
83 profile_.get(),
84 cb);
85 getData->NotifyStart();
86
87 // Parses a valid json string.
88 {
89 std::string valid_json_str =
90 "{"
91 " \"test\": {"
92 " \"foo\": true,"
93 " \"bar\": 3.14,"
94 " \"baz\": \"bat\","
95 " \"moo\": \"cow\""
96 " },"
97 " \"list\": ["
98 " \"a\","
99 " \"b\""
100 " ]"
101 "}";
102
103 getData->ParseResponse(GDATA_NO_STATUS, valid_json_str);
104 test_util::RunBlockingPoolTask();
105 message_loop_.RunAllPending();
106
107 EXPECT_EQ(GDATA_NO_STATUS, error);
108 ASSERT_TRUE(value.get());
109
110 DictionaryValue* root_dict = NULL;
111 ASSERT_TRUE(value->GetAsDictionary(&root_dict));
112
113 DictionaryValue* dict = NULL;
114 ListValue* list = NULL;
115 ASSERT_TRUE(root_dict->GetDictionary("test", &dict));
116 ASSERT_TRUE(root_dict->GetList("list", &list));
117
118 Value* dict_literals[2] = {0};
119 Value* dict_strings[2] = {0};
120 Value* list_values[2] = {0};
121 EXPECT_TRUE(dict->Remove("foo", &dict_literals[0]));
122 EXPECT_TRUE(dict->Remove("bar", &dict_literals[1]));
123 EXPECT_TRUE(dict->Remove("baz", &dict_strings[0]));
124 EXPECT_TRUE(dict->Remove("moo", &dict_strings[1]));
125 ASSERT_EQ(2u, list->GetSize());
126 EXPECT_TRUE(list->Remove(0, &list_values[0]));
127 EXPECT_TRUE(list->Remove(0, &list_values[1]));
128 }
129
130 // Parses an invalid json string.
131 {
132 std::string invalid_json_str =
133 "/* hogehoge *"
134 " \"test\": {"
135 " \"moo\": \"cow"
136 " "
137 " \"list\": ["
138 " \"foo\","
139 " \"bar\""
140 " ]";
141
142 getData->ParseResponse(GDATA_NO_STATUS, invalid_json_str);
143 test_util::RunBlockingPoolTask();
144 message_loop_.RunAllPending();
145
146 ASSERT_TRUE(value.get() == NULL);
147 EXPECT_EQ(GDATA_PARSE_ERROR, error);
148 }
149
150 getData->NotifySuccess();
151 }
152
153 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698