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.
4
5#include "content/browser/compositor/image_transport_factory.h"
6
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"
16
17namespace content {
18namespace {
19
20typedef ContentBrowserTest ImageTransportFactoryBrowserTest;
21
22class MockImageTransportFactoryObserver : public ImageTransportFactoryObserver {
23 public:
24  MOCK_METHOD0(OnLostResources, void());
25};
26
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
31#else
32#define MAYBE_TestLostContext TestLostContext
33#endif
34// Checks that upon context loss, the observer is called and the created
35// resources are reset.
36IN_PROC_BROWSER_TEST_F(ImageTransportFactoryBrowserTest,
37                       MAYBE_TestLostContext) {
38  // This test doesn't make sense in software compositing mode.
39  if (!GpuDataManager::GetInstance()->CanUseGpuBrowserCompositor())
40    return;
41
42  ImageTransportFactory* factory = ImageTransportFactory::GetInstance();
43
44  scoped_refptr<OwnedMailbox> mailbox =
45      new OwnedMailbox(factory->GetGLHelper());
46  EXPECT_FALSE(mailbox->mailbox().IsZero());
47
48  MockImageTransportFactoryObserver observer;
49  factory->AddObserver(&observer);
50
51  base::RunLoop run_loop;
52  EXPECT_CALL(observer, OnLostResources())
53      .WillOnce(testing::InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
54
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);
60
61  // We have to flush to make sure that the client side gets a chance to notice
62  // the context is gone.
63  gl->Flush();
64
65  run_loop.Run();
66  EXPECT_TRUE(mailbox->mailbox().IsZero());
67
68  factory->RemoveObserver(&observer);
69}
70
71class ImageTransportFactoryTearDownBrowserTest : public ContentBrowserTest {
72 public:
73  ImageTransportFactoryTearDownBrowserTest() {}
74
75  virtual void TearDown() {
76    if (mailbox_.get())
77      EXPECT_TRUE(mailbox_->mailbox().IsZero());
78    ContentBrowserTest::TearDown();
79  }
80
81 protected:
82  scoped_refptr<OwnedMailbox> mailbox_;
83};
84
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
92#else
93#define MAYBE_LoseOnTearDown LoseOnTearDown
94#endif
95// Checks that upon destruction of the ImageTransportFactory, the observer is
96// called and the created resources are reset.
97IN_PROC_BROWSER_TEST_F(ImageTransportFactoryTearDownBrowserTest,
98                       MAYBE_LoseOnTearDown) {
99  // This test doesn't make sense in software compositing mode.
100  if (!GpuDataManager::GetInstance()->CanUseGpuBrowserCompositor())
101    return;
102  ImageTransportFactory* factory = ImageTransportFactory::GetInstance();
103  GLHelper* helper = factory->GetGLHelper();
104  ASSERT_TRUE(helper);
105  mailbox_ = new OwnedMailbox(helper);
106  EXPECT_FALSE(mailbox_->mailbox().IsZero());
107}
108
109}  // anonymous namespace
110}  // namespace content
111