1 // Copyright (c) 2013 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 "tests/cefclient/browser/window_test.h"
6
7 #include <algorithm>
8 #include <sstream>
9 #include <string>
10 #include <vector>
11
12 #include "include/base/cef_callback.h"
13 #include "include/wrapper/cef_stream_resource_handler.h"
14 #include "tests/cefclient/browser/main_context.h"
15 #include "tests/cefclient/browser/test_runner.h"
16 #include "tests/cefclient/browser/window_test_runner.h"
17
18 #if defined(OS_WIN) || defined(OS_LINUX)
19 #include "tests/cefclient/browser/window_test_runner_views.h"
20 #endif
21
22 #if defined(OS_WIN)
23 #include "tests/cefclient/browser/window_test_runner_win.h"
24 #elif defined(OS_LINUX)
25 #include "tests/cefclient/browser/window_test_runner_gtk.h"
26 #elif defined(OS_MAC)
27 #include "tests/cefclient/browser/window_test_runner_mac.h"
28 #endif
29
30 namespace client {
31 namespace window_test {
32
33 namespace {
34
35 const char kTestUrlPath[] = "/window";
36 const char kMessagePositionName[] = "WindowTest.Position";
37 const char kMessageMinimizeName[] = "WindowTest.Minimize";
38 const char kMessageMaximizeName[] = "WindowTest.Maximize";
39 const char kMessageRestoreName[] = "WindowTest.Restore";
40
41 // Create the appropriate platform test runner object.
CreateWindowTestRunner()42 std::unique_ptr<WindowTestRunner> CreateWindowTestRunner() {
43 #if defined(OS_WIN) || defined(OS_LINUX)
44 if (MainContext::Get()->UseViews())
45 return std::make_unique<WindowTestRunnerViews>();
46 #endif
47
48 #if defined(OS_WIN)
49 return std::make_unique<WindowTestRunnerWin>();
50 #elif defined(OS_LINUX)
51 return std::make_unique<WindowTestRunnerGtk>();
52 #elif defined(OS_MAC)
53 return std::make_unique<WindowTestRunnerMac>();
54 #else
55 #error "No implementation available for your platform."
56 #endif
57 }
58
59 // Handle messages in the browser process.
60 class Handler : public CefMessageRouterBrowserSide::Handler {
61 public:
Handler()62 Handler() : runner_(CreateWindowTestRunner()) {}
63
64 // Called due to cefBroadcast execution in window.html.
OnQuery(CefRefPtr<CefBrowser> browser,CefRefPtr<CefFrame> frame,int64 query_id,const CefString & request,bool persistent,CefRefPtr<Callback> callback)65 virtual bool OnQuery(CefRefPtr<CefBrowser> browser,
66 CefRefPtr<CefFrame> frame,
67 int64 query_id,
68 const CefString& request,
69 bool persistent,
70 CefRefPtr<Callback> callback) override {
71 // Only handle messages from the test URL.
72 const std::string& url = frame->GetURL();
73 if (!test_runner::IsTestURL(url, kTestUrlPath))
74 return false;
75
76 const std::string& message_name = request;
77 if (message_name.find(kMessagePositionName) == 0) {
78 // Parse the comma-delimited list of integer values.
79 std::vector<int> vec;
80 const std::string& vals =
81 message_name.substr(sizeof(kMessagePositionName));
82 std::stringstream ss(vals);
83 int i;
84 while (ss >> i) {
85 vec.push_back(i);
86 if (ss.peek() == ',')
87 ss.ignore();
88 }
89
90 if (vec.size() == 4) {
91 // Execute SetPos() on the main thread.
92 runner_->SetPos(browser, vec[0], vec[1], vec[2], vec[3]);
93 }
94 } else if (message_name == kMessageMinimizeName) {
95 // Execute Minimize() on the main thread.
96 runner_->Minimize(browser);
97 } else if (message_name == kMessageMaximizeName) {
98 // Execute Maximize() on the main thread.
99 runner_->Maximize(browser);
100 } else if (message_name == kMessageRestoreName) {
101 // Execute Restore() on the main thread.
102 runner_->Restore(browser);
103 } else {
104 NOTREACHED();
105 }
106
107 callback->Success("");
108 return true;
109 }
110
111 private:
112 std::unique_ptr<WindowTestRunner> runner_;
113 };
114
115 } // namespace
116
CreateMessageHandlers(test_runner::MessageHandlerSet & handlers)117 void CreateMessageHandlers(test_runner::MessageHandlerSet& handlers) {
118 handlers.insert(new Handler());
119 }
120
121 } // namespace window_test
122 } // namespace client
123