OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "mojo/public/cpp/application/application_test_base.h" | |
6 #include "mojo/public/cpp/application/connect.h" | |
7 #include "mojo/public/cpp/system/macros.h" | |
8 #include "mojo/public/interfaces/bindings/tests/versioning_test_client.mojom.h" | |
9 | |
10 namespace mojo { | |
11 namespace test { | |
12 namespace versioning { | |
13 | |
14 class VersioningApplicationTest : public ApplicationTestBase { | |
15 public: | |
16 VersioningApplicationTest() : ApplicationTestBase() {} | |
17 ~VersioningApplicationTest() override {} | |
18 | |
19 protected: | |
20 // ApplicationTestBase overrides. | |
21 void SetUp() override { | |
22 ApplicationTestBase::SetUp(); | |
23 | |
24 ConnectToService(shell(), "mojo:versioning_test_service", | |
25 GetProxy(&database_)); | |
26 } | |
27 | |
28 HumanResourceDatabasePtr database_; | |
29 | |
30 private: | |
31 MOJO_DISALLOW_COPY_AND_ASSIGN(VersioningApplicationTest); | |
32 }; | |
33 | |
34 TEST_F(VersioningApplicationTest, Struct) { | |
35 // The service side uses a newer version of Employee defintion. | |
36 // The returned struct should be truncated. | |
37 EmployeePtr employee(Employee::New()); | |
38 employee->employee_id = 1; | |
39 employee->name = "Homer Simpson"; | |
40 employee->department = Department::DEV; | |
41 | |
42 database_->QueryEmployee(1, true, | |
43 [&employee](EmployeePtr returned_employee, | |
44 Array<uint8_t> returned_finger_print) { | |
45 EXPECT_TRUE(employee->Equals(*returned_employee)); | |
46 EXPECT_FALSE(returned_finger_print.is_null()); | |
47 }); | |
48 database_.WaitForIncomingResponse(); | |
49 | |
50 // Passing a struct of older version to the service side works. | |
51 EmployeePtr new_employee(Employee::New()); | |
52 new_employee->employee_id = 2; | |
53 new_employee->name = "Marge Simpson"; | |
54 new_employee->department = Department::SALES; | |
55 | |
56 database_->AddEmployee(new_employee.Clone(), | |
57 [](bool success) { EXPECT_TRUE(success); }); | |
58 database_.WaitForIncomingResponse(); | |
59 | |
60 database_->QueryEmployee( | |
61 2, false, [&new_employee](EmployeePtr returned_employee, | |
62 Array<uint8_t> returned_finger_print) { | |
63 EXPECT_TRUE(new_employee->Equals(*returned_employee)); | |
64 EXPECT_TRUE(returned_finger_print.is_null()); | |
65 }); | |
66 database_.WaitForIncomingResponse(); | |
67 } | |
68 | |
69 TEST_F(VersioningApplicationTest, QueryVersion) { | |
70 EXPECT_EQ(0u, database_.version()); | |
71 database_.QueryVersion([](uint32_t version) { EXPECT_EQ(1u, version); }); | |
72 database_.WaitForIncomingResponse(); | |
73 EXPECT_EQ(1u, database_.version()); | |
74 } | |
75 | |
76 TEST_F(VersioningApplicationTest, RequireVersion) { | |
77 EXPECT_EQ(0u, database_.version()); | |
78 | |
79 database_.RequireVersion(1); | |
80 EXPECT_EQ(1u, database_.version()); | |
81 database_->QueryEmployee(3, false, | |
82 [](EmployeePtr returned_employee, | |
83 Array<uint8_t> returned_finger_print) {}); | |
84 database_.WaitForIncomingResponse(); | |
85 EXPECT_FALSE(database_.encountered_error()); | |
86 | |
87 // Requiring a version higher than what the service side implements will close | |
88 // the pipe. | |
89 database_.RequireVersion(3); | |
90 EXPECT_EQ(3u, database_.version()); | |
91 database_->QueryEmployee(1, false, | |
92 [](EmployeePtr returned_employee, | |
93 Array<uint8_t> returned_finger_print) {}); | |
94 database_.WaitForIncomingResponse(); | |
95 EXPECT_TRUE(database_.encountered_error()); | |
96 } | |
97 | |
98 TEST_F(VersioningApplicationTest, CallNonexistentMethod) { | |
99 EXPECT_EQ(0u, database_.version()); | |
100 | |
101 auto new_finger_print = Array<uint8_t>::New(128); | |
102 for (size_t i = 0; i < 128; ++i) | |
103 new_finger_print[i] = i + 13; | |
104 | |
105 // Although the client side doesn't know whether the service side supports | |
106 // version 1, calling a version 1 method succeeds as long as the service side | |
107 // supports version 1. | |
108 database_->AttachFingerPrint(1, new_finger_print.Clone(), | |
109 [](bool success) { EXPECT_TRUE(success); }); | |
110 database_.WaitForIncomingResponse(); | |
111 | |
112 // Calling a version 2 method (which the service side doesn't support) closes | |
113 // the pipe. | |
114 database_->ListEmployeeIds([](Array<uint64_t> ids) { EXPECT_TRUE(false); }); | |
115 database_.WaitForIncomingResponse(); | |
116 EXPECT_TRUE(database_.encountered_error()); | |
117 } | |
118 | |
119 } // namespace versioning | |
120 } // namespace examples | |
121 } // namespace mojo | |
OLD | NEW |