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

Side by Side Diff: chrome/browser/google_apis/base_operations_unittest.cc

Issue 11577002: Converted ResumeUploadOperation to use JSON in response instead of XML. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed unit tests. Created 8 years 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/google_apis/base_operations.h" 5 #include "chrome/browser/google_apis/base_operations.h"
6 6
7 #include "base/bind.h"
7 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop.h" 9 #include "base/message_loop.h"
9 #include "base/values.h" 10 #include "base/values.h"
10 #include "chrome/browser/google_apis/operation_runner.h"
11 #include "chrome/browser/google_apis/test_util.h" 11 #include "chrome/browser/google_apis/test_util.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/test/test_browser_thread.h"
15 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
17 13
18 namespace google_apis { 14 namespace google_apis {
19 15
20 namespace { 16 namespace {
21 17
22 // The class is used to test that the JSON parsing is done in the blocking 18 const char kValidJsonString[] = "{ \"test\": 123 }";
23 // pool, instead of UI thread. 19 const char kInvalidJsonString[] = "$$$";
24 class JsonParseTestGetDataOperation : public GetDataOperation { 20
21 class FakeGetDataOperation : public GetDataOperation {
25 public: 22 public:
26 JsonParseTestGetDataOperation( 23 explicit FakeGetDataOperation(const GetDataCallback& callback)
27 OperationRegistry* registry, 24 : GetDataOperation(NULL, NULL, callback) {
28 net::URLRequestContextGetter* url_request_context_getter,
29 const GetDataCallback& callback)
30 : GetDataOperation(registry, url_request_context_getter, callback) {
31 }
32
33 virtual ~JsonParseTestGetDataOperation() {
34 }
35
36 void NotifyStart() {
37 NotifyStartToOperationRegistry();
38 } 25 }
39 26
40 protected: 27 protected:
41 // GetDataOperation overrides:
42 virtual GURL GetURL() const OVERRIDE { 28 virtual GURL GetURL() const OVERRIDE {
43 // This method is never called because this test does not fetch json from 29 NOTREACHED(); // This method is not called in tests.
44 // network.
45 NOTREACHED();
46 return GURL(); 30 return GURL();
47 } 31 }
48 }; 32 };
49 33
50 } // namespace 34 } // namespace
51 35
52 class BaseOperationsTest : public testing::Test { 36 class BaseOperationsTest : public testing::Test {
53 protected: 37 public:
54 BaseOperationsTest() 38 BaseOperationsTest() : parse_json_callback_called(false),
55 : ui_thread_(content::BrowserThread::UI, &message_loop_) { 39 get_data_callback_called(false) {
56 } 40 }
57 41
58 virtual void SetUp() OVERRIDE { 42 void ParseJsonCallback(scoped_ptr<base::Value> value) {
59 profile_.reset(new TestingProfile); 43 parse_json_result = value.Pass();;
60 runner_.reset(new OperationRunner(profile_.get(), 44 parse_json_callback_called = true;
61 NULL /* url_request_context_getter */,
62 std::vector<std::string>() /* scopes */,
63 "" /* custom_user_agent*/));
64 runner_->Initialize();
65 } 45 }
66 46
67 protected: 47 void GetDataCallback(GDataErrorCode error, scoped_ptr<base::Value> value) {
68 MessageLoopForUI message_loop_; 48 get_data_result_error = error;
69 content::TestBrowserThread ui_thread_; 49 get_data_result_value = value.Pass();
70 scoped_ptr<TestingProfile> profile_; 50 get_data_callback_called = true;
71 scoped_ptr<OperationRunner> runner_; 51 }
52
53 // Following members stores data returned with callbacks to be verified
54 // by tests.
55 scoped_ptr<base::Value> parse_json_result;
56 bool parse_json_callback_called;
57 GDataErrorCode get_data_result_error;
58 scoped_ptr<base::Value> get_data_result_value;
59 bool get_data_callback_called;
satorux1 2012/12/14 05:02:42 class data members should end with _;
mtomasz 2012/12/14 05:43:48 Done.
72 }; 60 };
73 61
74 TEST_F(BaseOperationsTest, GetDataOperation_ParseValidJson) { 62 TEST_F(BaseOperationsTest, ParseValidJson) {
75 scoped_ptr<base::Value> value; 63 scoped_ptr<base::Value> value;
76 GDataErrorCode error = GDATA_OTHER_ERROR;
77 JsonParseTestGetDataOperation* get_data =
78 new JsonParseTestGetDataOperation(
79 runner_->operation_registry(),
80 NULL, // request_context_getter.
81 base::Bind(&test_util::CopyResultsFromGetDataCallback,
82 &error,
83 &value));
84 get_data->NotifyStart();
85 64
86 const std::string valid_json_str = "{ \"test\": 123 }"; 65 ParseJson(kValidJsonString,
87 66 base::Bind(&BaseOperationsTest::ParseJsonCallback,
88 get_data->ParseResponse(HTTP_SUCCESS, valid_json_str); 67 base::Unretained(this)));
89 // Should wait for a blocking pool task, as the JSON parsing is done in the 68 // Should wait for a blocking pool task, as the JSON parsing is done in the
90 // blocking pool. 69 // blocking pool.
91 test_util::RunBlockingPoolTask(); 70 test_util::RunBlockingPoolTask();
92 71
93 EXPECT_EQ(HTTP_SUCCESS, error); 72 ASSERT_TRUE(parse_json_callback_called);
94 ASSERT_TRUE(value.get()); 73 ASSERT_TRUE(parse_json_result.get());
95 74
96 DictionaryValue* root_dict = NULL; 75 DictionaryValue* root_dict = NULL;
97 ASSERT_TRUE(value->GetAsDictionary(&root_dict)); 76 ASSERT_TRUE(value->GetAsDictionary(&root_dict));
98 77
99 int int_value = 0; 78 int int_value = 0;
100 ASSERT_TRUE(root_dict->GetInteger("test", &int_value)); 79 ASSERT_TRUE(root_dict->GetInteger("test", &int_value));
101 EXPECT_EQ(123, int_value); 80 EXPECT_EQ(123, int_value);
102 } 81 }
103 82
104 TEST_F(BaseOperationsTest, GetDataOperation_ParseInvalidJson) { 83 TEST_F(BaseOperationsTest, ParseInvalidJson) {
105 scoped_ptr<base::Value> value; 84 scoped_ptr<base::Value> value;
106 GDataErrorCode error = GDATA_OTHER_ERROR;
107 JsonParseTestGetDataOperation* get_data =
108 new JsonParseTestGetDataOperation(
109 runner_->operation_registry(),
110 NULL, // request_context_getter.
111 base::Bind(&test_util::CopyResultsFromGetDataCallback,
112 &error,
113 &value));
114 get_data->NotifyStart();
115 85
116 const std::string invalid_json_str = "$$$"; 86 ParseJson(kInvalidJsonString,
117 87 base::Bind(&BaseOperationsTest::ParseJsonCallback,
118 get_data->ParseResponse(HTTP_SUCCESS, invalid_json_str); 88 base::Unretained(this)));
89 // Should wait for a blocking pool task, as the JSON parsing is done in the
90 // blocking pool.
119 test_util::RunBlockingPoolTask(); 91 test_util::RunBlockingPoolTask();
120 92
121 // The parsing should fail. 93 ASSERT_TRUE(parse_json_callback_called);
122 EXPECT_EQ(GDATA_PARSE_ERROR, error); 94 ASSERT_FALSE(parse_json_result.get());
123 ASSERT_FALSE(value.get()); 95 }
96
97 TEST_F(BaseOperationsTest, GetDataOperationParseValidResponse) {
98 FakeGetDataOperation get_data_operation(
99 base::Bind(&BaseOperationsTest::GetDataCallback,
100 base::Unretained(this)));
101
102 get_data_operation.ParseResponse(HTTP_SUCCESS,
103 kValidJsonString);
104 // Should wait for a blocking pool task, as the JSON parsing is done in the
105 // blocking pool.
106 test_util::RunBlockingPoolTask();
107
108 ASSERT_TRUE(get_data_callback_called);
109 ASSERT_EQ(HTTP_SUCCESS, get_data_result_error);
110 ASSERT_TRUE(get_data_result_value.get());
111 }
112
113 TEST_F(BaseOperationsTest, GetDataOperationParseInvalidResponse) {
114 FakeGetDataOperation get_data_operation(
115 base::Bind(&BaseOperationsTest::GetDataCallback,
116 base::Unretained(this)));
117
118 get_data_operation.ParseResponse(HTTP_SUCCESS,
119 kInvalidJsonString);
120 // Should wait for a blocking pool task, as the JSON parsing is done in the
121 // blocking pool.
122 test_util::RunBlockingPoolTask();
123
124 ASSERT_TRUE(get_data_callback_called);
125 ASSERT_EQ(GDATA_PARSE_ERROR, get_data_result_error);
126 ASSERT_TRUE(get_data_result_value.get());
124 } 127 }
125 128
126 } // namespace google_apis 129 } // namespace google_apis
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698