[Unittest] service_handler_unittests 41/37441/2
authorTomasz Iwanek <t.iwanek@samsung.com>
Mon, 23 Mar 2015 10:05:19 +0000 (11:05 +0100)
committerPawel Sikorski <p.sikorski@samsung.com>
Fri, 27 Mar 2015 09:32:10 +0000 (02:32 -0700)
This commit removes checks on GetManifestData()
as it ws used for multithreading purpose in crosswalk.

Change-Id: I2c3a19f834bb2af02f9319008ed813d427b2460c

CMakeLists.txt
src/parser/application_data.cc
src/parser/application_data.h
src/parser/manifest_util.cc
src/unit_tests/CMakeLists.txt
src/unit_tests/common/CMakeLists.txt [new file with mode: 0644]
src/unit_tests/common/scoped_testing_manifest_handler_registry.cc [new file with mode: 0644]
src/unit_tests/common/scoped_testing_manifest_handler_registry.h [new file with mode: 0644]
src/unit_tests/service_handler_unittest.cc [new file with mode: 0644]
src/unit_tests/widget_manifest_parser_manifest_handler_unittest.cc

index 256b275..3de066d 100644 (file)
@@ -30,6 +30,7 @@ SET(TARGET_WGT_BACKEND "wgt-backend")
 SET(TARGET_XPK_BACKEND "xpk-backend")
 SET(TARGET_NATIVE_BACKEND "native-backend")
 SET(TARGET_TPK_BACKEND "tpk-backend")
+SET(TARGET_UNITTEST_COMMON "common-unittests")
 
 ADD_DEFINITIONS("-Wall")
 ADD_DEFINITIONS("-Wextra")
index 75e9924..ec568d1 100644 (file)
@@ -82,7 +82,6 @@ std::string ApplicationData::GetBaseURLFromApplicationId(
 
 ApplicationData::ManifestData* ApplicationData::GetManifestData(
         const std::string& key) const {
-  assert(finished_parsing_manifest_);
   ManifestDataMap::const_iterator iter = manifest_data_.find(key);
   if (iter != manifest_data_.end())
     return iter->second.get();
@@ -91,7 +90,6 @@ ApplicationData::ManifestData* ApplicationData::GetManifestData(
 
 void ApplicationData::SetManifestData(const std::string& key,
     std::shared_ptr<ApplicationData::ManifestData> data) {
-  assert(!finished_parsing_manifest_);
   manifest_data_[key] = data;
 }
 
@@ -121,7 +119,6 @@ ApplicationData::ApplicationData(const bf::path& path,
     : manifest_version_(0),
       path_(path),
       manifest_(std::move(manifest)),
-      finished_parsing_manifest_(false),
       source_type_(source_type) {
   assert(path_.empty() || path_.is_absolute());
 }
@@ -138,7 +135,6 @@ bool ApplicationData::Init(const std::string& explicit_id,
     return false;
 
   application_url_ = ApplicationData::GetBaseURLFromApplicationId(ID());
-  finished_parsing_manifest_ = true;
   return true;
 }
 
index 929a899..e01e6d1 100644 (file)
@@ -160,13 +160,13 @@ class ApplicationData : public std::enable_shared_from_this<ApplicationData> {
   // Stored parsed manifest data.
   ManifestDataMap manifest_data_;
 
-  // Set to true at the end of InitValue when initialization is finished.
-  bool finished_parsing_manifest_;
-
   // The source the application was loaded from.
   SourceType source_type_;
 
-//  DISALLOW_COPY_AND_ASSIGN(ApplicationData);
+  friend std::shared_ptr<ApplicationData> CreateApplicationDataForTest(
+      std::unique_ptr<Manifest> manifest);
+
+  DISALLOW_COPY_AND_ASSIGN(ApplicationData);
 };
 
 }  // namespace parser
index beec06d..626b88a 100644 (file)
@@ -57,8 +57,8 @@ const char* kSingletonElements[] = {
   "content"
 };
 
-const char kIdPattern[] = "\\A[0-9a-zA-Z]{10}[.][0-9a-zA-Z]{1,52}\\z";
-const char kPackagePattern[] = "\\A[0-9a-zA-Z]{10}\\z";
+const char kIdPattern[] = "^[0-9a-zA-Z]{10}[.][0-9a-zA-Z]{1,52}$";
+const char kPackagePattern[] = "^[0-9a-zA-Z]{10}$";
 
 }  // namespace
 
index 9b46d00..bc88825 100644 (file)
@@ -1,5 +1,7 @@
 SET(PRJ_PATH ..)
 
+ADD_SUBDIRECTORY("common")
+
 SET(TESTS
   values_unittest
   manifest_unittest
@@ -8,6 +10,7 @@ SET(TESTS
   string_util_unittest
   widget_manifest_parser_unittest
   xml_parser_unittest
+  service_handler_unittest
 )
 
 INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../)
@@ -26,6 +29,8 @@ ADD_EXECUTABLE(widget_manifest_parser_unittest
                widget_manifest_parser_unittest.cc)
 ADD_EXECUTABLE(xml_parser_unittest
                xml_parser_unittest.cc ../xml_parser/xml_parser.cc)
+ADD_EXECUTABLE(service_handler_unittest
+               service_handler_unittest.cc)
 
 INSTALL(DIRECTORY test_samples/ DESTINATION ${SHAREDIR}/app-installers-ut/test_samples)
 
@@ -45,11 +50,12 @@ ENDFOREACH(test)
 # GTEST_MAIN_LIBRARIES is needed.
 target_link_libraries(values_unittest PUBLIC ${TARGET_LIBNAME_UTILS} ${GTEST_MAIN_LIBRARIES})
 target_link_libraries(manifest_unittest PUBLIC ${TARGET_LIBNAME_WIDGET_MANIFEST_PARSER} ${GTEST_MAIN_LIBRARIES})
-target_link_libraries(manifest_handler_unittest PUBLIC ${TARGET_LIBNAME_WIDGET_MANIFEST_PARSER} ${GTEST_MAIN_LIBRARIES})
+target_link_libraries(manifest_handler_unittest PUBLIC ${GTEST_MAIN_LIBRARIES} ${TARGET_UNITTEST_COMMON})
 target_link_libraries(manifest_util_unittest PUBLIC ${TARGET_LIBNAME_WIDGET_MANIFEST_PARSER} ${GTEST_MAIN_LIBRARIES})
 target_link_libraries(string_util_unittest PUBLIC ${TARGET_LIBNAME_UTILS} ${GTEST_MAIN_LIBRARIES})
 target_link_libraries(widget_manifest_parser_unittest PUBLIC ${TARGET_LIBNAME_WIDGET_MANIFEST_PARSER} ${GTEST_MAIN_LIBRARIES})
 target_link_libraries(xml_parser_unittest PUBLIC ${TARGET_LIBNAME_XML_PARSER} ${GTEST_MAIN_LIBRARIES})
+target_link_libraries(service_handler_unittest PUBLIC ${GTEST_MAIN_LIBRARIES} ${TARGET_UNITTEST_COMMON})
 
 FOREACH(test ${TESTS})
 INSTALL(TARGETS ${test} DESTINATION ${BINDIR}/app-installers-ut)
diff --git a/src/unit_tests/common/CMakeLists.txt b/src/unit_tests/common/CMakeLists.txt
new file mode 100644 (file)
index 0000000..49a3fc9
--- /dev/null
@@ -0,0 +1,15 @@
+# Target - sources
+SET(SRCS
+  scoped_testing_manifest_handler_registry.cc
+)
+# Target - definition
+ADD_LIBRARY(${TARGET_UNITTEST_COMMON} STATIC ${SRCS})
+# Target - includes
+TARGET_INCLUDE_DIRECTORIES(${TARGET_UNITTEST_COMMON} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../../")
+
+# Target in-package deps
+TARGET_LINK_LIBRARIES(${TARGET_UNITTEST_COMMON} PUBLIC ${TARGET_LIBNAME_WIDGET_MANIFEST_PARSER})
+
+# Extra
+SET_TARGET_PROPERTIES(${TARGET_UNITTEST_COMMON} PROPERTIES VERSION ${VERSION})
+SET_TARGET_PROPERTIES(${TARGET_UNITTEST_COMMON} PROPERTIES SOVERSION ${VERSION_MAJOR})
diff --git a/src/unit_tests/common/scoped_testing_manifest_handler_registry.cc b/src/unit_tests/common/scoped_testing_manifest_handler_registry.cc
new file mode 100644 (file)
index 0000000..3467b65
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+
+#include "unit_tests/common/scoped_testing_manifest_handler_registry.h"
+
+namespace common_installer {
+
+namespace parser {
+
+ScopedTestingManifestHandlerRegistry::ScopedTestingManifestHandlerRegistry(
+    const std::vector<ManifestHandler*>& handlers)
+    : registry_(new ManifestHandlerRegistry(handlers)),
+      prev_registry_(ManifestHandlerRegistry::GetInstance()) {
+  ManifestHandlerRegistry::SetInstanceForTesting(registry_);
+}
+
+ScopedTestingManifestHandlerRegistry::~ScopedTestingManifestHandlerRegistry() {
+  ManifestHandlerRegistry::SetInstanceForTesting(prev_registry_);
+}
+
+}  // namespace parser
+
+}  // namespace common_installer
diff --git a/src/unit_tests/common/scoped_testing_manifest_handler_registry.h b/src/unit_tests/common/scoped_testing_manifest_handler_registry.h
new file mode 100644 (file)
index 0000000..4bea82f
--- /dev/null
@@ -0,0 +1,30 @@
+// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef UNIT_TESTS_COMMON_SCOPED_TESTING_MANIFEST_HANDLER_REGISTRY_H_
+#define UNIT_TESTS_COMMON_SCOPED_TESTING_MANIFEST_HANDLER_REGISTRY_H_
+
+#include <vector>
+
+#include "parser/manifest_handler.h"
+
+namespace common_installer {
+
+namespace parser {
+
+class ScopedTestingManifestHandlerRegistry {
+ public:
+  ScopedTestingManifestHandlerRegistry(
+      const std::vector<ManifestHandler*>& handlers);
+  ~ScopedTestingManifestHandlerRegistry();
+
+  ManifestHandlerRegistry* registry_;
+  ManifestHandlerRegistry* prev_registry_;
+};
+
+}  // namespace parser
+
+}  // namespace common_installer
+
+#endif  // UNIT_TESTS_COMMON_SCOPED_TESTING_MANIFEST_HANDLER_REGISTRY_H_
diff --git a/src/unit_tests/service_handler_unittest.cc b/src/unit_tests/service_handler_unittest.cc
new file mode 100644 (file)
index 0000000..f453a91
--- /dev/null
@@ -0,0 +1,236 @@
+// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache-2.0 license that can be
+// found in the LICENSE file.
+
+#include <boost/filesystem/path.hpp>
+#include <gtest/gtest.h>
+
+#include <vector>
+
+#include "parser/application_manifest_constants.h"
+#include "parser/manifest.h"
+#include "parser/manifest_handler.h"
+#include "parser/manifest_handlers/service_handler.h"
+#include "unit_tests/common/scoped_testing_manifest_handler_registry.h"
+
+namespace bf = boost::filesystem;
+
+namespace {
+
+const char kServiceKey[] = "service";
+
+}  // namespace
+
+namespace common_installer {
+
+namespace parser {
+
+std::shared_ptr<ApplicationData> CreateApplicationDataForTest(
+    std::unique_ptr<Manifest> manifest) {
+  return std::shared_ptr<ApplicationData>(
+      new ApplicationData(bf::path(), ApplicationData::INTERNAL,
+          std::move(manifest)));
+}
+
+namespace keys = application_widget_keys;
+
+class ServiceHandlerTest : public testing::Test {
+ public:
+  void SetUp() override {
+    std::vector<ManifestHandler*> handlers;
+    handlers.push_back(new ServiceHandler());
+    test_registry_.reset(new ScopedTestingManifestHandlerRegistry(handlers));
+  }
+ protected:
+  std::unique_ptr<ScopedTestingManifestHandlerRegistry> test_registry_;
+};
+
+TEST_F(ServiceHandlerTest, NoServiceEntry) {
+  // Set test values
+  std::unique_ptr<utils::DictionaryValue> value(new utils::DictionaryValue());
+  std::unique_ptr<Manifest> manifest(new Manifest(std::move(value)));
+  // Create application data
+  std::string error;
+  std::shared_ptr<ApplicationData> app_data =
+      CreateApplicationDataForTest(std::move(manifest));
+  ManifestHandlerRegistry* registry = ManifestHandlerRegistry::GetInstance();
+  // Check correctness
+  ASSERT_TRUE(!!app_data);
+  ASSERT_TRUE(registry);
+  ASSERT_TRUE(registry->ParseAppManifest(app_data, &error));
+  ASSERT_TRUE(registry->ValidateAppManifest(app_data, &error));
+}
+
+TEST_F(ServiceHandlerTest, SingleServiceEntryDefault) {
+  // Set test values
+  std::unique_ptr<utils::DictionaryValue> value(new utils::DictionaryValue());
+  std::unique_ptr<utils::DictionaryValue> widget(new utils::DictionaryValue());
+  std::unique_ptr<utils::DictionaryValue> service(new utils::DictionaryValue());
+  service->SetString(keys::kTizenServiceIdKey, "correct001.appId");
+  widget->Set(kServiceKey, service.release());
+  value->Set(keys::kWidgetKey, widget.release());
+  std::unique_ptr<Manifest> manifest(new Manifest(std::move(value)));
+  // Create application data
+  std::string error;
+  std::shared_ptr<ApplicationData> app_data =
+      CreateApplicationDataForTest(std::move(manifest));
+  ManifestHandlerRegistry* registry = ManifestHandlerRegistry::GetInstance();
+  // Check correctness
+  ASSERT_TRUE(!!app_data);
+  ASSERT_TRUE(registry);
+  ASSERT_TRUE(registry->ParseAppManifest(app_data, &error));
+  ASSERT_TRUE(registry->ValidateAppManifest(app_data, &error));
+  ASSERT_TRUE(app_data->GetManifestData(keys::kTizenServiceKey));
+  ServiceList* service_list =
+      dynamic_cast<ServiceList*>(
+          app_data->GetManifestData(keys::kTizenServiceKey));
+  ASSERT_TRUE(service_list);
+  ASSERT_EQ(service_list->services.size(), 1);
+  ASSERT_EQ(service_list->services[0].id(), "correct001.appId");
+  ASSERT_EQ(service_list->services[0].auto_restart(), false);
+  ASSERT_EQ(service_list->services[0].on_boot(), false);
+}
+
+TEST_F(ServiceHandlerTest, SingleServiceEntryOnBootOn) {
+  // Set test values
+  std::unique_ptr<utils::DictionaryValue> value(new utils::DictionaryValue());
+  std::unique_ptr<utils::DictionaryValue> widget(new utils::DictionaryValue());
+  std::unique_ptr<utils::DictionaryValue> service(new utils::DictionaryValue());
+  service->SetString(keys::kTizenServiceIdKey, "correct002.appId");
+  service->SetString(keys::kTizenServiceOnBootKey, "true");
+  widget->Set(kServiceKey, service.release());
+  value->Set(keys::kWidgetKey, widget.release());
+  std::unique_ptr<Manifest> manifest(new Manifest(std::move(value)));
+  // Create application data
+  std::string error;
+  std::shared_ptr<ApplicationData> app_data =
+      CreateApplicationDataForTest(std::move(manifest));
+  ManifestHandlerRegistry* registry = ManifestHandlerRegistry::GetInstance();
+  // Check correctness
+  ASSERT_TRUE(!!app_data);
+  ASSERT_TRUE(registry);
+  ASSERT_TRUE(registry->ParseAppManifest(app_data, &error));
+  ASSERT_TRUE(registry->ValidateAppManifest(app_data, &error));
+  ASSERT_TRUE(app_data->GetManifestData(keys::kTizenServiceKey));
+  ServiceList* service_list =
+      dynamic_cast<ServiceList*>(
+          app_data->GetManifestData(keys::kTizenServiceKey));
+  ASSERT_TRUE(service_list);
+  ASSERT_EQ(service_list->services.size(), 1);
+  ASSERT_EQ(service_list->services[0].id(), "correct002.appId");
+  ASSERT_EQ(service_list->services[0].auto_restart(), false);
+  ASSERT_EQ(service_list->services[0].on_boot(), true);
+}
+
+TEST_F(ServiceHandlerTest, SingleServiceEntryAutoRestartOn) {
+  // Set test values
+  std::unique_ptr<utils::DictionaryValue> value(new utils::DictionaryValue());
+  std::unique_ptr<utils::DictionaryValue> widget(new utils::DictionaryValue());
+  std::unique_ptr<utils::DictionaryValue> service(new utils::DictionaryValue());
+  service->SetString(keys::kTizenServiceIdKey, "correct003.appId");
+  service->SetString(keys::kTizenServiceOnBootKey, "false");
+  service->SetString(keys::kTizenServiceAutoRestartKey, "true");
+  widget->Set(kServiceKey, service.release());
+  value->Set(keys::kWidgetKey, widget.release());
+  std::unique_ptr<Manifest> manifest(new Manifest(std::move(value)));
+  // Create application data
+  std::string error;
+  std::shared_ptr<ApplicationData> app_data =
+      CreateApplicationDataForTest(std::move(manifest));
+  ManifestHandlerRegistry* registry = ManifestHandlerRegistry::GetInstance();
+  // Check correctness
+  ASSERT_TRUE(!!app_data);
+  ASSERT_TRUE(registry);
+  ASSERT_TRUE(registry->ParseAppManifest(app_data, &error));
+  ASSERT_TRUE(registry->ValidateAppManifest(app_data, &error));
+  ASSERT_TRUE(app_data->GetManifestData(keys::kTizenServiceKey));
+  ServiceList* service_list =
+      dynamic_cast<ServiceList*>(
+          app_data->GetManifestData(keys::kTizenServiceKey));
+  ASSERT_TRUE(service_list);
+  ASSERT_EQ(service_list->services.size(), 1);
+  ASSERT_EQ(service_list->services[0].id(), "correct003.appId");
+  ASSERT_EQ(service_list->services[0].auto_restart(), true);
+  ASSERT_EQ(service_list->services[0].on_boot(), false);
+}
+
+TEST_F(ServiceHandlerTest, SingleServiceEntryWrongId) {
+  // Set test values
+  std::unique_ptr<utils::DictionaryValue> value(new utils::DictionaryValue());
+  std::unique_ptr<utils::DictionaryValue> widget(new utils::DictionaryValue());
+  std::unique_ptr<utils::DictionaryValue> service(new utils::DictionaryValue());
+  service->SetString(keys::kTizenServiceIdKey, "wrongid.appId");
+  widget->Set(kServiceKey, service.release());
+  value->Set(keys::kWidgetKey, widget.release());
+  std::unique_ptr<Manifest> manifest(new Manifest(std::move(value)));
+  // Create application data
+  std::string error;
+  std::shared_ptr<ApplicationData> app_data =
+      CreateApplicationDataForTest(std::move(manifest));
+  ManifestHandlerRegistry* registry = ManifestHandlerRegistry::GetInstance();
+  // Check correctness
+  ASSERT_TRUE(!!app_data);
+  ASSERT_TRUE(registry);
+  ASSERT_TRUE(registry->ParseAppManifest(app_data, &error));
+  ASSERT_FALSE(registry->ValidateAppManifest(app_data, &error));
+}
+
+TEST_F(ServiceHandlerTest, SingleServiceEntryIdTypeMismatch) {
+  // Set test values
+  std::unique_ptr<utils::DictionaryValue> value(new utils::DictionaryValue());
+  std::unique_ptr<utils::DictionaryValue> widget(new utils::DictionaryValue());
+  std::unique_ptr<utils::DictionaryValue> service(new utils::DictionaryValue());
+  service->SetInteger(keys::kTizenServiceIdKey, 1410);
+  widget->Set(kServiceKey, service.release());
+  value->Set(keys::kWidgetKey, widget.release());
+  std::unique_ptr<Manifest> manifest(new Manifest(std::move(value)));
+  // Create application data
+  std::string error;
+  std::shared_ptr<ApplicationData> app_data =
+      CreateApplicationDataForTest(std::move(manifest));
+  ManifestHandlerRegistry* registry = ManifestHandlerRegistry::GetInstance();
+  // Check correctness
+  ASSERT_TRUE(!!app_data);
+  ASSERT_TRUE(registry);
+  ASSERT_FALSE(registry->ParseAppManifest(app_data, &error));
+}
+
+TEST_F(ServiceHandlerTest, MultipleServiceEntry) {
+  // Set test values
+  std::unique_ptr<utils::DictionaryValue> value(new utils::DictionaryValue());
+  std::unique_ptr<utils::DictionaryValue> widget(new utils::DictionaryValue());
+  std::unique_ptr<utils::ListValue> list(new utils::ListValue());
+  std::unique_ptr<utils::DictionaryValue> service1(
+      new utils::DictionaryValue());
+  std::unique_ptr<utils::DictionaryValue> service2(
+      new utils::DictionaryValue());
+  service1->SetString(keys::kTizenServiceIdKey, "correct004.appId");
+  service2->SetString(keys::kTizenServiceIdKey, "correct005.appId");
+  list->Append(service1.release());
+  list->Append(service2.release());
+  widget->Set(kServiceKey, list.release());
+  value->Set(keys::kWidgetKey, widget.release());
+  std::unique_ptr<Manifest> manifest(new Manifest(std::move(value)));
+  // Create application data
+  std::string error;
+  std::shared_ptr<ApplicationData> app_data =
+      CreateApplicationDataForTest(std::move(manifest));
+  ManifestHandlerRegistry* registry = ManifestHandlerRegistry::GetInstance();
+  // Check correctness
+  ASSERT_TRUE(!!app_data);
+  ASSERT_TRUE(registry);
+  ASSERT_TRUE(registry->ParseAppManifest(app_data, &error));
+  ASSERT_TRUE(registry->ValidateAppManifest(app_data, &error));
+  ASSERT_TRUE(app_data->GetManifestData(keys::kTizenServiceKey));
+  ServiceList* service_list =
+      dynamic_cast<ServiceList*>(
+          app_data->GetManifestData(keys::kTizenServiceKey));
+  ASSERT_TRUE(service_list);
+  ASSERT_EQ(service_list->services.size(), 2);
+  ASSERT_EQ(service_list->services[0].id(), "correct004.appId");
+  ASSERT_EQ(service_list->services[1].id(), "correct005.appId");
+}
+
+}  // namespace parser
+}  // namespace common_installer
+
index 7923c9a..5b1beff 100644 (file)
@@ -12,6 +12,7 @@
 #include "utils/values.h"
 #include "parser/application_data.h"
 #include "parser/manifest_handler.h"
+#include "unit_tests/common/scoped_testing_manifest_handler_registry.h"
 
 namespace bf = boost::filesystem;
 
@@ -26,23 +27,6 @@ std::vector<std::string> SingleKey(const std::string& key) {
 
 }  // namespace
 
-class ScopedTestingManifestHandlerRegistry {
- public:
-  ScopedTestingManifestHandlerRegistry(
-      const std::vector<ManifestHandler*>& handlers)
-      : registry_(new ManifestHandlerRegistry(handlers)),
-        prev_registry_(ManifestHandlerRegistry::GetInstance()) {
-    ManifestHandlerRegistry::SetInstanceForTesting(registry_);
-  }
-
-  ~ScopedTestingManifestHandlerRegistry() {
-    ManifestHandlerRegistry::SetInstanceForTesting(prev_registry_);
-  }
-
-  ManifestHandlerRegistry* registry_;
-  ManifestHandlerRegistry* prev_registry_;
-};
-
 class ManifestHandlerTest : public testing::Test {
  public:
   class ParsingWatcher {