Disable flaky test ExtensionActionContextMenuTest.RunInspectPopup
[chromium-blink-merge.git] / ui / gfx / video_decode_acceleration_support_mac_unittest.mm
blob59fe7e5bca2c6c6f30511def5c60ecfb9d6d8e4e
1 // Copyright (c) 2012 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 #include "ui/gfx/video_decode_acceleration_support_mac.h"
7 #import "base/bind.h"
8 #include "base/location.h"
9 #import "base/mac/foundation_util.h"
10 #include "base/mac/scoped_nsautorelease_pool.h"
11 #include "base/message_loop.h"
12 #import "base/sys_info.h"
13 #import "base/threading/platform_thread.h"
14 #import "base/threading/thread.h"
15 #import "ui/base/test/ui_cocoa_test_helper.h"
17 namespace {
19 // Aribtrary values used to test the callback.
20 const int kCallbackFrameID = 10;
21 const int kCallbackExpectedStatus = 7;
23 // Sample movie data to create the decoder.
24 const int kSampleWidth = 1280;
25 const int kSampleHeight = 720;
26 const uint8_t kSampleAVCData[] =  {
27   0x1, 0x64, 0x0, 0x1f, 0xff, 0xe1, 0x0, 0x1a,
28   0x67, 0x64, 0x0, 0x1f, 0xac, 0x2c, 0xc5, 0x1,
29   0x40, 0x16, 0xec, 0x4, 0x40, 0x0, 0x0, 0x3,
30   0x0, 0x40, 0x0, 0x0, 0xf, 0x23, 0xc6, 0xc,
31   0x65, 0x80, 0x1, 0x0, 0x5, 0x68, 0xe9, 0x2b,
32   0x2c, 0x8b,
35 // Check to see if the OS we're running on should have
36 // VideoDecodeAcceleration.framework installed.
37 bool OSShouldHaveFramework() {
38   // 10.6.2 and earlier doesn't have the framework.
39   // If we ever drop 10.6 support and clean up IsOSSnowLeopard() calls, this
40   // can be removed too.
41   int32 major, minor, bugfix;
42   base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
43   if (major == 10 && minor == 6 && bugfix <= 2)
44     return false;
46   return true;
49 // This function is provided as a callback for
50 // VideoDecodeAccelerationSupport::Decode. It verifies that callback
51 // arguments are correct and that the callback happened on the correct
52 // thread.
53 void FrameCallbackVerifier(bool* callback_done,
54                            MessageLoop* expected_loop,
55                            CVImageBufferRef image,
56                            int status) {
57   EXPECT_EQ(kCallbackExpectedStatus, status);
58   // Verify that the callback was invoked on the same thread that created the
59   // VDAStatus object.
60   EXPECT_EQ(expected_loop, MessageLoop::current());
61   *callback_done = true;
64 }  // namespace
66 namespace gfx {
68 class VideoDecodeAccelerationSupportTest : public ui::CocoaTest {
71 // Test that creating VideoDecodeAccelerationSupport works on hardware that
72 // supports it.
73 // http://crbug.com/103912
74 TEST_F(VideoDecodeAccelerationSupportTest, DISABLED_Create) {
75   scoped_refptr<gfx::VideoDecodeAccelerationSupport> vda(
76       new gfx::VideoDecodeAccelerationSupport);
77   gfx::VideoDecodeAccelerationSupport::Status status = vda->Create(
78       kSampleWidth, kSampleHeight, kCVPixelFormatType_422YpCbCr8,
79       kSampleAVCData, arraysize(kSampleAVCData));
81   // We should get an error loading the framework on 10.6.2 and earlier.
82   if (!OSShouldHaveFramework()) {
83     EXPECT_EQ(gfx::VideoDecodeAccelerationSupport::LOAD_FRAMEWORK_ERROR,
84               status);
85     return;
86   }
88   // If the hardware is not supported then there's not much we can do.
89   if (status ==
90       gfx::VideoDecodeAccelerationSupport::HARDWARE_NOT_SUPPORTED_ERROR) {
91     return;
92   }
94   EXPECT_EQ(gfx::VideoDecodeAccelerationSupport::SUCCESS, status);
95   EXPECT_EQ(gfx::VideoDecodeAccelerationSupport::SUCCESS,
96             vda->Flush(false));
97   EXPECT_EQ(gfx::VideoDecodeAccelerationSupport::SUCCESS, vda->Destroy());
100 // Test that callback works.
101 TEST_F(VideoDecodeAccelerationSupportTest, Callback) {
102   MessageLoop loop;
103   bool callback_done = false;
105   scoped_refptr<gfx::VideoDecodeAccelerationSupport> vda(
106       new gfx::VideoDecodeAccelerationSupport);
107   vda->frame_ready_callbacks_[kCallbackFrameID] = base::Bind(
108       &FrameCallbackVerifier, &callback_done, &loop);
110   base::Thread thread("calllback");
111   thread.Start();
113   NSDictionary* info = [NSDictionary
114       dictionaryWithObject:[NSNumber numberWithInt:kCallbackFrameID]
115                     forKey:@"frame_id"];
116   thread.message_loop()->PostTask(FROM_HERE, base::Bind(
117       &gfx::VideoDecodeAccelerationSupport::OnFrameReadyCallback,
118       static_cast<void*>(vda.get()),
119       base::mac::NSToCFCast(info), kCallbackExpectedStatus, 0,
120       static_cast<CVImageBufferRef>(NULL)));
121   // Wait for the thread to complete.
122   thread.Stop();
124   // Verify that the callback occured.
125   EXPECT_FALSE(callback_done);
126   loop.RunUntilIdle();
127   EXPECT_TRUE(callback_done);
130 }  // namespace gfx