1 // Copyright 2015 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.
4 
5 #ifndef CONTENT_PUBLIC_TEST_TEST_WEB_CONTENTS_FACTORY_H_
6 #define CONTENT_PUBLIC_TEST_TEST_WEB_CONTENTS_FACTORY_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/macros.h"
12 
13 namespace content {
14 class BrowserContext;
15 class RenderViewHostTestEnabler;
16 class WebContents;
17 
18 // A helper class to create test web contents (tabs) for unit tests, without
19 // inheriting from RenderViewTestHarness. Can create web contents, and will
20 // clean up after itself upon destruction. Owns all created web contents.
21 // A few notes:
22 // - Works well allocated on the stack, because it should be destroyed before
23 //   associated browser context.
24 // - Doesn't play nice with web contents created any other way (because of
25 //   the implementation of RenderViewHostTestEnabler). But if you are creating
26 //   web contents already, what do you need this for? ;)
27 // TODO(devlin): The API is currently a bit sparse; there may need to be methods
28 // to, e.g., delete/close a web contents, access existing web contents, etc.
29 // These can be added as-needed.
30 class TestWebContentsFactory {
31  public:
32   TestWebContentsFactory();
33   ~TestWebContentsFactory();
34 
35   // Creates a new WebContents with the given |context|, and returns it.
36   // Ownership remains with the TestWebContentsFactory.
37   WebContents* CreateWebContents(BrowserContext* context);
38 
39   // Destroys the provided WebContents.
40   void DestroyWebContents(WebContents* contents);
41 
42  private:
43   // The test factory (and friends) for creating test web contents.
44   std::unique_ptr<RenderViewHostTestEnabler> rvh_enabler_;
45 
46   // The vector of web contents that this class created.
47   std::vector<std::unique_ptr<WebContents>> web_contents_;
48 
49   DISALLOW_COPY_AND_ASSIGN(TestWebContentsFactory);
50 };
51 
52 }  // namespace content
53 
54 #endif  // CONTENT_PUBLIC_TEST_TEST_WEB_CONTENTS_FACTORY_H_
55