Skip to content

Commit

Permalink
Add fake Buffer for testing
Browse files Browse the repository at this point in the history
Adding a class to represent the Windows Buffer class within tests, as
well as basic unit tests to validate the test class behaviors.

Bug: 1035527
Change-Id: Ib4507c66d7b9c975e7580b2053f23b1dc54f3402
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2485553
Reviewed-by: Eric Willigers <ericwilligers@chromium.org>
Commit-Queue: Hoch Hochkeppel <mhochk@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#818753}
  • Loading branch information
mhochk authored and Commit Bot committed Oct 20, 2020
1 parent c651a37 commit 776be78
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
38 changes: 38 additions & 0 deletions chrome/browser/webshare/win/fake_buffer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2020 The Chromium 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 "chrome/browser/webshare/win/fake_buffer.h"

#include "testing/gtest/include/gtest/gtest.h"

namespace webshare {

FakeBuffer::FakeBuffer(UINT32 capacity) : data_(capacity) {}

FakeBuffer::~FakeBuffer() = default;

IFACEMETHODIMP FakeBuffer::get_Capacity(UINT32* value) {
*value = data_.size();
return S_OK;
}
IFACEMETHODIMP FakeBuffer::get_Length(UINT32* value) {
*value = length_;
return S_OK;
}
IFACEMETHODIMP FakeBuffer::put_Length(UINT32 value) {
if (value > data_.size()) {
ADD_FAILURE() << "put_Length called with a value (" << value
<< ") greater than the capacity (" << data_.size() << ")";
return E_INVALIDARG;
}
length_ = value;
return S_OK;
}

IFACEMETHODIMP FakeBuffer::Buffer(byte** value) {
*value = data_.data();
return S_OK;
}

} // namespace webshare
43 changes: 43 additions & 0 deletions chrome/browser/webshare/win/fake_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_WEBSHARE_WIN_FAKE_BUFFER_H_
#define CHROME_BROWSER_WEBSHARE_WIN_FAKE_BUFFER_H_

#include <robuffer.h>
#include <windows.storage.streams.h>
#include <wrl/implements.h>

#include <vector>

namespace webshare {

// Provides an implementation of IBuffer for use in GTests.
class __declspec(uuid("23BAE777-CA37-40B3-8F1C-0B86C1F43EE5")) FakeBuffer final
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::WinRtClassicComMix>,
ABI::Windows::Storage::Streams::IBuffer,
Windows::Storage::Streams::IBufferByteAccess> {
public:
explicit FakeBuffer(UINT32 capacity);
FakeBuffer(const FakeBuffer&) = delete;
FakeBuffer& operator=(const FakeBuffer&) = delete;
~FakeBuffer() final;

// ABI::Windows::Storage::Streams::IBuffer:
IFACEMETHODIMP get_Capacity(UINT32* value) final;
IFACEMETHODIMP get_Length(UINT32* value) final;
IFACEMETHODIMP put_Length(UINT32 value) final;

// Windows::Storage::Streams::IBufferByteAccess
IFACEMETHODIMP Buffer(byte** value) final;

private:
std::vector<unsigned char> data_;
UINT32 length_ = 0;
};

} // namespace webshare

#endif // CHROME_BROWSER_WEBSHARE_WIN_FAKE_BUFFER_H_
57 changes: 57 additions & 0 deletions chrome/browser/webshare/win/fake_buffer_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2020 The Chromium 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 "chrome/browser/webshare/win/fake_buffer.h"

#include <wrl/implements.h>

#include "testing/gtest/include/gtest/gtest-spi.h"
#include "testing/gtest/include/gtest/gtest.h"

using Microsoft::WRL::Make;

namespace webshare {

TEST(FakeBufferTest, Length) {
auto buffer = Make<FakeBuffer>(47);

UINT32 capacity;
ASSERT_HRESULT_SUCCEEDED(buffer->get_Capacity(&capacity));
ASSERT_EQ(capacity, 47u);

UINT32 length;
ASSERT_HRESULT_SUCCEEDED(buffer->get_Length(&length));
ASSERT_EQ(length, 0u);

ASSERT_HRESULT_SUCCEEDED(buffer->put_Length(23));
ASSERT_HRESULT_SUCCEEDED(buffer->get_Length(&length));
ASSERT_EQ(length, 23u);

ASSERT_HRESULT_SUCCEEDED(buffer->put_Length(47));
ASSERT_HRESULT_SUCCEEDED(buffer->get_Length(&length));
ASSERT_EQ(length, 47u);

EXPECT_NONFATAL_FAILURE(ASSERT_HRESULT_FAILED(buffer->put_Length(48)),
"put_Length");
}

TEST(FakeBufferTest, Bytes) {
auto buffer = Make<FakeBuffer>(2);

byte* raw_buffer;
ASSERT_HRESULT_SUCCEEDED(buffer->Buffer(&raw_buffer));

raw_buffer[0] = 'a';
raw_buffer[1] = 'b';

auto buffer2 = buffer;
byte* raw_buffer_2;
ASSERT_HRESULT_SUCCEEDED(buffer2->Buffer(&raw_buffer_2));

ASSERT_EQ(raw_buffer, raw_buffer_2);
ASSERT_EQ(raw_buffer_2[0], 'a');
ASSERT_EQ(raw_buffer_2[1], 'b');
}

} // namespace webshare
3 changes: 3 additions & 0 deletions chrome/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ static_library("test_support") {

if (is_win) {
sources += [
"../browser/webshare/win/fake_buffer.cc",
"../browser/webshare/win/fake_buffer.h",
"../browser/webshare/win/fake_data_transfer_manager.cc",
"../browser/webshare/win/fake_data_transfer_manager.h",
"../browser/webshare/win/fake_data_transfer_manager_interop.cc",
Expand Down Expand Up @@ -5810,6 +5812,7 @@ test("unit_tests") {
sources += [
"../browser/notifications/win/notification_template_builder_unittest.cc",
"../browser/ui/views/uninstall_view_unittest.cc",
"../browser/webshare/win/fake_buffer_unittest.cc",
"../browser/webshare/win/fake_data_transfer_manager_interop_unittest.cc",
"../browser/webshare/win/fake_data_transfer_manager_unittest.cc",
"../browser/webshare/win/show_share_ui_for_window_operation_unittest.cc",
Expand Down

0 comments on commit 776be78

Please sign in to comment.