This repository was archived by the owner on Sep 7, 2022. It is now read-only.
forked from parse-community/Parse-SDK-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseDefaultACLControllerTest.java
More file actions
162 lines (130 loc) · 5.36 KB
/
Copy pathParseDefaultACLControllerTest.java
File metadata and controls
162 lines (130 loc) · 5.36 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.parse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.lang.ref.WeakReference;
import bolts.Task;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class ParseDefaultACLControllerTest {
@Before
public void setUp() {
ParseObject.registerSubclass(ParseRole.class);
}
@After
public void tearDown() {
ParseObject.unregisterSubclass(ParseRole.class);
ParseCorePlugins.getInstance().reset();
}
//region testSetDefaultACL
@Test
public void testSetDefaultACLWithACL() {
ParseACL acl = mock(ParseACL.class);
ParseACL copiedACL = mock(ParseACL.class);
when(acl.copy()).thenReturn(copiedACL);
ParseDefaultACLController controller = new ParseDefaultACLController();
controller.set(acl, true);
assertNull(controller.defaultACLWithCurrentUser);
assertNull(controller.lastCurrentUser);
assertTrue(controller.defaultACLUsesCurrentUser);
verify(copiedACL, times(1)).setShared(true);
assertEquals(copiedACL, controller.defaultACL);
}
@Test
public void testSetDefaultACLWithNull() {
ParseDefaultACLController controller = new ParseDefaultACLController();
controller.set(null, true);
assertNull(controller.defaultACLWithCurrentUser);
assertNull(controller.lastCurrentUser);
assertNull(controller.defaultACL);
}
//endregion
//region testGetDefaultACL
@Test
public void testGetDefaultACLWithNoDefaultACL() {
ParseDefaultACLController controller = new ParseDefaultACLController();
ParseACL defaultACL = controller.get();
assertNull(defaultACL);
}
@Test
public void testGetDefaultACLWithNoDefaultACLUsesCurrentUser() {
ParseDefaultACLController controller = new ParseDefaultACLController();
ParseACL acl = new ParseACL();
controller.defaultACL = acl;
controller.defaultACLUsesCurrentUser = false;
ParseACL defaultACL = controller.get();
assertSame(acl, defaultACL);
}
@Test
public void testGetDefaultACLWithNoCurrentUser() {
ParseDefaultACLController controller = new ParseDefaultACLController();
ParseACL acl = new ParseACL();
controller.defaultACL = acl;
controller.defaultACLUsesCurrentUser = true;
// Register currentUser
ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class);
when(currentUserController.getAsync(anyBoolean())).thenReturn(Task.<ParseUser>forResult(null));
ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController);
ParseACL defaultACL = controller.get();
assertSame(acl, defaultACL);
}
@Test
public void testGetDefaultACLWithSameCurrentUserAndLastCurrentUser() {
ParseDefaultACLController controller = new ParseDefaultACLController();
ParseACL acl = new ParseACL();
controller.defaultACL = acl;
controller.defaultACLUsesCurrentUser = true;
ParseACL aclAgain = new ParseACL();
controller.defaultACLWithCurrentUser = aclAgain;
ParseUser currentUser = mock(ParseUser.class);
controller.lastCurrentUser = new WeakReference<>(currentUser);
// Register currentUser
ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class);
when(currentUserController.getAsync(anyBoolean())).thenReturn(Task.forResult(currentUser));
ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController);
ParseACL defaultACL = controller.get();
assertNotSame(acl, defaultACL);
assertSame(aclAgain, defaultACL);
}
@Test
public void testGetDefaultACLWithCurrentUserAndLastCurrentUserNotSame() {
ParseDefaultACLController controller = new ParseDefaultACLController();
ParseACL acl = mock(ParseACL.class);
ParseACL copiedACL = mock(ParseACL.class);
when(acl.copy()).thenReturn(copiedACL);
controller.defaultACL = acl;
controller.defaultACLUsesCurrentUser = true;
ParseACL aclAgain = new ParseACL();
controller.defaultACLWithCurrentUser = aclAgain;
// Register currentUser
ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class);
ParseUser currentUser = mock(ParseUser.class);
when(currentUserController.getAsync(anyBoolean())).thenReturn(Task.forResult(currentUser));
ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController);
ParseACL defaultACL = controller.get();
verify(copiedACL, times(1)).setShared(true);
verify(copiedACL, times(1)).setReadAccess(eq(currentUser), eq(true));
verify(copiedACL, times(1)).setWriteAccess(eq(currentUser), eq(true));
assertSame(currentUser, controller.lastCurrentUser.get());
assertNotSame(acl, defaultACL);
assertSame(copiedACL, defaultACL);
}
//endregion
}