1 // Copyright 2014-2016 Samsung Electronics. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * @file utc_blink_ewk_base.h
7 * @author Kamil Klimek <k.klimek@partner.samsung.com>
9 * @brief A header file to be used by all unit test cases for Chromium EFL API
11 * This header file contains a common content for unit test cases, i.e.:
12 * it includes useful header files, defines shared object pointers,
13 * useful macros, functions and enumeration types.
17 #ifndef UTC_BLINK_EWK_BASE_H
18 #define UTC_BLINK_EWK_BASE_H
20 /* google test header */
21 #include "gtest/gtest.h"
23 #include <Ecore_Evas.h>
27 #include <Elementary.h>
28 #include <glib-object.h>
36 #include "ewk_auth_challenge.h"
37 #include "ewk_certificate.h"
38 #include "ewk_console_message.h"
39 #include "ewk_context.h"
40 #include "ewk_context_menu.h"
41 #include "ewk_cookie_manager.h"
42 #include "ewk_custom_handlers.h"
43 #include "ewk_enums.h"
44 #include "ewk_error.h"
45 #include "ewk_geolocation.h"
48 #include "ewk_notification.h"
49 #include "ewk_policy_decision.h"
50 #include "ewk_settings.h"
51 #include "ewk_touch.h"
52 #include "ewk_user_media.h"
54 #include "ewk_hit_test.h"
55 #include "ewk_text_style.h"
56 #include "ewk_web_application_icon_data.h"
58 /* Definitions and macros */
59 #define DEFAULT_WIDTH_OF_WINDOW 480
60 #define DEFAULT_HEIGHT_OF_WINDOW 800
62 /* Compatibility macros only. Please use gtest macros instead. This will be removed. */
63 #define utc_check_true(condition) EXPECT_TRUE(condition)
64 #define utc_check_false(condition) EXPECT_FALSE(condition)
65 #define utc_check_eq(value, needed) EXPECT_EQ(value, needed)
66 #define utc_check_ne(value, needed) EXPECT_NE(value, needed)
67 #define utc_check_gt(value, needed) EXPECT_GT(value, needed)
68 #define utc_check_ge(value, needed) EXPECT_GE(value, needed)
69 #define utc_check_lt(value, needed) EXPECT_LT(value, needed)
70 #define utc_check_le(value, needed) EXPECT_LE(value, needed)
71 #define utc_check_str_eq(value, needed) EXPECT_STREQ(value, needed)
72 #define utc_check_str_ne(value, needed) EXPECT_STRNE(value, needed)
73 #define utc_assert_eq(value, needed) ASSERT_EQ(value, needed)
74 #define utc_assert_ne(value, needed) ASSERT_NE(value, needed)
75 #define utc_assert_gt(value, needed) ASSERT_GT(value, needed)
76 #define utc_assert_ge(value, needed) ASSERT_GE(value, needed)
77 #define utc_assert_lt(value, needed) ASSERT_LT(value, needed)
78 #define utc_assert_le(value, needed) ASSERT_LE(value, needed)
79 #define utc_assert_str_eq(value, needed) ASSERT_STREQ(value, needed)
80 #define utc_assert_str_ne(value, needed) ASSERT_STRNE(value, needed)
81 #define utc_pass() SUCCEED()
82 #define utc_fail() FAIL()
85 typedef int UtcLogSeverity;
86 const UtcLogSeverity UTC_LOG_DEBUG = 0;
87 const UtcLogSeverity UTC_LOG_INFO = 1;
88 const UtcLogSeverity UTC_LOG_WARNING = 2;
89 const UtcLogSeverity UTC_LOG_ERROR = 3;
90 void utc_log(UtcLogSeverity severity, const char* msg, ...);
92 #define utc_debug(msg, ...) utc_log(UTC_LOG_DEBUG, msg "\n", ##__VA_ARGS__)
93 #define utc_info(msg, ...) utc_log(UTC_LOG_INFO, msg "\n", ##__VA_ARGS__)
94 #define utc_warning(msg, ...) utc_log(UTC_LOG_WARNING, msg "\n", ##__VA_ARGS__)
95 #define utc_error(msg, ...) utc_log(UTC_LOG_ERROR, msg "\n", ##__VA_ARGS__)
97 #define utc_message utc_warning
99 class utc_blink_ewk_base: public ::testing::Test
102 enum MainLoopResult {
110 class evas_object_smart_callback_auto
113 evas_object_smart_callback_auto(Evas_Object* obj, const char* event, Evas_Smart_Cb func, const void* data)
115 , event_(eina_stringshare_add(event))
118 evas_object_smart_callback_add(obj, event, func, data);
121 ~evas_object_smart_callback_auto()
123 evas_object_smart_callback_del(obj_, event_, func_);
124 eina_stringshare_del(event_);
129 Eina_Stringshare* event_;
133 utc_blink_ewk_base();
134 ~utc_blink_ewk_base() override;
136 void SetLogJavascript(bool enabled) { log_javascript = enabled; }
139 * Checks if given Evas_Object contains image pointed by source
141 * @param [in] image_object Evas_Object containing image
142 * @param [in] resource relative path to the resource. This method will expand it to absolute path.
143 * @param [in] pixel_fuzziness maximum allowed pixel difference between resource and image_object.
144 * image_object may be created from different image decoder than resource
145 * will be. It may cause little differences in pixels. Difference is calculated
146 * by summing absolute values of differences between each pixel color. e.g.:
147 * ARGB(123,231,5,1) vs ARGB(125,231,5,1) gives difference of 2 points
148 * ARGB(123,231,5,1) vs ARGB(124,231,6,1) gives difference of 2 points
149 * ARGB(123,231,5,1) vs ARGB(124,231,5,1) gives difference of 1 point:q
151 * @return true if Evas_Image produced from path is equal to given Evas_Image (memory compare)
153 bool CompareEvasImageWithResource(Evas_Object* image_object, const char* resource, int pixel_fuzziness) const;
155 // Adds allow-file-access-from-files param to Chromium.
156 // Must be called within PreSetUp to be taken into account.
157 void AllowFileAccessFromFiles();
160 * Generates proper path for resource file
162 * @param [in] resource_path resource file subpath
164 * @retval full resource path
166 std::string GetResourcePath(const char* resource_path) const;
169 * Generates proper url for resource file
171 * @param [in] resource_path resource file subpath
173 * @retval full resource url including file://
175 std::string GetResourceUrl(const char* resource_path) const;
178 * Starts main application loop.
180 * @param [in] max_time main loop timeout
182 * @retval MainLoopResult::NoOp if event loop is already running.
183 * MainLoopResult::Success if event loop was stopped normally.
184 * MainLoopResult::Failure if stopped abnormally.
185 * MainLoopResult::LoadFailure if "load,error" smart callback was invoked.
186 * MainLoopResult::Timeout if max_time was reached.
188 MainLoopResult EventLoopStart(double max_time = 30.0);
191 * Waits given time on main application loop.
193 * @params [in] time time to wait
195 bool EventLoopWait(double time);
198 * Stop main application loop.
200 * @param [in] result result to be returned by EventLoopStart.
201 * if result is MainLoopResult::NoOp this method will do nothing.
203 * @retval true if this call stopped event loop, false otherwise.
205 * @note This metod may be called multiple times, but only first call will change
206 * event loop result. All other calls will do nothing and return false.
208 bool EventLoopStop(MainLoopResult result);
211 * Override this method if you need to peform some extra
212 * initialization before initializing ewk.
214 virtual void PreSetUp() {}
217 * Override this method if you need to perform some extra
218 * initialization after initializing ewk.
220 virtual void PostSetUp() {}
223 * Override this method if you need to perform some extra
224 * deinitialization before deinitializng ewk.
226 virtual void PreTearDown() {}
229 * Override this method if you need to perform some extra
230 * deinitialization after deinitialzing ewk.
232 virtual void PostTearDown() {}
235 * Called when main application loop timeouted.
237 * @retval true if timeout was handled by method, false otherwise.
238 * If method returns false then main application event loop will be stopped with error.
240 virtual bool TimeOut();
243 * Override this method if you need to handle "load,finished" ewk callback.
245 virtual void LoadFinished(Evas_Object* webview) {}
248 * Override this method if you need to handle "load,started" ewk callback.
250 virtual void LoadStarted(Evas_Object* webview) {}
253 * Override this method if you need to handle "load,error" ewk callback.
255 * @retval true if "load,error" was handled by method, false otherwise.
256 * If method returns false then main application event loop will be stopped with error.
258 virtual bool LoadError(Evas_Object* webview, Ewk_Error* error);
261 * Override this method if you need to handle "load,progress" ewk callback.
263 virtual void LoadProgress(Evas_Object* webview, double progress) {}
266 * Override this method if you need to handle "console,message" ewk callback.
268 virtual void ConsoleMessage(Evas_Object* webview, const Ewk_Console_Message*);
271 * Performs unit test setup. It calls PreSetUp before default initialization
272 * and PostSetUp after default initialization.
274 * @note This method should not be overriden.
279 * Sets a (test) job that should be performed during main loop.
280 * @param [in] job_function name of function (static void method of test
282 * that will be called from main loop
284 * @note If exit from main loop depends on code included in body of callback
285 * that could be called before program enters main loop
286 * there could be a race codition.
287 * Example: exit from main loop is a result of call to
288 * ewk_view_text_selection_clear.
291 * > ASSERT_EQ(EINA_TRUE,
292 * ewk_view_text_selection_clear(GetEwkWebView()));
293 * > ASSERT_EQ(Success, EventLoopStart());
295 * It would be better to:
296 * 1. define a additional callback, and move there testing code
297 * 2. set additional callback with SetTestJob to be called from main
299 * 3. result of code called in callback store in auxiliary field of
304 * in class definition:
305 * > static void job_selection_clear(utc_blink_ewk_base *data)
307 * > utc_blink_cb_textselection_mode *owner =
308 * > static_cast<utc_blink_cb_textselection_mode *>(data);
309 * > owner->result_text_selection_clear =
310 * ewk_view_text_selection_clear(owner->GetEwkWebView());
315 * > SetTestJob(utc_blink_cb_textselection_mode::job_selection_clear);
316 * > ASSERT_EQ(Success, EventLoopStart());
317 * > ASSERT_EQ(EINA_TRUE, result_text_selection_clear);
319 void SetTestJob(utc_blink_ewk_job_cb_t job_function)
321 test_job_function = job_function;
325 * Performs unit test deinitialization. It calls PreTearDown before default deinitialization
326 * and PostTearDown after default deinitialization.
328 * @note This method should not be overriden.
330 void TearDown() final;
332 template<class T> static void OwnerFromVoid(void *owner, T** out)
335 *out = static_cast<T*>(owner);
339 template<typename T> static Evas_Smart_Cb ToSmartCallback(T proc)
341 return reinterpret_cast<Evas_Smart_Cb>(proc);
344 inline Evas_Object* GetEwkWebView() const { return ewk_webview; }
345 inline Evas_Object* GetEwkWindow() const { return ewk_window; }
346 inline Evas* GetEwkEvas() const { return ewk_evas; }
348 void FeedMouseClick(int x, int y, int button);
352 * Default "load,started" event callback, will call LoadStarted method
356 static void load_started_cb(void* data, Evas_Object* webview, void* event_info);
359 * Default "load,finished" event callback, will call LoadFinish method
363 static void load_finished_cb(void* data, Evas_Object* webview, void* event_info);
366 * Default "load,error" event callback, will call LoadError method
370 static void load_error_cb(void* data, Evas_Object* webview, void* event_info);
373 * Default "load,progress" event callback, will call LoadProgress method
377 static void load_progress_cb(void* data, Evas_Object* webview, void* event_info);
380 * Default "console,message" event callback, will call ConsoleMessage method
382 static void console_message_cb(utc_blink_ewk_base*, Evas_Object*, Ewk_Console_Message*);
385 * Default event loop timeout callback, will call TimeOut method
389 static Eina_Bool timeout_cb(void *data);
392 * EventLoopWait timeotut callback, will stop event loop
394 static Eina_Bool timeout_cb_event_loop_wait(void *data);
398 * Initializes objects used in the test
400 * This method is always called in SetUp method.
406 * Deinitializes and frees resources used during test.
408 * This method is always called in TearDown method.
414 Ecore_Timer *timeout;
415 bool main_loop_running;
416 MainLoopResult main_loop_result;
420 Evas_Object* ewk_window;
422 Evas_Object* ewk_background;
423 Evas_Object* ewk_webview;
424 Ewk_Context* ewk_context;
429 utc_blink_ewk_job_cb_t test_job_function;
431 std::vector<char*> argv;
434 #endif // UTC_BLINK_EWK_BASE_HUTC_BLINK_EWK_BASE_H