Fix auto file key generation 96/239896/4
authorSangyoon Jang <jeremy.jang@samsung.com>
Thu, 30 Jul 2020 10:19:46 +0000 (19:19 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Mon, 3 Aug 2020 04:37:30 +0000 (13:37 +0900)
Iterates shared/res directory and find all resource files to add
auto-generated file key.

Change-Id: Iaacc4cafc3dab37a44a6856e7786667024cba038
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
CMakeLists.txt
packaging/tizen-theme-manager.spec
src/theme_plugin/CMakeLists.txt
src/theme_plugin/theme_info_builder.cc
src/theme_plugin/theme_info_builder.h
src/theme_plugin/theme_parser.cc
test/unit_tests/test_samples/shared/res/picture.png [new file with mode: 0644]
test/unit_tests/test_samples/shared/res/test [new file with mode: 0644]
test/unit_tests/test_samples/test_theme.json
test/unit_tests/test_theme_parser.cc

index 2b6a1ba56618c74a65b255c909ce7b7e99dff47d..8f818faaaa3f3589996bfa60416d483b16d8f4e3 100644 (file)
@@ -42,5 +42,7 @@ PKG_CHECK_MODULES(PKGMGR_INSTALLER_DEPS REQUIRED pkgmgr-installer)
 PKG_CHECK_MODULES(SQLITE_DEPS REQUIRED sqlite3)
 PKG_CHECK_MODULES(GOBJECT_DEPS REQUIRED gobject-2.0)
 
+FIND_PACKAGE(Boost REQUIRED COMPONENTS filesystem)
+
 ADD_SUBDIRECTORY(src)
 ADD_SUBDIRECTORY(test)
index a0d9f260000ed69e09b34c445dd3b2104a3bb9bc..b13415a2ed92098c4e9623decc2bea67e196bd0a 100644 (file)
@@ -8,6 +8,7 @@ Source0:    %{name}-%{version}.tar.gz
 Source1001: %{name}.manifest
 Source1002: %{name}.service
 Source1003: %{name}.conf
+BuildRequires:  boost-devel
 BuildRequires:  cmake
 BuildRequires:  pkgconfig(bundle)
 BuildRequires:  pkgconfig(dlog)
index fe12d965c89e2f1a583cbaeca51f2bc66854b083..e99335a4cfbe7e946322a73a137ed1ec757cc448 100644 (file)
@@ -5,6 +5,7 @@ TARGET_LINK_LIBRARIES(${TARGET_TIZEN_THEME_PLUGIN} PRIVATE ${TARGET_TIZEN_THEME}
 TARGET_INCLUDE_DIRECTORIES(${TARGET_TIZEN_THEME_PLUGIN} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}../)
 
 APPLY_PKG_CONFIG(${TARGET_TIZEN_THEME_PLUGIN} PUBLIC
+  Boost
   GLIB_DEPS
   JSONCPP_DEPS
   PKGMGR_INFO_DEPS
index d1331bd13ce96df82b6131e27c6d556e358333e2..66a87e091f4785038aabde53b95f0597a18885aa 100644 (file)
@@ -7,8 +7,12 @@
 #include <algorithm>
 #include <string>
 
+#include <boost/filesystem.hpp>
+
 #include "theme/utils/logging.h"
 
+namespace bf = boost::filesystem;
+
 namespace {
 
 const char kThemeFilesPrefix[] = "/theme/files";
@@ -43,7 +47,25 @@ loader::ThemeInfo ThemeInfoBuilder::Build() {
   return loader::ThemeInfo(bundle_);
 }
 
-void ThemeInfoBuilder::GenerateFileKey() {
+void ThemeInfoBuilder::GenerateFileKey(const std::string& root) {
+  for (bf::recursive_directory_iterator it(root);
+      it != bf::recursive_directory_iterator(); ++it) {
+    if (!bf::is_regular_file(it->path()))
+      continue;
+
+    if (!StartsWith(it->path().string(), root))
+      continue;
+
+    // root.size() -1 to remove trailing '/' charater
+    std::string filepath = it->path().string().substr(root.size() - 1);
+    std::string key = kThemeFilesPrefix + filepath;
+    bundle_.Add(key, it->path().string());
+  }
+
+  GenerateFileKeyWithoutExtension();
+}
+
+void ThemeInfoBuilder::GenerateFileKeyWithoutExtension() {
   std::vector<tizen_base::Bundle::KeyInfo> keyinfos = bundle_.GetKeys();
   for (const auto& keyinfo : keyinfos) {
     std::string key = keyinfo.GetName();
@@ -55,7 +77,6 @@ void ThemeInfoBuilder::GenerateFileKey() {
       continue;
 
     std::string newkey = key.substr(0, dot_pos);
-    LOG(DEBUG) << "Adding generated key: " << newkey;
     bundle_.Add(newkey, bundle_.GetString(key));
   }
 }
index 2099dc4a82cfb30a3c99cbab6ee9e1ddad54f7cd..bb99bb16adc4cbfab6a14960682a91731a3752a1 100644 (file)
@@ -23,7 +23,8 @@ class ThemeInfoBuilder {
   ThemeInfoBuilder& PutStringArray(const std::string& key,
       const std::vector<std::string>& value);
   loader::ThemeInfo Build();
-  void GenerateFileKey();
+  void GenerateFileKey(const std::string& root);
+  void GenerateFileKeyWithoutExtension();
 
  private:
   tizen_base::Bundle bundle_;
index aaac347b7dcb39228995c529b8fbdfefa59401a2..7b66b420e6f0cabd7064640720eee07e26f93252 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "theme_plugin/theme_parser.h"
 
+#include <pkgmgr-info.h>
 #include <sys/types.h>
 
 #include <fstream>
 #include "theme/utils/logging.h"
 #include "theme_plugin/theme_info_builder.h"
 
+namespace {
+
+std::string GetFileKeyRootPath(const char* pkgid) {
+  pkgmgrinfo_pkginfo_h handle;
+  int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
+  if (ret != PMINFO_R_OK)
+    return {};
+  char *root_path;
+  ret = pkgmgrinfo_pkginfo_get_root_path(handle, &root_path);
+  if (ret != PMINFO_R_OK) {
+    pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+    return {};
+  }
+  std::string path(root_path);
+  path += "/shared/res/";
+  pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+  return path;
+}
+
+}  // namespace
+
 namespace ttm {
 namespace plugin {
 
@@ -100,7 +122,7 @@ loader::ThemeInfo ThemeParser::Inflate(const std::string id,
   }
 
   if (auto_file_key_gen_)
-    builder.GenerateFileKey();
+    builder.GenerateFileKey(GetFileKeyRootPath(pkgid.c_str()));
 
   return builder.Build();
 }
diff --git a/test/unit_tests/test_samples/shared/res/picture.png b/test/unit_tests/test_samples/shared/res/picture.png
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test/unit_tests/test_samples/shared/res/test b/test/unit_tests/test_samples/shared/res/test
new file mode 100644 (file)
index 0000000..e69de29
index 8ac0120320de9dbc927324a2dab506f94db96e73..ee49513967739fa22a5d587eba6c496971b35c73 100644 (file)
                 },
                 "keyboard": {
                         "keypad_bg_color": "#050a28"
-                },
-                "files": {
-                        "shared/res/image1.png": "shared/res/image1.png",
-                        "shared/res/image2.svg": "shared/res/image2.svg"
                 }
         }
 }
index 40fc2da8081887c243325d5b5be01427cf429ff3..c572e38606e8fadf1c69e2d4adce3431630fe4a1 100644 (file)
 
 #include <gtest/gtest.h>
 
+#include <string>
+#include <vector>
+
 #include "theme/loader/theme_info.h"
 #include "theme_plugin/theme_parser.h"
+#include "unit_tests/mock/gio_mock.h"
+#include "unit_tests/mock/pkgmgr_info_mock.h"
+#include "unit_tests/mock/test_fixture.h"
 
-#include <string>
-#include <vector>
+using ::testing::_;
+using ::testing::Return;
+using ::testing::SetArgPointee;
+
+class Mocks : public ::testing::NiceMock<GioMock>,
+              public ::testing::NiceMock<PkgmgrInfoMock> {};
 
-class ThemeParserTest : public testing::Test {
+class ThemeParserTest : public TestFixture {
  public:
-  virtual ~ThemeParserTest() {}
+  ThemeParserTest() : TestFixture(std::make_unique<Mocks>()) {}
 
   virtual void SetUp() {
   }
@@ -34,6 +44,13 @@ class ThemeParserTest : public testing::Test {
 };
 
 TEST_F(ThemeParserTest, Inflate) {
+  char root_path[] = "test_samples";
+  EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
+      pkgmgrinfo_pkginfo_get_root_path(_, _)).
+          WillOnce(DoAll(
+                  SetArgPointee<1>(const_cast<char*>(root_path)),
+                  Return(PMINFO_R_OK)));
+
   ttm::plugin::ThemeParser parser("test_samples/test_theme.json", true);
   auto info = parser.Inflate("testid", "testpkgid", 5001, true);
   EXPECT_EQ(info.GetId(), "testid");
@@ -63,14 +80,10 @@ TEST_F(ThemeParserTest, Inflate) {
       "https://www.download.gogowatch/get");
   EXPECT_EQ(info.GetStringArray("/theme/preview"),
       std::vector<std::string>({"GOGO_Preview.png"}));
-  EXPECT_EQ(info.GetString("/theme/files/shared/res/image1.png"),
-      "shared/res/image1.png");
-  EXPECT_EQ(info.GetString("/theme/files/shared/res/image1"),
-      "shared/res/image1.png");
-  EXPECT_EQ(info.GetString("/theme/files/shared/res/image2.svg"),
-      "shared/res/image2.svg");
-  EXPECT_EQ(info.GetString("/theme/files/shared/res/image2"),
-      "shared/res/image2.svg");
+  EXPECT_EQ(info.GetString("/theme/files/test"),
+      "test_samples/shared/res/test");
+  EXPECT_EQ(info.GetString("/theme/files/picture"),
+      "test_samples/shared/res/picture.png");
 }
 
 TEST_F(ThemeParserTest, MultidimensionalArray) {