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.
4
5#include "base/basictypes.h"
6#include "base/file_path.h"
7#include "base/string_util.h"
8#include "base/test/test_timeouts.h"
9#include "base/threading/platform_thread.h"
10#include "chrome/common/chrome_switches.h"
11#include "chrome/test/test_launcher_utils.h"
12#include "chrome/test/ui/ui_layout_test.h"
13#include "chrome/test/ui/ui_test.h"
14#include "net/base/net_util.h"
15#include "ui/gfx/gl/gl_implementation.h"
16
17class MediaTest : public UITest {
18 protected:
19  virtual void SetUp() {
20    EXPECT_TRUE(test_launcher_utils::OverrideGLImplementation(
21        &launch_arguments_,
22        gfx::kGLImplementationOSMesaName));
23
24#if defined(OS_MACOSX)
25    // Accelerated compositing does not work with OSMesa. AcceleratedSurface
26    // assumes GL contexts are native.
27    launch_arguments_.AppendSwitch(switches::kDisableAcceleratedCompositing);
28#endif
29
30    UITest::SetUp();
31  }
32
33  void PlayMedia(const char* tag, const char* media_file) {
34    FilePath test_file(test_data_directory_);
35    test_file = test_file.AppendASCII("media/player.html");
36
37    GURL player_gurl = net::FilePathToFileURL(test_file);
38    std::string url = StringPrintf("%s?%s=%s",
39                                   player_gurl.spec().c_str(),
40                                   tag,
41                                   media_file);
42
43    NavigateToURL(GURL(url));
44
45    // Allow the media file to be loaded.
46    const std::wstring kPlaying = L"PLAYING";
47    const std::wstring kFailed = L"FAILED";
48    const std::wstring kError = L"ERROR";
49    for (int i = 0; i < 10; ++i) {
50      base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms());
51      const std::wstring& title = GetActiveTabTitle();
52      if (title == kPlaying || title == kFailed ||
53          StartsWith(title, kError, true))
54        break;
55    }
56
57    EXPECT_EQ(kPlaying, GetActiveTabTitle());
58  }
59
60  void PlayAudio(const char* url) {
61    PlayMedia("audio", url);
62  }
63
64  void PlayVideo(const char* url) {
65    PlayMedia("video", url);
66  }
67};
68
69TEST_F(MediaTest, VideoBearTheora) {
70  PlayVideo("bear.ogv");
71}
72
73TEST_F(MediaTest, VideoBearSilentTheora) {
74  PlayVideo("bear_silent.ogv");
75}
76
77TEST_F(MediaTest, VideoBearWebm) {
78  PlayVideo("bear.webm");
79}
80
81TEST_F(MediaTest, VideoBearSilentWebm) {
82  PlayVideo("bear_silent.webm");
83}
84
85#if defined(GOOGLE_CHROME_BUILD) || defined(USE_PROPRIETARY_CODECS)
86TEST_F(MediaTest, VideoBearMp4) {
87  PlayVideo("bear.mp4");
88}
89
90TEST_F(MediaTest, VideoBearSilentMp4) {
91  PlayVideo("bear_silent.mp4");
92}
93#endif
94
95TEST_F(MediaTest, VideoBearWav) {
96  PlayVideo("bear.wav");
97}
98
99TEST_F(UILayoutTest, MediaUILayoutTest) {
100  static const char* kResources[] = {
101    "content",
102    "media-file.js",
103    "media-fullscreen.js",
104    "video-paint-test.js",
105    "video-played.js",
106    "video-test.js",
107  };
108
109  static const char* kMediaTests[] = {
110    "video-autoplay.html",
111    // "video-loop.html", disabled due to 52887.
112    "video-no-autoplay.html",
113    // TODO(sergeyu): Add more tests here.
114  };
115
116  FilePath test_dir;
117  FilePath media_test_dir;
118  media_test_dir = media_test_dir.AppendASCII("media");
119  InitializeForLayoutTest(test_dir, media_test_dir, kNoHttpPort);
120
121  // Copy resources first.
122  for (size_t i = 0; i < arraysize(kResources); ++i)
123    AddResourceForLayoutTest(
124        test_dir, media_test_dir.AppendASCII(kResources[i]));
125
126  for (size_t i = 0; i < arraysize(kMediaTests); ++i)
127    RunLayoutTest(kMediaTests[i], kNoHttpPort);
128}
129