<commit_before>const {DModel, EvinceDocument, Gio, GObject, Gtk, WebKit2} = imports.gi;

const Utils = imports.tests.utils;
Utils.register_gresource();

const {Document} = imports.framework.modules.view.document;
const Knowledge = imports.framework.knowledge;
const CssClassMatcher = imports.tests.CssClassMatcher;
const InstanceOfMatcher = imports.tests.InstanceOfMatcher;
const Minimal = imports.tests.minimal;
const MockWidgets = imports.tests.mockWidgets;
const TableOfContents = imports.framework.widgets.tableOfContents;
const WidgetDescendantMatcher = imports.tests.WidgetDescendantMatcher;

Gtk.init(null);
EvinceDocument.init();

const TEST_CONTENT_DIR = Utils.get_test_content_srcdir();

const MockEknWebviewDecision = new Knowledge.Class({
    Name: 'MockEknWebviewDecision',
    Extends: GObject.Object,
    Properties: {
        'uri': GObject.ParamSpec.string('uri', '', '',
            GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
            ''),
    },
    _init: function (props={}) {
        this.parent(props);
        this.request = { uri: this.uri };
    },
    use: function () {
    },
    ignore: function () {
    },
});

describe('View.Document', function () {
    let view, model, real_session_descriptor, toc;

    beforeEach(function () {
        jasmine.addMatchers(CssClassMatcher.customMatchers);
        jasmine.addMatchers(InstanceOfMatcher.customMatchers);
        jasmine.addMatchers(WidgetDescendantMatcher.customMatchers);
        // Mock out the entire session bus or we will error in an environment
        // without a session bus. Because session is a property with only a
        // getter, it can't be mocked with regular spyOn methods yet. See bug
        // https://github.com/jasmine/jasmine/issues/943
        real_session_descriptor = Object.getOwnPropertyDescriptor(Gio.DBus, 'session');
        let mock_session = jasmine.createSpyObj('session', ['signal_subscribe']);
        Object.defineProperty(Gio.DBus, 'session', {
            value: mock_session,
            configurable: true,
        });

        toc =
            [{"hasIndex": 0, "hasIndexLabel": "1", "hasLabel": "Foo", "hasContent": "#Foo"},
             {"hasIndex": 1, "hasIndexLabel": "2", "hasLabel": "Bar", "hasContent": "#Bar"},
             {"hasIndex": 2, "hasIndexLabel": "3", "hasLabel": "Baz", "hasContent": "#Baz"}];

        model = DModel.Article.new_from_props({
            title: '!!!',
            table_of_contents: toc,
        });
        view = new Document({
            model: model,
            show_toc: true,
        });
    });

    afterEach(function () {
        Object.defineProperty(Gio.DBus, 'session', real_session_descriptor);
    });

    it('instantiates a table of contents widget', function () {
        expect(view.toc).toBeA(TableOfContents.TableOfContents);
    });

    it('has labels that understand Pango markup', function () {
        expect(Gtk.test_find_label(view, '*!!!*').use_markup).toBeTruthy();
        // FIXME: the above line will find either title_label or top_title_label
        // but not both
    });

    describe('Style class of document view', function () {
        it('has a descendant with title class', function () {
            expect(view).toHaveDescendantWithCssClass('ViewDocument__title');
        });
        it('has a descendant with toolbar frame class', function () {
            expect(view).toHaveDescendantWithCssClass('ViewDocument__toolbarFrame');
        });
        it('has an expanded table of contents by default', function () {
            expect(view.toc).not.toHaveCssClass('ViewDocument__toolbarFrame--collapsed');
        });
    });

    describe('with pdf model', function () {
        let pdf_model;
        beforeEach(function (done) {
            pdf_model = DModel.Article.new_from_props({
                content_type: 'application/pdf',
                title: 'Pdf title',
                table_of_contents: toc,
            });
            pdf_model.get_content_stream = () => {
                let file = Gio.File.new_for_path(TEST_CONTENT_DIR + 'pdf-sample1.pdf');
                return file.read(null);
            };

            view = new Document({
                model: pdf_model,
            });
            view.load_content_promise().then(done);
        });

        it('can be loaded', function () {});

        it('never show a table of contents', function () {
            expect(view.toc.visible).toBe(false);
        });
    });

    describe('with html model', function () {
        let html_model;
        beforeEach(function (done) {
            html_model = DModel.Article.new_from_props({
                id: 'ekn:///foo/bar',
                content_type: 'text/html',
                title: 'Html title',
                table_of_contents: toc,
            });
            view = new Document({
                model: html_model,
                show_toc: true,
            });
            spyOn(view, '_create_webview').and.returnValue(new MockWidgets.MockEknWebview());
            view.load_content_promise().then(done);
            view.content_view.emit('load-changed', WebKit2.LoadEvent.COMMITTED);
        });

        it('can be loaded', function () {});

        xdescribe('table of contents', function () {
            let win;
            const TOP_BOTTOM_BAR_HEIGHT = 36 + 30;
            beforeEach(function () {
                win = new Gtk.OffscreenWindow();
                win.add(view);
            });

            it('section list is populated', function () {
                let labels = [];
                for (let obj of toc) {
                    if (!('hasParent' in obj)) {
                        labels.push(obj['hasLabel']);
                    }<commit_msg>Re-enable disabled tests in testDocument<commit_after>const {DModel, EvinceDocument, Gio, GObject, Gtk, WebKit2} = imports.gi;

const Utils = imports.tests.utils;
Utils.register_gresource();

const {Document} = imports.framework.modules.view.document;
const Knowledge = imports.framework.knowledge;
const CssClassMatcher = imports.tests.CssClassMatcher;
const InstanceOfMatcher = imports.tests.InstanceOfMatcher;
const Minimal = imports.tests.minimal;
const MockWidgets = imports.tests.mockWidgets;
const TableOfContents = imports.framework.widgets.tableOfContents;
const WidgetDescendantMatcher = imports.tests.WidgetDescendantMatcher;

Gtk.init(null);
EvinceDocument.init();

const TEST_CONTENT_DIR = Utils.get_test_content_srcdir();

const MockEknWebviewDecision = new Knowledge.Class({
    Name: 'MockEknWebviewDecision',
    Extends: GObject.Object,
    Properties: {
        'uri': GObject.ParamSpec.string('uri', '', '',
            GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
            ''),
    },
    _init: function (props={}) {
        this.parent(props);
        this.request = { uri: this.uri };
    },
    use: function () {
    },
    ignore: function () {
    },
});

describe('View.Document', function () {
    let view, model, real_session_descriptor, toc;

    beforeEach(function () {
        jasmine.addMatchers(CssClassMatcher.customMatchers);
        jasmine.addMatchers(InstanceOfMatcher.customMatchers);
        jasmine.addMatchers(WidgetDescendantMatcher.customMatchers);
        // Mock out the entire session bus or we will error in an environment
        // without a session bus. Because session is a property with only a
        // getter, it can't be mocked with regular spyOn methods yet. See bug
        // https://github.com/jasmine/jasmine/issues/943
        real_session_descriptor = Object.getOwnPropertyDescriptor(Gio.DBus, 'session');
        let mock_session = jasmine.createSpyObj('session', ['signal_subscribe']);
        Object.defineProperty(Gio.DBus, 'session', {
            value: mock_session,
            configurable: true,
        });

        toc =
            [{"hasIndex": 0, "hasIndexLabel": "1", "hasLabel": "Foo", "hasContent": "#Foo"},
             {"hasIndex": 1, "hasIndexLabel": "2", "hasLabel": "Bar", "hasContent": "#Bar"},
             {"hasIndex": 2, "hasIndexLabel": "3", "hasLabel": "Baz", "hasContent": "#Baz"}];

        model = DModel.Article.new_from_props({
            title: '!!!',
            table_of_contents: toc,
        });
        view = new Document({
            model: model,
            show_toc: true,
        });
    });

    afterEach(function () {
        Object.defineProperty(Gio.DBus, 'session', real_session_descriptor);
    });

    it('instantiates a table of contents widget', function () {
        expect(view.toc).toBeA(TableOfContents.TableOfContents);
    });

    it('has labels that understand Pango markup', function () {
        expect(Gtk.test_find_label(view, '*!!!*').use_markup).toBeTruthy();
        // FIXME: the above line will find either title_label or top_title_label
        // but not both
    });

    describe('Style class of document view', function () {
        it('has a descendant with title class', function () {
            expect(view).toHaveDescendantWithCssClass('ViewDocument__title');
        });
        it('has a descendant with toolbar frame class', function () {
            expect(view).toHaveDescendantWithCssClass('ViewDocument__toolbarFrame');
        });
        it('has an expanded table of contents by default', function () {
            expect(view.toc).not.toHaveCssClass('ViewDocument__toolbarFrame--collapsed');
        });
    });

    describe('with pdf model', function () {
        let pdf_model;
        beforeEach(function (done) {
            pdf_model = DModel.Article.new_from_props({
                content_type: 'application/pdf',
                title: 'Pdf title',
                table_of_contents: toc,
            });
            pdf_model.get_content_stream = () => {
                let file = Gio.File.new_for_path(TEST_CONTENT_DIR + 'pdf-sample1.pdf');
                return file.read(null);
            };

            view = new Document({
                model: pdf_model,
            });
            view.load_content_promise().then(done);
        });

        it('can be loaded', function () {});

        it('never show a table of contents', function () {
            expect(view.toc.visible).toBe(false);
        });
    });

    describe('with html model', function () {
        let html_model;
        beforeEach(function (done) {
            html_model = DModel.Article.new_from_props({
                id: 'ekn:///foo/bar',
                content_type: 'text/html',
                title: 'Html title',
                table_of_contents: toc,
            });
            view = new Document({
                model: html_model,
                show_toc: true,
            });
            spyOn(view, '_create_webview').and.returnValue(new MockWidgets.MockEknWebview());
            view.load_content_promise().then(done);
            view.content_view.emit('load-changed', WebKit2.LoadEvent.COMMITTED);
        });

        it('can be loaded', function () {});

        describe('table of contents', function () {
            let win;
            const TOP_BOTTOM_BAR_HEIGHT = 36 + 30;
            beforeEach(function () {
                win = new Gtk.OffscreenWindow();
                win.add(view);
            });

            it('section list is populated', function () {
                let labels = [];
                for (let obj of toc) {
                    if (!('hasParent' in obj)) {
                        labels.push(obj['hasLabel']);
                    }
{'fraction_non_alphanumeric': 0.1270818875780708, 'fraction_numerical': 0.002775850104094379, 'mean_word_length': 2.2808764940239046, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1880558', 'n_tokens_mistral': 3310, 'n_tokens_neox': 3173, 'n_words': 686}