• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef MOJO_SERVICES_VIEW_MANAGER_TEST_CHANGE_TRACKER_H_
6 #define MOJO_SERVICES_VIEW_MANAGER_TEST_CHANGE_TRACKER_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "mojo/public/cpp/bindings/array.h"
12 #include "mojo/services/public/cpp/view_manager/types.h"
13 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h"
14 #include "ui/gfx/rect.h"
15 
16 namespace mojo {
17 namespace service {
18 
19 enum ChangeType {
20   CHANGE_TYPE_EMBED,
21   // TODO(sky): NODE->VIEW.
22   CHANGE_TYPE_NODE_BOUNDS_CHANGED,
23   CHANGE_TYPE_NODE_HIERARCHY_CHANGED,
24   CHANGE_TYPE_NODE_REORDERED,
25   CHANGE_TYPE_NODE_VISIBILITY_CHANGED,
26   CHANGE_TYPE_NODE_DRAWN_STATE_CHANGED,
27   CHANGE_TYPE_NODE_DELETED,
28   CHANGE_TYPE_INPUT_EVENT,
29   CHANGE_TYPE_DELEGATE_EMBED,
30 };
31 
32 // TODO(sky): consider nuking and converting directly to ViewData.
33 struct TestView {
34   // Returns a string description of this.
35   std::string ToString() const;
36 
37   // Returns a string description that includes visible and drawn.
38   std::string ToString2() const;
39 
40   Id parent_id;
41   Id view_id;
42   bool visible;
43   bool drawn;
44 };
45 
46 // Tracks a call to ViewManagerClient. See the individual functions for the
47 // fields that are used.
48 struct Change {
49   Change();
50   ~Change();
51 
52   ChangeType type;
53   ConnectionSpecificId connection_id;
54   std::vector<TestView> views;
55   Id view_id;
56   Id view_id2;
57   Id view_id3;
58   gfx::Rect bounds;
59   gfx::Rect bounds2;
60   int32 event_action;
61   String creator_url;
62   String embed_url;
63   OrderDirection direction;
64   bool bool_value;
65 };
66 
67 // Converts Changes to string descriptions.
68 std::vector<std::string> ChangesToDescription1(
69     const std::vector<Change>& changes);
70 
71 // Returns a string description of |changes[0].views|. Returns an empty string
72 // if change.size() != 1.
73 std::string ChangeViewDescription(const std::vector<Change>& changes);
74 
75 // Converts ViewDatas to TestViews.
76 void ViewDatasToTestViews(const Array<ViewDataPtr>& data,
77                           std::vector<TestView>* test_views);
78 
79 // TestChangeTracker is used to record ViewManagerClient functions. It notifies
80 // a delegate any time a change is added.
81 class TestChangeTracker {
82  public:
83   // Used to notify the delegate when a change is added. A change corresponds to
84   // a single ViewManagerClient function.
85   class Delegate {
86    public:
87     virtual void OnChangeAdded() = 0;
88 
89    protected:
~Delegate()90     virtual ~Delegate() {}
91   };
92 
93   TestChangeTracker();
94   ~TestChangeTracker();
95 
set_delegate(Delegate * delegate)96   void set_delegate(Delegate* delegate) {
97     delegate_ = delegate;
98   }
99 
changes()100   std::vector<Change>* changes() { return &changes_; }
101 
102   // Each of these functions generate a Change. There is one per
103   // ViewManagerClient function.
104   void OnEmbed(ConnectionSpecificId connection_id,
105                const String& creator_url,
106                ViewDataPtr root);
107   void OnViewBoundsChanged(Id view_id, RectPtr old_bounds, RectPtr new_bounds);
108   void OnViewHierarchyChanged(Id view_id,
109                               Id new_parent_id,
110                               Id old_parent_id,
111                               Array<ViewDataPtr> views);
112   void OnViewReordered(Id view_id,
113                        Id relative_view_id,
114                        OrderDirection direction);
115   void OnViewDeleted(Id view_id);
116   void OnViewVisibilityChanged(Id view_id, bool visible);
117   void OnViewDrawnStateChanged(Id view_id, bool drawn);
118   void OnViewInputEvent(Id view_id, EventPtr event);
119   void DelegateEmbed(const String& url);
120 
121  private:
122   void AddChange(const Change& change);
123 
124   Delegate* delegate_;
125   std::vector<Change> changes_;
126 
127   DISALLOW_COPY_AND_ASSIGN(TestChangeTracker);
128 };
129 
130 }  // namespace service
131 }  // namespace mojo
132 
133 #endif  // MOJO_SERVICES_VIEW_MANAGER_TEST_CHANGE_TRACKER_H_
134