Java Code Examples for com.intellij.openapi.application.ApplicationManager#setApplication()

The following examples show how to use com.intellij.openapi.application.ApplicationManager#setApplication() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: intellij   File: TestUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void createMockApplication(Disposable parentDisposable) {
  final BlazeMockApplication instance = new BlazeMockApplication(parentDisposable);

  // If there was no previous application,
  // ApplicationManager leaves the MockApplication in place, which can break future tests.
  Application oldApplication = ApplicationManager.getApplication();
  if (oldApplication == null) {
    Disposer.register(
        parentDisposable,
        () -> {
          new ApplicationManager() {
            {
              ourApplication = null;
            }
          };
        });
  }

  ApplicationManager.setApplication(instance, FileTypeManager::getInstance, parentDisposable);
  instance.registerService(EncodingManager.class, EncodingManagerImpl.class);
}
 
Example 2
Source Project: intellij   File: TestUtils.java    License: Apache License 2.0 6 votes vote down vote up
static void createMockApplication(Disposable parentDisposable) {
  final MyMockApplication instance = new MyMockApplication(parentDisposable);

  // If there was no previous application, ApplicationManager leaves the MockApplication in place,
  // which can break future tests.
  Application oldApplication = ApplicationManager.getApplication();
  if (oldApplication == null) {
    Disposer.register(
        parentDisposable,
        () ->
            new ApplicationManager() {
              {
                ourApplication = null;
              }
            });
  }

  ApplicationManager.setApplication(instance, FileTypeManager::getInstance, parentDisposable);
  instance.registerService(EncodingManager.class, EncodingManagerImpl.class);
}
 
Example 3
Source Project: buck   File: BuckClientTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testConnectDisconnect() {
  Extensions.registerAreaClass("IDEA_PROJECT", null);
  MockDisposable mockDisposable = new MockDisposable();

  MockApplication application = new MockApplicationEx(mockDisposable);
  ApplicationManager.setApplication(application, mockDisposable);

  Project project = new MockProjectEx(new MockDisposable());

  TestBuckEventHandler handler = new TestBuckEventHandler();
  BuckSocket buckSocket = new BuckSocket(handler);
  BuckClientManager.getOrCreateClient(project, handler).setBuckSocket(buckSocket);

  BuckClientManager.getOrCreateClient(project, handler).connect();
  buckSocket.onConnect(new MockSession());

  BuckClientManager.getOrCreateClient(project, handler).disconnectWithoutRetry();
  buckSocket.onClose(0, "FOO");

  assertFalse(BuckClientManager.getOrCreateClient(project, handler).isConnected());
}
 
Example 4
Source Project: buck   File: BuckClientTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void hasBuckDisconnectedThenWeReconnectIfSoSpecified() {
  Extensions.registerAreaClass("IDEA_PROJECT", null);
  MockDisposable mockDisposable = new MockDisposable();

  MockApplication application = new MockApplicationEx(mockDisposable);
  ApplicationManager.setApplication(application, mockDisposable);

  Project project = new MockProjectEx(new MockDisposable());

  TestBuckEventHandler handler = new TestBuckEventHandler();
  BuckSocket buckSocket = new BuckSocket(handler);
  BuckClientManager.getOrCreateClient(project, handler).setBuckSocket(buckSocket);

  BuckClientManager.getOrCreateClient(project, handler).connect();
  buckSocket.onConnect(new MockSession());

  BuckClientManager.getOrCreateClient(project, handler).disconnectWithRetry();
  buckSocket.onClose(0, "FOO");
  buckSocket.onConnect(new MockSession());

  assertTrue(BuckClientManager.getOrCreateClient(project, handler).isConnected());
}
 
Example 5
Source Project: buck   File: BuckModuleTest.java    License: Apache License 2.0 6 votes vote down vote up
public Project initBuckModule() {
  Extensions.registerAreaClass("IDEA_PROJECT", null);
  MockDisposable mockDisposable = new MockDisposable();
  MockProjectEx project = new MockProjectEx(mockDisposable);

  MockApplication application = new MockApplicationEx(mockDisposable);
  ApplicationManager.setApplication(application, mockDisposable);
  application.registerService(UISettings.class, UISettings.getShadowInstance());
  application.registerService(PropertiesComponent.class, new ProjectPropertiesComponentImpl());
  FileTypeManager fileTypeManager = EasyMock.createMock(FileTypeManager.class);
  EasyMock.expect(
          fileTypeManager.getFileTypeByFileName(BuckFileType.INSTANCE.getDefaultExtension()))
      .andReturn(BuckFileType.INSTANCE)
      .times(3);
  EasyMock.replay(fileTypeManager);
  application.registerService(FileTypeManager.class, fileTypeManager);
  project.addComponent(PsiDocumentManager.class, EasyMock.createMock(PsiDocumentManager.class));

  return project;
}
 
Example 6
Source Project: consulo   File: ActivityMonitorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void setUp() throws Exception {
  super.setUp();
  myCurrentState = ModalityState.NON_MODAL;
  final ModalityStateEx any = new ModalityStateEx();
  ApplicationManager.setApplication(new MockApplication(getTestRootDisposable()) {
    @Nonnull
    @Override
    public ModalityState getCurrentModalityState() {
      return myCurrentState;
    }

    @Override
    public ModalityState getAnyModalityState() {
      return any;
    }
  }, getTestRootDisposable());
  myMonitor = new UiActivityMonitorImpl();
  myMonitor.setActive(true);
  disposeOnTearDown(myMonitor);
}
 
Example 7
@BeforeClass
public static void setUp() {
  Disposable disposable = Disposer.newDisposable();
  ApplicationManager.setApplication(new MockApplication(disposable), disposable);
  Extensions.registerAreaClass("IDEA_PROJECT", null);
  ourProject = new MockProject(ApplicationManager.getApplication().getPicoContainer(), disposable);
}
 
Example 8
@Override
public void beforeEach(ExtensionContext extensionContext) {
    contextStack.add(0, extensionContext);

    DisposableRegistry parent = new DisposableRegistry();
    getStore(extensionContext).put("parent", parent);

    final FileTypeRegistry fileTypeRegistry = mock(FileTypeRegistry.class);
    getStore(extensionContext).put("fileTypeRegistry", fileTypeRegistry);

    Application original = ApplicationManager.getApplication();
    getStore(extensionContext).put("original-application", original);

    Application application = mock(Application.class);
    ApplicationManager.setApplication(application, () -> fileTypeRegistry, parent);
    getStore(extensionContext).put("application", application);
    initializeApplication(application);

    Project project = mock(Project.class);
    when(project.isInitialized()).thenReturn(true);
    when(project.isDisposed()).thenReturn(false);
    getStore(extensionContext).put("project", project);
    initializeProject(project);

    LocalFileSystem lfs = mock(LocalFileSystem.class);
    getStore(extensionContext).put("local-filesystem", lfs);
    setupLocalFileSystem(lfs);
}
 
Example 9
@Override
public void afterEach(ExtensionContext extensionContext) {
    Application app = (Application) getStore(extensionContext).get("original-application");
    if (app != null) {
        ApplicationManager.setApplication(
                app,
                new DisposableRegistry());
    }
    contextStack.remove(0);
}
 
Example 10
Source Project: buck   File: BuckClientTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMessages() {
  Extensions.registerAreaClass("IDEA_PROJECT", null);
  MockDisposable mockDisposable = new MockDisposable();

  MockApplication application = new MockApplicationEx(mockDisposable);
  ApplicationManager.setApplication(application, mockDisposable);
  Project project = new MockProjectEx(new MockDisposable());

  TestBuckEventHandler handler = new TestBuckEventHandler();
  BuckClient client = BuckClientManager.getOrCreateClient(project, handler);

  // Set the socket we control
  BuckSocket socket = new BuckSocket(handler);

  client.setBuckSocket(socket);

  client.connect();

  assertEquals("", handler.getLastMessage());

  socket.onMessage("some text");
  assertEquals("some text", handler.getLastMessage());

  socket.onMessage("some text 1");
  socket.onMessage("some text 2");
  socket.onMessage("some text 3");
  socket.onMessage("some text 4");
  assertEquals("some text 4", handler.getLastMessage());
}
 
Example 11
Source Project: buck   File: BuckEventsConsumerTest.java    License: Apache License 2.0 5 votes vote down vote up
public BuckEventsConsumer initialiseEventsConsumer(MockProject project) {
  MockDisposable mockDisposable = new MockDisposable();

  MockApplication application = new MyMockApplication(mockDisposable);
  ApplicationManager.setApplication(application, mockDisposable);
  application.registerService(
      FileDocumentManager.class, new MockFileDocumentManagerImpl(null, null));
  application.registerService(
      VirtualFileManager.class,
      (VirtualFileManager) EasyMock.createMock(VirtualFileManager.class));
  project.registerService(BuckUIManager.class, new MockBuckUIManager(project));
  project.registerService(ToolWindowManager.class, new Mock.MyToolWindowManager());
  return new BuckEventsConsumer(project);
}
 
Example 12
public void setUp() {
  final Application application = ApplicationManager.getApplication();
  if (application == null) {
    myAppDisposable = Disposable.newDisposable();
    ApplicationManager.setApplication(new MockApplication(myAppDisposable), myAppDisposable);
  }
}
 
Example 13
public CoreApplicationEnvironment(@Nonnull Disposable parentDisposable) {
  myParentDisposable = parentDisposable;

  myFileTypeRegistry = new CoreFileTypeRegistry();

  myApplication = createApplication(myParentDisposable);
  ApplicationManager.setApplication(myApplication, myParentDisposable);
  myLocalFileSystem = createLocalFileSystem();
  myJarFileSystem = createJarFileSystem();

  final InjectingContainer appContainer = myApplication.getInjectingContainer();
  registerComponentInstance(appContainer, FileDocumentManager.class, new MockFileDocumentManagerImpl(DocumentImpl::new, null));

  VirtualFileSystem[] fs = {myLocalFileSystem, myJarFileSystem};
  VirtualFileManagerImpl virtualFileManager = new VirtualFileManagerImpl(myApplication, fs);
  registerComponentInstance(appContainer, VirtualFileManager.class, virtualFileManager);

  registerApplicationExtensionPoint(ASTLazyFactory.EP.getExtensionPointName(), ASTLazyFactory.class);
  registerApplicationExtensionPoint(ASTLeafFactory.EP.getExtensionPointName(), ASTLeafFactory.class);
  registerApplicationExtensionPoint(ASTCompositeFactory.EP.getExtensionPointName(), ASTCompositeFactory.class);

  addExtension(ASTLazyFactory.EP.getExtensionPointName(), new DefaultASTLazyFactory());
  addExtension(ASTLeafFactory.EP.getExtensionPointName(), new DefaultASTLeafFactory());
  addExtension(ASTCompositeFactory.EP.getExtensionPointName(), new DefaultASTCompositeFactory());

  registerApplicationService(EncodingManager.class, new CoreEncodingRegistry());
  registerApplicationService(VirtualFilePointerManager.class, createVirtualFilePointerManager());
  registerApplicationService(PsiBuilderFactory.class, new PsiBuilderFactoryImpl());
  registerApplicationService(ReferenceProvidersRegistry.class, new MockReferenceProvidersRegistry());
  registerApplicationService(StubTreeLoader.class, new CoreStubTreeLoader());
  registerApplicationService(PsiReferenceService.class, new PsiReferenceServiceImpl());
  registerApplicationService(MetaDataRegistrar.class, new MetaRegistry());

  registerApplicationService(ProgressManager.class, createProgressIndicatorProvider());

  registerApplicationService(JobLauncher.class, createJobLauncher());
  registerApplicationService(CodeFoldingSettings.class, new CodeFoldingSettings());
  registerApplicationService(CommandProcessor.class, new CoreCommandProcessor());
}
 
Example 14
@Before
public void setup() {
  testDisposable = Disposer.newDisposable();
  ApplicationManager.setApplication(new MockApplication(testDisposable), testDisposable);
}
 
Example 15
Source Project: consulo   File: PlatformLiteFixture.java    License: Apache License 2.0 4 votes vote down vote up
public void initApplication() {
  //if (ApplicationManager.getApplication() instanceof MockApplicationEx) return;
  final MockApplicationEx instance = new MockApplicationEx(getTestRootDisposable());
  ApplicationManager.setApplication(instance, getTestRootDisposable());
  getApplication().registerService(EncodingManager.class, EncodingManagerImpl.class);
}
 
Example 16
Source Project: consulo   File: WebApplicationImpl.java    License: Apache License 2.0 4 votes vote down vote up
public WebApplicationImpl(@Nonnull Ref<? extends StartupProgress> splash) {
  super(splash);

  ApplicationManager.setApplication(this);
}