1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <algorithm>
18 #include <fstream>
19 #include <iostream>
20
21 #include <android-base/file.h>
22
23 #include <gtest/gtest.h>
24
25 #include "host/commands/cvd/parser/cf_configs_common.h"
26 #include "host/commands/cvd/unittests/parser/test_common.h"
27
28 namespace cuttlefish {
29
TEST(FlagsInheritanceTest,MergeTwoIndependentJson)30 TEST(FlagsInheritanceTest, MergeTwoIndependentJson) {
31 const char* dst_string = R""""(
32 {
33 "instances" :
34 [
35 {
36 "vm": {
37 "memory_mb": 2048
38 }
39 }
40 ]
41 }
42 )"""";
43
44 const char* src_string = R""""(
45 {
46 "instances" :
47 [
48 {
49 "graphics":{
50 "displays":[
51 {
52 "width": 720,
53 "height": 1280,
54 "dpi": 320
55 }
56 ]
57 }
58 }
59 ]
60 }
61 )"""";
62
63 Json::Value src_object, dst_object;
64 std::string src_text(src_string);
65 std::string dst_text(dst_string);
66 EXPECT_TRUE(ParseJsonString(dst_text, dst_object)) << "Invalid Json string";
67 EXPECT_TRUE(ParseJsonString(src_text, src_object)) << "Invalid Json string";
68
69 cuttlefish::MergeTwoJsonObjs(dst_object, src_object);
70 EXPECT_TRUE(dst_object["instances"][0].isMember("graphics"));
71 EXPECT_TRUE(dst_object["instances"][0]["graphics"].isMember("displays"));
72 EXPECT_TRUE(
73 dst_object["instances"][0]["graphics"]["displays"][0].isMember("width"));
74 EXPECT_TRUE(
75 dst_object["instances"][0]["graphics"]["displays"][0].isMember("height"));
76 EXPECT_TRUE(
77 dst_object["instances"][0]["graphics"]["displays"][0].isMember("dpi"));
78
79 EXPECT_EQ(dst_object["instances"][0]["graphics"]["displays"][0]["width"],
80 720);
81 EXPECT_EQ(dst_object["instances"][0]["graphics"]["displays"][0]["height"],
82 1280);
83 EXPECT_EQ(dst_object["instances"][0]["graphics"]["displays"][0]["dpi"], 320);
84 }
85
TEST(FlagsInheritanceTest,MergeTwoOverlappedJson)86 TEST(FlagsInheritanceTest, MergeTwoOverlappedJson) {
87 const char* dst_string = R""""(
88 {
89 "instances" :
90 [
91 {
92 "vm": {
93 "memory_mb": 1024
94 }
95 }
96 ]
97 }
98 )"""";
99
100 const char* src_string = R""""(
101 {
102 "instances" :
103 [
104 {
105 "vm": {
106 "memory_mb": 2048
107 },
108 "graphics":{
109 "displays":[
110 {
111 "width": 720,
112 "height": 1280,
113 "dpi": 320
114 }
115 ]
116 }
117 }
118 ]
119 }
120 )"""";
121
122 Json::Value src_object, dst_object;
123 std::string src_text(src_string);
124 std::string dst_text(dst_string);
125 EXPECT_TRUE(ParseJsonString(dst_text, dst_object)) << "Invalid Json string";
126 EXPECT_TRUE(ParseJsonString(src_text, src_object)) << "Invalid Json string";
127
128 cuttlefish::MergeTwoJsonObjs(dst_object, src_object);
129 EXPECT_TRUE(dst_object["instances"][0].isMember("graphics"));
130 EXPECT_TRUE(dst_object["instances"][0]["graphics"].isMember("displays"));
131 EXPECT_TRUE(
132 dst_object["instances"][0]["graphics"]["displays"][0].isMember("width"));
133 EXPECT_TRUE(
134 dst_object["instances"][0]["graphics"]["displays"][0].isMember("height"));
135 EXPECT_TRUE(
136 dst_object["instances"][0]["graphics"]["displays"][0].isMember("dpi"));
137
138 EXPECT_EQ(dst_object["instances"][0]["graphics"]["displays"][0]["width"],
139 720);
140 EXPECT_EQ(dst_object["instances"][0]["graphics"]["displays"][0]["height"],
141 1280);
142 EXPECT_EQ(dst_object["instances"][0]["graphics"]["displays"][0]["dpi"], 320);
143 // Check for overlapped values
144 EXPECT_EQ(dst_object["instances"][0]["vm"]["memory_mb"], 2048);
145 }
146
147 } // namespace cuttlefish
148