summaryrefslogtreecommitdiff
path: root/tests/completion.vala
blob: be6698e22d82120088d5b3a1f58e8b3b2bdc1fcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 Copyright (C) 2012 Christian Dywan <christian@twotoasts.de>

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 See the file COPYING for the full license text.
*/

class TestCompletion : Midori.Completion {
    public bool test_can_complete { get; set; }
    public uint test_suggestions { get; set; }

    public TestCompletion () {
    }

    public override void prepare (GLib.Object app) {
    }

    public override bool can_complete (string text) {
        return test_can_complete;
    }

    public override bool can_action (string action) {
        return false;
    }

    public override async List<Midori.Suggestion>? complete (string text, string? action, Cancellable cancellable) {
        var suggestions = new List<Midori.Suggestion> ();
        if (test_suggestions == 0)
            return null;
        if (test_suggestions >= 1)
            suggestions.append (new Midori.Suggestion ("about:first", "First"));
        if (test_suggestions >= 2)
            suggestions.append (new Midori.Suggestion ("about:second", "Second"));
        if (test_suggestions >= 3)
            suggestions.append (new Midori.Suggestion ("about:third", "Third"));
        if (cancellable.is_cancelled ())
            return null;
        return suggestions;
    }
}

class CompletionAutocompleter : Midori.Test.Job {
    public static void test () { new CompletionAutocompleter ().run_sync (); }
    public override async void run (Cancellable cancellable) throws GLib.Error {
        var app = new Midori.App ();
        var autocompleter = new Midori.Autocompleter (app);
        assert (!autocompleter.can_complete (""));
        var completion = new TestCompletion ();
        autocompleter.add (completion);
        completion.test_can_complete = false;
        assert (!autocompleter.can_complete (""));
        completion.test_can_complete = true;
        assert (autocompleter.can_complete (""));

        completion.test_suggestions = 0;
        yield autocompleter.complete ("");
        assert (autocompleter.model.iter_n_children (null) == 0);

        completion.test_suggestions = 1;
        yield autocompleter.complete ("");
        assert (autocompleter.model.iter_n_children (null) == 1);

        /* Order */
        completion.test_suggestions = 2;
        yield autocompleter.complete ("");
        assert (autocompleter.model.iter_n_children (null) == 2);
        Gtk.TreeIter iter_first;
        autocompleter.model.get_iter_first (out iter_first);
        string title;
        autocompleter.model.get (iter_first, Midori.Autocompleter.Columns.MARKUP, out title);
        if (title != "First")
            error ("Expected %s but got %s", "First", title);

        /* Cancellation */
        yield autocompleter.complete ("");
        completion.test_suggestions = 3;
        yield autocompleter.complete ("");
        int n = autocompleter.model.iter_n_children (null);
        if (n != 3)
            error ("Expected %d but got %d", 3, n);
    }
}

class CompletionHistory : Midori.Test.Job {
    public static void test () { new CompletionHistory ().run_sync (); }
    public override async void run (Cancellable cancellable) throws GLib.Error {
        var bookmarks_database = new Midori.BookmarksDatabase ();
        assert (bookmarks_database.db != null);

        Midori.HistoryDatabase history = new Midori.HistoryDatabase (null);
        assert (history.db != null);
        history.clear (0);

        history.insert ("http://example.com", "Ejemplo", 0, 0);
        var results = yield history.list_by_count_with_bookmarks ("example", 1, cancellable);
        assert (results.length () == 1);
        var first = results.nth_data (0);
        assert (first.title == "Ejemplo");
        results = yield history.list_by_count_with_bookmarks ("ejemplo", 1, cancellable);
        assert (results.length () == 1);
        first = results.nth_data (0);
        assert (first.title == "Ejemplo");
    }
}

struct TestCaseRender {
    public string keys;
    public string uri;
    public string title;
    public string expected_uri;
    public string expected_title;
}

const TestCaseRender[] renders = {
    { "debian", "planet.debian.org", "Planet Debian", "planet.<b>debian</b>.org", "Planet <b>Debian</b>" },
    { "p debian o", "planet.debian.org", "Planet Debian", "<b>p</b>lanet.<b>debian</b>.<b>o</b>rg", "Planet Debian" },
    { "pla deb o", "planet.debian.org", "Planet Debian", "<b>pla</b>net.<b>deb</b>ian.<b>o</b>rg", "Planet Debian" },
    { "ebi", "planet.debian.org", "Planet Debian", "planet.d<b>ebi</b>an.org", "Planet D<b>ebi</b>an" },
    { "an ebi", "planet.debian.org", "Planet Debian", "pl<b>an</b>et.d<b>ebi</b>an.org", "Pl<b>an</b>et D<b>ebi</b>an" }
};

void completion_location_action () {
    foreach (var spec in renders) {
        string uri = Midori.LocationAction.render_uri (spec.keys.split (" ", 0), spec.uri);
        string title = Midori.LocationAction.render_title (spec.keys.split (" ", 0), spec.title);
        if (uri != spec.expected_uri || title != spec.expected_title)
            error ("\nExpected: %s/ %s\nInput   : %s/ %s/ %s\nResult  : %s/ %s",
                   spec.expected_uri, spec.expected_title,
                   spec.keys, spec.uri, spec.title, uri, title);
    }
}

void main (string[] args) {
    Midori.Test.init (ref args);
    Midori.App.setup (ref args, null);
    Midori.Paths.init (Midori.RuntimeMode.NORMAL, null);
    Test.add_func ("/completion/autocompleter", CompletionAutocompleter.test);
    Test.add_func ("/completion/history", CompletionHistory.test);
    Test.add_func ("/completion/location-action", completion_location_action);
    Test.run ();
}