Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_test_helper.h
blob6640d853fd6bfef62a52443a48f1d41026958d30
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|.
48 explicit EmbeddedWorkerTestHelper(int mock_render_process_id);
49 ~EmbeddedWorkerTestHelper() override;
51 // Call this to simulate add/associate a process to a pattern.
52 // This also registers this sender for the process.
53 void SimulateAddProcessToPattern(const GURL& pattern, int process_id);
55 // IPC::Sender implementation.
56 bool Send(IPC::Message* message) override;
58 // IPC::Listener implementation.
59 bool OnMessageReceived(const IPC::Message& msg) override;
61 // IPC sink for EmbeddedWorker messages.
62 IPC::TestSink* ipc_sink() { return &sink_; }
63 // Inner IPC sink for script context messages sent via EmbeddedWorker.
64 IPC::TestSink* inner_ipc_sink() { return &inner_sink_; }
66 ServiceWorkerContextCore* context();
67 ServiceWorkerContextWrapper* context_wrapper() { return wrapper_.get(); }
68 void ShutdownContext();
70 int mock_render_process_id() const { return mock_render_process_id_;}
72 protected:
73 // Called when StartWorker, StopWorker and SendMessageToWorker message
74 // is sent to the embedded worker. Override if necessary. By default
75 // they verify given parameters and:
76 // - OnStartWorker calls SimulateWorkerStarted
77 // - OnStopWorker calls SimulateWorkerStoped
78 // - OnSendMessageToWorker calls the message's respective On*Event handler
79 virtual void OnStartWorker(int embedded_worker_id,
80 int64 service_worker_version_id,
81 const GURL& scope,
82 const GURL& script_url,
83 bool pause_after_download);
84 virtual void OnResumeAfterDownload(int embedded_worker_id);
85 virtual void OnStopWorker(int embedded_worker_id);
86 virtual bool OnMessageToWorker(int thread_id,
87 int embedded_worker_id,
88 const IPC::Message& message);
90 // On*Event handlers. Called by the default implementation of
91 // OnMessageToWorker when events are sent to the embedded
92 // worker. By default they just return success via
93 // SimulateSendReplyToBrowser.
94 virtual void OnActivateEvent(int embedded_worker_id, int request_id);
95 virtual void OnInstallEvent(int embedded_worker_id,
96 int request_id,
97 int active_version_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, int active_version_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_