Chromium Code Reviews| Index: chrome/browser/extensions/api/copresence/copresence_apitest.cc |
| diff --git a/chrome/browser/extensions/api/copresence/copresence_apitest.cc b/chrome/browser/extensions/api/copresence/copresence_apitest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..39ea7679f4e7f97fe28a94217f6f2762cacfede2 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/copresence/copresence_apitest.cc |
| @@ -0,0 +1,115 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/api/copresence/copresence_api.h" |
| +#include "chrome/browser/extensions/extension_apitest.h" |
| +#include "components/copresence/public/copresence_client.h" |
| + |
| +using copresence::CopresenceClient; |
| +using copresence::CopresenceClientDelegate; |
| +using copresence::FAIL; |
| +using copresence::Message; |
| +using copresence::PublishedMessage; |
| +using copresence::ReportRequest; |
| +using copresence::Subscription; |
| +using google::protobuf::RepeatedPtrField; |
| + |
| +namespace extensions { |
| + |
| +typedef ExtensionApiTest CopresenceApiTest; |
| + |
| +bool ValidatePublishes(const RepeatedPtrField<PublishedMessage>& publishes) { |
| + return publishes.size() == 1 && |
| + publishes.Get(0).id() == "call" && |
|
not at google - send to devlin
2014/08/07 22:36:40
um can you save publishes.Get(0) to a variable so
|
| + publishes.Get(0).access_policy().acl().acl_type() |
| + == copresence::NO_ACL_CHECK && |
| + publishes.Get(0).message().type().type() == "joke" && |
| + publishes.Get(0).message().payload() == "Knock Knock!" && |
| + publishes.Get(0).token_exchange_strategy() |
| + .broadcast_scan_configuration() == copresence::BROADCAST_ONLY; |
| +} |
| + |
| +bool ValidateSubscriptions( |
| + const RepeatedPtrField<Subscription>& subscriptions) { |
| + return subscriptions.size() == 1 && |
| + subscriptions.Get(0).id() == "response" && |
|
not at google - send to devlin
2014/08/07 22:36:40
ditto and everywhere else
|
| + subscriptions.Get(0).ttl_millis() == 1000 && |
| + subscriptions.Get(0).message_type().type() == "joke" && |
| + subscriptions.Get(0).token_exchange_strategy() |
| + .broadcast_scan_configuration() == copresence::SCAN_ONLY; |
| +} |
| + |
| +bool ValidateUnpublishUnsubscribe(const ReportRequest& request) { |
| + const RepeatedPtrField<std::string>& unpublishes = |
| + request.manage_messages_request().id_to_unpublish(); |
| + const RepeatedPtrField<std::string>& unsubscribes = |
| + request.manage_subscriptions_request().id_to_unsubscribe(); |
| + |
| + return unpublishes.size() == 1 && unsubscribes.size() == 1 && |
| + unpublishes.Get(0) == "call" && unsubscribes.Get(0) == "response"; |
| +} |
| + |
| +class MockCopresenceClient : public CopresenceClient { |
| + public: |
| + explicit MockCopresenceClient(CopresenceClientDelegate* delegate) |
| + : delegate_(delegate) {} |
| + virtual ~MockCopresenceClient() {} |
| + |
| + virtual void ExecuteReportRequest( |
| + ReportRequest request, |
| + const std::string& app_id, |
| + const copresence::StatusCallback& status_callback) OVERRIDE { |
| + |
| + const RepeatedPtrField<PublishedMessage>& publishes = |
| + request.manage_messages_request().message_to_publish(); |
| + const RepeatedPtrField<Subscription>& subscriptions = |
| + request.manage_subscriptions_request().subscription(); |
| + |
| + // Expecting either a publish, a subscribe, or unpublish *and* unsubscribe. |
| + if (publishes.size() > 0) { |
| + if (!ValidatePublishes(publishes)) { |
| + LOG(ERROR) << "Found invalid publish"; |
| + status_callback.Run(FAIL); |
| + return; |
| + } |
| + } else if (subscriptions.size() > 0) { |
| + if (!ValidateSubscriptions(subscriptions)) { |
| + LOG(ERROR) << "Found invalid subscribe"; |
| + status_callback.Run(FAIL); |
| + return; |
| + } else { |
| + // For subscriptions, deliver messages. |
| + Message returned_message; |
| + returned_message.mutable_type()->set_type("joke"); |
| + returned_message.set_payload("Who's there?"); |
| + delegate_->HandleMessages( |
| + app_id, "response", std::vector<Message>(2, returned_message)); |
| + } |
| + } else { |
| + if (!ValidateUnpublishUnsubscribe(request)) { |
| + LOG(ERROR) << "Found invalid unpublish/unsubscribe"; |
| + status_callback.Run(FAIL); |
| + return; |
| + } |
| + } |
| + |
| + status_callback.Run(copresence::SUCCESS); |
| + } |
| + |
| + private: |
| + CopresenceClientDelegate* delegate_; |
| +}; |
| + |
| +// TODO(ckehoe): Instead of using the full browser test stack, |
| +// test individual functions. See extension_function_test_utils.h. |
| +IN_PROC_BROWSER_TEST_F(CopresenceApiTest, Basic) { |
| + CopresenceService* service = |
| + CopresenceService::GetFactoryInstance()->Get(browser()->profile()); |
| + service->set_client_for_testing( |
| + make_scoped_ptr<CopresenceClient>(new MockCopresenceClient(service))); |
| + |
| + ASSERT_TRUE(RunExtensionTestNoFileAccess("copresence")) << message_; |
| +} |
| + |
| +} // namespace extensions |