~mterry/qtmir/warn-on-xapp

« back to all changes in this revision

Viewing changes to tests/modules/common/mock_application_controller.h

  • Committer: Gerry Boland
  • Date: 2014-07-01 13:38:06 UTC
  • mto: This revision was merged to the branch mainline in revision 158.
  • Revision ID: gerry.boland@canonical.com-20140701133806-lwfnplkijthydzj4
Update QML plugin: rename to Unity.Application, merge latest unity-mir changes, add tests (not passing yet) and use category logging

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it under
 
5
 * the terms of the GNU Lesser General Public License version 3, as published by
 
6
 * the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
9
 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
 
10
 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 */
 
17
 
 
18
#ifndef MOCK_APPLICATION_CONTROLLER_H
 
19
#define MOCK_APPLICATION_CONTROLLER_H
 
20
 
 
21
#include <Unity/Application/applicationcontroller.h>
 
22
 
 
23
#include <core/posix/fork.h>
 
24
 
 
25
#include <gmock/gmock.h>
 
26
 
 
27
namespace testing
 
28
{
 
29
struct MockApplicationController : public qtmir::ApplicationController
 
30
{
 
31
    MOCK_METHOD1(primaryPidForAppId, pid_t(const QString& appId));
 
32
    MOCK_METHOD2(appIdHasProcessId, bool(pid_t, const QString&));
 
33
    MOCK_CONST_METHOD1(findDesktopFileForAppId, QFileInfo(const QString &appId));
 
34
 
 
35
    MOCK_METHOD1(stopApplicationWithAppId, bool(const QString&));
 
36
    MOCK_METHOD2(startApplicationWithAppIdAndArgs, bool(const QString&, const QStringList&));
 
37
 
 
38
    MockApplicationController()
 
39
    {
 
40
        using namespace ::testing;
 
41
        ON_CALL(*this, primaryPidForAppId(_))
 
42
                .WillByDefault(
 
43
                    Invoke(this, &MockApplicationController::doPrimaryPidForAppId));
 
44
 
 
45
        ON_CALL(*this, appIdHasProcessId(_, _))
 
46
                .WillByDefault(
 
47
                    Invoke(this, &MockApplicationController::doAppIdHasProcessId));
 
48
 
 
49
        ON_CALL(*this, findDesktopFileForAppId(_))
 
50
                .WillByDefault(
 
51
                    Invoke(this, &MockApplicationController::doFindDesktopFileForAppId));
 
52
 
 
53
        ON_CALL(*this, stopApplicationWithAppId(_))
 
54
                .WillByDefault(
 
55
                    Invoke(this, &MockApplicationController::doStopApplicationWithAppId));
 
56
 
 
57
        ON_CALL(*this, startApplicationWithAppIdAndArgs(_, _))
 
58
                .WillByDefault(
 
59
                    Invoke(this, &MockApplicationController::doStartApplicationWithAppIdAndArgs));
 
60
    }
 
61
 
 
62
    pid_t doPrimaryPidForAppId(const QString& appId)
 
63
    {
 
64
        auto it = children.find(appId);
 
65
        if (it == children.end())
 
66
            return -1;
 
67
 
 
68
        return it->pid();
 
69
    }
 
70
 
 
71
    bool doAppIdHasProcessId(pid_t pid, const QString& appId)
 
72
    {
 
73
        auto it = children.find(appId);
 
74
        if (it == children.end())
 
75
            return -1;
 
76
 
 
77
        return it->pid() == pid;
 
78
    }
 
79
 
 
80
    QFileInfo doFindDesktopFileForAppId(const QString& appId) const
 
81
    {
 
82
        QString path = QString("/usr/share/applications/%1.desktop").arg(appId);
 
83
        return QFileInfo(path);
 
84
    }
 
85
 
 
86
    bool doStopApplicationWithAppId(const QString& appId)
 
87
    {
 
88
        (void) appId;
 
89
 
 
90
        return false;
 
91
    }
 
92
 
 
93
    bool doStartApplicationWithAppIdAndArgs(const QString& appId, const QStringList& args)
 
94
    {
 
95
        (void) args;
 
96
 
 
97
        auto child = core::posix::fork([]()
 
98
        {
 
99
            while (true);
 
100
 
 
101
            return core::posix::exit::Status::success;
 
102
        }, core::posix::StandardStream::empty);
 
103
 
 
104
        if (child.pid() > 0)
 
105
        {
 
106
            children.insert(appId, child);
 
107
            return true;
 
108
        }
 
109
 
 
110
        return false;
 
111
    }
 
112
 
 
113
    QMap<QString, core::posix::ChildProcess> children;
 
114
};
 
115
} // namespace testing
 
116
 
 
117
#endif // MOCK_APPLICATION_CONTROLLER_H