Skip to content

Commit 4675b94

Browse files
Improve coverage of core unit tests for Picker, Validator, Login, and JSObject (#4248)
Added tests for: - Picker$3$1 (Picker3Test) - Validator$3$1 (Validator3Test) - Login$1 (Login1Test) - JSObject$1, $6, $7 (JSObjectCoverageTest) - JavascriptContext$3, $4, $6 (JSObjectCoverageTest) Note: Picker$1$8 is unreachable dead code and cannot be tested. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 7e824f2 commit 4675b94

File tree

4 files changed

+222
-0
lines changed

4 files changed

+222
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.codename1.javascript;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.ui.BrowserComponent;
6+
import com.codename1.ui.TestPeerComponent;
7+
import com.codename1.util.SuccessCallback;
8+
import com.codename1.testing.TestCodenameOneImplementation;
9+
import java.util.function.Function;
10+
11+
public class JSObjectCoverageTest extends UITestBase {
12+
13+
@FormTest
14+
public void testJSObjectAndContextAnonymousClasses() {
15+
TestPeerComponent peer = new TestPeerComponent(null);
16+
TestCodenameOneImplementation.getInstance().setBrowserComponent(peer);
17+
18+
BrowserComponent browser = new BrowserComponent();
19+
JavascriptContext ctx = new JavascriptContext(browser);
20+
21+
TestCodenameOneImplementation.getInstance().setBrowserScriptResponder(new Function<String, String>() {
22+
@Override
23+
public String apply(String script) {
24+
// ID generation script ends with "} id" or checks ID_KEY
25+
if (script.endsWith("} id")) {
26+
return "0";
27+
}
28+
if (script.contains("typeof")) {
29+
return "object";
30+
}
31+
return null;
32+
}
33+
});
34+
35+
JSObject jsObj = new JSObject(ctx, "window");
36+
37+
// JSObject$1: callAsync(String, Object[], SuccessCallback)
38+
jsObj.callAsync("method", new Object[]{}, new SuccessCallback() {
39+
public void onSucess(Object value) {}
40+
});
41+
42+
// JSObject$6: callStringAsync
43+
jsObj.callStringAsync("method", new SuccessCallback<String>() {
44+
public void onSucess(String value) {}
45+
});
46+
47+
// JSObject$7: callObjectAsync
48+
jsObj.callObjectAsync("method", new SuccessCallback<JSObject>() {
49+
public void onSucess(JSObject value) {}
50+
});
51+
52+
// JavascriptContext$3: getAsync(String, SuccessCallback)
53+
ctx.getAsync("1+1", new SuccessCallback() {
54+
public void onSucess(Object value) {}
55+
});
56+
57+
// JavascriptContext$4: callAsync(JSObject, JSObject, Object[], SuccessCallback)
58+
ctx.callAsync(jsObj, jsObj, new Object[]{}, new SuccessCallback() {
59+
public void onSucess(Object value) {}
60+
});
61+
62+
// JavascriptContext$6: call(String, JSObject, Object[], boolean, SuccessCallback)
63+
ctx.callAsync("func", jsObj, new Object[]{}, new SuccessCallback() {
64+
public void onSucess(Object value) {}
65+
});
66+
}
67+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.codename1.social;
2+
3+
import com.codename1.io.AccessToken;
4+
import com.codename1.io.Oauth2;
5+
import com.codename1.junit.FormTest;
6+
import com.codename1.junit.UITestBase;
7+
import com.codename1.ui.events.ActionEvent;
8+
import com.codename1.ui.events.ActionListener;
9+
10+
public class Login1Test extends UITestBase {
11+
12+
@FormTest
13+
public void testLoginActionListener() {
14+
Login login = new Login() {
15+
@Override
16+
public boolean isNativeLoginSupported() {
17+
return false;
18+
}
19+
20+
@Override
21+
protected boolean validateToken(String token) {
22+
return false;
23+
}
24+
25+
@Override
26+
protected Oauth2 createOauth2() {
27+
return new Oauth2("url", "client", "redirect", "scope") {
28+
@Override
29+
public void showAuthentication(ActionListener al) {
30+
// Test success with AccessToken
31+
AccessToken token = new AccessToken("token", null);
32+
al.actionPerformed(new ActionEvent(token));
33+
34+
// Test success with String
35+
al.actionPerformed(new ActionEvent("tokenString"));
36+
37+
// Test failure with Exception
38+
al.actionPerformed(new ActionEvent(new Exception("Fail")));
39+
}
40+
};
41+
}
42+
};
43+
44+
login.setClientId("id");
45+
login.setClientSecret("secret");
46+
login.setOauth2URL("url");
47+
login.setRedirectURI("uri");
48+
49+
login.doLogin();
50+
}
51+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.codename1.ui.spinner;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.ui.Display;
6+
import com.codename1.ui.DisplayTest;
7+
import com.codename1.ui.Form;
8+
import com.codename1.ui.layouts.BoxLayout;
9+
import com.codename1.ui.spinner.Picker;
10+
11+
public class Picker3Test extends UITestBase {
12+
13+
@FormTest
14+
public void testPickerInputDeviceClose() {
15+
Form f = new Form("Picker Test", new BoxLayout(BoxLayout.Y_AXIS));
16+
Picker p = new Picker();
17+
p.setType(Display.PICKER_TYPE_STRINGS);
18+
p.setStrings("A", "B", "C");
19+
p.setUseLightweightPopup(true);
20+
21+
f.add(p);
22+
f.show();
23+
24+
// Trigger editing
25+
p.pressed();
26+
p.released();
27+
28+
DisplayTest.flushEdt();
29+
30+
if (p.isEditing()) {
31+
final boolean[] called = new boolean[1];
32+
p.stopEditing(new Runnable() {
33+
public void run() {
34+
called[0] = true;
35+
}
36+
});
37+
f.getAnimationManager().flushAnimation(new Runnable() { public void run() {} });
38+
DisplayTest.flushEdt();
39+
}
40+
}
41+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.codename1.ui.validation;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.ui.Component;
6+
import com.codename1.ui.Container;
7+
import com.codename1.ui.Form;
8+
import com.codename1.ui.FontImage;
9+
import com.codename1.ui.layouts.BoxLayout;
10+
import com.codename1.ui.validation.Constraint;
11+
import com.codename1.ui.validation.Validator;
12+
13+
public class Validator3Test extends UITestBase {
14+
15+
public static class TestContainer extends Container {
16+
@Override
17+
public void setScrollX(int scrollX) {
18+
super.setScrollX(scrollX);
19+
}
20+
@Override
21+
public boolean isScrollable() {
22+
return true;
23+
}
24+
@Override
25+
public Component getScrollable() {
26+
return this;
27+
}
28+
}
29+
30+
@FormTest
31+
public void testValidatorScrollListener() {
32+
Form f = new Form("Validator Test");
33+
f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
34+
35+
TestContainer scrollableCmp = new TestContainer();
36+
scrollableCmp.setFocusable(true);
37+
f.add(scrollableCmp);
38+
39+
Validator v = new Validator();
40+
v.setShowErrorMessageForFocusedComponent(true);
41+
42+
v.addConstraint(scrollableCmp, new Constraint() {
43+
@Override
44+
public boolean isValid(Object value) {
45+
return false;
46+
}
47+
48+
@Override
49+
public String getDefaultFailMessage() {
50+
return "Failed";
51+
}
52+
});
53+
54+
v.setValidationFailureHighlightMode(Validator.HighlightMode.EMBLEM);
55+
v.setValidationFailedEmblem(FontImage.createMaterial(FontImage.MATERIAL_ERROR, "Error", 4));
56+
57+
f.show();
58+
scrollableCmp.requestFocus();
59+
60+
// Trigger scroll
61+
scrollableCmp.setScrollX(50);
62+
}
63+
}

0 commit comments

Comments
 (0)