1 //
2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // WindowTest.cpp: Sample used to test various function of OSWindow
8
9 #include <algorithm>
10 #include <iostream>
11
12 #include "util/OSWindow.h"
13 #include "util/test_utils.h"
14
main(int argc,char * argv[])15 int main(int argc, char *argv[])
16 {
17 OSWindow *window = OSWindow::New();
18 int width = 400;
19 int height = 400;
20 int x = 0;
21 int y = 0;
22
23 if (!window->initialize("Window Test", width, height))
24 {
25 return -1;
26 }
27 window->setVisible(true);
28 window->setPosition(x, y);
29
30 bool running = true;
31 while (running)
32 {
33 Event event;
34 while (window->popEvent(&event))
35 {
36 if (event.Type == Event::EVENT_CLOSED)
37 {
38 running = false;
39 break;
40 }
41
42 if (event.Type == Event::EVENT_KEY_PRESSED)
43 {
44 int newWidth = width;
45 int newHeight = height;
46 int newX = x;
47 int newY = y;
48 switch (event.Key.Code)
49 {
50 case KEY_ESCAPE:
51 running = false;
52 break;
53
54 case KEY_W:
55 newWidth = std::max(0, width + (event.Key.Shift ? -20 : 20));
56 break;
57 case KEY_H:
58 newHeight = std::max(0, height + (event.Key.Shift ? -20 : 20));
59 break;
60
61 case KEY_LEFT:
62 newX = x - 20;
63 break;
64 case KEY_RIGHT:
65 newX = x + 20;
66 break;
67 case KEY_UP:
68 newY = y - 20;
69 break;
70 case KEY_DOWN:
71 newY = y + 20;
72 break;
73
74 case KEY_C:
75 window->setMousePosition(width / 2, height / 2);
76 break;
77 case KEY_T:
78 window->signalTestEvent();
79 window->messageLoop();
80 if (window->didTestEventFire())
81 {
82 std::cout << "Test event did fire" << std::endl;
83 }
84 else
85 {
86 std::cout << "Test event did not fire" << std::endl;
87 }
88 break;
89 case KEY_S:
90 window->setVisible(false);
91 window->messageLoop();
92 angle::Sleep(1000);
93 window->setVisible(true);
94 window->messageLoop();
95 break;
96
97 default:
98 break;
99 }
100
101 if (newWidth != width || newHeight != height)
102 {
103 width = newWidth;
104 height = newHeight;
105 window->resize(width, height);
106 }
107 if (newX != x || newY != y)
108 {
109 x = newX;
110 y = newY;
111 window->setPosition(x, y);
112 }
113
114 angle::Sleep(0);
115 window->messageLoop();
116 if (window->getWidth() != width || window->getHeight() != height)
117 {
118 std::cout << "Discrepancy between set dimensions and retrieved dimensions"
119 << std::endl;
120 std::cout << "Width: " << width << " vs. " << window->getWidth() << std::endl;
121 std::cout << "Height: " << height << " vs. " << window->getHeight()
122 << std::endl;
123 }
124 if (window->getX() != x || window->getY() != y)
125 {
126 std::cout << "Discrepancy between set position and retrieved position"
127 << std::endl;
128 std::cout << "X: " << x << " vs. " << window->getX() << std::endl;
129 std::cout << "Y: " << y << " vs. " << window->getY() << std::endl;
130 }
131 }
132 }
133
134 angle::Sleep(0);
135 window->messageLoop();
136 }
137
138 window->destroy();
139 }
140