OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Note: This test depends on |GlHelper|, which depends on an implementation of | |
6 // mojo:native_viewport_service and mojo:surfaces_service. | |
7 | |
8 #include "apps/moterm/gl_helper.h" | |
9 | |
10 #include <GLES2/gl2.h> | |
11 | |
12 #include "base/logging.h" | |
13 #include "base/macros.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/message_loop/message_loop.h" | |
16 #include "mojo/public/cpp/application/application_impl.h" | |
17 #include "mojo/public/cpp/application/application_test_base.h" | |
18 #include "mojo/services/surfaces/interfaces/surface_id.mojom.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 namespace { | |
22 | |
23 class GlHelperTest : public mojo::test::ApplicationTestBase, | |
24 public GlHelper::Client { | |
25 public: | |
26 GlHelperTest() | |
27 : surface_id_changed_call_count_(0), | |
28 on_frame_displayed_call_count_(0), | |
29 last_frame_id_(0) {} | |
30 ~GlHelperTest() override {} | |
31 | |
32 void SetUp() override { | |
33 mojo::test::ApplicationTestBase::SetUp(); | |
34 | |
35 initial_size_.width = 100; | |
36 initial_size_.height = 100; | |
37 gl_helper_.reset(new GlHelper(this, application_impl()->shell(), GL_RGBA, | |
38 false, initial_size_)); | |
39 } | |
40 | |
41 protected: | |
42 const mojo::Size& initial_size() const { return initial_size_; } | |
43 GlHelper* gl_helper() { return gl_helper_.get(); } | |
44 | |
45 unsigned surface_id_changed_call_count() const { | |
46 return surface_id_changed_call_count_; | |
47 } | |
48 const mojo::SurfaceId* last_surface_id() const { | |
49 return last_surface_id_.get(); | |
50 } | |
51 unsigned on_frame_displayed_call_count() const { | |
52 return on_frame_displayed_call_count_; | |
53 } | |
54 uint32_t last_frame_id() const { return last_frame_id_; } | |
55 | |
56 private: | |
57 // |GlHelper::Client|: | |
58 void OnSurfaceIdChanged(mojo::SurfaceIdPtr surface_id) override { | |
59 surface_id_changed_call_count_++; | |
60 last_surface_id_ = surface_id.Pass(); | |
61 base::MessageLoop::current()->Quit(); | |
62 } | |
63 | |
64 void OnContextLost() override { | |
65 // We don't currently test this, but we'll get it on teardown. | |
66 // TODO(vtl): We probably should figure out how to test this explicitly. | |
67 } | |
68 | |
69 void OnFrameDisplayed(uint32_t frame_id) override { | |
70 on_frame_displayed_call_count_++; | |
71 last_frame_id_ = frame_id; | |
72 base::MessageLoop::current()->Quit(); | |
73 } | |
74 | |
75 mojo::Size initial_size_; | |
76 scoped_ptr<GlHelper> gl_helper_; | |
77 | |
78 unsigned surface_id_changed_call_count_; | |
79 mojo::SurfaceIdPtr last_surface_id_; | |
80 unsigned on_frame_displayed_call_count_; | |
81 uint32_t last_frame_id_; | |
82 | |
83 DISALLOW_COPY_AND_ASSIGN(GlHelperTest); | |
84 }; | |
85 | |
86 TEST_F(GlHelperTest, Basic) { | |
87 gl_helper()->StartFrame(); | |
88 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
89 | |
90 scoped_ptr<unsigned char[]> bitmap( | |
91 new unsigned char[initial_size().width * initial_size().height * 4]); | |
92 memset(bitmap.get(), 123, initial_size().width * initial_size().height * 4); | |
93 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, initial_size().width, | |
94 initial_size().height, GL_RGBA, GL_UNSIGNED_BYTE, | |
95 bitmap.get()); | |
96 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
97 | |
98 uint32_t frame_id = gl_helper()->EndFrame(); | |
99 | |
100 // We should get |OnSurfaceIdChanged()| and then |OnFrameDisplayed()|. | |
101 while (surface_id_changed_call_count() < 1u || | |
102 on_frame_displayed_call_count() < 1u) | |
103 base::MessageLoop::current()->Run(); | |
104 | |
105 EXPECT_EQ(1u, surface_id_changed_call_count()); | |
106 EXPECT_TRUE(last_surface_id()); | |
107 EXPECT_NE(last_surface_id()->local, 0u); | |
108 EXPECT_NE(last_surface_id()->id_namespace, 0u); | |
109 EXPECT_EQ(1u, on_frame_displayed_call_count()); | |
110 EXPECT_EQ(frame_id, last_frame_id()); | |
111 } | |
112 | |
113 TEST_F(GlHelperTest, SetSurfaceSizeAndMakeCurrent) { | |
114 // It only creates surfaces lazily, so we have to start/end a frame. | |
115 gl_helper()->StartFrame(); | |
116 GLuint frame_id = gl_helper()->EndFrame(); | |
117 while (surface_id_changed_call_count() < 1u || | |
118 on_frame_displayed_call_count() < 1u) | |
119 base::MessageLoop::current()->Run(); | |
120 EXPECT_EQ(1u, surface_id_changed_call_count()); | |
121 mojo::SurfaceId surface_id = *last_surface_id(); | |
122 EXPECT_EQ(1u, on_frame_displayed_call_count()); | |
123 EXPECT_EQ(frame_id, last_frame_id()); | |
124 | |
125 // Set a new surface size. | |
126 mojo::Size new_size; | |
127 new_size.width = initial_size().width * 2; | |
128 new_size.height = initial_size().height + 50; | |
129 gl_helper()->SetSurfaceSize(new_size); | |
130 | |
131 gl_helper()->StartFrame(); | |
132 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
133 | |
134 GLuint frame_texture = gl_helper()->GetFrameTexture(); | |
135 EXPECT_NE(frame_texture, 0u); | |
136 | |
137 // Binding it should be OK. | |
138 glBindTexture(GL_TEXTURE_2D, frame_texture); | |
139 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
140 | |
141 scoped_ptr<unsigned char[]> bitmap( | |
142 new unsigned char[new_size.width * new_size.height * 4]); | |
143 memset(bitmap.get(), 123, new_size.width * new_size.height * 4); | |
144 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, new_size.width, new_size.height, | |
145 GL_RGBA, GL_UNSIGNED_BYTE, bitmap.get()); | |
146 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
147 | |
148 frame_id = gl_helper()->EndFrame(); | |
149 EXPECT_NE(frame_id, last_frame_id()); | |
150 | |
151 // We should get another |OnSurfaceIdChanged()| and another | |
152 // |OnFrameDisplayed()|. | |
153 while (surface_id_changed_call_count() < 2u || | |
154 on_frame_displayed_call_count() < 2u) | |
155 base::MessageLoop::current()->Run(); | |
156 EXPECT_EQ(2u, surface_id_changed_call_count()); | |
157 EXPECT_TRUE(last_surface_id()); | |
158 // Only the local part of the surface ID should have changed. | |
159 EXPECT_NE(last_surface_id()->local, surface_id.local); | |
160 EXPECT_EQ(surface_id.id_namespace, last_surface_id()->id_namespace); | |
161 EXPECT_EQ(2u, on_frame_displayed_call_count()); | |
162 EXPECT_EQ(frame_id, last_frame_id()); | |
163 | |
164 // An out-of-the-blue |MakeCurrent()| should work. | |
165 gl_helper()->MakeCurrent(); | |
166 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
167 } | |
168 | |
169 } // namespace | |
OLD | NEW |