1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 #include <gtest/gtest.h>
18 #include "task/TaskAll.h"
19 
20 static const android::String8 AAA("aaa");
21 static const android::String8 BBB("bbb");
22 
23 class TaskTest : public testing::Test {
24 public:
25     TaskCase* mTestCase;
26     // should not delete
27     TaskGeneric* mTaskSetup;
28     TaskGeneric* mTaskAction;
29     TaskGeneric* mTaskSequential;
30     TaskGeneric* mTaskProcess;
31     TaskGeneric* mTaskInput;
32     TaskGeneric* mTaskOutput;
33     TaskGeneric* mTaskSound;
34 
35     class TestTaskDummy: public TaskGeneric {
36     public:
37         static int mRunCounter;
38         static int mLiveInstanceCounter;
39 
TestTaskDummy(TaskGeneric::TaskType type)40         explicit TestTaskDummy(TaskGeneric::TaskType type)
41             : TaskGeneric(type) {
42             mLiveInstanceCounter++;
43 
44 
45             const android::String8* list[] = {&AAA, &BBB, NULL};
46             registerSupportedStringAttributes(list);
47         };
~TestTaskDummy()48         virtual ~TestTaskDummy(){
49             mLiveInstanceCounter--;
50         };
51 
run()52         virtual TaskGeneric::ExecutionResult run()
53         {
54             mRunCounter++;
55             return TaskGeneric::run();
56         };
addStringAttributePublic(const android::String8 & key,android::String8 & value)57         bool addStringAttributePublic(const android::String8& key, android::String8& value){
58             return addStringAttribute(key, value);
59         }
findStringAttributePublic(const android::String8 & key,android::String8 & value)60         bool findStringAttributePublic(const android::String8& key, android::String8& value){
61             return findStringAttribute(key, value);
62         }
63     };
64 
SetUp()65     virtual void SetUp() {
66         TestTaskDummy::mRunCounter = 0;
67         TestTaskDummy::mLiveInstanceCounter = 0;
68         mTestCase = new TaskCase();
69         mTaskSetup = new TestTaskDummy(TaskGeneric::ETaskSetup);
70         mTaskAction = new TestTaskDummy(TaskGeneric::ETaskAction);
71         ASSERT_TRUE(mTestCase->addChild(mTaskSetup));
72         ASSERT_TRUE(mTestCase->addChild(mTaskAction));
73         mTaskSequential = new TestTaskDummy(TaskGeneric::ETaskSequential);
74         ASSERT_TRUE(mTaskAction->addChild(mTaskSequential));
75         mTaskProcess = new TestTaskDummy(TaskGeneric::ETaskProcess);
76         mTaskInput = new TestTaskDummy(TaskGeneric::ETaskInput);
77         mTaskOutput = new TestTaskDummy(TaskGeneric::ETaskOutput);
78         ASSERT_TRUE(mTaskSequential->addChild(mTaskOutput));
79         ASSERT_TRUE(mTaskSequential->addChild(mTaskInput));
80         ASSERT_TRUE(mTaskSequential->addChild(mTaskProcess));
81         mTaskSound = new TestTaskDummy(TaskGeneric::ETaskSound);
82         ASSERT_TRUE(mTaskSetup->addChild(mTaskSound));
83         ASSERT_TRUE(TestTaskDummy::mLiveInstanceCounter == 7);
84     }
85 
TearDown()86     virtual void TearDown() {
87         if(mTestCase != NULL) {
88             delete mTestCase;
89         }
90         ASSERT_TRUE(TestTaskDummy::mLiveInstanceCounter == 0);
91     }
92 };
93 
94 int TaskTest::TestTaskDummy::mRunCounter = 0;
95 int TaskTest::TestTaskDummy::mLiveInstanceCounter = 0;
96 
TEST_F(TaskTest,HierarchyTest)97 TEST_F(TaskTest, HierarchyTest) {
98     // verify hierarchy
99     ASSERT_TRUE(mTaskSetup->getTestCase() == mTestCase);
100     ASSERT_TRUE(mTaskAction->getTestCase() == mTestCase);
101     ASSERT_TRUE(mTaskSequential->getTestCase() == mTestCase);
102     ASSERT_TRUE(mTaskProcess->getTestCase() == mTestCase);
103     ASSERT_TRUE(mTaskInput->getTestCase() == mTestCase);
104     ASSERT_TRUE(mTaskOutput->getTestCase() == mTestCase);
105     ASSERT_TRUE(mTaskSound->getTestCase() == mTestCase);
106 }
107 
TEST_F(TaskTest,RunTest)108 TEST_F(TaskTest, RunTest) {
109     ASSERT_TRUE(mTestCase->run() == TaskGeneric::EResultOK);
110     ASSERT_TRUE(TestTaskDummy::mRunCounter == 7);
111 }
112 
TEST_F(TaskTest,StringAttributeTest)113 TEST_F(TaskTest, StringAttributeTest) {
114     android::String8 aaaVal("aaa_val");
115     android::String8 bbbVal("bbb_val");
116     android::String8 read;
117     TestTaskDummy* task = reinterpret_cast<TestTaskDummy*>(mTaskSetup);
118     ASSERT_TRUE(task->addStringAttributePublic(AAA, aaaVal));
119     ASSERT_TRUE(task->addStringAttributePublic(BBB, bbbVal));
120     const android::String8 CCC("ccc");
121     ASSERT_TRUE(!task->addStringAttributePublic(CCC, bbbVal));
122     ASSERT_TRUE(task->findStringAttributePublic(AAA, read));
123     ASSERT_TRUE(read == aaaVal);
124     ASSERT_TRUE(task->findStringAttributePublic(BBB, read));
125     ASSERT_TRUE(read == bbbVal);
126     const android::String8 VERSION("version");
127     const android::String8 NAME("name");
128     ASSERT_TRUE(!task->findStringAttributePublic(VERSION, read));
129     ASSERT_TRUE(!task->findStringAttributePublic(NAME, read));
130 }
131 
132 
133