This source file includes following definitions.
- frame_
- Initialize
- Start
- Stop
- Redirect
- SendResponse
- FailLoad
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "content/renderer/media/android/media_info_loader.h"
#include "content/test/mock_webframeclient.h"
#include "content/test/mock_weburlloader.h"
#include "third_party/WebKit/public/platform/WebMediaPlayer.h"
#include "third_party/WebKit/public/platform/WebURLError.h"
#include "third_party/WebKit/public/platform/WebURLRequest.h"
#include "third_party/WebKit/public/platform/WebURLResponse.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebView.h"
using ::testing::_;
using ::testing::InSequence;
using ::testing::NiceMock;
using blink::WebFrame;
using blink::WebString;
using blink::WebURLError;
using blink::WebURLResponse;
using blink::WebView;
namespace content {
static const char* kHttpUrl = "http://test";
static const char kHttpRedirectToSameDomainUrl1[] = "http://test/ing";
static const char kHttpRedirectToSameDomainUrl2[] = "http://test/ing2";
static const char kHttpRedirectToDifferentDomainUrl1[] = "http://test2";
static const char kHttpDataUrl[] = "data:audio/wav;base64,UklGRhwMAABXQVZFZm10";
static const int kHttpOK = 200;
static const int kHttpNotFound = 404;
class MediaInfoLoaderTest : public testing::Test {
public:
MediaInfoLoaderTest()
: view_(WebView::create(NULL)), frame_(WebFrame::create(&client_)) {
view_->setMainFrame(frame_);
}
virtual ~MediaInfoLoaderTest() {
view_->close();
frame_->close();
}
void Initialize(
const char* url,
blink::WebMediaPlayer::CORSMode cors_mode) {
gurl_ = GURL(url);
loader_.reset(new MediaInfoLoader(
gurl_, cors_mode,
base::Bind(&MediaInfoLoaderTest::ReadyCallback,
base::Unretained(this))));
url_loader_ = new NiceMock<MockWebURLLoader>();
loader_->test_loader_ = scoped_ptr<blink::WebURLLoader>(url_loader_);
}
void Start() {
InSequence s;
EXPECT_CALL(*url_loader_, loadAsynchronously(_, _));
loader_->Start(view_->mainFrame());
}
void Stop() {
InSequence s;
EXPECT_CALL(*url_loader_, cancel());
loader_.reset();
}
void Redirect(const char* url) {
GURL redirect_url(url);
blink::WebURLRequest new_request(redirect_url);
blink::WebURLResponse redirect_response(gurl_);
loader_->willSendRequest(url_loader_, new_request, redirect_response);
base::MessageLoop::current()->RunUntilIdle();
}
void SendResponse(
int http_status, MediaInfoLoader::Status expected_status) {
EXPECT_CALL(*this, ReadyCallback(expected_status));
EXPECT_CALL(*url_loader_, cancel());
WebURLResponse response(gurl_);
response.setHTTPHeaderField(WebString::fromUTF8("Content-Length"),
WebString::fromUTF8("0"));
response.setExpectedContentLength(0);
response.setHTTPStatusCode(http_status);
loader_->didReceiveResponse(url_loader_, response);
}
void FailLoad() {
EXPECT_CALL(*this, ReadyCallback(MediaInfoLoader::kFailed));
loader_->didFail(url_loader_, WebURLError());
}
MOCK_METHOD1(ReadyCallback, void(MediaInfoLoader::Status));
protected:
GURL gurl_;
scoped_ptr<MediaInfoLoader> loader_;
NiceMock<MockWebURLLoader>* url_loader_;
MockWebFrameClient client_;
WebView* view_;
WebFrame* frame_;
base::MessageLoop message_loop_;
private:
DISALLOW_COPY_AND_ASSIGN(MediaInfoLoaderTest);
};
TEST_F(MediaInfoLoaderTest, StartStop) {
Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
Start();
Stop();
}
TEST_F(MediaInfoLoaderTest, LoadFailure) {
Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
Start();
FailLoad();
}
TEST_F(MediaInfoLoaderTest, DataUri) {
Initialize(kHttpDataUrl, blink::WebMediaPlayer::CORSModeUnspecified);
Start();
SendResponse(0, MediaInfoLoader::kOk);
}
TEST_F(MediaInfoLoaderTest, HasSingleOriginNoRedirect) {
Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
Start();
SendResponse(kHttpOK, MediaInfoLoader::kOk);
EXPECT_TRUE(loader_->HasSingleOrigin());
}
TEST_F(MediaInfoLoaderTest, HasSingleOriginSingleRedirect) {
Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
Start();
Redirect(kHttpRedirectToSameDomainUrl1);
SendResponse(kHttpOK, MediaInfoLoader::kOk);
EXPECT_TRUE(loader_->HasSingleOrigin());
}
TEST_F(MediaInfoLoaderTest, HasSingleOriginDoubleRedirect) {
Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
Start();
Redirect(kHttpRedirectToSameDomainUrl1);
Redirect(kHttpRedirectToSameDomainUrl2);
SendResponse(kHttpOK, MediaInfoLoader::kOk);
EXPECT_TRUE(loader_->HasSingleOrigin());
}
TEST_F(MediaInfoLoaderTest, HasSingleOriginDifferentDomain) {
Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
Start();
Redirect(kHttpRedirectToDifferentDomainUrl1);
SendResponse(kHttpOK, MediaInfoLoader::kOk);
EXPECT_FALSE(loader_->HasSingleOrigin());
}
TEST_F(MediaInfoLoaderTest, HasSingleOriginMultipleDomains) {
Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUnspecified);
Start();
Redirect(kHttpRedirectToSameDomainUrl1);
Redirect(kHttpRedirectToDifferentDomainUrl1);
SendResponse(kHttpOK, MediaInfoLoader::kOk);
EXPECT_FALSE(loader_->HasSingleOrigin());
}
TEST_F(MediaInfoLoaderTest, CORSAccessCheckPassed) {
Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUseCredentials);
Start();
SendResponse(kHttpOK, MediaInfoLoader::kOk);
EXPECT_TRUE(loader_->DidPassCORSAccessCheck());
}
TEST_F(MediaInfoLoaderTest, CORSAccessCheckFailed) {
Initialize(kHttpUrl, blink::WebMediaPlayer::CORSModeUseCredentials);
Start();
SendResponse(kHttpNotFound, MediaInfoLoader::kFailed);
EXPECT_FALSE(loader_->DidPassCORSAccessCheck());
}
}