2424
2525#include " TestIcon.h"
2626#include " shellanything/Icon.h"
27+ #include " shellanything/Context.h"
28+ #include " PropertyManager.h"
29+ #include " GlogUtils.h"
30+ #include " rapidassist/filesystem.h"
2731
2832namespace shellanything { namespace test
2933{
34+ int CountString (const std::string & text, const std::string & token)
35+ {
36+ int count = 0 ;
37+ size_t offset = 0 ;
38+ size_t pos = 0 ;
39+ do
40+ {
41+ pos = text.find (token, offset);
42+ if (pos != std::string::npos)
43+ {
44+ count++;
45+ offset = pos + 1 ;
46+ }
47+ } while (pos != std::string::npos);
48+ return count;
49+ }
50+
3051 // --------------------------------------------------------------------------------------------------
3152 void TestIcon::SetUp ()
3253 {
54+ // delete all previous log to be able to identify the current log file
55+ static const int MAX_AGE_SECONDS = -1 ;
56+ DeletePreviousLogs (MAX_AGE_SECONDS);
3357 }
3458 // --------------------------------------------------------------------------------------------------
3559 void TestIcon::TearDown ()
@@ -49,6 +73,93 @@ namespace shellanything { namespace test
4973 ASSERT_NE ( Icon::INVALID_ICON_INDEX, icon.GetIndex () );
5074 }
5175 // --------------------------------------------------------------------------------------------------
76+ TEST_F (TestIcon, testResolveMultipleFileExtensionIcon) // issue #98
77+ {
78+ // Solve icon for txt files
79+ Icon icon_txt;
80+ icon_txt.SetFileExtension (" txt" );
81+ icon_txt.ResolveFileExtensionIcon ();
82+ ASSERT_TRUE (icon_txt.GetFileExtension ().empty ());
83+ ASSERT_FALSE (icon_txt.GetPath ().empty ());
84+ ASSERT_NE (Icon::INVALID_ICON_INDEX, icon_txt.GetIndex ());
85+
86+ // get the separator for multiple values
87+ shellanything::PropertyManager & pmgr = shellanything::PropertyManager::GetInstance ();
88+ const std::string separator = pmgr.GetProperty (Context::MULTI_SELECTION_SEPARATOR_PROPERTY_NAME);
89+
90+ // build a list of multiple file extensions
91+ std::string file_extensions;
92+ file_extensions += " txt" ;
93+ file_extensions += separator;
94+ file_extensions += " xml" ;
95+ file_extensions += separator;
96+ file_extensions += " jpg" ;
97+ file_extensions += separator;
98+ file_extensions += " mp3" ;
99+ file_extensions += separator;
100+ file_extensions += " exe" ;
101+ file_extensions += separator;
102+ file_extensions += " dll" ;
103+ file_extensions += separator;
104+ file_extensions += " mp4" ;
105+
106+ Icon icon;
107+ icon.SetFileExtension (file_extensions);
108+
109+ // act
110+ icon.ResolveFileExtensionIcon ();
111+
112+ ASSERT_TRUE (icon.GetFileExtension ().empty ());
113+ ASSERT_EQ (icon_txt.GetPath (), icon.GetPath ());
114+ ASSERT_EQ (icon_txt.GetIndex (), icon.GetIndex ());
115+ }
116+ // --------------------------------------------------------------------------------------------------
117+ TEST_F (TestIcon, testMultipleResolveFailures) // issue #98
118+ {
119+ static const std::string UNKNOWN_FILE_EXTENSION = " 123456789" ;
120+
121+ Icon icon;
122+ icon.SetFileExtension (UNKNOWN_FILE_EXTENSION);
123+
124+ // act
125+ icon.ResolveFileExtensionIcon ();
126+
127+ // assert to unknown file type
128+ static const std::string UNKNOWN_ICON_FILE_PATH = " C:\\ Windows\\ system32\\ imageres.dll" ;
129+ static const int UNKNOWN_ICON_INDEX = 2 ;
130+ ASSERT_TRUE (icon.GetFileExtension ().empty ());
131+ ASSERT_EQ (UNKNOWN_ICON_FILE_PATH, icon.GetPath ());
132+ ASSERT_EQ (UNKNOWN_ICON_INDEX, icon.GetIndex ());
133+
134+ // act (issue #98)
135+ for (int i = 0 ; i < 500 ; i++)
136+ {
137+ // this should create multiple log entries if the feature is not properly implemented.
138+ Icon tmp_icon;
139+ tmp_icon.SetFileExtension (UNKNOWN_FILE_EXTENSION);
140+ tmp_icon.ResolveFileExtensionIcon ();
141+ }
142+
143+ // get log files content
144+ std::string log_dir = GetLogDirectory ();
145+ ra::strings::StringVector files;
146+ bool find_success = ra::filesystem::FindFiles (files, log_dir.c_str ());
147+ ASSERT_TRUE (find_success);
148+ for (size_t i = 0 ; i < files.size (); i++)
149+ {
150+ const std::string & path = files[i];
151+ if (IsLogFile (path))
152+ {
153+ std::string content;
154+ bool read = ra::filesystem::ReadTextFile (path, content);
155+ ASSERT_TRUE (read) << " Failed to read log file: " << path;
156+
157+ int count = CountString (content, UNKNOWN_FILE_EXTENSION);
158+ ASSERT_LE (count, 1 ) << " Log file '" << path << " ' contains " << count << " references to '" << UNKNOWN_FILE_EXTENSION << " '." ;
159+ }
160+ }
161+ }
162+ // --------------------------------------------------------------------------------------------------
52163
53164} // namespace test
54165} // namespace shellanything
0 commit comments