2014-01-31 00:15:55 +01:00
|
|
|
// Copyright (c) 2014 The Chromium Embedded Framework 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 <algorithm>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <string>
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
#include "include/base/cef_callback.h"
|
2014-01-31 00:15:55 +01:00
|
|
|
#include "include/cef_stream.h"
|
2014-07-15 20:10:40 +02:00
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2014-01-31 00:15:55 +01:00
|
|
|
#include "include/wrapper/cef_stream_resource_handler.h"
|
2016-11-18 18:31:21 +01:00
|
|
|
#include "tests/ceftests/routing_test_handler.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
2014-01-31 00:15:55 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char kTestUrl[] = "http://tests-srh/test.html";
|
|
|
|
const size_t kReadBlockSize = 1024U; // 1k.
|
|
|
|
|
|
|
|
// The usual network buffer size is about 32k. Choose a value that's larger.
|
|
|
|
const size_t kReadDesiredSize = 100U * 1024U; // 100k
|
|
|
|
|
|
|
|
class ReadHandler : public CefReadHandler {
|
|
|
|
public:
|
|
|
|
explicit ReadHandler(bool may_block)
|
2017-05-17 11:29:28 +02:00
|
|
|
: may_block_(may_block), offset_(0), expected_result_(0) {}
|
2014-01-31 00:15:55 +01:00
|
|
|
|
|
|
|
void CreateContent() {
|
|
|
|
// To verify that the data transers successfully we're going to make a big
|
|
|
|
// math problem.
|
|
|
|
content_.reserve(kReadDesiredSize + 50U);
|
|
|
|
content_ = "<html><body><script>var myratherlongvariablename=0;";
|
|
|
|
|
|
|
|
while (content_.size() < kReadDesiredSize) {
|
|
|
|
content_ += "myratherlongvariablename=myratherlongvariablename+1;";
|
|
|
|
expected_result_++;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
content_ +=
|
|
|
|
"window.testQuery({request:myratherlongvariablename+''});"
|
|
|
|
"</script></body></html>";
|
2014-01-31 00:15:55 +01:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
int GetExpectedResult() const { return expected_result_; }
|
2014-01-31 00:15:55 +01:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
size_t Read(void* ptr, size_t size, size_t n) override {
|
2014-02-01 00:35:23 +01:00
|
|
|
EXPECT_EQ(1U, size);
|
2014-01-31 00:15:55 +01:00
|
|
|
|
|
|
|
// Read the minimum of requested size, remaining size or kReadBlockSize.
|
|
|
|
const size_t read_bytes =
|
|
|
|
std::min(std::min(size * n, content_.size() - offset_), kReadBlockSize);
|
|
|
|
if (read_bytes > 0) {
|
|
|
|
memcpy(ptr, content_.c_str() + offset_, read_bytes);
|
|
|
|
offset_ += read_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return read_bytes;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
int Seek(int64 offset, int whence) override {
|
2014-01-31 00:15:55 +01:00
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
int64 Tell() override {
|
2014-01-31 00:15:55 +01:00
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
int Eof() override {
|
2014-01-31 00:15:55 +01:00
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool MayBlock() override { return may_block_; }
|
2014-01-31 00:15:55 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
const bool may_block_;
|
|
|
|
std::string content_;
|
|
|
|
size_t offset_;
|
|
|
|
int expected_result_;
|
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
IMPLEMENT_REFCOUNTING(ReadHandler);
|
2014-01-31 00:15:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class ReadTestHandler : public RoutingTestHandler {
|
|
|
|
public:
|
|
|
|
explicit ReadTestHandler(bool may_block)
|
2017-05-17 11:29:28 +02:00
|
|
|
: may_block_(may_block), expected_result_(0) {}
|
2014-01-31 00:15:55 +01:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void RunTest() override {
|
2014-01-31 00:15:55 +01:00
|
|
|
// Create the browser.
|
|
|
|
CreateBrowser(kTestUrl);
|
2014-02-06 01:18:33 +01:00
|
|
|
|
|
|
|
// Time out the test after a reasonable period of time.
|
2015-01-10 00:40:26 +01:00
|
|
|
SetTestTimeout();
|
2014-01-31 00:15:55 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefResourceHandler> GetResourceHandler(
|
2014-01-31 00:15:55 +01:00
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefRequest> request) override {
|
2014-01-31 00:15:55 +01:00
|
|
|
got_resource_handler_.yes();
|
|
|
|
|
|
|
|
const std::string& url = request->GetURL();
|
|
|
|
EXPECT_STREQ(kTestUrl, url.c_str());
|
|
|
|
|
|
|
|
CefRefPtr<ReadHandler> handler = new ReadHandler(may_block_);
|
|
|
|
handler->CreateContent();
|
|
|
|
expected_result_ = handler->GetExpectedResult();
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamReader> stream =
|
|
|
|
CefStreamReader::CreateForHandler(handler.get());
|
|
|
|
return new CefStreamResourceHandler("text/html", stream);
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool OnQuery(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
int64 query_id,
|
|
|
|
const CefString& request,
|
|
|
|
bool persistent,
|
|
|
|
CefRefPtr<Callback> callback) override {
|
2014-01-31 00:15:55 +01:00
|
|
|
got_on_query_.yes();
|
|
|
|
|
|
|
|
const int actual_result = atoi(request.ToString().c_str());
|
|
|
|
EXPECT_EQ(expected_result_, actual_result);
|
|
|
|
|
2014-02-06 01:18:33 +01:00
|
|
|
DestroyTestIfDone();
|
2014-01-31 00:15:55 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
|
|
|
|
bool isLoading,
|
|
|
|
bool canGoBack,
|
|
|
|
bool canGoForward) override {
|
2014-02-06 01:18:33 +01:00
|
|
|
if (!isLoading) {
|
|
|
|
got_on_loading_state_change_done_.yes();
|
|
|
|
DestroyTestIfDone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-31 00:15:55 +01:00
|
|
|
private:
|
2014-02-06 01:18:33 +01:00
|
|
|
void DestroyTestIfDone() {
|
|
|
|
if (got_on_query_ && got_on_loading_state_change_done_)
|
|
|
|
DestroyTest();
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void DestroyTest() override {
|
2014-01-31 00:15:55 +01:00
|
|
|
EXPECT_TRUE(got_resource_handler_);
|
|
|
|
EXPECT_TRUE(got_on_query_);
|
2014-02-06 01:18:33 +01:00
|
|
|
EXPECT_TRUE(got_on_loading_state_change_done_);
|
2014-01-31 00:15:55 +01:00
|
|
|
RoutingTestHandler::DestroyTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool may_block_;
|
|
|
|
|
|
|
|
int expected_result_;
|
|
|
|
TrackCallback got_resource_handler_;
|
|
|
|
TrackCallback got_on_query_;
|
2014-02-06 01:18:33 +01:00
|
|
|
TrackCallback got_on_loading_state_change_done_;
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(ReadTestHandler);
|
2014-01-31 00:15:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(StreamResourceHandlerTest, ReadWillBlock) {
|
|
|
|
CefRefPtr<ReadTestHandler> handler = new ReadTestHandler(true);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2014-01-31 00:15:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StreamResourceHandlerTest, ReadWontBlock) {
|
|
|
|
CefRefPtr<ReadTestHandler> handler = new ReadTestHandler(false);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2014-01-31 00:15:55 +01:00
|
|
|
}
|