function Test(browser) {
    this._init(browser);
}

Test.prototype = {
    _init: function (browser) {
        this._state = 0;
        this._url = browser.getUrl();
        this._login = browser.getLogin();
        this._password = browser.getPassword();
        this._browser = browser;
        browser.open('https://www.amazon.com/ap/signin?openid.assoc_handle=amzn_kweb&openid.return_to=https%3A%2F%2Fread.amazon.com%2F&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select');
    },

    onLoad: function () {
        if (!this._login) {
            this._browser.skipTest();
        }
        if (this._state === 0) {
            this._browser.injectScript(makeAuthorizer('ap_email', 'ap_password', this._login, this._password));
        } else if (this._state === 1) {
            this._browser.open(this._url);
            var self = this;
            setTimeout(function () { self._browser.injectScript("(function () {"
                   + " if(window.frames[1].document.getElementsByClassName('ui-dialog firstRunDialog').length) "
                   + "window.frames[1].document.getElementsByClassName('ui-dialog firstRunDialog')[0].getElementsByClassName('primary_btn')[0].click();  }) ();"); }, 3000);
        } else if (this._state >= 2) {
            setTimeout(this._browser.finish.bind(this._browser), 10000);
        }
        this._state++;
    },

    validateCallLog: function (log) {
        var i, res = false;
        // test account should have at least one book
        for (i = 0; i < log.length; i++) {
            if (log[i].func === 'Unity.Launcher.addAction') {
                res = true;
            }
        }
        assertTrue('Test account should have at least one book - Unity.Launcher.addAction', res);
    },

    scriptName: 'AmazonCloudReader.user.js'
};
