OLD | NEW |
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/operation_runner.h" |
11 #include "chrome/browser/google_apis/test_util.h" | 12 #include "chrome/browser/google_apis/test_util.h" |
12 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "content/public/test/test_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" | 15 #include "testing/gtest/include/gtest/gtest.h" |
17 | 16 |
18 namespace google_apis { | 17 namespace google_apis { |
19 | 18 |
20 namespace { | 19 namespace { |
21 | 20 |
22 // The class is used to test that the JSON parsing is done in the blocking | 21 const char kValidJsonString[] = "{ \"test\": 123 }"; |
23 // pool, instead of UI thread. | 22 const char kInvalidJsonString[] = "$$$"; |
24 class JsonParseTestGetDataOperation : public GetDataOperation { | 23 |
| 24 class FakeGetDataOperation : public GetDataOperation { |
25 public: | 25 public: |
26 JsonParseTestGetDataOperation( | 26 explicit FakeGetDataOperation(OperationRegistry* registry, |
27 OperationRegistry* registry, | 27 const GetDataCallback& callback) |
28 net::URLRequestContextGetter* url_request_context_getter, | 28 : GetDataOperation(registry, NULL, callback) { |
29 const GetDataCallback& callback) | |
30 : GetDataOperation(registry, url_request_context_getter, callback) { | |
31 } | 29 } |
32 | 30 |
33 virtual ~JsonParseTestGetDataOperation() { | 31 virtual ~FakeGetDataOperation() { |
34 } | 32 } |
35 | 33 |
36 void NotifyStart() { | 34 void NotifyStart() { |
37 NotifyStartToOperationRegistry(); | 35 NotifyStartToOperationRegistry(); |
38 } | 36 } |
39 | 37 |
40 protected: | 38 protected: |
41 // GetDataOperation overrides: | |
42 virtual GURL GetURL() const OVERRIDE { | 39 virtual GURL GetURL() const OVERRIDE { |
43 // This method is never called because this test does not fetch json from | 40 NOTREACHED(); // This method is not called in tests. |
44 // network. | |
45 NOTREACHED(); | |
46 return GURL(); | 41 return GURL(); |
47 } | 42 } |
48 }; | 43 }; |
49 | 44 |
50 } // namespace | 45 } // namespace |
51 | 46 |
52 class BaseOperationsTest : public testing::Test { | 47 class BaseOperationsTest : public testing::Test { |
53 protected: | 48 public: |
54 BaseOperationsTest() | 49 BaseOperationsTest() |
55 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | 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; |
56 } | 64 } |
57 | 65 |
58 virtual void SetUp() OVERRIDE { | 66 virtual void SetUp() OVERRIDE { |
59 profile_.reset(new TestingProfile); | 67 profile_.reset(new TestingProfile); |
60 runner_.reset(new OperationRunner(profile_.get(), | 68 runner_.reset(new OperationRunner(profile_.get(), |
61 NULL /* url_request_context_getter */, | 69 NULL /* url_request_context_getter */, |
62 std::vector<std::string>() /* scopes */, | 70 std::vector<std::string>() /* scopes */, |
63 "" /* custom_user_agent*/)); | 71 "" /* custom user agent */)); |
64 runner_->Initialize(); | 72 runner_->Initialize(); |
| 73 LOG(ERROR) << "Initialized."; |
65 } | 74 } |
66 | 75 |
67 protected: | |
68 MessageLoopForUI message_loop_; | 76 MessageLoopForUI message_loop_; |
69 content::TestBrowserThread ui_thread_; | 77 content::TestBrowserThread ui_thread_; |
70 scoped_ptr<TestingProfile> profile_; | 78 scoped_ptr<TestingProfile> profile_; |
71 scoped_ptr<OperationRunner> runner_; | 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_; |
72 }; | 88 }; |
73 | 89 |
74 TEST_F(BaseOperationsTest, GetDataOperation_ParseValidJson) { | 90 TEST_F(BaseOperationsTest, ParseValidJson) { |
75 scoped_ptr<base::Value> value; | 91 ParseJson(kValidJsonString, |
76 GDataErrorCode error = GDATA_OTHER_ERROR; | 92 base::Bind(&BaseOperationsTest::ParseJsonCallback, |
77 JsonParseTestGetDataOperation* get_data = | 93 base::Unretained(this))); |
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 | |
86 const std::string valid_json_str = "{ \"test\": 123 }"; | |
87 | |
88 get_data->ParseResponse(HTTP_SUCCESS, valid_json_str); | |
89 // Should wait for a blocking pool task, as the JSON parsing is done in the | 94 // Should wait for a blocking pool task, as the JSON parsing is done in the |
90 // blocking pool. | 95 // blocking pool. |
91 test_util::RunBlockingPoolTask(); | 96 test_util::RunBlockingPoolTask(); |
92 | 97 |
93 EXPECT_EQ(HTTP_SUCCESS, error); | 98 ASSERT_TRUE(parse_json_callback_called_); |
94 ASSERT_TRUE(value.get()); | 99 ASSERT_TRUE(parse_json_result_.get()); |
95 | 100 |
96 DictionaryValue* root_dict = NULL; | 101 DictionaryValue* root_dict = NULL; |
97 ASSERT_TRUE(value->GetAsDictionary(&root_dict)); | 102 ASSERT_TRUE(parse_json_result_->GetAsDictionary(&root_dict)); |
98 | 103 |
99 int int_value = 0; | 104 int int_value = 0; |
100 ASSERT_TRUE(root_dict->GetInteger("test", &int_value)); | 105 ASSERT_TRUE(root_dict->GetInteger("test", &int_value)); |
101 EXPECT_EQ(123, int_value); | 106 EXPECT_EQ(123, int_value); |
102 } | 107 } |
103 | 108 |
104 TEST_F(BaseOperationsTest, GetDataOperation_ParseInvalidJson) { | 109 TEST_F(BaseOperationsTest, ParseInvalidJson) { |
105 scoped_ptr<base::Value> value; | 110 ParseJson(kInvalidJsonString, |
106 GDataErrorCode error = GDATA_OTHER_ERROR; | 111 base::Bind(&BaseOperationsTest::ParseJsonCallback, |
107 JsonParseTestGetDataOperation* get_data = | 112 base::Unretained(this))); |
108 new JsonParseTestGetDataOperation( | 113 // Should wait for a blocking pool task, as the JSON parsing is done in the |
109 runner_->operation_registry(), | 114 // blocking pool. |
110 NULL, // request_context_getter. | |
111 base::Bind(&test_util::CopyResultsFromGetDataCallback, | |
112 &error, | |
113 &value)); | |
114 get_data->NotifyStart(); | |
115 | |
116 const std::string invalid_json_str = "$$$"; | |
117 | |
118 get_data->ParseResponse(HTTP_SUCCESS, invalid_json_str); | |
119 test_util::RunBlockingPoolTask(); | 115 test_util::RunBlockingPoolTask(); |
120 | 116 |
121 // The parsing should fail. | 117 ASSERT_TRUE(parse_json_callback_called_); |
122 EXPECT_EQ(GDATA_PARSE_ERROR, error); | 118 ASSERT_FALSE(parse_json_result_.get()); |
123 ASSERT_FALSE(value.get()); | 119 } |
| 120 |
| 121 TEST_F(BaseOperationsTest, GetDataOperationParseValidResponse) { |
| 122 FakeGetDataOperation* get_data_operation = |
| 123 new FakeGetDataOperation( |
| 124 runner_->operation_registry(), |
| 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_->operation_registry(), |
| 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()); |
124 } | 155 } |
125 | 156 |
126 } // namespace google_apis | 157 } // namespace google_apis |
OLD | NEW |