#include "content/test/ppapi/ppapi_test.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/ppapi_test_utils.h"
#include "content/shell/browser/shell.h"
#include "media/base/media_switches.h"
#include "net/base/filename_util.h"
#include "ppapi/shared_impl/ppapi_switches.h"
#include "third_party/blink/public/common/switches.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chromeos/ash/components/audio/cras_audio_handler.h"
#include "chromeos/ash/components/dbus/audio/cras_audio_client.h"
#endif
namespace content {
PPAPITestMessageHandler::PPAPITestMessageHandler() {
}
TestMessageHandler::MessageResponse PPAPITestMessageHandler::HandleMessage(
const std::string& json) {
std::string trimmed;
base::TrimString(json, "\"", &trimmed);
if (trimmed == "...")
return CONTINUE;
message_ = trimmed;
return DONE;
}
void PPAPITestMessageHandler::Reset() {
TestMessageHandler::Reset();
message_.clear();
}
PPAPITestBase::PPAPITestBase() { }
void PPAPITestBase::SetUpCommandLine(base::CommandLine* command_line) {
command_line->AppendSwitch(switches::kEnablePepperTesting);
command_line->AppendSwitch(switches::kDisableSmoothScrolling);
command_line->AppendSwitchASCII(blink::switches::kJavaScriptFlags,
"--expose_gc");
command_line->AppendSwitch(switches::kUseFakeUIForMediaStream);
command_line->AppendSwitchASCII(
switches::kAutoplayPolicy,
switches::autoplay::kNoUserGestureRequiredPolicy);
command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
command_line->AppendSwitchASCII(blink::switches::kBlinkSettings,
"allowNonEmptyNavigatorPlugins=true");
}
GURL PPAPITestBase::GetTestFileUrl(const std::string& test_case) {
base::FilePath test_path;
{
base::ScopedAllowBlockingForTesting allow_blocking;
EXPECT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &test_path));
test_path = test_path.Append(FILE_PATH_LITERAL("ppapi"));
test_path = test_path.Append(FILE_PATH_LITERAL("tests"));
test_path = test_path.Append(FILE_PATH_LITERAL("test_case.html"));
EXPECT_TRUE(base::PathExists(test_path));
}
GURL test_url = net::FilePathToFileURL(test_path);
GURL::Replacements replacements;
std::string query = BuildQuery(std::string(), test_case);
replacements.SetQueryStr(query);
return test_url.ReplaceComponents(replacements);
}
void PPAPITestBase::RunTest(const std::string& test_case) {
GURL url = GetTestFileUrl(test_case);
RunTestURL(url);
}
void PPAPITestBase::RunTestAndReload(const std::string& test_case) {
GURL url = GetTestFileUrl(test_case);
RunTestURL(url);
RunTestURL(url);
}
void PPAPITestBase::RunTestURL(const GURL& test_url) {
PPAPITestMessageHandler handler;
JavascriptTestObserver observer(shell()->web_contents(), &handler);
shell()->LoadURL(test_url);
shell()->web_contents()->Focus();
ASSERT_TRUE(observer.Run()) << handler.error_message();
EXPECT_STREQ("PASS", handler.message().c_str());
}
PPAPITest::PPAPITest() : in_process_(true) {
}
void PPAPITest::SetUpCommandLine(base::CommandLine* command_line) {
PPAPITestBase::SetUpCommandLine(command_line);
ASSERT_TRUE(ppapi::RegisterTestPlugin(command_line));
if (in_process_)
command_line->AppendSwitch(switches::kPpapiInProcess);
}
std::string PPAPITest::BuildQuery(const std::string& base,
const std::string& test_case){
return base::StringPrintf("%stestcase=%s", base.c_str(), test_case.c_str());
}
OutOfProcessPPAPITest::OutOfProcessPPAPITest() {
in_process_ = false;
}
void OutOfProcessPPAPITest::SetUp() {
#if BUILDFLAG(IS_CHROMEOS_ASH)
ash::CrasAudioClient::InitializeFake();
ash::CrasAudioHandler::InitializeForTesting();
#endif
ContentBrowserTest::SetUp();
}
void OutOfProcessPPAPITest::TearDown() {
ContentBrowserTest::TearDown();
#if BUILDFLAG(IS_CHROMEOS_ASH)
ash::CrasAudioHandler::Shutdown();
ash::CrasAudioClient::Shutdown();
#endif
}
}