Fix crash on app list start page keyboard navigation with <4 apps.
[chromium-blink-merge.git] / gpu / command_buffer / common / gles2_cmd_format_test.cc
blobea683a668eb5fa94f07423a712c71cc37724e424
1 // Copyright (c) 2011 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.
5 // This file contains unit tests for gles2 commmands
7 #include <limits>
9 #include "base/bind.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "base/threading/thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "gpu/command_buffer/common/gles2_cmd_format.h"
15 namespace gpu {
16 namespace gles2 {
18 class GLES2FormatTest : public testing::Test {
19 protected:
20 static const unsigned char kInitialValue = 0xBD;
22 void SetUp() override { memset(buffer_, kInitialValue, sizeof(buffer_)); }
24 void TearDown() override {}
26 template <typename T>
27 T* GetBufferAs() {
28 return static_cast<T*>(static_cast<void*>(&buffer_));
31 void CheckBytesWritten(
32 const void* end, size_t expected_size, size_t written_size) {
33 size_t actual_size = static_cast<const unsigned char*>(end) -
34 GetBufferAs<const unsigned char>();
35 EXPECT_LT(actual_size, sizeof(buffer_));
36 EXPECT_GT(actual_size, 0u);
37 EXPECT_EQ(expected_size, actual_size);
38 EXPECT_EQ(kInitialValue, buffer_[written_size]);
39 EXPECT_NE(kInitialValue, buffer_[written_size - 1]);
42 void CheckBytesWrittenMatchesExpectedSize(
43 const void* end, size_t expected_size) {
44 CheckBytesWritten(end, expected_size, expected_size);
47 private:
48 unsigned char buffer_[1024];
51 void SignalCompletion(uint32* assigned_async_token_ptr,
52 uint32 async_token,
53 AsyncUploadSync* sync) {
54 EXPECT_EQ(async_token, *assigned_async_token_ptr);
55 sync->SetAsyncUploadToken(async_token);
58 TEST(GLES2FormatAsyncUploadSyncTest, AsyncUploadSync) {
59 const size_t kSize = 10;
60 const size_t kCount = 1000;
62 base::Thread thread("GLES2FormatUploadSyncTest - Fake Upload Thread");
63 thread.Start();
65 // Run the same test 50 times so we retest the wrap as well.
66 for (size_t test_run = 0; test_run < 50; ++test_run) {
67 AsyncUploadSync sync;
68 sync.Reset();
70 uint32 buffer_tokens[kSize];
71 memset(buffer_tokens, 0, sizeof(buffer_tokens));
73 // Start with a token large enough so that we'll wrap.
74 uint32 async_token = std::numeric_limits<uint32>::max() - kCount / 2;
76 // Set initial async token.
77 sync.SetAsyncUploadToken(async_token);
79 for (size_t i = 0; i < kCount; ++i) {
80 size_t buffer = i % kSize;
82 // Loop until previous async token has passed if any was set.
83 while (buffer_tokens[buffer] &&
84 !sync.HasAsyncUploadTokenPassed(buffer_tokens[buffer]))
85 base::PlatformThread::YieldCurrentThread();
87 // Next token, skip 0.
88 async_token++;
89 if (async_token == 0)
90 async_token++;
92 // Set the buffer's associated token.
93 buffer_tokens[buffer] = async_token;
95 // Set the async upload token on the fake upload thread and assert that
96 // the associated buffer still has the given token.
97 thread.message_loop()->PostTask(FROM_HERE,
98 base::Bind(&SignalCompletion,
99 &buffer_tokens[buffer],
100 async_token,
101 &sync));
104 // Flush the thread message loop before starting again.
105 base::WaitableEvent waitable(false, false);
106 thread.message_loop()->PostTask(FROM_HERE,
107 base::Bind(&base::WaitableEvent::Signal,
108 base::Unretained(&waitable)));
109 waitable.Wait();
113 // GCC requires these declarations, but MSVC requires they not be present
114 #ifndef _MSC_VER
115 const unsigned char GLES2FormatTest::kInitialValue;
116 #endif
118 #include "gpu/command_buffer/common/gles2_cmd_format_test_autogen.h"
120 } // namespace gles2
121 } // namespace gpu