[M120 Migration][MM] Support W3C EME
[platform/framework/web/chromium-efl.git] / tizen_src / ewk / unittest / utc_blink_ewk_base.h
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.
4
5 /**
6  * @file   utc_blink_ewk_base.h
7  * @author Kamil Klimek <k.klimek@partner.samsung.com>
8  * @date   2014-02-28
9  * @brief  A header file to be used by all unit test cases for Chromium EFL API
10  *
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.
14  */
15
16
17 #ifndef UTC_BLINK_EWK_BASE_H
18 #define UTC_BLINK_EWK_BASE_H
19
20 /* google test header */
21 #include "gtest/gtest.h"
22 /* Common headers */
23 #include <Ecore_Evas.h>
24 #include <Ecore.h>
25 #include <Edje.h>
26 #include <Eina.h>
27 #include <Elementary.h>
28 #include <glib-object.h>
29 #include <glib.h>
30 #include <Evas.h>
31
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <string.h>
35 /* ewk header */
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"
46 #include "ewk_log.h"
47 #include "ewk_main.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"
53 #include "ewk_view.h"
54 #include "ewk_hit_test.h"
55 #include "ewk_text_style.h"
56 #include "ewk_web_application_icon_data.h"
57
58 /* Definitions and macros */
59 #define DEFAULT_WIDTH_OF_WINDOW 480
60 #define DEFAULT_HEIGHT_OF_WINDOW 800
61
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()
83
84
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, ...);
91 /* User macros */
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__)
96
97 #define utc_message utc_warning
98
99 class utc_blink_ewk_base: public ::testing::Test
100 {
101 public:
102     enum MainLoopResult {
103       NoOp = 0,
104       Success,
105       Failure,
106       LoadFailure,
107       Timeout
108     };
109 protected:
110     class evas_object_smart_callback_auto
111     {
112     public:
113       evas_object_smart_callback_auto(Evas_Object* obj, const char* event, Evas_Smart_Cb func, const void* data)
114         : obj_(obj)
115         , event_(eina_stringshare_add(event))
116         , func_(func)
117       {
118         evas_object_smart_callback_add(obj, event, func, data);
119       }
120
121       ~evas_object_smart_callback_auto()
122       {
123         evas_object_smart_callback_del(obj_, event_, func_);
124         eina_stringshare_del(event_);
125       }
126
127     private:
128       Evas_Object* obj_;
129       Eina_Stringshare* event_;
130       Evas_Smart_Cb func_;
131     };
132
133     utc_blink_ewk_base();
134     ~utc_blink_ewk_base() override;
135
136     void SetLogJavascript(bool enabled) { log_javascript = enabled; }
137
138     /**
139      * Checks if given Evas_Object contains image pointed by source
140      *
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
150      *
151      * @return true if Evas_Image produced from path is equal to given Evas_Image (memory compare)
152      */
153     bool CompareEvasImageWithResource(Evas_Object* image_object, const char* resource, int pixel_fuzziness) const;
154
155     // Adds allow-file-access-from-files param to Chromium.
156     // Must be called within PreSetUp to be taken into account.
157     void AllowFileAccessFromFiles();
158
159     /**
160      * Generates proper path for resource file
161      *
162      * @param [in] resource_path  resource file subpath
163      *
164      * @retval  full resource path
165      */
166     std::string GetResourcePath(const char* resource_path) const;
167
168     /**
169      * Generates proper url for resource file
170      *
171      * @param [in] resource_path  resource file subpath
172      *
173      * @retval  full resource url including file://
174      */
175     std::string GetResourceUrl(const char* resource_path) const;
176
177     /**
178      * Starts main application loop.
179      *
180      * @param [in] max_time  main loop timeout
181      *
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.
187      */
188     MainLoopResult EventLoopStart(double max_time = 30.0);
189
190     /**
191      * Waits given time on main application loop.
192      *
193      * @params [in] time  time to wait
194      */
195     bool EventLoopWait(double time);
196
197     /**
198      * Stop main application loop.
199      *
200      * @param [in] result  result to be returned by EventLoopStart.
201      *                     if result is MainLoopResult::NoOp this method will do nothing.
202      *
203      * @retval true if this call stopped event loop, false otherwise.
204      *
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.
207      */
208     bool EventLoopStop(MainLoopResult result);
209
210     /**
211      * Override this method if you need to peform some extra
212      * initialization before initializing ewk.
213      */
214     virtual void PreSetUp() {}
215
216     /**
217      * Override this method if you need to perform some extra
218      * initialization after initializing ewk.
219      */
220     virtual void PostSetUp() {}
221
222     /**
223      * Override this method if you need to perform some extra
224      * deinitialization before deinitializng ewk.
225      */
226     virtual void PreTearDown() {}
227
228     /**
229      * Override this method if you need to perform some extra
230      * deinitialization after deinitialzing ewk.
231      */
232     virtual void PostTearDown() {}
233
234     /**
235      * Called when main application loop timeouted.
236      *
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.
239      */
240     virtual bool TimeOut();
241
242     /**
243      * Override this method if you need to handle "load,finished" ewk callback.
244      */
245     virtual void LoadFinished(Evas_Object* webview) {}
246
247     /**
248      * Override this method if you need to handle "load,started" ewk callback.
249      */
250     virtual void LoadStarted(Evas_Object* webview) {}
251
252     /**
253      * Override this method if you need to handle "load,error" ewk callback.
254      *
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.
257      */
258     virtual bool LoadError(Evas_Object* webview, Ewk_Error* error);
259
260     /**
261      * Override this method if you need to handle "load,progress" ewk callback.
262      */
263     virtual void LoadProgress(Evas_Object* webview, double progress) {}
264
265     /**
266      * Override this method if you need to handle "console,message" ewk callback.
267      */
268     virtual void ConsoleMessage(Evas_Object* webview, const Ewk_Console_Message*);
269
270     /**
271      * Performs unit test setup. It calls PreSetUp before default initialization
272      * and PostSetUp after default initialization.
273      *
274      * @note This method should not be overriden.
275      */
276     void SetUp() final;
277
278     /**
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
281      * class)
282      *                           that will be called from main loop
283      *
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.
289      *
290      *       in TEST_F body:
291      *       > ASSERT_EQ(EINA_TRUE,
292      * ewk_view_text_selection_clear(GetEwkWebView()));
293      *       > ASSERT_EQ(Success, EventLoopStart());
294      *
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
298      * loop
299      *       3. result of code called in callback store in auxiliary field of
300      * class
301      *       and test later
302      *       Example:
303      *
304      *       in class definition:
305      *       > static void job_selection_clear(utc_blink_ewk_base *data)
306      *       > {
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());
311      *       > }
312      *
313      *       in TEST_F body:
314      *       >
315      *       > SetTestJob(utc_blink_cb_textselection_mode::job_selection_clear);
316      *       > ASSERT_EQ(Success, EventLoopStart());
317      *       > ASSERT_EQ(EINA_TRUE, result_text_selection_clear);
318      */
319     void SetTestJob(utc_blink_ewk_job_cb_t job_function)
320     {
321         test_job_function = job_function;
322     }
323
324     /**
325      * Performs unit test deinitialization. It calls PreTearDown before default deinitialization
326      * and PostTearDown after default deinitialization.
327      *
328      * @note This method should not be overriden.
329      */
330     void TearDown() final;
331
332     template<class T> static void OwnerFromVoid(void *owner, T** out)
333     {
334       if(out) {
335         *out = static_cast<T*>(owner);
336       }
337     }
338
339     template<typename T> static Evas_Smart_Cb ToSmartCallback(T proc)
340     {
341       return reinterpret_cast<Evas_Smart_Cb>(proc);
342     }
343
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; }
347
348     void FeedMouseClick(int x, int y, int button);
349
350   private:
351     /**
352      * Default "load,started" event callback, will call LoadStarted method
353      *
354      * @see LoadStarted
355      */
356     static void load_started_cb(void* data, Evas_Object* webview, void* event_info);
357
358     /**
359      * Default "load,finished" event callback, will call LoadFinish method
360      *
361      * @see LoadFinished
362      */
363     static void load_finished_cb(void* data, Evas_Object* webview, void* event_info);
364
365     /**
366      * Default "load,error" event callback, will call LoadError method
367      *
368      * @see LoadError
369      */
370     static void load_error_cb(void* data, Evas_Object* webview, void* event_info);
371
372     /**
373      * Default "load,progress" event callback, will call LoadProgress method
374      *
375      * @see LoadProgress
376      */
377     static void load_progress_cb(void* data, Evas_Object* webview, void* event_info);
378
379     /**
380      * Default "console,message" event callback, will call ConsoleMessage method
381      */
382     static void console_message_cb(utc_blink_ewk_base*, Evas_Object*, Ewk_Console_Message*);
383
384     /**
385      * Default event loop timeout callback, will call TimeOut method
386      *
387      * @see TimeOut
388      */
389     static Eina_Bool timeout_cb(void *data);
390
391     /**
392      * EventLoopWait timeotut callback, will stop event loop
393      */
394     static Eina_Bool timeout_cb_event_loop_wait(void *data);
395
396 private:
397     /**
398      * Initializes objects used in the test
399      *
400      * This method is always called in SetUp method.
401      * @see SetUp
402      */
403     void EwkInit();
404
405     /**
406      * Deinitializes and frees resources used during test.
407      *
408      * This method is always called in TearDown method.
409      * @see TearDown
410      */
411     void EwkDeinit();
412
413 private:
414     Ecore_Timer *timeout;
415     bool main_loop_running;
416     MainLoopResult main_loop_result;
417
418     bool log_javascript;
419
420     Evas_Object* ewk_window;
421     Evas* ewk_evas;
422     Evas_Object* ewk_background;
423     Evas_Object* ewk_webview;
424     Ewk_Context* ewk_context;
425
426     char *resource_dir;
427
428     Ecore_Job* test_job;
429     utc_blink_ewk_job_cb_t test_job_function;
430
431     std::vector<char*> argv;
432 };
433
434 #endif // UTC_BLINK_EWK_BASE_HUTC_BLINK_EWK_BASE_H
435