Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_test_helper.h
blob7cd817df6b9ee7d4eff3145904285b77eb675b5b
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 EmbeddedWorkerTestHelper(int mock_render_process_id);
49 virtual ~EmbeddedWorkerTestHelper();
51 // Call this to simulate add/associate a process to a worker.
52 // This also registers this sender for the process.
53 void SimulateAddProcessToWorker(int embedded_worker_id, int process_id);
55 // IPC::Sender implementation.
56 virtual bool Send(IPC::Message* message) OVERRIDE;
58 // IPC::Listener implementation.
59 virtual 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 protected:
71 // Called when StartWorker, StopWorker and SendMessageToWorker message
72 // is sent to the embedded worker. Override if necessary. By default
73 // they verify given parameters and:
74 // - OnStartWorker calls SimulateWorkerStarted
75 // - OnStopWorker calls SimulateWorkerStoped
76 // - OnSendMessageToWorker calls the message's respective On*Event handler
77 virtual void OnStartWorker(int embedded_worker_id,
78 int64 service_worker_version_id,
79 const GURL& scope,
80 const GURL& script_url);
81 virtual void OnStopWorker(int embedded_worker_id);
82 virtual bool OnMessageToWorker(int thread_id,
83 int embedded_worker_id,
84 const IPC::Message& message);
86 // On*Event handlers. Called by the default implementation of
87 // OnMessageToWorker when events are sent to the embedded
88 // worker. By default they just return success via
89 // SimulateSendReplyToBrowser.
90 virtual void OnActivateEvent(int embedded_worker_id, int request_id);
91 virtual void OnInstallEvent(int embedded_worker_id,
92 int request_id,
93 int active_version_id);
94 virtual void OnFetchEvent(int embedded_worker_id,
95 int request_id,
96 const ServiceWorkerFetchRequest& request);
98 // These functions simulate sending an EmbeddedHostMsg message to the
99 // browser.
100 void SimulateWorkerStarted(int thread_id, int embedded_worker_id);
101 void SimulateWorkerStopped(int embedded_worker_id);
102 void SimulateSend(IPC::Message* message);
104 protected:
105 EmbeddedWorkerRegistry* registry();
107 private:
108 void OnStartWorkerStub(const EmbeddedWorkerMsg_StartWorker_Params& params);
109 void OnStopWorkerStub(int embedded_worker_id);
110 void OnMessageToWorkerStub(int thread_id,
111 int embedded_worker_id,
112 const IPC::Message& message);
113 void OnActivateEventStub(int request_id);
114 void OnInstallEventStub(int request_id, int active_version_id);
115 void OnFetchEventStub(int request_id,
116 const ServiceWorkerFetchRequest& request);
118 scoped_refptr<ServiceWorkerContextWrapper> wrapper_;
120 IPC::TestSink sink_;
121 IPC::TestSink inner_sink_;
123 int next_thread_id_;
125 // Updated each time MessageToWorker message is received.
126 int current_embedded_worker_id_;
128 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_;
130 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper);
133 } // namespace content
135 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_