1 /*
2  * Copyright (c) 2013, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_TESTING_DUMMY_PAGE_HOLDER_H_
32 #define THIRD_PARTY_BLINK_RENDERER_CORE_TESTING_DUMMY_PAGE_HOLDER_H_
33 
34 #include <memory>
35 
36 #include "base/callback.h"
37 #include "base/callback_helpers.h"
38 #include "base/macros.h"
39 #include "base/time/default_tick_clock.h"
40 #include "third_party/blink/renderer/core/frame/local_frame_client.h"
41 #include "third_party/blink/renderer/core/page/page.h"
42 #include "third_party/blink/renderer/core/testing/scoped_mock_overlay_scrollbars.h"
43 #include "third_party/blink/renderer/platform/geometry/int_size.h"
44 #include "third_party/blink/renderer/platform/heap/handle.h"
45 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
46 
47 namespace blink {
48 
49 class Document;
50 class IntSize;
51 class LocalFrame;
52 class LocalFrameView;
53 class Settings;
54 
55 // Creates a dummy Page, LocalFrame, and LocalFrameView whose clients are all
56 // no-op.
57 //
58 // This class can be used when you write unit tests for components which do not
59 // work correctly without layoutObjects.  To make sure the layoutObjects are
60 // created, you need to call |frameView().layout()| after you add nodes into
61 // |document()|.
62 //
63 // Since DummyPageHolder stores empty clients in it, it must outlive the Page,
64 // LocalFrame, LocalFrameView and any other objects created by it.
65 // DummyPageHolder's destructor ensures this condition by checking remaining
66 // references to the LocalFrame.
67 
68 class DummyPageHolder {
69   USING_FAST_MALLOC(DummyPageHolder);
70 
71  public:
72   DummyPageHolder(
73       const IntSize& initial_view_size = IntSize(),
74       Page::PageClients* = nullptr,
75       LocalFrameClient* = nullptr,
76       base::OnceCallback<void(Settings&)> setting_overrider =
77           base::NullCallback(),
78       const base::TickClock* clock = base::DefaultTickClock::GetInstance());
79   ~DummyPageHolder();
80 
81   Page& GetPage() const;
82   LocalFrame& GetFrame() const;
83   LocalFrameView& GetFrameView() const;
84   Document& GetDocument() const;
85 
86  private:
87   Persistent<Page> page_;
88 
89   // Unit tests need to run with a mock theme enabled. This is necessitated
90   // particularly by Android on which unit tests run without a platform theme
91   // engine.
92   ScopedMockOverlayScrollbars enable_mock_scrollbars_;
93 
94   // The LocalFrame is accessed from worker threads by unit tests
95   // (ThreadableLoaderTest), hence we need to allow cross-thread
96   // usage of |m_frame|.
97   //
98   // TODO: rework the tests to not require cross-thread access.
99   CrossThreadPersistent<LocalFrame> frame_;
100 
101   Persistent<LocalFrameClient> local_frame_client_;
102   DISALLOW_COPY_AND_ASSIGN(DummyPageHolder);
103 };
104 
105 }  // namespace blink
106 
107 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_TESTING_DUMMY_PAGE_HOLDER_H_
108