~gerboland/unity-mir/demo-stuff-manta-osk-fix

« back to all changes in this revision

Viewing changes to tests/mock_application_controller.h

  • Committer: Gerry Boland
  • Date: 2014-02-13 23:02:39 UTC
  • mfrom: (172.1.6 unity-mir)
  • Revision ID: gerry.boland@canonical.com-20140213230239-4znuxxmv1j79odei
Merge trunk

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 ApplicationController
 
30
{
 
31
    MOCK_METHOD1(primaryPidForAppId, pid_t(const QString& appId));
 
32
    MOCK_METHOD2(appIdHasProcessId, bool(pid_t, const QString&));
 
33
 
 
34
    MOCK_METHOD1(stopApplicationWithAppId, bool(const QString&));
 
35
    MOCK_METHOD2(startApplicationWithAppIdAndArgs, bool(const QString&, const QStringList&));
 
36
 
 
37
    MockApplicationController()
 
38
    {
 
39
        using namespace ::testing;
 
40
        ON_CALL(*this, primaryPidForAppId(_))
 
41
                .WillByDefault(
 
42
                    Invoke(this, &MockApplicationController::doPrimaryPidForAppId));
 
43
 
 
44
        ON_CALL(*this, appIdHasProcessId(_, _))
 
45
                .WillByDefault(
 
46
                    Invoke(this, &MockApplicationController::doAppIdHasProcessId));
 
47
 
 
48
        ON_CALL(*this, stopApplicationWithAppId(_))
 
49
                .WillByDefault(
 
50
                    Invoke(this, &MockApplicationController::doStopApplicationWithAppId));
 
51
 
 
52
        ON_CALL(*this, startApplicationWithAppIdAndArgs(_, _))
 
53
                .WillByDefault(
 
54
                    Invoke(this, &MockApplicationController::doStartApplicationWithAppIdAndArgs));
 
55
    }
 
56
 
 
57
    pid_t doPrimaryPidForAppId(const QString& appId)
 
58
    {
 
59
        auto it = children.find(appId);
 
60
        if (it == children.end())
 
61
            return -1;
 
62
 
 
63
        return it->pid();
 
64
    }
 
65
 
 
66
    bool doAppIdHasProcessId(pid_t pid, const QString& appId)
 
67
    {
 
68
        auto it = children.find(appId);
 
69
        if (it == children.end())
 
70
            return -1;
 
71
 
 
72
        return it->pid() == pid;
 
73
    }
 
74
 
 
75
    bool doStopApplicationWithAppId(const QString& appId)
 
76
    {
 
77
        (void) appId;
 
78
 
 
79
        return false;
 
80
    }
 
81
 
 
82
    bool doStartApplicationWithAppIdAndArgs(const QString& appId, const QStringList& args)
 
83
    {
 
84
        (void) args;
 
85
 
 
86
        auto child = core::posix::fork([]()
 
87
        {
 
88
            while (true);
 
89
 
 
90
            return core::posix::exit::Status::success;
 
91
        }, core::posix::StandardStream::empty);
 
92
 
 
93
        if (child.pid() > 0)
 
94
        {
 
95
            children.insert(appId, child);
 
96
            return true;
 
97
        }
 
98
 
 
99
        return false;
 
100
    }
 
101
 
 
102
    QMap<QString, core::posix::ChildProcess> children;
 
103
};
 
104
}
 
105
 
 
106
#endif // MOCK_APPLICATION_CONTROLLER_H