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

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: add missing header Created 3 years, 6 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_.reset(new base::RunLoop());
Lei Zhang 2017/06/02 21:54:09 run_loop_ = base::MakeUnique<base::RunLoop>();
Wei Li 2017/07/07 21:23:57 Done.
48 }
49
50 void TearDown() override {
51 // Clean up
52 compositor_.reset();
53 }
54
55 base::SharedMemoryHandle LoadFileInSharedMemory(size_t* len) {
Lei Zhang 2017/06/02 21:54:09 base::SharedMemoryHandle has a GetSize() method. D
Wei Li 2017/07/07 21:23:57 Added a check to make sure these two are same so w
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::ReadFileToString(test_file, &content);
Lei Zhang 2017/06/02 21:54:09 Do we want to return early if this fails? Same for
Wei Li 2017/07/07 21:23:57 Checks added.
62 *len = content.size();
63 base::SharedMemoryCreateOptions options;
64 options.size = *len;
65 options.share_read_only = true;
66 base::SharedMemory shared_memory;
67 shared_memory.Create(options);
68 shared_memory.Map(*len);
69 memcpy(shared_memory.memory(), content.data(), *len);
70 return base::SharedMemory::DuplicateHandle(shared_memory.handle());
71 }
72
73 void CallCompositorWithSuccess(mojom::PdfCompositorPtr ptr) {
74 size_t len;
75 mojo::ScopedSharedBufferHandle buffer_handle =
76 mojo::WrapSharedMemoryHandle(LoadFileInSharedMemory(&len), len, true);
77 ASSERT_TRUE(buffer_handle->is_valid());
78 EXPECT_CALL(*this, CallbackOnSuccess(testing::_)).Times(1);
79 ptr->CompositePdf(std::move(buffer_handle),
80 base::BindOnce(&PdfCompositorServiceTest::OnCallback,
81 base::Unretained(this)));
82 run_loop_->Run();
83 }
84
85 std::unique_ptr<base::RunLoop> run_loop_;
86 mojom::PdfCompositorPtr compositor_;
87
88 private:
89 DISALLOW_COPY_AND_ASSIGN(PdfCompositorServiceTest);
90 };
91
92 // Test callback is called on error conditions in service.
93 TEST_F(PdfCompositorServiceTest, InvokeCallbackOnContentError) {
94 size_t len;
95 mojo::ScopedSharedBufferHandle buffer_handle =
96 mojo::WrapSharedMemoryHandle(LoadFileInSharedMemory(&len), 10, true);
97 // The size of mapped area is not equal to the original buffer,
98 // so the content is invalid.
99 ASSERT_NE(10u, len);
Lei Zhang 2017/06/02 21:54:09 Can we check for the expected size of |len| instea
Wei Li 2017/07/07 21:23:57 The len is still file content size. But we truncat
100 ASSERT_TRUE(buffer_handle->is_valid());
101 EXPECT_CALL(*this, CallbackOnError(
102 mojom::PdfCompositor::Status::CONTENT_FORMAT_ERROR))
103 .Times(1);
104 compositor_->CompositePdf(
105 std::move(buffer_handle),
106 base::BindOnce(&PdfCompositorServiceTest::OnCallback,
107 base::Unretained(this)));
108 run_loop_->Run();
109 }
110
111 TEST_F(PdfCompositorServiceTest, InvokeCallbackOnSuccess) {
112 CallCompositorWithSuccess(std::move(compositor_));
113 }
114
115 TEST_F(PdfCompositorServiceTest, ServiceInstances) {
116 // One service can bind multiple interfaces.
117 mojom::PdfCompositorPtr another_compositor;
118 ASSERT_FALSE(another_compositor);
119 connector()->BindInterface(mojom::kServiceName, &another_compositor);
120 ASSERT_TRUE(another_compositor);
121 ASSERT_NE(compositor_.get(), another_compositor.get());
122
123 // Terminating one interface won't affect another.
124 compositor_.reset();
125 CallCompositorWithSuccess(std::move(another_compositor));
126 }
127
128 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698