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