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 #ifndef CHROME_BROWSER_SERVICES_GCM_GCM_CLIENT_MOCK_H_ | 5 #ifndef CHROME_BROWSER_SERVICES_GCM_GCM_CLIENT_MOCK_H_ |
6 #define CHROME_BROWSER_SERVICES_GCM_GCM_CLIENT_MOCK_H_ | 6 #define CHROME_BROWSER_SERVICES_GCM_GCM_CLIENT_MOCK_H_ |
7 | 7 |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/memory/weak_ptr.h" |
9 #include "google_apis/gcm/gcm_client.h" | 10 #include "google_apis/gcm/gcm_client.h" |
10 | 11 |
11 namespace gcm { | 12 namespace gcm { |
12 | 13 |
13 class GCMClientMock : public GCMClient { | 14 class GCMClientMock : public GCMClient { |
14 public: | 15 public: |
15 enum Status { | 16 enum Status { |
16 NOT_READY, | 17 UNINITIALIZED, |
17 READY | 18 LOADED, |
| 19 CHECKED_OUT |
| 20 }; |
| 21 |
| 22 enum LoadingDelay { |
| 23 NO_DELAY_LOADING, |
| 24 DELAY_LOADING, |
18 }; | 25 }; |
19 | 26 |
20 enum ErrorSimulation { | 27 enum ErrorSimulation { |
21 ALWAYS_SUCCEED, | 28 ALWAYS_SUCCEED, |
22 FORCE_ERROR | 29 FORCE_ERROR |
23 }; | 30 }; |
24 | 31 |
25 // |status| denotes if the fake client is ready or not at the beginning. | 32 // |loading_delay| denotes if the check-in should be delayed. |
26 // |error_simulation| denotes if we should simulate server error. | 33 // |error_simulation| denotes if we should simulate server error. |
27 GCMClientMock(Status status, ErrorSimulation error_simulation); | 34 GCMClientMock(LoadingDelay loading_delay, ErrorSimulation error_simulation); |
28 virtual ~GCMClientMock(); | 35 virtual ~GCMClientMock(); |
29 | 36 |
30 // Overridden from GCMClient: | 37 // Overridden from GCMClient: |
31 // Called on IO thread. | 38 // Called on IO thread. |
32 virtual void Initialize( | 39 virtual void Initialize( |
33 const checkin_proto::ChromeBuildProto& chrome_build_proto, | 40 const checkin_proto::ChromeBuildProto& chrome_build_proto, |
34 const base::FilePath& store_path, | 41 const base::FilePath& store_path, |
35 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, | 42 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, |
36 const scoped_refptr<net::URLRequestContextGetter>& | 43 const scoped_refptr<net::URLRequestContextGetter>& |
37 url_request_context_getter, | 44 url_request_context_getter, |
38 Delegate* delegate) OVERRIDE; | 45 Delegate* delegate) OVERRIDE; |
| 46 virtual void Load() OVERRIDE; |
39 virtual void CheckOut() OVERRIDE; | 47 virtual void CheckOut() OVERRIDE; |
40 virtual void Register(const std::string& app_id, | 48 virtual void Register(const std::string& app_id, |
41 const std::string& cert, | 49 const std::string& cert, |
42 const std::vector<std::string>& sender_ids) OVERRIDE; | 50 const std::vector<std::string>& sender_ids) OVERRIDE; |
43 virtual void Unregister(const std::string& app_id) OVERRIDE; | 51 virtual void Unregister(const std::string& app_id) OVERRIDE; |
44 virtual void Send(const std::string& app_id, | 52 virtual void Send(const std::string& app_id, |
45 const std::string& receiver_id, | 53 const std::string& receiver_id, |
46 const OutgoingMessage& message) OVERRIDE; | 54 const OutgoingMessage& message) OVERRIDE; |
47 virtual bool IsReady() const OVERRIDE; | 55 |
| 56 // Initiate the loading that has been delayed. |
| 57 // Called on UI thread. |
| 58 void PerformDelayedLoading(); |
48 | 59 |
49 // Simulate receiving something from the server. | 60 // Simulate receiving something from the server. |
50 // Called on UI thread. | 61 // Called on UI thread. |
51 void ReceiveMessage(const std::string& app_id, | 62 void ReceiveMessage(const std::string& app_id, |
52 const IncomingMessage& message); | 63 const IncomingMessage& message); |
53 void DeleteMessages(const std::string& app_id); | 64 void DeleteMessages(const std::string& app_id); |
54 | 65 |
55 // Can only transition from non-ready to ready. | |
56 void SetReady(); | |
57 | |
58 static std::string GetRegistrationIdFromSenderIds( | 66 static std::string GetRegistrationIdFromSenderIds( |
59 const std::vector<std::string>& sender_ids); | 67 const std::vector<std::string>& sender_ids); |
60 | 68 |
| 69 Status status() const { return status_; } |
| 70 |
61 private: | 71 private: |
62 // Called on IO thread. | 72 // Called on IO thread. |
| 73 void DoLoading(); |
| 74 void CheckinFinished(); |
63 void RegisterFinished(const std::string& app_id, | 75 void RegisterFinished(const std::string& app_id, |
64 const std::string& registrion_id); | 76 const std::string& registrion_id); |
65 void SendFinished(const std::string& app_id, const std::string& message_id); | 77 void SendFinished(const std::string& app_id, const std::string& message_id); |
66 void MessageReceived(const std::string& app_id, | 78 void MessageReceived(const std::string& app_id, |
67 const IncomingMessage& message); | 79 const IncomingMessage& message); |
68 void MessagesDeleted(const std::string& app_id); | 80 void MessagesDeleted(const std::string& app_id); |
69 void MessageSendError(const std::string& app_id, | 81 void MessageSendError(const std::string& app_id, |
70 const std::string& message_id); | 82 const std::string& message_id); |
71 void SetReadyOnIO(); | |
72 | 83 |
73 Delegate* delegate_; | 84 Delegate* delegate_; |
74 Status status_; | 85 Status status_; |
| 86 LoadingDelay loading_delay_; |
75 ErrorSimulation error_simulation_; | 87 ErrorSimulation error_simulation_; |
| 88 base::WeakPtrFactory<GCMClientMock> weak_ptr_factory_; |
76 | 89 |
77 DISALLOW_COPY_AND_ASSIGN(GCMClientMock); | 90 DISALLOW_COPY_AND_ASSIGN(GCMClientMock); |
78 }; | 91 }; |
79 | 92 |
80 } // namespace gcm | 93 } // namespace gcm |
81 | 94 |
82 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_CLIENT_MOCK_H_ | 95 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_CLIENT_MOCK_H_ |
OLD | NEW |