Add ICU message format support
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_test_helper.h
blobfb00bc388cdcd4cbf2615f3cb7d4828880105491
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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/memory/weak_ptr.h"
14 #include "ipc/ipc_listener.h"
15 #include "ipc/ipc_test_sink.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h"
19 struct EmbeddedWorkerMsg_StartWorker_Params;
20 class GURL;
22 namespace content {
24 class EmbeddedWorkerRegistry;
25 class EmbeddedWorkerTestHelper;
26 class MessagePortMessageFilter;
27 class ServiceWorkerContextCore;
28 class ServiceWorkerContextWrapper;
29 struct ServiceWorkerFetchRequest;
31 // In-Process EmbeddedWorker test helper.
33 // Usage: create an instance of this class to test browser-side embedded worker
34 // code without creating a child process. This class will create a
35 // ServiceWorkerContextWrapper and ServiceWorkerContextCore for you.
37 // By default this class just notifies back WorkerStarted and WorkerStopped
38 // for StartWorker and StopWorker requests. The default implementation
39 // also returns success for event messages (e.g. InstallEvent, FetchEvent).
41 // Alternatively consumers can subclass this helper and override On*()
42 // methods to add their own logic/verification code.
44 // See embedded_worker_instance_unittest.cc for example usages.
46 class EmbeddedWorkerTestHelper : public IPC::Sender,
47 public IPC::Listener {
48 public:
49 // Initialize this helper for |context|, and enable this as an IPC
50 // sender for |mock_render_process_id|. If |user_data_directory| is empty,
51 // the context makes storage stuff in memory.
52 EmbeddedWorkerTestHelper(const base::FilePath& user_data_directory,
53 int mock_render_process_id);
54 ~EmbeddedWorkerTestHelper() override;
56 // Call this to simulate add/associate a process to a pattern.
57 // This also registers this sender for the process.
58 void SimulateAddProcessToPattern(const GURL& pattern, int process_id);
60 // IPC::Sender implementation.
61 bool Send(IPC::Message* message) override;
63 // IPC::Listener implementation.
64 bool OnMessageReceived(const IPC::Message& msg) override;
66 // IPC sink for EmbeddedWorker messages.
67 IPC::TestSink* ipc_sink() { return &sink_; }
68 // Inner IPC sink for script context messages sent via EmbeddedWorker.
69 IPC::TestSink* inner_ipc_sink() { return &inner_sink_; }
71 ServiceWorkerContextCore* context();
72 ServiceWorkerContextWrapper* context_wrapper() { return wrapper_.get(); }
73 void ShutdownContext();
75 int mock_render_process_id() const { return mock_render_process_id_;}
77 protected:
78 // Called when StartWorker, StopWorker and SendMessageToWorker message
79 // is sent to the embedded worker. Override if necessary. By default
80 // they verify given parameters and:
81 // - OnStartWorker calls SimulateWorkerStarted
82 // - OnStopWorker calls SimulateWorkerStoped
83 // - OnSendMessageToWorker calls the message's respective On*Event handler
84 virtual void OnStartWorker(int embedded_worker_id,
85 int64 service_worker_version_id,
86 const GURL& scope,
87 const GURL& script_url);
88 virtual void OnStopWorker(int embedded_worker_id);
89 virtual bool OnMessageToWorker(int thread_id,
90 int embedded_worker_id,
91 const IPC::Message& message);
93 // On*Event handlers. Called by the default implementation of
94 // OnMessageToWorker when events are sent to the embedded
95 // worker. By default they just return success via
96 // SimulateSendReplyToBrowser.
97 virtual void OnActivateEvent(int embedded_worker_id, int request_id);
98 virtual void OnInstallEvent(int embedded_worker_id, int request_id);
99 virtual void OnFetchEvent(int embedded_worker_id,
100 int request_id,
101 const ServiceWorkerFetchRequest& request);
102 virtual void OnPushEvent(int embedded_worker_id,
103 int request_id,
104 const std::string& data);
106 // These functions simulate sending an EmbeddedHostMsg message to the
107 // browser.
108 void SimulateWorkerReadyForInspection(int embedded_worker_id);
109 void SimulateWorkerScriptCached(int embedded_worker_id);
110 void SimulateWorkerScriptLoaded(int thread_id, int embedded_worker_id);
111 void SimulateWorkerScriptEvaluated(int embedded_worker_id);
112 void SimulateWorkerStarted(int embedded_worker_id);
113 void SimulateWorkerStopped(int embedded_worker_id);
114 void SimulateSend(IPC::Message* message);
116 EmbeddedWorkerRegistry* registry();
118 private:
119 void OnStartWorkerStub(const EmbeddedWorkerMsg_StartWorker_Params& params);
120 void OnStopWorkerStub(int embedded_worker_id);
121 void OnMessageToWorkerStub(int thread_id,
122 int embedded_worker_id,
123 const IPC::Message& message);
124 void OnActivateEventStub(int request_id);
125 void OnInstallEventStub(int request_id);
126 void OnFetchEventStub(int request_id,
127 const ServiceWorkerFetchRequest& request);
128 void OnPushEventStub(int request_id, const std::string& data);
130 MessagePortMessageFilter* NewMessagePortMessageFilter();
132 scoped_refptr<ServiceWorkerContextWrapper> wrapper_;
134 IPC::TestSink sink_;
135 IPC::TestSink inner_sink_;
137 int next_thread_id_;
138 int mock_render_process_id_;
140 std::map<int, int64> embedded_worker_id_service_worker_version_id_map_;
142 // Updated each time MessageToWorker message is received.
143 int current_embedded_worker_id_;
145 std::vector<scoped_refptr<MessagePortMessageFilter>>
146 message_port_message_filters_;
148 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_;
150 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper);
153 } // namespace content
155 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_