1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <array>
#include "base/command_line.h"
#include "base/path_service.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/api/desktop_capture/desktop_capture_api.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/media/webrtc/fake_desktop_media_picker_factory.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/common/switches.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_capture_types.h"
using content::DesktopMediaID;
using content::WebContentsMediaCaptureId;
namespace extensions {
namespace {
// TODO(crbug.com/805145): Uncomment this when test is re-enabled.
#if 0
DesktopMediaID MakeFakeWebContentsMediaId(bool audio_share) {
DesktopMediaID media_id(DesktopMediaID::TYPE_WEB_CONTENTS,
DesktopMediaID::kNullId,
WebContentsMediaCaptureId(DesktopMediaID::kFakeId,
DesktopMediaID::kFakeId));
media_id.audio_share = audio_share;
return media_id;
}
#endif
class DesktopCaptureApiTest : public ExtensionApiTest {
public:
DesktopCaptureApiTest() {
DesktopCaptureChooseDesktopMediaFunction::
SetPickerFactoryForTests(&picker_factory_);
}
~DesktopCaptureApiTest() override {
DesktopCaptureChooseDesktopMediaFunction::
SetPickerFactoryForTests(NULL);
}
void SetUpOnMainThread() override {
ExtensionApiTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
}
protected:
GURL GetURLForPath(const std::string& host, const std::string& path) {
std::string port = base::UintToString(embedded_test_server()->port());
GURL::Replacements replacements;
replacements.SetHostStr(host);
replacements.SetPortStr(port);
return embedded_test_server()->GetURL(path).ReplaceComponents(replacements);
}
FakeDesktopMediaPickerFactory picker_factory_;
};
} // namespace
// Flaky on Windows: http://crbug.com/301887
// Fails on Chrome OS: http://crbug.com/718512
// Flaky on macOS: http://crbug.com/804897
#if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
#define MAYBE_ChooseDesktopMedia DISABLED_ChooseDesktopMedia
#else
#define MAYBE_ChooseDesktopMedia ChooseDesktopMedia
#endif
IN_PROC_BROWSER_TEST_F(DesktopCaptureApiTest, MAYBE_ChooseDesktopMedia) {
// Each element in the following array corresponds to one test in
// chrome/test/data/extensions/api_test/desktop_capture/test.js .
FakeDesktopMediaPickerFactory::TestFlags test_flags[] = {
// pickerUiCanceled()
{true, true, false, false, DesktopMediaID()},
// chooseMedia()
{true, true, false, false,
DesktopMediaID(DesktopMediaID::TYPE_SCREEN, DesktopMediaID::kNullId)},
// screensOnly()
{true, false, false, false, DesktopMediaID()},
// WindowsOnly()
{false, true, false, false, DesktopMediaID()},
// tabOnly()
{false, false, true, false, DesktopMediaID()},
// audioShareNoApproval()
{true, true, true, true,
DesktopMediaID(DesktopMediaID::TYPE_WEB_CONTENTS, 123, false)},
// audioShareApproval()
{true, true, true, true,
DesktopMediaID(DesktopMediaID::TYPE_WEB_CONTENTS, 123, true)},
// chooseMediaAndGetStream()
{true, true, false, false,
DesktopMediaID(DesktopMediaID::TYPE_SCREEN, webrtc::kFullDesktopScreenId)},
// chooseMediaAndTryGetStreamWithInvalidId()
{true, true, false, false,
DesktopMediaID(DesktopMediaID::TYPE_SCREEN, webrtc::kFullDesktopScreenId)},
// cancelDialog()
{true, true, false, false, DesktopMediaID(), true},
// TODO(crbug.com/805145): Test fails; invalid device IDs being generated.
#if 0
// tabShareWithAudioGetStream()
{false, false, true, true, MakeFakeWebContentsMediaId(true)},
#endif
// windowShareWithAudioGetStream()
{false, true, false, true,
DesktopMediaID(DesktopMediaID::TYPE_WINDOW, DesktopMediaID::kFakeId,
true)},
// screenShareWithAudioGetStream()
{true, false, false, true,
DesktopMediaID(DesktopMediaID::TYPE_SCREEN, webrtc::kFullDesktopScreenId,
true)},
// TODO(crbug.com/805145): Test fails; invalid device IDs being generated.
#if 0
// tabShareWithoutAudioGetStream()
{false, false, true, true, MakeFakeWebContentsMediaId(false)},
#endif
// windowShareWithoutAudioGetStream()
{false, true, false, true,
DesktopMediaID(DesktopMediaID::TYPE_WINDOW, DesktopMediaID::kFakeId)},
// screenShareWithoutAudioGetStream()
{true, false, false, true,
DesktopMediaID(DesktopMediaID::TYPE_SCREEN, webrtc::kFullDesktopScreenId)},
};
picker_factory_.SetTestFlags(test_flags, base::size(test_flags));
ASSERT_TRUE(RunExtensionTest("desktop_capture")) << message_;
}
// Test is flaky http://crbug.com/301887.
IN_PROC_BROWSER_TEST_F(DesktopCaptureApiTest, DISABLED_Delegation) {
// Initialize test server.
base::FilePath test_data;
EXPECT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &test_data));
embedded_test_server()->ServeFilesFromDirectory(test_data.AppendASCII(
"extensions/api_test/desktop_capture_delegate"));
ASSERT_TRUE(embedded_test_server()->Start());
// Load extension.
base::FilePath extension_path =
test_data_dir_.AppendASCII("desktop_capture_delegate");
const Extension* extension = LoadExtensionWithFlags(
extension_path, ExtensionBrowserTest::kFlagNone);
ASSERT_TRUE(extension);
ui_test_utils::NavigateToURL(
browser(), GetURLForPath("example.com", "/example.com.html"));
FakeDesktopMediaPickerFactory::TestFlags test_flags[] = {
{true, true, false, false,
DesktopMediaID(DesktopMediaID::TYPE_SCREEN, DesktopMediaID::kNullId)},
{true, true, false, false,
DesktopMediaID(DesktopMediaID::TYPE_SCREEN, DesktopMediaID::kNullId)},
{true, true, false, false,
DesktopMediaID(DesktopMediaID::TYPE_SCREEN, DesktopMediaID::kNullId),
true},
};
picker_factory_.SetTestFlags(test_flags, base::size(test_flags));
bool result;
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
web_contents, "getStream()", &result));
EXPECT_TRUE(result);
ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
web_contents, "getStreamWithInvalidId()", &result));
EXPECT_TRUE(result);
// Verify that the picker is closed once the tab is closed.
content::WebContentsDestroyedWatcher destroyed_watcher(web_contents);
ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
web_contents, "openPickerDialogAndReturn()", &result));
EXPECT_TRUE(result);
EXPECT_TRUE(test_flags[2].picker_created);
EXPECT_FALSE(test_flags[2].picker_deleted);
web_contents->Close();
destroyed_watcher.Wait();
EXPECT_TRUE(test_flags[2].picker_deleted);
}
} // namespace extensions
|