summaryrefslogtreecommitdiffstats
path: root/mojo/services/view_manager/test_change_tracker.h
blob: a1d501062a1da8f732ec96bdb3a94d2d391284bf (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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_SERVICES_VIEW_MANAGER_TEST_CHANGE_TRACKER_H_
#define MOJO_SERVICES_VIEW_MANAGER_TEST_CHANGE_TRACKER_H_

#include <string>
#include <vector>

#include "mojo/public/cpp/bindings/array.h"
#include "mojo/services/public/cpp/view_manager/types.h"
#include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h"
#include "ui/gfx/rect.h"

namespace mojo {
namespace view_manager {
namespace service {

enum ChangeType {
  CHANGE_TYPE_CONNECTION_ESTABLISHED,
  CHANGE_TYPE_ROOTS_ADDED,
  CHANGE_TYPE_NODE_BOUNDS_CHANGED,
  CHANGE_TYPE_NODE_HIERARCHY_CHANGED,
  CHANGE_TYPE_NODE_REORDERED,
  CHANGE_TYPE_NODE_DELETED,
  CHANGE_TYPE_VIEW_DELETED,
  CHANGE_TYPE_VIEW_REPLACED,
  CHANGE_TYPE_INPUT_EVENT,
  CHANGE_TYPE_EMBED_ROOT,
};

// TODO(sky): consider nuking and converting directly to NodeData.
struct TestNode {
  // Returns a string description of this.
  std::string ToString() const;

  Id parent_id;
  Id node_id;
  Id view_id;
};

// Tracks a call to ViewManagerClient. See the individual functions for the
// fields that are used.
struct Change {
  Change();
  ~Change();

  ChangeType type;
  ConnectionSpecificId connection_id;
  std::vector<TestNode> nodes;
  Id node_id;
  Id node_id2;
  Id node_id3;
  Id view_id;
  Id view_id2;
  gfx::Rect bounds;
  gfx::Rect bounds2;
  int32 event_action;
  String creator_url;
  String embed_url;
  OrderDirection direction;
};

// Converts Changes to string descriptions.
std::vector<std::string> ChangesToDescription1(
    const std::vector<Change>& changes);

// Returns a string description of |changes[0].nodes|. Returns an empty string
// if change.size() != 1.
std::string ChangeNodeDescription(const std::vector<Change>& changes);

// Converts NodeDatas to TestNodes.
void NodeDatasToTestNodes(const Array<NodeDataPtr>& data,
                          std::vector<TestNode>* test_nodes);

// TestChangeTracker is used to record ViewManagerClient functions. It notifies
// a delegate any time a change is added.
class TestChangeTracker {
 public:
  // Used to notify the delegate when a change is added. A change corresponds to
  // a single ViewManagerClient function.
  class Delegate {
   public:
    virtual void OnChangeAdded() = 0;

   protected:
    virtual ~Delegate() {}
  };

  TestChangeTracker();
  ~TestChangeTracker();

  void set_delegate(Delegate* delegate) { delegate_ = delegate; }

  std::vector<Change>* changes() { return &changes_; }

  // Each of these functions generate a Change. There is one per
  // ViewManagerClient function.
  void OnViewManagerConnectionEstablished(ConnectionSpecificId connection_id,
                                          const String& creator_url,
                                          Array<NodeDataPtr> nodes);
  void OnRootAdded(Array<NodeDataPtr> nodes);
  void OnNodeBoundsChanged(Id node_id, RectPtr old_bounds, RectPtr new_bounds);
  void OnNodeHierarchyChanged(Id node_id,
                              Id new_parent_id,
                              Id old_parent_id,
                              Array<NodeDataPtr> nodes);
  void OnNodeReordered(Id node_id,
                       Id relative_node_id,
                       OrderDirection direction);
  void OnNodeDeleted(Id node_id);
  void OnViewDeleted(Id view_id);
  void OnNodeViewReplaced(Id node_id, Id new_view_id, Id old_view_id);
  void OnViewInputEvent(Id view_id, EventPtr event);
  void OnEmbedRoot(const String& url);

 private:
  void AddChange(const Change& change);

  Delegate* delegate_;
  std::vector<Change> changes_;

  DISALLOW_COPY_AND_ASSIGN(TestChangeTracker);
};

}  // namespace service
}  // namespace view_manager
}  // namespace mojo

#endif  // MOJO_SERVICES_VIEW_MANAGER_TEST_CHANGE_TRACKER_H_