Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320)
[chromium-blink-merge.git] / content / test / plugin / plugin_request_read_test.cc
blob8d04a5e62ef84d660550ac82a7a6867b67d7bafe
1 // Copyright 2013 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.
5 #include "content/test/plugin/plugin_request_read_test.h"
7 #include "base/basictypes.h"
9 namespace NPAPIClient {
11 PluginRequestReadTest::PluginRequestReadTest(NPP id,
12 NPNetscapeFuncs* host_functions)
13 : PluginTest(id, host_functions),
14 tests_started_(false),
15 read_requested_(false) {
18 PluginRequestReadTest::~PluginRequestReadTest() {
21 NPError PluginRequestReadTest::New(uint16 mode, int16 argc, const char* argn[],
22 const char* argv[], NPSavedData* saved) {
23 url_to_request_ = GetArgValue("url_to_request", argc, argn, argv);
24 return PluginTest::New(mode, argc, argn, argv, saved);
27 NPError PluginRequestReadTest::SetWindow(NPWindow* window) {
28 if (!tests_started_) {
29 tests_started_ = true;
30 NPError result = HostFunctions()->geturl(id(),
31 url_to_request_.c_str(),
32 NULL);
33 if (result != NPERR_NO_ERROR)
34 SetError("Failed request anURL.");
36 return PluginTest::SetWindow(window);
39 NPError PluginRequestReadTest::NewStream(NPMIMEType type, NPStream* stream,
40 NPBool seekable, uint16* stream_type) {
41 *stream_type = NP_SEEK;
42 if (!read_requested_) {
43 requested_ranges_.resize(1);
44 requested_ranges_[0].offset = 4;
45 requested_ranges_[0].length = 6;
46 requested_ranges_[0].next = NULL;
47 NPError result = HostFunctions()->requestread(stream,
48 &requested_ranges_[0]);
49 if (result != NPERR_NO_ERROR)
50 SetError("Failed request read from stream.");
51 read_requested_ = true;
53 return PluginTest::NewStream(type, stream, seekable, stream_type);
56 NPError PluginRequestReadTest::DestroyStream(NPStream *stream, NPError reason) {
57 if (!requested_ranges_.empty())
58 SetError("Some requested ranges are not received!");
59 SignalTestCompleted();
60 return PluginTest::DestroyStream(stream, reason);
63 int32 PluginRequestReadTest::WriteReady(NPStream* stream) {
64 int32 result = 0;
65 for (size_t i = 0; i < requested_ranges_.size(); ++i)
66 result += requested_ranges_[i].length;
67 return result;
70 int32 PluginRequestReadTest::Write(NPStream* stream, int32 offset, int32 len,
71 void* buffer) {
72 std::vector<NPByteRange>::iterator it;
73 // Remove received range (or sub-range) from requested_ranges_, and
74 // verify that we have received proper data.
76 for (it = requested_ranges_.begin(); it != requested_ranges_.end(); ++it) {
77 if (it->offset == offset)
78 break;
80 if (it == requested_ranges_.end()) {
81 // It is Ok for browser to write some data from start of the stream before
82 // we've issued any read requests.
83 return len;
85 // Shrink range to mark area we have just received.
86 it->offset += len;
87 if (static_cast<int32>(it->length) < len)
88 it->length = 0;
89 else
90 it->length -= len;
91 if (it->length == 0)
92 requested_ranges_.erase(it);
94 // Verify the data we got is right. We expect a string like "01234...".
95 const char* data = static_cast<const char*>(buffer);
96 for (int32 i = 0; i < len; ++i) {
97 char expected = '0' + static_cast<char>(offset + i);
98 if (data[i] != expected) {
99 SetError("Content mismatch between data and source!");
100 break;
103 if (requested_ranges_.empty())
104 SignalTestCompleted();
106 return len;
109 } // namespace NPAPIClient