Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2822)

Unified Diff: chrome/browser/extensions/api/copresence/copresence_apitest.cc

Issue 441103002: Tests for the Copresence API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@api
Patch Set: "Assertions" cleanup Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..5e39d57443cc2c5b8d300c74e170b84042633c0b
--- /dev/null
+++ b/chrome/browser/extensions/api/copresence/copresence_apitest.cc
@@ -0,0 +1,128 @@
+// 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;
+
+template <typename T>
+bool GetOnly(const RepeatedPtrField<T>& things, T* out) {
+ if (things.size() != 1) {
+ return false;
+ } else {
not at google - send to devlin 2014/08/07 23:21:12 no else after return
Charlie 2014/08/07 23:52:24 Done.
+ *out = things.Get(0);
+ return true;
+ }
+}
+
+bool ValidatePublishes(const RepeatedPtrField<PublishedMessage>& publishes) {
+ PublishedMessage publish;
+ return GetOnly(publishes, &publish) &&
+ publish.id() == "call" &&
+ publish.access_policy().acl().acl_type() == copresence::NO_ACL_CHECK &&
+ publish.message().type().type() == "joke" &&
+ publish.message().payload() == "Knock Knock!" &&
+ publish.token_exchange_strategy().broadcast_scan_configuration()
+ == copresence::BROADCAST_ONLY;
+}
+
+bool ValidateSubscriptions(
+ const RepeatedPtrField<Subscription>& subscriptions) {
+ Subscription subscription;
+ return GetOnly(subscriptions, &subscription) &&
+ subscription.id() == "response" &&
+ subscription.ttl_millis() == 1000 &&
+ subscription.message_type().type() == "joke" &&
+ subscription.token_exchange_strategy().broadcast_scan_configuration()
+ == copresence::SCAN_ONLY;
+}
+
+bool ValidateUnpublishUnsubscribe(const ReportRequest& request) {
+ const RepeatedPtrField<std::string>& unpublish_ids =
+ request.manage_messages_request().id_to_unpublish();
+ const RepeatedPtrField<std::string>& unsubscribe_ids =
+ request.manage_subscriptions_request().id_to_unsubscribe();
+
+ std::string unpublish_id;
+ std::string unsubscribe_id;
+ return GetOnly(unpublish_ids, &unpublish_id) && unpublish_id == "call" &&
+ GetOnly(unsubscribe_ids, &unsubscribe_id) && unsubscribe_id == "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

Powered by Google App Engine
This is Rietveld 408576698