libroot_debug: Merge guarded heap into libroot_debug.
[haiku.git] / src / tests / servers / app / gradients / main.cpp
blob47810a8145d264d96138e3ab08254f5b0daf711d
1 /*
2 * Copyright 2014 Haiku, Inc.
3 * Distributed under the terms of the MIT license.
5 * Authors:
6 * Adrien Destugues <pulkomandy@pulkomandy.tk>
7 */
10 #include <algorithm>
11 #include <stdio.h>
12 #include <string.h>
14 #include <Application.h>
15 #include <GradientLinear.h>
16 #include <GradientRadial.h>
17 #include <LayoutBuilder.h>
18 #include <List.h>
19 #include <Message.h>
20 #include <Picture.h>
21 #include <PopUpMenu.h>
22 #include <Roster.h>
23 #include <ScrollView.h>
24 #include <String.h>
25 #include <StringView.h>
26 #include <View.h>
27 #include <Window.h>
29 #include "harness.h"
32 static const char* kAppSignature = "application/x-vnd.Haiku-Gradients";
35 // #pragma mark - Test1
38 class RadialGradientTest : public Test {
39 public:
40 RadialGradientTest()
42 Test("Radial Gradient")
46 virtual void Draw(BView* view, BRect updateRect)
48 // Draws two radial gradients with the same stops and different radiis,
49 // and their enclosing circles. The gradients and circles should have
50 // the same size.
51 BGradientRadial g1(100, 100, 50);
52 BGradientRadial g2(300, 100, 100);
54 g1.AddColor(make_color(0,0,0,255), 0);
55 g1.AddColor(make_color(255,255,255,255), 255);
57 g2.AddColor(make_color(0,0,0,255), 0);
58 g2.AddColor(make_color(255,255,255,255), 255);
60 BRect r1(0, 0, 200, 200);
61 BRect r2(200, 0, 400, 200);
62 view->FillRect(r1, g1);
63 view->FillRect(r2, g2);
65 r1.InsetBy(50, 50);
66 view->StrokeEllipse(r1);
67 view->StrokeEllipse(r2);
72 // Test for https://dev.haiku-os.org/ticket/2946
73 // Gradients with an alpha channel are not drawn properly
74 class AlphaGradientTest : public Test {
75 public:
76 AlphaGradientTest()
78 Test("Alpha gradients")
82 virtual void Draw(BView* view, BRect updateRect)
84 view->SetDrawingMode(B_OP_ALPHA);
86 // These draw as almost transparent
88 // Radial gradient
89 BPoint center(50, 50);
90 float radius = 50.0;
91 BGradientRadial g(center, radius);
92 g.AddColor((rgb_color){ 255, 0, 0, 255 }, 0.0);
93 g.AddColor((rgb_color){ 0, 255, 0, 0 }, 255.0);
94 view->FillEllipse(center, radius, radius, g);
96 // Linear gradient - Horizontal
97 BPoint from(100, 0);
98 BPoint to(200, 0);
99 BGradientLinear l(from, to);
100 l.AddColor((rgb_color){ 255, 0, 0, 0 }, 0.0);
101 l.AddColor((rgb_color){ 0, 255, 0, 255 }, 255.0);
102 view->FillRect(BRect(100, 0, 200, 100), l);
104 // Linear gradient - Vertical
105 // (this uses a different code path in app_server)
106 BPoint top(0, 0);
107 BPoint bot(0, 100);
108 BGradientLinear v(top, bot);
109 v.AddColor((rgb_color){ 255, 0, 0, 0 }, 0.0);
110 v.AddColor((rgb_color){ 0, 255, 0, 255 }, 255.0);
111 view->FillRect(BRect(200, 0, 300, 100), v);
113 // These draw as opaque or almost opaque
115 view->SetOrigin(BPoint(0, 100));
117 // Radial gradient
118 BGradientRadial go(center, radius);
119 go.AddColor((rgb_color){ 255, 0, 0, 0 }, 0.0);
120 go.AddColor((rgb_color){ 0, 255, 0, 255 }, 255.0);
121 view->FillEllipse(center, radius, radius, go);
123 // Linear gradient - Horizontal
124 BGradientLinear lo(from, to);
125 lo.AddColor((rgb_color){ 255, 0, 0, 255 }, 0.0);
126 lo.AddColor((rgb_color){ 0, 255, 0, 0 }, 255.0);
127 view->FillRect(BRect(100, 0, 200, 100), lo);
129 // Linear gradient - Vertical
130 // (this uses a different code path in app_server)
131 BGradientLinear vo(top, bot);
132 vo.AddColor((rgb_color){ 255, 0, 0, 255 }, 0.0);
133 vo.AddColor((rgb_color){ 0, 255, 0, 0 }, 255.0);
134 view->FillRect(BRect(200, 0, 300, 100), vo);
136 // Finally, do the same using ClipToPicture. This forces using an
137 // unpacked scanline rasterizer, and it works.
138 view->SetOrigin(BPoint(0, 0));
140 view->SetDrawingMode(B_OP_COPY);
142 BPicture picture;
143 view->BeginPicture(&picture);
144 view->SetHighColor(make_color(0,0,0,255));
145 view->FillRect(BRect(0, 200, 300, 300));
146 view->EndPicture();
147 view->ClipToPicture(&picture);
149 view->SetOrigin(BPoint(0, 200));
151 view->SetDrawingMode(B_OP_ALPHA);
153 view->FillEllipse(center, radius, radius, g);
154 view->FillRect(BRect(100, 0, 200, 100), l);
155 view->FillRect(BRect(200, 0, 300, 100), v);
160 // Test for https://dev.haiku-os.org/ticket/2945
161 // Gradients with no stop at offset 0 or 255 draw random colors
162 class OutOfBoundsGradientTest : public Test {
163 public:
164 OutOfBoundsGradientTest()
166 Test("Out of bounds gradients")
170 virtual void Draw(BView* view, BRect updateRect)
173 // Linear gradient - Vertical
174 BPoint from(275, 10);
175 BPoint to(275, 138);
176 BGradientLinear l(from, to);
177 l.AddColor((rgb_color){ 255, 0, 0, 255 }, 100.0);
178 l.AddColor((rgb_color){ 255, 255, 0, 255 }, 156.0);
179 view->FillRect(BRect(275, 10, 2*265, 265), l);
183 // Linear gradient - Horizontal
184 BPoint from(10, 10);
185 BPoint to(138, 10);
186 BGradientLinear l(from, to);
187 l.AddColor((rgb_color){ 255, 0, 0, 255 }, 100.0);
188 l.AddColor((rgb_color){ 255, 255, 0, 255 }, 156.0);
189 view->FillRect(BRect(10, 10, 265, 265), l);
195 // #pragma mark -
199 main(int argc, char** argv)
201 BApplication app(kAppSignature);
203 TestWindow* window = new TestWindow("Gradient tests");
205 window->AddTest(new RadialGradientTest());
206 window->AddTest(new AlphaGradientTest());
207 window->AddTest(new OutOfBoundsGradientTest());
209 window->SetToTest(0);
210 window->Show();
212 app.Run();
213 return 0;