1 // Copyright 2014 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 "content/browser/compositor/image_transport_factory.h"
7 #include "base/run_loop.h"
8 #include "cc/output/context_provider.h"
9 #include "content/browser/compositor/owned_mailbox.h"
10 #include "content/public/browser/gpu_data_manager.h"
11 #include "content/public/test/content_browser_test.h"
12 #include "gpu/GLES2/gl2extchromium.h"
13 #include "gpu/command_buffer/client/gles2_interface.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "ui/compositor/compositor.h"
20 typedef ContentBrowserTest ImageTransportFactoryBrowserTest
;
22 class MockImageTransportFactoryObserver
: public ImageTransportFactoryObserver
{
24 MOCK_METHOD0(OnLostResources
, void());
27 // This crashes on Mac ASAN: http://crbug.com/335083
28 // Flaky on ChromeOS: crbug.com/394083
29 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
30 #define MAYBE_TestLostContext DISABLED_TestLostContext
32 #define MAYBE_TestLostContext TestLostContext
34 // Checks that upon context loss, the observer is called and the created
35 // resources are reset.
36 IN_PROC_BROWSER_TEST_F(ImageTransportFactoryBrowserTest
,
37 MAYBE_TestLostContext
) {
38 // This test doesn't make sense in software compositing mode.
39 if (!GpuDataManager::GetInstance()->CanUseGpuBrowserCompositor())
42 ImageTransportFactory
* factory
= ImageTransportFactory::GetInstance();
44 scoped_refptr
<OwnedMailbox
> mailbox
=
45 new OwnedMailbox(factory
->GetGLHelper());
46 EXPECT_FALSE(mailbox
->mailbox().IsZero());
48 MockImageTransportFactoryObserver observer
;
49 factory
->AddObserver(&observer
);
51 base::RunLoop run_loop
;
52 EXPECT_CALL(observer
, OnLostResources())
53 .WillOnce(testing::InvokeWithoutArgs(&run_loop
, &base::RunLoop::Quit
));
55 ui::ContextFactory
* context_factory
= factory
->GetContextFactory();
56 gpu::gles2::GLES2Interface
* gl
=
57 context_factory
->SharedMainThreadContextProvider()->ContextGL();
58 gl
->LoseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB
,
59 GL_INNOCENT_CONTEXT_RESET_ARB
);
61 // We have to flush to make sure that the client side gets a chance to notice
62 // the context is gone.
66 EXPECT_TRUE(mailbox
->mailbox().IsZero());
68 factory
->RemoveObserver(&observer
);
71 class ImageTransportFactoryTearDownBrowserTest
: public ContentBrowserTest
{
73 ImageTransportFactoryTearDownBrowserTest() {}
75 void TearDown() override
{
77 EXPECT_TRUE(mailbox_
->mailbox().IsZero());
78 ContentBrowserTest::TearDown();
82 scoped_refptr
<OwnedMailbox
> mailbox_
;
85 // This crashes on Mac. ImageTransportFactory is NULL unless
86 // --enable-delegated-renderer is passed, and after that, we'd need to spawn a
87 // renderer and get a frame before we create a browser compositor, necessary for
88 // the GLHelper to not be NULL.
89 // http://crbug.com/335083
90 #if defined(OS_MACOSX)
91 #define MAYBE_LoseOnTearDown DISABLED_LoseOnTearDown
93 #define MAYBE_LoseOnTearDown LoseOnTearDown
95 // Checks that upon destruction of the ImageTransportFactory, the observer is
96 // called and the created resources are reset.
97 IN_PROC_BROWSER_TEST_F(ImageTransportFactoryTearDownBrowserTest
,
98 MAYBE_LoseOnTearDown
) {
99 // This test doesn't make sense in software compositing mode.
100 if (!GpuDataManager::GetInstance()->CanUseGpuBrowserCompositor())
102 ImageTransportFactory
* factory
= ImageTransportFactory::GetInstance();
103 GLHelper
* helper
= factory
->GetGLHelper();
105 mailbox_
= new OwnedMailbox(helper
);
106 EXPECT_FALSE(mailbox_
->mailbox().IsZero());
109 } // anonymous namespace
110 } // namespace content