forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters