blob: 5d252dad3579ad3c40909ec0a62e56828d72db10 [file] [log] [blame]
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <algorithm>
#include "cobalt/dom/blob.h"
#include "cobalt/dom/data_view.h"
#include "cobalt/script/testing/mock_exception_state.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cobalt {
namespace dom {
namespace {
using script::testing::MockExceptionState;
using testing::_;
using testing::SaveArg;
using testing::StrictMock;
TEST(BlobTest, Constructors) {
scoped_refptr<Blob> blob_default_buffer = new Blob(NULL);
EXPECT_EQ(0, blob_default_buffer->size());
StrictMock<MockExceptionState> exception_state;
scoped_refptr<ArrayBuffer> array_buffer = new ArrayBuffer(NULL, 5);
scoped_refptr<DataView> data_view =
new DataView(array_buffer, &exception_state);
data_view->SetInt16(0, static_cast<int16>(0x0607), &exception_state);
scoped_refptr<Blob> blob_with_buffer = new Blob(NULL, array_buffer);
ASSERT_EQ(5, blob_with_buffer->size());
ASSERT_TRUE(blob_with_buffer->data());
EXPECT_EQ(0x6, blob_with_buffer->data()[0]);
EXPECT_EQ(0x7, blob_with_buffer->data()[1]);
EXPECT_EQ(0, blob_with_buffer->data()[2]);
EXPECT_EQ(0, blob_with_buffer->data()[3]);
EXPECT_EQ(0, blob_with_buffer->data()[4]);
}
// Tests that further changes to a buffer from which a blob was constructed
// no longer affect the blob's buffer, since it must be a separate copy of
// its construction arguments.
TEST(BlobTest, HasOwnBuffer) {
StrictMock<MockExceptionState> exception_state;
scoped_refptr<ArrayBuffer> array_buffer = new ArrayBuffer(NULL, 2);
scoped_refptr<DataView> data_view =
new DataView(array_buffer, &exception_state);
data_view->SetInt16(0, static_cast<int16>(0x0607), &exception_state);
scoped_refptr<Blob> blob_with_buffer = new Blob(NULL, array_buffer);
ASSERT_EQ(2, blob_with_buffer->size());
ASSERT_TRUE(blob_with_buffer->data());
EXPECT_NE(array_buffer->data(), blob_with_buffer->data());
data_view->SetUint8(1, static_cast<uint8>(0xff), &exception_state);
EXPECT_EQ(0x6, blob_with_buffer->data()[0]);
EXPECT_EQ(0x7, blob_with_buffer->data()[1]);
}
} // namespace
} // namespace dom
} // namespace cobalt