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 | |
| 3 // that can be 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::CopresenceClientDelegate; | |
| 10 using copresence::FAIL; | |
| 11 using copresence::Message; | |
| 12 using google::protobuf::RepeatedPtrField; | |
| 13 | |
| 14 namespace extensions { | |
| 15 | |
| 16 class CopresenceApiTest : public ExtensionApiTest { | |
|
not at google - send to devlin
2014/08/06 21:45:26
a pattern I've seen that saves creating this vacuo
Charlie
2014/08/07 22:24:15
Interesting. Done.
| |
| 17 public: | |
| 18 CopresenceApiTest() {} | |
| 19 virtual ~CopresenceApiTest() {} | |
| 20 }; | |
| 21 | |
| 22 class MockCopresenceClient : public copresence::CopresenceClient { | |
| 23 public: | |
| 24 explicit MockCopresenceClient(CopresenceClientDelegate* delegate) | |
| 25 : delegate_(delegate) {} | |
| 26 virtual ~MockCopresenceClient() {} | |
| 27 | |
| 28 virtual void Initialize(CopresenceClientDelegate*) OVERRIDE {} | |
| 29 | |
| 30 virtual void ExecuteReportRequest( | |
| 31 copresence::ReportRequest request, | |
| 32 const std::string& app_id, | |
| 33 const copresence::StatusCallback& status_callback) OVERRIDE { | |
| 34 | |
|
not at google - send to devlin
2014/08/06 21:45:27
if possible a nicer pattern is to have the Mock ju
rkc
2014/08/07 08:56:57
I am not sure if that would work with this test. I
not at google - send to devlin
2014/08/07 15:34:25
could you just hold off on calling |status_callbac
Charlie
2014/08/07 22:24:15
Not sure how that would look. IIUC RunExtensionTes
| |
| 35 const RepeatedPtrField<copresence::PublishedMessage>& publishes = | |
| 36 request.manage_messages_request().message_to_publish(); | |
| 37 const RepeatedPtrField<std::string>& unpublishes = | |
| 38 request.manage_messages_request().id_to_unpublish(); | |
| 39 const RepeatedPtrField<copresence::Subscription>& subscriptions = | |
| 40 request.manage_subscriptions_request().subscription(); | |
| 41 const RepeatedPtrField<std::string>& unsubscribes = | |
| 42 request.manage_subscriptions_request().id_to_unsubscribe(); | |
| 43 | |
| 44 // Expecting either a publish, a subscribe, or unpublish *and* unsubscribe. | |
| 45 if (publishes.size() > 0) { | |
| 46 if (!(publishes.size() == 1 && | |
| 47 publishes.Get(0).id() == "call" && | |
|
rkc
2014/08/07 08:56:57
Instead of checking every single operation and res
Charlie
2014/08/07 22:24:15
To get the ids you still have to drill down into t
| |
| 48 publishes.Get(0).access_policy().acl().acl_type() | |
| 49 == copresence::NO_ACL_CHECK && | |
| 50 publishes.Get(0).message().type().type() == "joke" && | |
| 51 publishes.Get(0).message().payload() == "Knock Knock!" && | |
| 52 publishes.Get(0).token_exchange_strategy() | |
| 53 .broadcast_scan_configuration() == copresence::BROADCAST_ONLY)) { | |
| 54 LOG(ERROR) << "Found invalid publish"; | |
| 55 status_callback.Run(FAIL); | |
| 56 return; | |
| 57 } | |
| 58 } else if (subscriptions.size() > 0) { | |
| 59 if (!(subscriptions.size() == 1 && | |
| 60 subscriptions.Get(0).id() == "response" && | |
| 61 subscriptions.Get(0).ttl_millis() == 1000 && | |
| 62 subscriptions.Get(0).message_type().type() == "joke" && | |
| 63 subscriptions.Get(0).token_exchange_strategy() | |
| 64 .broadcast_scan_configuration() == copresence::SCAN_ONLY)) { | |
| 65 LOG(ERROR) << "Found invalid subscribe"; | |
| 66 status_callback.Run(FAIL); | |
| 67 return; | |
| 68 } else { | |
| 69 // For subscriptions, deliver messages. | |
| 70 Message returned_message; | |
| 71 returned_message.mutable_type()->set_type("joke"); | |
| 72 returned_message.set_payload("Who's there?"); | |
| 73 delegate_->HandleMessages( | |
| 74 app_id, "response", std::vector<Message>(2, returned_message)); | |
| 75 | |
| 76 } | |
| 77 } else { | |
| 78 if (!(unpublishes.size() == 1 && unsubscribes.size() == 1 && | |
| 79 unpublishes.Get(0) == "call" && unsubscribes.Get(0) == "response")) { | |
| 80 LOG(ERROR) << "Found invalid unpublish/unsubscribe"; | |
| 81 status_callback.Run(FAIL); | |
| 82 return; | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 status_callback.Run(copresence::SUCCESS); | |
| 87 } | |
| 88 | |
| 89 private: | |
| 90 CopresenceClientDelegate* delegate_; | |
| 91 }; | |
| 92 | |
| 93 IN_PROC_BROWSER_TEST_F(CopresenceApiTest, Basic) { | |
| 94 CopresenceService* service = | |
| 95 CopresenceService::GetFactoryInstance()->Get(browser()->profile()); | |
| 96 scoped_ptr<copresence::CopresenceClient> mockClient( | |
|
not at google - send to devlin
2014/08/06 21:45:27
mock_client
Charlie
2014/08/07 22:24:15
Done.
| |
| 97 new MockCopresenceClient(service)); | |
| 98 service->set_client_for_testing(mockClient.Pass()); | |
|
rkc
2014/08/07 08:56:57
nit: service->set_client_for_testing(make_scoped_p
Charlie
2014/08/07 22:24:15
It's a little wordier than that since you have to
| |
| 99 | |
| 100 ASSERT_TRUE(RunExtensionTestNoFileAccess("copresence")) << message_; | |
| 101 } | |
| 102 | |
| 103 } // namespace extensions | |
| OLD | NEW |