1--- Test luakit clib functionality.
2--
3-- @copyright 2012 Mason Larobina <mason.larobina@gmail.com>
4
5local assert = require "luassert"
6local test = require "tests.lib"
7
8local T = {}
9
10T.test_luakit = function ()
11    assert.is_table(luakit)
12    -- Check metatable
13    local mt = getmetatable(luakit)
14    assert.is_function(mt.__index, "luakit mt missing __index")
15end
16
17T.test_luakit_index = function ()
18    local funcprops = { "exec", "quit", "save_file", "spawn", "spawn_sync",
19        "time", "uri_decode", "uri_encode", "idle_add", "idle_remove" }
20    for _, p in ipairs(funcprops) do
21        assert.is_function(luakit[p], "Missing/invalid function: luakit."..p)
22    end
23
24    local strprops = { "cache_dir", "config_dir", "data_dir", "execpath",
25        "confpath", "install_path", "version" }
26    for _, p in ipairs(strprops) do
27        assert.is_string(luakit[p], "Missing/invalid property: luakit."..p)
28    end
29
30    local boolprops = { "dev_paths", "verbose", "nounique" }
31    for _, p in ipairs(boolprops) do
32        assert.is_boolean(luakit[p], "Missing/invalid property: luakit."..p)
33    end
34
35    assert.is_number(luakit.time(), "Invalid: luakit.time()")
36end
37
38T.test_webkit_version = function ()
39    assert.is_match("^%d+%.%d+%.%d+$", luakit.webkit_version,
40        "Invalid format: luakit.webkit_version")
41    assert.is_match("^%d+%.%d+$", luakit.webkit_user_agent_version,
42        "Invalid format: luakit.webkit_user_agent_version")
43end
44
45T.test_windows_table = function ()
46    assert.is_table(luakit.windows, "Missing/invalid luakit.windows table.")
47    local baseline = #luakit.windows
48    assert.is_number(baseline, "Invalid number of windows")
49    local win = widget{type="window"}
50    assert.is_equal(baseline+1, #luakit.windows,
51        "luakit.windows not tracking opened windows.")
52    win:destroy()
53    assert.is_equal(baseline, #luakit.windows,
54        "luakit.windows not tracking closed windows.")
55end
56
57T.test_invalid_prop = function ()
58    assert.is_nil(luakit.invalid_property)
59end
60
61T.test_idle_add_del = function ()
62    local f = function () end
63    assert.is_false(luakit.idle_remove(f),
64        "Function can't be removed before it's been added.")
65    for _ = 1,5 do
66        luakit.idle_add(f)
67    end
68    for _ = 1,5 do
69        assert.is_true(luakit.idle_remove(f), "Error removing callback.")
70    end
71    assert.is_false(luakit.idle_remove(f),
72        "idle_remove removed incorrect number of callbacks.")
73end
74
75T.test_register_scheme = function ()
76    assert.has_error(function () luakit.register_scheme("") end)
77    assert.has_error(function () luakit.register_scheme("http") end)
78    assert.has_error(function () luakit.register_scheme("https") end)
79    assert.has_error(function () luakit.register_scheme("ABC") end)
80    assert.has_error(function () luakit.register_scheme(" ") end)
81    luakit.register_scheme("test-scheme-name")
82    luakit.register_scheme("a-.++...--8970d-d-")
83end
84
85T.test_website_data = function ()
86    local _, minor = luakit.webkit_version:match("^(%d+)%.(%d+)%.")
87    if tonumber(minor) < 16 then return end
88
89    local wd = luakit.website_data
90    assert.is_table(wd)
91    assert.is_function(wd.fetch)
92    assert.has_error(function () wd.fetch("") end)
93    assert.has_error(function () wd.fetch({}) end)
94    assert.has_error(function () wd.remove("") end)
95    assert.has_error(function () wd.remove({}) end)
96    assert.has_error(function () wd.remove({"all"}) end)
97
98    local v = widget{type="webview"}
99
100    coroutine.wrap(function ()
101        assert(not wd.fetch({"all"})["Local files"])
102        test.continue()
103    end)()
104    test.wait(1000)
105
106    v.uri = "file:///"
107    test.wait_for_view(v)
108
109    coroutine.wrap(function ()
110        assert(wd.fetch({"all"})["Local files"])
111        wd.remove({"all"}, "Local files")
112        assert(not wd.fetch({"all"})["Local files"])
113        test.continue()
114    end)()
115    test.wait()
116
117    v:destroy()
118end
119
120T.test_luakit_install_paths = function ()
121    local paths = assert(luakit.install_paths)
122    assert.equal(paths.install_dir, luakit.install_path)
123    for _, k in ipairs {"install_dir", "config_dir", "doc_dir", "man_dir", "pixmap_dir", "app_dir"} do
124        assert(type(paths[k]) == "string")
125    end
126end
127
128return T
129
130-- vim: et:sw=4:ts=8:sts=4:tw=80
131