OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" | 5 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "content/public/browser/browser_context.h" | 11 #include "content/public/browser/browser_context.h" |
12 | 12 |
13 namespace gcm { | 13 namespace gcm { |
14 | 14 |
| 15 namespace { |
| 16 |
| 17 class FakeGCMDriver : public GCMDriver { |
| 18 public: |
| 19 explicit FakeGCMDriver(FakeGCMProfileService* service); |
| 20 virtual ~FakeGCMDriver(); |
| 21 |
| 22 // GCMDriver overrides. |
| 23 virtual void Register(const std::string& app_id, |
| 24 const std::vector<std::string>& sender_ids, |
| 25 const RegisterCallback& callback) OVERRIDE; |
| 26 virtual void Unregister(const std::string& app_id, |
| 27 const UnregisterCallback& callback) OVERRIDE; |
| 28 virtual void Send(const std::string& app_id, |
| 29 const std::string& receiver_id, |
| 30 const GCMClient::OutgoingMessage& message, |
| 31 const SendCallback& callback) OVERRIDE; |
| 32 |
| 33 private: |
| 34 FakeGCMProfileService* service_; |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(FakeGCMDriver); |
| 37 }; |
| 38 |
| 39 FakeGCMDriver::FakeGCMDriver(FakeGCMProfileService* service) |
| 40 : service_(service) { |
| 41 } |
| 42 |
| 43 FakeGCMDriver::~FakeGCMDriver() { |
| 44 } |
| 45 |
| 46 void FakeGCMDriver::Register(const std::string& app_id, |
| 47 const std::vector<std::string>& sender_ids, |
| 48 const RegisterCallback& callback) { |
| 49 base::MessageLoop::current()->PostTask( |
| 50 FROM_HERE, |
| 51 base::Bind(&FakeGCMProfileService::RegisterFinished, |
| 52 base::Unretained(service_), |
| 53 app_id, |
| 54 sender_ids, |
| 55 callback)); |
| 56 } |
| 57 |
| 58 void FakeGCMDriver::Unregister(const std::string& app_id, |
| 59 const UnregisterCallback& callback) { |
| 60 base::MessageLoop::current()->PostTask( |
| 61 FROM_HERE, base::Bind( |
| 62 &FakeGCMProfileService::UnregisterFinished, |
| 63 base::Unretained(service_), |
| 64 app_id, |
| 65 callback)); |
| 66 } |
| 67 |
| 68 void FakeGCMDriver::Send(const std::string& app_id, |
| 69 const std::string& receiver_id, |
| 70 const GCMClient::OutgoingMessage& message, |
| 71 const SendCallback& callback) { |
| 72 base::MessageLoop::current()->PostTask( |
| 73 FROM_HERE, |
| 74 base::Bind(&FakeGCMProfileService::SendFinished, |
| 75 base::Unretained(service_), |
| 76 app_id, |
| 77 receiver_id, |
| 78 message, |
| 79 callback)); |
| 80 } |
| 81 |
| 82 } // namespace |
| 83 |
15 // static | 84 // static |
16 KeyedService* FakeGCMProfileService::Build(content::BrowserContext* context) { | 85 KeyedService* FakeGCMProfileService::Build(content::BrowserContext* context) { |
17 Profile* profile = static_cast<Profile*>(context); | 86 Profile* profile = static_cast<Profile*>(context); |
18 return new FakeGCMProfileService(profile); | 87 FakeGCMProfileService* service = new FakeGCMProfileService(profile); |
| 88 service->SetDriverForTesting(new FakeGCMDriver(service)); |
| 89 return service; |
19 } | 90 } |
20 | 91 |
21 FakeGCMProfileService::FakeGCMProfileService(Profile* profile) | 92 FakeGCMProfileService::FakeGCMProfileService(Profile* profile) |
22 : GCMProfileService(profile), | 93 : GCMProfileService(profile), |
23 collect_(false) {} | 94 collect_(false) {} |
24 | 95 |
25 FakeGCMProfileService::~FakeGCMProfileService() {} | 96 FakeGCMProfileService::~FakeGCMProfileService() {} |
26 | 97 |
27 void FakeGCMProfileService::Register(const std::string& app_id, | |
28 const std::vector<std::string>& sender_ids, | |
29 const RegisterCallback& callback) { | |
30 base::MessageLoop::current()->PostTask( | |
31 FROM_HERE, | |
32 base::Bind(&FakeGCMProfileService::RegisterFinished, | |
33 base::Unretained(this), | |
34 app_id, | |
35 sender_ids, | |
36 callback)); | |
37 } | |
38 | |
39 void FakeGCMProfileService::RegisterFinished( | 98 void FakeGCMProfileService::RegisterFinished( |
40 const std::string& app_id, | 99 const std::string& app_id, |
41 const std::vector<std::string>& sender_ids, | 100 const std::vector<std::string>& sender_ids, |
42 const RegisterCallback& callback) { | 101 const GCMDriver::RegisterCallback& callback) { |
43 if (collect_) { | 102 if (collect_) { |
44 last_registered_app_id_ = app_id; | 103 last_registered_app_id_ = app_id; |
45 last_registered_sender_ids_ = sender_ids; | 104 last_registered_sender_ids_ = sender_ids; |
46 } | 105 } |
47 | 106 |
48 callback.Run(base::UintToString(sender_ids.size()), GCMClient::SUCCESS); | 107 callback.Run(base::UintToString(sender_ids.size()), GCMClient::SUCCESS); |
49 } | 108 } |
50 | 109 |
51 void FakeGCMProfileService::Unregister(const std::string& app_id, | 110 void FakeGCMProfileService::UnregisterFinished( |
52 const UnregisterCallback& callback) { | 111 const std::string& app_id, |
53 base::MessageLoop::current()->PostTask( | 112 const GCMDriver::UnregisterCallback& callback) { |
54 FROM_HERE, base::Bind(callback, GetNextExpectedUnregisterResponse())); | 113 GCMClient::Result result = GCMClient::SUCCESS; |
55 } | 114 if (!unregister_responses_.empty()) { |
| 115 result = unregister_responses_.front(); |
| 116 unregister_responses_.pop_front(); |
| 117 } |
56 | 118 |
57 void FakeGCMProfileService::Send(const std::string& app_id, | 119 callback.Run(result); |
58 const std::string& receiver_id, | |
59 const GCMClient::OutgoingMessage& message, | |
60 const SendCallback& callback) { | |
61 base::MessageLoop::current()->PostTask( | |
62 FROM_HERE, | |
63 base::Bind(&FakeGCMProfileService::SendFinished, | |
64 base::Unretained(this), | |
65 app_id, | |
66 receiver_id, | |
67 message, | |
68 callback)); | |
69 } | 120 } |
70 | 121 |
71 void FakeGCMProfileService::SendFinished( | 122 void FakeGCMProfileService::SendFinished( |
72 const std::string& app_id, | 123 const std::string& app_id, |
73 const std::string& receiver_id, | 124 const std::string& receiver_id, |
74 const GCMClient::OutgoingMessage& message, | 125 const GCMClient::OutgoingMessage& message, |
75 const SendCallback& callback) { | 126 const GCMDriver::SendCallback& callback) { |
76 if (collect_) { | 127 if (collect_) { |
77 last_sent_message_ = message; | 128 last_sent_message_ = message; |
78 last_receiver_id_ = receiver_id; | 129 last_receiver_id_ = receiver_id; |
79 } | 130 } |
80 | 131 |
81 callback.Run(message.id, GCMClient::SUCCESS); | 132 callback.Run(message.id, GCMClient::SUCCESS); |
82 } | 133 } |
83 | 134 |
84 void FakeGCMProfileService::AddExpectedUnregisterResponse( | 135 void FakeGCMProfileService::AddExpectedUnregisterResponse( |
85 GCMClient::Result result) { | 136 GCMClient::Result result) { |
86 unregister_responses_.push_back(result); | 137 unregister_responses_.push_back(result); |
87 } | 138 } |
88 | 139 |
89 GCMClient::Result FakeGCMProfileService::GetNextExpectedUnregisterResponse() { | |
90 if (unregister_responses_.empty()) | |
91 return GCMClient::SUCCESS; | |
92 GCMClient::Result response = *unregister_responses_.begin(); | |
93 unregister_responses_.erase(unregister_responses_.begin()); | |
94 return response; | |
95 } | |
96 | |
97 } // namespace gcm | 140 } // namespace gcm |
OLD | NEW |