Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/api/copresence/copresence_api.h" | |
| 6 #include "chrome/browser/extensions/extension_apitest.h" | |
| 7 #include "components/copresence/public/copresence_client.h" | |
| 8 | |
| 9 using copresence::CopresenceClient; | |
| 10 using copresence::CopresenceClientDelegate; | |
| 11 using copresence::FAIL; | |
| 12 using copresence::Message; | |
| 13 using copresence::PublishedMessage; | |
| 14 using copresence::ReportRequest; | |
| 15 using copresence::Subscription; | |
| 16 using google::protobuf::RepeatedPtrField; | |
| 17 | |
| 18 namespace extensions { | |
| 19 | |
| 20 typedef ExtensionApiTest CopresenceApiTest; | |
| 21 | |
| 22 bool ValidatePublishes(const RepeatedPtrField<PublishedMessage>& publishes) { | |
| 23 return publishes.size() == 1 && | |
| 24 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
| |
| 25 publishes.Get(0).access_policy().acl().acl_type() | |
| 26 == copresence::NO_ACL_CHECK && | |
| 27 publishes.Get(0).message().type().type() == "joke" && | |
| 28 publishes.Get(0).message().payload() == "Knock Knock!" && | |
| 29 publishes.Get(0).token_exchange_strategy() | |
| 30 .broadcast_scan_configuration() == copresence::BROADCAST_ONLY; | |
| 31 } | |
| 32 | |
| 33 bool ValidateSubscriptions( | |
| 34 const RepeatedPtrField<Subscription>& subscriptions) { | |
| 35 return subscriptions.size() == 1 && | |
| 36 subscriptions.Get(0).id() == "response" && | |
|
not at google - send to devlin
2014/08/07 22:36:40
ditto and everywhere else
| |
| 37 subscriptions.Get(0).ttl_millis() == 1000 && | |
| 38 subscriptions.Get(0).message_type().type() == "joke" && | |
| 39 subscriptions.Get(0).token_exchange_strategy() | |
| 40 .broadcast_scan_configuration() == copresence::SCAN_ONLY; | |
| 41 } | |
| 42 | |
| 43 bool ValidateUnpublishUnsubscribe(const ReportRequest& request) { | |
| 44 const RepeatedPtrField<std::string>& unpublishes = | |
| 45 request.manage_messages_request().id_to_unpublish(); | |
| 46 const RepeatedPtrField<std::string>& unsubscribes = | |
| 47 request.manage_subscriptions_request().id_to_unsubscribe(); | |
| 48 | |
| 49 return unpublishes.size() == 1 && unsubscribes.size() == 1 && | |
| 50 unpublishes.Get(0) == "call" && unsubscribes.Get(0) == "response"; | |
| 51 } | |
| 52 | |
| 53 class MockCopresenceClient : public CopresenceClient { | |
| 54 public: | |
| 55 explicit MockCopresenceClient(CopresenceClientDelegate* delegate) | |
| 56 : delegate_(delegate) {} | |
| 57 virtual ~MockCopresenceClient() {} | |
| 58 | |
| 59 virtual void ExecuteReportRequest( | |
| 60 ReportRequest request, | |
| 61 const std::string& app_id, | |
| 62 const copresence::StatusCallback& status_callback) OVERRIDE { | |
| 63 | |
| 64 const RepeatedPtrField<PublishedMessage>& publishes = | |
| 65 request.manage_messages_request().message_to_publish(); | |
| 66 const RepeatedPtrField<Subscription>& subscriptions = | |
| 67 request.manage_subscriptions_request().subscription(); | |
| 68 | |
| 69 // Expecting either a publish, a subscribe, or unpublish *and* unsubscribe. | |
| 70 if (publishes.size() > 0) { | |
| 71 if (!ValidatePublishes(publishes)) { | |
| 72 LOG(ERROR) << "Found invalid publish"; | |
| 73 status_callback.Run(FAIL); | |
| 74 return; | |
| 75 } | |
| 76 } else if (subscriptions.size() > 0) { | |
| 77 if (!ValidateSubscriptions(subscriptions)) { | |
| 78 LOG(ERROR) << "Found invalid subscribe"; | |
| 79 status_callback.Run(FAIL); | |
| 80 return; | |
| 81 } else { | |
| 82 // For subscriptions, deliver messages. | |
| 83 Message returned_message; | |
| 84 returned_message.mutable_type()->set_type("joke"); | |
| 85 returned_message.set_payload("Who's there?"); | |
| 86 delegate_->HandleMessages( | |
| 87 app_id, "response", std::vector<Message>(2, returned_message)); | |
| 88 } | |
| 89 } else { | |
| 90 if (!ValidateUnpublishUnsubscribe(request)) { | |
| 91 LOG(ERROR) << "Found invalid unpublish/unsubscribe"; | |
| 92 status_callback.Run(FAIL); | |
| 93 return; | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 status_callback.Run(copresence::SUCCESS); | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 CopresenceClientDelegate* delegate_; | |
| 102 }; | |
| 103 | |
| 104 // TODO(ckehoe): Instead of using the full browser test stack, | |
| 105 // test individual functions. See extension_function_test_utils.h. | |
| 106 IN_PROC_BROWSER_TEST_F(CopresenceApiTest, Basic) { | |
| 107 CopresenceService* service = | |
| 108 CopresenceService::GetFactoryInstance()->Get(browser()->profile()); | |
| 109 service->set_client_for_testing( | |
| 110 make_scoped_ptr<CopresenceClient>(new MockCopresenceClient(service))); | |
| 111 | |
| 112 ASSERT_TRUE(RunExtensionTestNoFileAccess("copresence")) << message_; | |
| 113 } | |
| 114 | |
| 115 } // namespace extensions | |
| OLD | NEW |