Search in sources :

Example 6 with UserAgent

use of org.eclipse.scout.rt.shared.ui.UserAgent in project scout.rt by eclipse.

the class ClientRunContextTest method testCurrentSessionAndDerivedValues.

@Test
public void testCurrentSessionAndDerivedValues() {
    final IClientSession session = mock(IClientSession.class);
    final UserAgent sessionUserAgent = UserAgents.create().build();
    final Locale sessionLocale = Locale.CANADA_FRENCH;
    final Subject sessionSubject = new Subject();
    final IDesktop sessionDesktop = mock(IDesktop.class);
    when(session.getUserAgent()).thenReturn(sessionUserAgent);
    when(session.getLocale()).thenReturn(sessionLocale);
    when(session.getSubject()).thenReturn(sessionSubject);
    when(session.getDesktopElseVirtualDesktop()).thenReturn(sessionDesktop);
    ClientRunContexts.empty().withSession(null, false).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            assertNull(ISession.CURRENT.get());
            assertNull(NlsLocale.CURRENT.get());
            assertNull(UserAgent.CURRENT.get());
            assertNull(IDesktop.CURRENT.get());
            assertNull(ClientRunContexts.copyCurrent().getSession());
            assertNull(ClientRunContexts.copyCurrent().getLocale());
            assertNull(ClientRunContexts.copyCurrent().getUserAgent());
            assertNull(ClientRunContexts.copyCurrent().getDesktop());
        }
    });
    ClientRunContexts.empty().withSession(session, false).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            assertSame(session, ISession.CURRENT.get());
            assertNull(NlsLocale.CURRENT.get());
            assertNull(UserAgent.CURRENT.get());
            assertNull(IDesktop.CURRENT.get());
            assertSame(session, ClientRunContexts.copyCurrent().getSession());
            assertNull(ClientRunContexts.copyCurrent().getLocale());
            assertNull(ClientRunContexts.copyCurrent().getUserAgent());
            assertNull(ClientRunContexts.copyCurrent().getDesktop());
            final UserAgent customUserAgent = UserAgents.create().build();
            assertSame(customUserAgent, ClientRunContexts.copyCurrent().withUserAgent(customUserAgent).getUserAgent());
            final Locale customLocale = Locale.ITALIAN;
            assertSame(customLocale, ClientRunContexts.copyCurrent().withLocale(customLocale).getLocale());
        }
    });
    ClientRunContexts.empty().withSession(session, true).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            assertSame(session, ISession.CURRENT.get());
            assertSame(sessionLocale, NlsLocale.CURRENT.get());
            assertSame(sessionUserAgent, UserAgent.CURRENT.get());
            assertSame(sessionDesktop, IDesktop.CURRENT.get());
            assertSame(session, ClientRunContexts.copyCurrent().getSession());
            assertSame(sessionLocale, ClientRunContexts.copyCurrent().getLocale());
            assertSame(sessionUserAgent, ClientRunContexts.copyCurrent().getUserAgent());
            assertSame(sessionDesktop, ClientRunContexts.copyCurrent().getDesktop());
            final UserAgent customUserAgent = UserAgents.create().build();
            assertSame(customUserAgent, ClientRunContexts.copyCurrent().withUserAgent(customUserAgent).getUserAgent());
            final Locale customLocale = Locale.ITALIAN;
            assertSame(customLocale, ClientRunContexts.copyCurrent().withLocale(customLocale).getLocale());
            final IDesktop customDesktop = mock(IDesktop.class);
            assertSame(customDesktop, ClientRunContexts.copyCurrent().withDesktop(customDesktop).getDesktop());
        }
    });
}
Also used : NlsLocale(org.eclipse.scout.rt.platform.nls.NlsLocale) Locale(java.util.Locale) UserAgent(org.eclipse.scout.rt.shared.ui.UserAgent) IClientSession(org.eclipse.scout.rt.client.IClientSession) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Subject(javax.security.auth.Subject) IDesktop(org.eclipse.scout.rt.client.ui.desktop.IDesktop) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 7 with UserAgent

use of org.eclipse.scout.rt.shared.ui.UserAgent in project scout.rt by eclipse.

the class ClientUIPreferences method getUserAgentPrefix.

protected String getUserAgentPrefix() {
    UserAgent currentUserAgent = null;
    if (m_prefs != null && m_prefs.userScope() instanceof IClientSession) {
        currentUserAgent = ((IClientSession) m_prefs.userScope()).getUserAgent();
    } else {
        currentUserAgent = UserAgentUtility.getCurrentUserAgent();
    }
    if (currentUserAgent == null) {
        return "";
    }
    String uiLayer = null;
    if (!UiLayer.UNKNOWN.equals(currentUserAgent.getUiLayer())) {
        uiLayer = currentUserAgent.getUiLayer().getIdentifier();
    }
    String uiDeviceType = null;
    if (!UiDeviceType.UNKNOWN.equals(currentUserAgent.getUiDeviceType())) {
        uiDeviceType = currentUserAgent.getUiDeviceType().getIdentifier();
    }
    return StringUtility.concatenateTokens(uiLayer, ".", uiDeviceType, ".");
}
Also used : UserAgent(org.eclipse.scout.rt.shared.ui.UserAgent) IClientSession(org.eclipse.scout.rt.client.IClientSession)

Aggregations

UserAgent (org.eclipse.scout.rt.shared.ui.UserAgent)7 Subject (javax.security.auth.Subject)4 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)4 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)4 Test (org.junit.Test)4 Locale (java.util.Locale)3 IClientSession (org.eclipse.scout.rt.client.IClientSession)3 IDesktop (org.eclipse.scout.rt.client.ui.desktop.IDesktop)2 RunContext (org.eclipse.scout.rt.platform.context.RunContext)2 NlsLocale (org.eclipse.scout.rt.platform.nls.NlsLocale)2 IServerSession (org.eclipse.scout.rt.server.IServerSession)2 BeanMetaData (org.eclipse.scout.rt.platform.BeanMetaData)1 IBean (org.eclipse.scout.rt.platform.IBean)1 ISession (org.eclipse.scout.rt.shared.ISession)1 RunWithSubject (org.eclipse.scout.rt.testing.platform.runner.RunWithSubject)1 SafeStatementInvoker (org.eclipse.scout.rt.testing.platform.runner.SafeStatementInvoker)1