OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/json/json_writer.h" | 5 #include "base/json/json_writer.h" |
6 #include "base/values.h" | 6 #include "base/values.h" |
7 #include "chrome/browser/extensions/api/copresence/copresence_api.h" | 7 #include "chrome/browser/extensions/api/copresence/copresence_api.h" |
8 #include "chrome/browser/extensions/extension_api_unittest.h" | 8 #include "chrome/browser/extensions/extension_api_unittest.h" |
9 #include "chrome/browser/extensions/extension_function_test_utils.h" | 9 #include "chrome/browser/extensions/extension_function_test_utils.h" |
10 #include "components/copresence/proto/data.pb.h" | 10 #include "components/copresence/proto/data.pb.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 template <typename T> | 61 template <typename T> |
62 bool GetOnly(const RepeatedPtrField<T>& things, T* out) { | 62 bool GetOnly(const RepeatedPtrField<T>& things, T* out) { |
63 if (things.size() != 1) | 63 if (things.size() != 1) |
64 return false; | 64 return false; |
65 | 65 |
66 *out = things.Get(0); | 66 *out = things.Get(0); |
67 return true; | 67 return true; |
68 } | 68 } |
69 | 69 |
70 class MockCopresenceManager : public CopresenceManager { | 70 class FakeCopresenceManager : public CopresenceManager { |
71 public: | 71 public: |
72 explicit MockCopresenceManager(CopresenceDelegate* delegate) | 72 explicit FakeCopresenceManager(CopresenceDelegate* delegate) |
73 : delegate_(delegate) {} | 73 : delegate_(delegate) {} |
74 ~MockCopresenceManager() override {} | 74 ~FakeCopresenceManager() override {} |
75 | 75 |
| 76 // CopresenceManager overrides. |
| 77 copresence::CopresenceState* state() override { |
| 78 NOTREACHED(); |
| 79 return nullptr; |
| 80 } |
76 void ExecuteReportRequest( | 81 void ExecuteReportRequest( |
77 const ReportRequest& request, | 82 const ReportRequest& request, |
78 const std::string& app_id, | 83 const std::string& app_id, |
79 const std::string& /* auth_token */, | 84 const std::string& /* auth_token */, |
80 const copresence::StatusCallback& status_callback) override { | 85 const copresence::StatusCallback& status_callback) override { |
81 request_ = request; | 86 request_ = request; |
82 app_id_ = app_id; | 87 app_id_ = app_id; |
83 status_callback.Run(copresence::SUCCESS); | 88 status_callback.Run(copresence::SUCCESS); |
84 } | 89 } |
85 | 90 |
86 CopresenceDelegate* delegate_; | 91 CopresenceDelegate* delegate_; |
87 | 92 |
88 ReportRequest request_; | 93 ReportRequest request_; |
89 std::string app_id_; | 94 std::string app_id_; |
90 }; | 95 }; |
91 | 96 |
92 class CopresenceApiUnittest : public ExtensionApiUnittest { | 97 class CopresenceApiUnittest : public ExtensionApiUnittest { |
93 public: | 98 public: |
94 CopresenceApiUnittest() {} | 99 CopresenceApiUnittest() {} |
95 ~CopresenceApiUnittest() override {} | 100 ~CopresenceApiUnittest() override {} |
96 | 101 |
97 void SetUp() override { | 102 void SetUp() override { |
98 ExtensionApiUnittest::SetUp(); | 103 ExtensionApiUnittest::SetUp(); |
99 | 104 |
100 CopresenceService* service = | 105 CopresenceService* service = |
101 CopresenceService::GetFactoryInstance()->Get(profile()); | 106 CopresenceService::GetFactoryInstance()->Get(profile()); |
102 copresence_manager_ = new MockCopresenceManager(service); | 107 copresence_manager_ = new FakeCopresenceManager(service); |
103 service->set_manager_for_testing( | 108 service->set_manager_for_testing( |
104 make_scoped_ptr<CopresenceManager>(copresence_manager_)); | 109 make_scoped_ptr<CopresenceManager>(copresence_manager_)); |
105 } | 110 } |
106 | 111 |
107 // Takes ownership of the operation_list. | 112 // Takes ownership of the operation_list. |
108 bool ExecuteOperations(ListValue* operation_list) { | 113 bool ExecuteOperations(ListValue* operation_list) { |
109 scoped_ptr<ListValue> args_list(new ListValue); | 114 scoped_ptr<ListValue> args_list(new ListValue); |
110 args_list->Append(operation_list); | 115 args_list->Append(operation_list); |
111 | 116 |
112 scoped_refptr<UIThreadExtensionFunction> function = | 117 scoped_refptr<UIThreadExtensionFunction> function = |
(...skipping 14 matching lines...) Expand all Loading... |
127 | 132 |
128 const ReportRequest& request_sent() const { | 133 const ReportRequest& request_sent() const { |
129 return copresence_manager_->request_; | 134 return copresence_manager_->request_; |
130 } | 135 } |
131 | 136 |
132 const std::string& app_id_sent() const { | 137 const std::string& app_id_sent() const { |
133 return copresence_manager_->app_id_; | 138 return copresence_manager_->app_id_; |
134 } | 139 } |
135 | 140 |
136 void clear_app_id() { | 141 void clear_app_id() { |
137 copresence_manager_->app_id_ = ""; | 142 copresence_manager_->app_id_.clear(); |
138 } | 143 } |
139 | 144 |
140 CopresenceDelegate* delegate() { | 145 CopresenceDelegate* delegate() { |
141 return copresence_manager_->delegate_; | 146 return copresence_manager_->delegate_; |
142 } | 147 } |
143 | 148 |
144 protected: | 149 protected: |
145 MockCopresenceManager* copresence_manager_; | 150 FakeCopresenceManager* copresence_manager_; |
146 }; | 151 }; |
147 | 152 |
148 TEST_F(CopresenceApiUnittest, Publish) { | 153 TEST_F(CopresenceApiUnittest, Publish) { |
149 scoped_ptr<PublishOperation> publish(CreatePublish("pub")); | 154 scoped_ptr<PublishOperation> publish(CreatePublish("pub")); |
150 publish->strategies.reset(new Strategy); | 155 publish->strategies.reset(new Strategy); |
151 publish->strategies->only_broadcast.reset(new bool(true)); // Default | 156 publish->strategies->only_broadcast.reset(new bool(true)); // Default |
152 | 157 |
153 scoped_ptr<Operation> operation(new Operation); | 158 scoped_ptr<Operation> operation(new Operation); |
154 operation->publish = publish.Pass(); | 159 operation->publish = publish.Pass(); |
155 | 160 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 scoped_ptr<Operation> multi_operation(new Operation); | 287 scoped_ptr<Operation> multi_operation(new Operation); |
283 multi_operation->publish.reset(CreatePublish("pub")); | 288 multi_operation->publish.reset(CreatePublish("pub")); |
284 multi_operation->subscribe.reset(CreateSubscribe("sub")); | 289 multi_operation->subscribe.reset(CreateSubscribe("sub")); |
285 | 290 |
286 EXPECT_FALSE(ExecuteOperation(multi_operation.Pass())); | 291 EXPECT_FALSE(ExecuteOperation(multi_operation.Pass())); |
287 } | 292 } |
288 | 293 |
289 } // namespace extensions | 294 } // namespace extensions |
290 | 295 |
291 // TODO(ckehoe): add tests for auth tokens and api key functionality | 296 // TODO(ckehoe): add tests for auth tokens and api key functionality |
OLD | NEW |