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"
17 namespace copresence
{
21 // TODO(ckehoe): Move this to a central place.
22 std::string
ToUrlSafe(std::string token
) {
23 base::ReplaceChars(token
, "+", "-", &token
);
24 base::ReplaceChars(token
, "/", "_", &token
);
28 using google::protobuf::RepeatedPtrField
;
30 const RepeatedPtrField
<SubscribedMessage
>& /* messages */) {}
35 class GCMHandlerTest
: public testing::Test
{
38 : driver_(new gcm::FakeGCMDriver
),
39 directive_handler_(new FakeDirectiveHandler
),
40 gcm_handler_(driver_
.get(),
41 directive_handler_
.get(),
42 base::Bind(&IgnoreMessages
)) {
46 void ProcessMessage(const GCMClient::IncomingMessage
& message
) {
47 gcm_handler_
.OnMessage(GCMHandlerImpl::kCopresenceAppId
, message
);
50 scoped_ptr
<gcm::GCMDriver
> driver_
;
51 scoped_ptr
<FakeDirectiveHandler
> directive_handler_
;
52 GCMHandlerImpl gcm_handler_
;
55 TEST_F(GCMHandlerTest
, OnMessage
) {
56 // Create a PushMessage.
57 PushMessage push_message
;
58 push_message
.set_type(PushMessage::REPORT
);
59 Report
* report
= push_message
.mutable_report();
60 report
->add_directive()->set_subscription_id("subscription 1");
61 report
->add_directive()->set_subscription_id("subscription 2");
64 std::string serialized_proto
;
65 std::string encoded_proto
;
66 push_message
.SerializeToString(&serialized_proto
);
67 base::Base64Encode(serialized_proto
, &encoded_proto
);
69 // Send it in a GCM message.
70 GCMClient::IncomingMessage gcm_message
;
71 gcm_message
.data
[GCMHandlerImpl::kGcmMessageKey
] = ToUrlSafe(encoded_proto
);
72 ProcessMessage(gcm_message
);
74 // Check that the correct directives were passed along.
75 EXPECT_THAT(directive_handler_
->added_directives(),
76 testing::ElementsAre("subscription 1", "subscription 2"));
79 } // namespace copresence