Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(434)

Side by Side Diff: components/printing/service/pdf_compositor_service_unittest.cc

Issue 2919823004: Add error handling and unit test for pdf compositor service (Closed)
Patch Set: rebase Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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.
4
5 #include <memory>
6
7 #include "base/callback.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/memory/shared_memory.h"
11 #include "base/path_service.h"
12 #include "components/printing/service/public/interfaces/pdf_compositor.mojom.h"
13 #include "mojo/public/cpp/system/platform_handle.h"
14 #include "services/service_manager/public/cpp/service_test.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace printing {
19
20 class PdfCompositorServiceTest : public service_manager::test::ServiceTest {
21 public:
22 PdfCompositorServiceTest() : ServiceTest("pdf_compositor_service_unittest") {}
23 ~PdfCompositorServiceTest() override {}
24
25 MOCK_METHOD1(CallbackOnSuccess, void(mojo::SharedBufferHandle));
26 MOCK_METHOD1(CallbackOnError, void(mojom::PdfCompositor::Status));
27 void OnCallback(mojom::PdfCompositor::Status status,
28 mojo::ScopedSharedBufferHandle handle) {
29 if (status == mojom::PdfCompositor::Status::SUCCESS)
30 CallbackOnSuccess(handle.get());
31 else
32 CallbackOnError(status);
33 run_loop_->Quit();
34 }
35
36 MOCK_METHOD0(ConnectionClosed, void());
37
38 protected:
39 // service_manager::test::ServiceTest:
40 void SetUp() override {
41 ServiceTest::SetUp();
42
43 ASSERT_FALSE(compositor_);
44 connector()->BindInterface(mojom::kServiceName, &compositor_);
45 ASSERT_TRUE(compositor_);
46
47 run_loop_ = base::MakeUnique<base::RunLoop>();
48 }
49
50 void TearDown() override {
51 // Clean up
52 compositor_.reset();
53 }
54
55 base::SharedMemoryHandle LoadFileInSharedMemory() {
56 base::FilePath path;
57 PathService::Get(base::DIR_SOURCE_ROOT, &path);
58 base::FilePath test_file =
59 path.AppendASCII("components/test/data/printing/google.mskp");
60 std::string content;
61 base::SharedMemoryHandle invalid_handle;
62 if (!base::ReadFileToString(test_file, &content))
63 return invalid_handle;
64 size_t len = content.size();
65 base::SharedMemoryCreateOptions options;
66 options.size = len;
67 options.share_read_only = true;
68 base::SharedMemory shared_memory;
69 if (shared_memory.Create(options) && shared_memory.Map(len)) {
70 memcpy(shared_memory.memory(), content.data(), len);
71 base::SharedMemoryHandle handle = shared_memory.handle();
72 if (len == handle.GetSize())
73 return base::SharedMemory::DuplicateHandle(handle);
74 }
75 return invalid_handle;
76 }
77
78 void CallCompositorWithSuccess(mojom::PdfCompositorPtr ptr) {
79 auto handle = LoadFileInSharedMemory();
80 ASSERT_TRUE(handle.IsValid());
81 mojo::ScopedSharedBufferHandle buffer_handle =
82 mojo::WrapSharedMemoryHandle(handle, handle.GetSize(), true);
83 ASSERT_TRUE(buffer_handle->is_valid());
84 EXPECT_CALL(*this, CallbackOnSuccess(testing::_)).Times(1);
85 ptr->CompositePdf(std::move(buffer_handle),
86 base::BindOnce(&PdfCompositorServiceTest::OnCallback,
87 base::Unretained(this)));
88 run_loop_->Run();
89 }
90
91 std::unique_ptr<base::RunLoop> run_loop_;
92 mojom::PdfCompositorPtr compositor_;
93
94 private:
95 DISALLOW_COPY_AND_ASSIGN(PdfCompositorServiceTest);
96 };
97
98 // Test callback is called on error conditions in service.
99 TEST_F(PdfCompositorServiceTest, InvokeCallbackOnContentError) {
100 auto handle = LoadFileInSharedMemory();
101 ASSERT_TRUE(handle.IsValid());
102 mojo::ScopedSharedBufferHandle buffer_handle =
103 mojo::WrapSharedMemoryHandle(handle, 10, true);
104 // The size of mapped area is not equal to the original buffer,
105 // so the content is invalid.
106 ASSERT_LT(10u, handle.GetSize());
107 ASSERT_TRUE(buffer_handle->is_valid());
108 EXPECT_CALL(*this, CallbackOnError(
109 mojom::PdfCompositor::Status::CONTENT_FORMAT_ERROR))
110 .Times(1);
111 compositor_->CompositePdf(
112 std::move(buffer_handle),
113 base::BindOnce(&PdfCompositorServiceTest::OnCallback,
114 base::Unretained(this)));
115 run_loop_->Run();
116 }
117
118 TEST_F(PdfCompositorServiceTest, InvokeCallbackOnSuccess) {
119 CallCompositorWithSuccess(std::move(compositor_));
120 }
121
122 TEST_F(PdfCompositorServiceTest, ServiceInstances) {
123 // One service can bind multiple interfaces.
124 mojom::PdfCompositorPtr another_compositor;
125 ASSERT_FALSE(another_compositor);
126 connector()->BindInterface(mojom::kServiceName, &another_compositor);
127 ASSERT_TRUE(another_compositor);
128 ASSERT_NE(compositor_.get(), another_compositor.get());
129
130 // Terminating one interface won't affect another.
131 compositor_.reset();
132 CallCompositorWithSuccess(std::move(another_compositor));
133 }
134
135 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698