Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / components / copresence / handlers / gcm_handler_unittest.cc
blob39a873bf88e1ab45ba4b16dbe4376f919e6f8a98
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
3 // found in the LICENSE file.
5 #include "base/base64.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/strings/string_util.h"
8 #include "components/copresence/handlers/gcm_handler_impl.h"
9 #include "components/copresence/proto/push_message.pb.h"
10 #include "components/copresence/test/fake_directive_handler.h"
11 #include "components/gcm_driver/fake_gcm_driver.h"
12 #include "components/gcm_driver/gcm_client.h"
13 #include "testing/gmock/include/gmock/gmock.h"
15 namespace copresence {
17 namespace {
19 // TODO(ckehoe): Move this to a central place.
20 std::string ToUrlSafe(std::string token) {
21 base::ReplaceChars(token, "+", "-", &token);
22 base::ReplaceChars(token, "/", "_", &token);
23 return token;
26 using google::protobuf::RepeatedPtrField;
27 void IgnoreMessages(
28 const RepeatedPtrField<SubscribedMessage>& /* messages */) {}
30 } // namespace
33 class GCMHandlerTest : public testing::Test {
34 public:
35 GCMHandlerTest()
36 : driver_(new gcm::FakeGCMDriver),
37 directive_handler_(new FakeDirectiveHandler),
38 gcm_handler_(driver_.get(),
39 directive_handler_.get(),
40 base::Bind(&IgnoreMessages)) {
43 protected:
44 void ProcessMessage(const gcm::IncomingMessage& message) {
45 gcm_handler_.OnMessage(GCMHandlerImpl::kCopresenceAppId, message);
48 scoped_ptr<gcm::GCMDriver> driver_;
49 scoped_ptr<FakeDirectiveHandler> directive_handler_;
50 GCMHandlerImpl gcm_handler_;
53 TEST_F(GCMHandlerTest, OnMessage) {
54 // Create a PushMessage.
55 PushMessage push_message;
56 push_message.set_type(PushMessage::REPORT);
57 Report* report = push_message.mutable_report();
58 report->add_directive()->set_subscription_id("subscription 1");
59 report->add_directive()->set_subscription_id("subscription 2");
61 // Encode it.
62 std::string serialized_proto;
63 std::string encoded_proto;
64 push_message.SerializeToString(&serialized_proto);
65 base::Base64Encode(serialized_proto, &encoded_proto);
67 // Send it in a GCM message.
68 gcm::IncomingMessage gcm_message;
69 gcm_message.data[GCMHandlerImpl::kGcmMessageKey] = ToUrlSafe(encoded_proto);
70 ProcessMessage(gcm_message);
72 // Check that the correct directives were passed along.
73 EXPECT_THAT(directive_handler_->added_directives(),
74 testing::ElementsAre("subscription 1", "subscription 2"));
77 } // namespace copresence