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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/google_apis/base_operations.h" | 8 #include "chrome/browser/google_apis/base_operations.h" |
9 #include "chrome/browser/google_apis/operation_runner.h" | 9 #include "chrome/browser/google_apis/operation_runner.h" |
10 #include "chrome/browser/google_apis/test_util.h" | 10 #include "chrome/browser/google_apis/test_util.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 protected: | 36 protected: |
37 // GetDataOperation overrides: | 37 // GetDataOperation overrides: |
38 virtual GURL GetURL() const OVERRIDE { | 38 virtual GURL GetURL() const OVERRIDE { |
39 // This method is never called because this test does not fetch json from | 39 // This method is never called because this test does not fetch json from |
40 // network. | 40 // network. |
41 NOTREACHED(); | 41 NOTREACHED(); |
42 return GURL(); | 42 return GURL(); |
43 } | 43 } |
44 }; | 44 }; |
45 | 45 |
46 // Copies the results from GetDataCallback. | |
47 void CopyResultsFromGetDataCallback(GDataErrorCode* error_out, | |
48 scoped_ptr<base::Value>* value_out, | |
49 GDataErrorCode error_in, | |
50 scoped_ptr<base::Value> value_in) { | |
51 value_out->swap(value_in); | |
52 *error_out = error_in; | |
53 } | |
54 | |
55 } // namespace | 46 } // namespace |
56 | 47 |
57 class BaseOperationsTest : public testing::Test { | 48 class BaseOperationsTest : public testing::Test { |
58 protected: | 49 protected: |
59 BaseOperationsTest() | 50 BaseOperationsTest() |
60 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | 51 : ui_thread_(content::BrowserThread::UI, &message_loop_) { |
61 } | 52 } |
62 | 53 |
63 virtual void SetUp() OVERRIDE { | 54 virtual void SetUp() OVERRIDE { |
64 profile_.reset(new TestingProfile); | 55 profile_.reset(new TestingProfile); |
65 runner_.reset(new OperationRunner(profile_.get(), | 56 runner_.reset(new OperationRunner(profile_.get(), |
66 std::vector<std::string>() /* scopes */, | 57 std::vector<std::string>() /* scopes */, |
67 "" /* custom_user_agent*/)); | 58 "" /* custom_user_agent*/)); |
68 runner_->Initialize(); | 59 runner_->Initialize(); |
69 } | 60 } |
70 | 61 |
71 protected: | 62 protected: |
72 MessageLoopForUI message_loop_; | 63 MessageLoopForUI message_loop_; |
73 content::TestBrowserThread ui_thread_; | 64 content::TestBrowserThread ui_thread_; |
74 scoped_ptr<TestingProfile> profile_; | 65 scoped_ptr<TestingProfile> profile_; |
75 scoped_ptr<OperationRunner> runner_; | 66 scoped_ptr<OperationRunner> runner_; |
76 }; | 67 }; |
77 | 68 |
78 TEST_F(BaseOperationsTest, GetDataOperation_ParseValidJson) { | 69 TEST_F(BaseOperationsTest, GetDataOperation_ParseValidJson) { |
79 scoped_ptr<base::Value> value; | 70 scoped_ptr<base::Value> value; |
80 GDataErrorCode error = GDATA_OTHER_ERROR; | 71 GDataErrorCode error = GDATA_OTHER_ERROR; |
81 JsonParseTestGetDataOperation* get_data = | 72 JsonParseTestGetDataOperation* get_data = |
82 new JsonParseTestGetDataOperation( | 73 new JsonParseTestGetDataOperation( |
83 runner_->operation_registry(), | 74 runner_->operation_registry(), |
84 base::Bind(&CopyResultsFromGetDataCallback, &error, &value)); | 75 base::Bind(&test_util::CopyResultsFromGetDataCallback, |
| 76 &error, |
| 77 &value)); |
85 get_data->NotifyStart(); | 78 get_data->NotifyStart(); |
86 | 79 |
87 const std::string valid_json_str = "{ \"test\": 123 }"; | 80 const std::string valid_json_str = "{ \"test\": 123 }"; |
88 | 81 |
89 get_data->ParseResponse(HTTP_SUCCESS, valid_json_str); | 82 get_data->ParseResponse(HTTP_SUCCESS, valid_json_str); |
90 // Should wait for a blocking pool task, as the JSON parsing is done in the | 83 // Should wait for a blocking pool task, as the JSON parsing is done in the |
91 // blocking pool. | 84 // blocking pool. |
92 test_util::RunBlockingPoolTask(); | 85 test_util::RunBlockingPoolTask(); |
93 | 86 |
94 EXPECT_EQ(HTTP_SUCCESS, error); | 87 EXPECT_EQ(HTTP_SUCCESS, error); |
95 ASSERT_TRUE(value.get()); | 88 ASSERT_TRUE(value.get()); |
96 | 89 |
97 DictionaryValue* root_dict = NULL; | 90 DictionaryValue* root_dict = NULL; |
98 ASSERT_TRUE(value->GetAsDictionary(&root_dict)); | 91 ASSERT_TRUE(value->GetAsDictionary(&root_dict)); |
99 | 92 |
100 int int_value = 0; | 93 int int_value = 0; |
101 ASSERT_TRUE(root_dict->GetInteger("test", &int_value)); | 94 ASSERT_TRUE(root_dict->GetInteger("test", &int_value)); |
102 EXPECT_EQ(123, int_value); | 95 EXPECT_EQ(123, int_value); |
103 } | 96 } |
104 | 97 |
105 TEST_F(BaseOperationsTest, GetDataOperation_ParseInvalidJson) { | 98 TEST_F(BaseOperationsTest, GetDataOperation_ParseInvalidJson) { |
106 scoped_ptr<base::Value> value; | 99 scoped_ptr<base::Value> value; |
107 GDataErrorCode error = GDATA_OTHER_ERROR; | 100 GDataErrorCode error = GDATA_OTHER_ERROR; |
108 JsonParseTestGetDataOperation* get_data = | 101 JsonParseTestGetDataOperation* get_data = |
109 new JsonParseTestGetDataOperation( | 102 new JsonParseTestGetDataOperation( |
110 runner_->operation_registry(), | 103 runner_->operation_registry(), |
111 base::Bind(&CopyResultsFromGetDataCallback, &error, &value)); | 104 base::Bind(&test_util::CopyResultsFromGetDataCallback, |
| 105 &error, |
| 106 &value)); |
112 get_data->NotifyStart(); | 107 get_data->NotifyStart(); |
113 | 108 |
114 const std::string invalid_json_str = "$$$"; | 109 const std::string invalid_json_str = "$$$"; |
115 | 110 |
116 get_data->ParseResponse(HTTP_SUCCESS, invalid_json_str); | 111 get_data->ParseResponse(HTTP_SUCCESS, invalid_json_str); |
117 test_util::RunBlockingPoolTask(); | 112 test_util::RunBlockingPoolTask(); |
118 | 113 |
119 // The parsing should fail. | 114 // The parsing should fail. |
120 EXPECT_EQ(GDATA_PARSE_ERROR, error); | 115 EXPECT_EQ(GDATA_PARSE_ERROR, error); |
121 ASSERT_FALSE(value.get()); | 116 ASSERT_FALSE(value.get()); |
122 } | 117 } |
123 | 118 |
124 } // namespace google_apis | 119 } // namespace google_apis |
OLD | NEW |