RDK Documentation (Open Sourced RDK Components)
ExecuteTests.cpp
1 /*
2 * If not stated otherwise in this file or this component's license file the
3 * following copyright and licenses apply:
4 *
5 * Copyright 2022 RDK Management
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 
20 #include <gtest/gtest.h>
21 #include <gmock/gmock.h>
22 #include <iostream>
23 #include <fstream>
24 #include "MockPlayerInstanceAAMP.h"
25 #include "AampcliSet.h"
26 
27 using ::testing::_;
28 using ::testing::Return;
29 using ::testing::AtLeast;
30 
31 class ExecuteTests : public ::testing::Test
32 {
33 protected:
34  Set *mSet = nullptr;
35 
36  void SetUp() override
37  {
38  mSet = new Set();
39 
40  g_mockPlayerInstanceAAMP = new MockPlayerInstanceAAMP();
41 
42  mSet->registerSetCommands();
43  }
44 
45  void TearDown() override
46  {
47  delete g_mockPlayerInstanceAAMP;
48  g_mockPlayerInstanceAAMP = nullptr;
49 
50  delete mSet;
51  mSet =nullptr;
52  }
53 };
54 
55 // Test calling "set ssStyle" with a valid default option
56 TEST_F(ExecuteTests, Set_CCStyle_NumberedOptions)
57 {
58  char cmd1[] = "set ccStyle 1";
59  char cmd2[] = "set ccStyle 2";
60  char cmd3[] = "set ccStyle 3";
61 
62  EXPECT_CALL(*g_mockPlayerInstanceAAMP, SetTextStyle(CC_OPTION_1)).Times(1);
63  mSet->execute(cmd1, g_mockPlayerInstanceAAMP);
64 
65  EXPECT_CALL(*g_mockPlayerInstanceAAMP, SetTextStyle(CC_OPTION_2)).Times(1);
66  mSet->execute(cmd2, g_mockPlayerInstanceAAMP);
67 
68  EXPECT_CALL(*g_mockPlayerInstanceAAMP, SetTextStyle(CC_OPTION_3)).Times(1);
69  mSet->execute(cmd3, g_mockPlayerInstanceAAMP);
70 }
71 
72 // Test calling "set 45" (ccStyle) with an valid default option
73 TEST_F(ExecuteTests, Set_45_NumberedOptions)
74 {
75  char cmd1[] = "set 45 1";
76 
77  EXPECT_CALL(*g_mockPlayerInstanceAAMP, SetTextStyle(CC_OPTION_1)).Times(1);
78  mSet->execute(cmd1, g_mockPlayerInstanceAAMP);
79 }
80 
81 // Test calling "set ssStyle" with no parameter
82 TEST_F(ExecuteTests, Set_CCStyle_NoParameter)
83 {
84  char cmd[] = "set ccStyle ";
85 
86  EXPECT_CALL(*g_mockPlayerInstanceAAMP, SetTextStyle(_)).Times(0);
87  mSet->execute(cmd, g_mockPlayerInstanceAAMP);
88 }
89 
90 // Test calling "set ssStyle" with an invalid default option
91 TEST_F(ExecuteTests, Set_CCStyle_InvalidNumberedOption)
92 {
93  char cmd0[] = "set ccStyle 0";
94  char cmd4[] = "set ccStyle 4";
95 
96  EXPECT_CALL(*g_mockPlayerInstanceAAMP, SetTextStyle(_)).Times(0);
97  mSet->execute(cmd0, g_mockPlayerInstanceAAMP);
98 
99  EXPECT_CALL(*g_mockPlayerInstanceAAMP, SetTextStyle(_)).Times(0);
100  mSet->execute(cmd4, g_mockPlayerInstanceAAMP);
101 }
102 
103 // Test calling "set ssStyle" with a valid json file
104 // Note: This test Creates a json file in the build output directory, and then removes it
105 TEST_F(ExecuteTests, Set_CCStyle_File)
106 {
107  const char json_file[] = "test_json_file";
108  char cmd[] = "set ccStyle test_json_file";
109  const char json[] = "{ \"penSize\":\"small\" }";
110 
111  std::ifstream ifs(json_file, std::ifstream::in);
112  ASSERT_FALSE(ifs.good());
113 
114  // Creates a json file in the build output directory
115  std::ofstream of(json_file, std::ofstream::out);
116  ASSERT_TRUE(of.is_open());
117 
118  of << json << std::endl;
119  of.flush();
120  of.close();
121 
122  EXPECT_CALL(*g_mockPlayerInstanceAAMP, SetTextStyle(json)).Times(1);
123  mSet->execute(cmd, g_mockPlayerInstanceAAMP);
124 
125  // Remove json file
126  ASSERT_FALSE(std::remove(json_file));
127 }
128 
129 // Test calling "set ssStyle" with a valid json file which starts with an option number
130 // Make sure file is opened, and does not assume an option number
131 // Note: This test Creates a json file in the build output directory, and then removes it
132 TEST_F(ExecuteTests, Set_CCStyle_FileStartingWithNumber)
133 {
134  const char json_file[] = "1test_json_file";
135  char cmd[] = "set ccStyle 1test_json_file";
136  const char json[] = "{ \"penSize\":\"small\" }";
137 
138  std::ifstream ifs(json_file, std::ifstream::in);
139  ASSERT_FALSE(ifs.good());
140 
141  // Creates a json file in the build output directory
142  std::ofstream of(json_file, std::ofstream::out);
143  ASSERT_TRUE(of.is_open());
144 
145  of << json << std::endl;
146  of.flush();
147  of.close();
148 
149  EXPECT_CALL(*g_mockPlayerInstanceAAMP, SetTextStyle(json)).Times(1);
150  mSet->execute(cmd, g_mockPlayerInstanceAAMP);
151 
152  // Remove json file
153  ASSERT_FALSE(std::remove(json_file));
154 }
155 
156 // Test calling "set ssStyle" with a file that doesn't exist
157 TEST_F(ExecuteTests, Set_CCStyle_MissingFile)
158 {
159  char cmd[] = "set ccStyle MissingFile";
160 
161  EXPECT_CALL(*g_mockPlayerInstanceAAMP, SetTextStyle(_)).Times(0);
162  mSet->execute(cmd, g_mockPlayerInstanceAAMP);
163 }
164 
165 // Test calling "set ssStyle" with a file that exceeds the file path buffer
166 TEST_F(ExecuteTests, Set_CCStyle_FilenameExceedsMaxLength)
167 {
168  char cmd [255] = "set ccStyle ";
169  int i;
170 
171  // Fill array with characters and null terminate
172  for (i = strlen(cmd); i < sizeof(cmd) - 1; i++)
173  {
174  cmd[i] = 'A' + (i % 26);
175  }
176  cmd[i] = 0;
177 
178  EXPECT_CALL(*g_mockPlayerInstanceAAMP, SetTextStyle(_)).Times(0);
179  mSet->execute(cmd, g_mockPlayerInstanceAAMP);
180 }
181 
Set::registerSetCommands
void registerSetCommands()
Show help menu with aamp command line interface.
Definition: AampcliSet.cpp:1222
ExecuteTests
Definition: ExecuteTests.cpp:31
AampcliSet.h
AampcliSet header file.
MockPlayerInstanceAAMP
Definition: MockPlayerInstanceAAMP.h:26
Set
Definition: AampcliSet.h:42