• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #include <memory>
6 
7 #include "include/base/cef_callback.h"
8 #include "include/base/cef_weak_ptr.h"
9 #include "include/cef_waitable_event.h"
10 #include "include/views/cef_window.h"
11 #include "include/views/cef_window_delegate.h"
12 
13 class TestWindowDelegate : public CefWindowDelegate {
14  public:
15   // Default window size.
16   static const int kWSize;
17 
18   // Test execution callback.
19   using OnWindowCreatedCallback =
20       base::OnceCallback<void(CefRefPtr<CefWindow>)>;
21   using OnWindowDestroyedCallback =
22       base::OnceCallback<void(CefRefPtr<CefWindow>)>;
23   using OnAcceleratorCallback =
24       base::RepeatingCallback<bool(CefRefPtr<CefWindow>, int)>;
25   using OnKeyEventCallback =
26       base::RepeatingCallback<bool(CefRefPtr<CefWindow>, const CefKeyEvent&)>;
27 
28   struct Config {
29     OnWindowCreatedCallback on_window_created;
30     OnWindowDestroyedCallback on_window_destroyed;
31     OnAcceleratorCallback on_accelerator;
32     OnKeyEventCallback on_key_event;
33     bool frameless = false;
34     bool close_window = true;
35     int window_size = kWSize;
36     CefPoint window_origin = {};
37   };
38 
39   // Creates a Window with a new TestWindowDelegate instance and executes
40   // |window_test| after the Window is created. |event| will be signaled once
41   // the Window is closed. If |frameless| is true the Window will be created
42   // without a frame. If |close_window| is true the Window will be closed
43   // immediately after |window_test| returns. Otherwise, the caller is
44   // responsible for closing the Window passed to |window_test|.
45   static void RunTest(CefRefPtr<CefWaitableEvent> event,
46                       std::unique_ptr<Config> config);
47 
48   // CefWindowDelegate methods:
49   void OnWindowCreated(CefRefPtr<CefWindow> window) override;
50   void OnWindowDestroyed(CefRefPtr<CefWindow> window) override;
51   bool IsFrameless(CefRefPtr<CefWindow> window) override;
52   CefRect GetInitialBounds(CefRefPtr<CefWindow> window) override;
53   CefSize GetPreferredSize(CefRefPtr<CefView> view) override;
54   bool OnAccelerator(CefRefPtr<CefWindow> window, int command_id) override;
55   bool OnKeyEvent(CefRefPtr<CefWindow> window,
56                   const CefKeyEvent& event) override;
57 
58  private:
59   TestWindowDelegate(CefRefPtr<CefWaitableEvent> event,
60                      std::unique_ptr<Config> config,
61                      const CefSize& window_size);
62   ~TestWindowDelegate() override;
63 
64   void OnCloseWindow();
65   void OnTimeoutWindow();
66 
67   CefRefPtr<CefWaitableEvent> event_;
68   std::unique_ptr<Config> config_;
69   const CefSize window_size_;
70 
71   CefRefPtr<CefWindow> window_;
72 
73   bool got_get_initial_bounds_ = false;
74   bool got_get_preferred_size_ = false;
75 
76   // Must be the last member.
77   base::WeakPtrFactory<TestWindowDelegate> weak_ptr_factory_;
78 
79   IMPLEMENT_REFCOUNTING(TestWindowDelegate);
80   DISALLOW_COPY_AND_ASSIGN(TestWindowDelegate);
81 };
82